1
0
mirror of synced 2024-11-22 04:26:01 +03:00

add update channels settings

This commit is contained in:
DmitryZagorulko 2018-09-21 12:06:33 +03:00
parent 686b24b957
commit 38b6cac55c
8 changed files with 130 additions and 35 deletions

View File

@ -0,0 +1 @@
alter table bot drop column channel_settings_hash;

View File

@ -0,0 +1 @@
alter table bot add column channel_settings_hash varchar(70);

View File

@ -14,6 +14,8 @@ type Options struct {
Config string `short:"c" long:"config" default:"config.yml" description:"Path to configuration file"` Config string `short:"c" long:"config" default:"config.yml" description:"Path to configuration file"`
} }
const Type = "telegram"
var ( var (
config *TransportConfig config *TransportConfig
orm *Orm orm *Orm

View File

@ -21,6 +21,7 @@ type Bot struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ConnectionID int `gorm:"connection_id" json:"connectionId,omitempty"` ConnectionID int `gorm:"connection_id" json:"connectionId,omitempty"`
Channel uint64 `gorm:"channel;not null;unique" json:"channel,omitempty"` Channel uint64 `gorm:"channel;not null;unique" json:"channel,omitempty"`
ChannelSettingsHash string `gorm:"channel_settings_hash type:varchar(70)"`
Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"` Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"`
Name string `gorm:"name type:varchar(40)" json:"name,omitempty"` Name string `gorm:"name type:varchar(40)" json:"name,omitempty"`
CreatedAt time.Time CreatedAt time.Time

View File

@ -13,6 +13,13 @@ func getConnection(uid string) *Connection {
return &connection return &connection
} }
func getConnections() []*Connection {
var connection []*Connection
orm.DB.Find(&connection)
return connection
}
func getConnectionByURL(urlCrm string) *Connection { func getConnectionByURL(urlCrm string) *Connection {
var connection Connection var connection Connection
orm.DB.First(&connection, "api_url = ?", urlCrm) orm.DB.First(&connection, "api_url = ?", urlCrm)
@ -52,6 +59,10 @@ func getBotByToken(token string) (*Bot, error) {
return &bot, nil return &bot, nil
} }
func (b *Bot) save() error {
return orm.DB.Save(b).Error
}
func (b *Bot) deleteBot() error { func (b *Bot) deleteBot() error {
return orm.DB.Delete(b, "token = ?", b.Token).Error return orm.DB.Delete(b, "token = ?", b.Token).Error
} }

View File

@ -58,36 +58,10 @@ func addBotHandler(c *gin.Context) {
} }
b.Name = bot.Self.FirstName b.Name = bot.Self.FirstName
ch := v1.Channel{
Type: "telegram",
Settings: v1.ChannelSettings{
SpamAllowed: false,
Status: v1.Status{
Delivered: v1.ChannelFeatureSend,
Read: v1.ChannelFeatureNone,
},
Text: v1.ChannelSettingsText{
Creating: v1.ChannelFeatureBoth,
Editing: v1.ChannelFeatureBoth,
Quoting: v1.ChannelFeatureBoth,
Deleting: v1.ChannelFeatureReceive,
},
Product: v1.Product{
Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive,
},
Order: v1.Order{
Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive,
},
},
}
conn := getConnectionById(b.ConnectionID) conn := getConnectionById(b.ConnectionID)
client := v1.New(conn.MGURL, conn.MGToken)
var client = v1.New(conn.MGURL, conn.MGToken) data, status, err := client.ActivateTransportChannel(getChannelSettings())
data, status, err := client.ActivateTransportChannel(ch)
if status != http.StatusCreated { if status != http.StatusCreated {
c.AbortWithStatusJSON(BadRequest("error_activating_channel")) c.AbortWithStatusJSON(BadRequest("error_activating_channel"))
logger.Error(conn.APIURL, status, err.Error(), data) logger.Error(conn.APIURL, status, err.Error(), data)
@ -305,6 +279,98 @@ func getIntegrationModule(clientId string) v5.IntegrationModule {
} }
} }
func getChannelSettings(cid ...uint64) v1.Channel {
var channelID uint64
if len(cid) > 0 {
channelID = cid[0]
}
return v1.Channel{
ID: channelID,
Type: Type,
Settings: v1.ChannelSettings{
SpamAllowed: false,
Status: v1.Status{
Delivered: v1.ChannelFeatureSend,
Read: v1.ChannelFeatureNone,
},
Text: v1.ChannelSettingsText{
Creating: v1.ChannelFeatureBoth,
Editing: v1.ChannelFeatureBoth,
Quoting: v1.ChannelFeatureBoth,
Deleting: v1.ChannelFeatureReceive,
},
Product: v1.Product{
Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive,
},
Order: v1.Order{
Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive,
},
},
}
}
func updateChannelsSettings() {
hashSettings, err := getChannelSettingsHash()
if err != nil {
logger.Error(err.Error())
return
}
connections := getConnections()
if len(connections) > 0 {
for _, conn := range connections {
if !conn.Active {
logger.Infof(
"updateChannelsSettings connection %s deactivated",
conn.APIURL,
)
continue
}
updateBots(conn, hashSettings)
}
}
return
}
func updateBots(conn *Connection, hashSettings string) {
bots := conn.getBotsByClientID()
if len(bots) > 0 {
client := v1.New(conn.MGURL, conn.MGToken)
for _, bot := range bots {
if bot.ChannelSettingsHash == hashSettings {
continue
}
data, status, err := client.UpdateTransportChannel(getChannelSettings(bot.Channel))
if config.Debug {
logger.Infof(
"updateChannelsSettings apiURL: %s, ChannelID: %d, Data: %v, Status: %d, err: %v",
conn.APIURL, bot.Channel, data, status, err,
)
}
if err == nil {
bot.ChannelSettingsHash = hashSettings
err = bot.save()
if err != nil {
logger.Error(
"updateChannelsSettings bot.save apiURL: %s, bot.Channel: %d , err: %v",
conn.APIURL, bot.Channel, err,
)
}
}
}
}
return
}
func telegramWebhookHandler(c *gin.Context) { func telegramWebhookHandler(c *gin.Context) {
token := c.Param("token") token := c.Param("token")
b, err := getBotByToken(token) b, err := getBotByToken(token)

View File

@ -53,6 +53,7 @@ func start() {
func setup() *gin.Engine { func setup() *gin.Engine {
loadTranslateFile() loadTranslateFile()
setValidation() setValidation()
updateChannelsSettings()
if config.Debug == false { if config.Debug == false {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)

View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"crypto/sha1"
"crypto/sha256" "crypto/sha256"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -119,3 +121,13 @@ func UploadUserAvatar(url string) (picURLs3 string, err error) {
return return
} }
func getChannelSettingsHash() (hash string, err error) {
res, err := json.Marshal(getChannelSettings())
h := sha1.New()
h.Write(res)
hash = fmt.Sprintf("%x", h.Sum(nil))
return
}