diff --git a/migrations/1537271655_app.down.sql b/migrations/1537271655_app.down.sql new file mode 100644 index 0000000..b524f32 --- /dev/null +++ b/migrations/1537271655_app.down.sql @@ -0,0 +1 @@ +alter table bot drop column channel_settings_hash; diff --git a/migrations/1537271655_app.up.sql b/migrations/1537271655_app.up.sql new file mode 100644 index 0000000..35f92c5 --- /dev/null +++ b/migrations/1537271655_app.up.sql @@ -0,0 +1 @@ +alter table bot add column channel_settings_hash varchar(70); diff --git a/src/main.go b/src/main.go index ac990c4..35e8319 100644 --- a/src/main.go +++ b/src/main.go @@ -14,6 +14,8 @@ type Options struct { Config string `short:"c" long:"config" default:"config.yml" description:"Path to configuration file"` } +const Type = "telegram" + var ( config *TransportConfig orm *Orm diff --git a/src/models.go b/src/models.go index 7a118f0..4d9622f 100644 --- a/src/models.go +++ b/src/models.go @@ -18,13 +18,14 @@ type Connection struct { // Bot model type Bot struct { - ID int `gorm:"primary_key"` - ConnectionID int `gorm:"connection_id" json:"connectionId,omitempty"` - Channel uint64 `gorm:"channel;not null;unique" json:"channel,omitempty"` - Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"` - Name string `gorm:"name type:varchar(40)" json:"name,omitempty"` - CreatedAt time.Time - UpdatedAt time.Time + ID int `gorm:"primary_key"` + ConnectionID int `gorm:"connection_id" json:"connectionId,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"` + Name string `gorm:"name type:varchar(40)" json:"name,omitempty"` + CreatedAt time.Time + UpdatedAt time.Time } // User model diff --git a/src/repository.go b/src/repository.go index 522f280..089651c 100644 --- a/src/repository.go +++ b/src/repository.go @@ -13,6 +13,13 @@ func getConnection(uid string) *Connection { return &connection } +func getConnections() []*Connection { + var connection []*Connection + orm.DB.Find(&connection) + + return connection +} + func getConnectionByURL(urlCrm string) *Connection { var connection Connection orm.DB.First(&connection, "api_url = ?", urlCrm) @@ -52,6 +59,10 @@ func getBotByToken(token string) (*Bot, error) { return &bot, nil } +func (b *Bot) save() error { + return orm.DB.Save(b).Error +} + func (b *Bot) deleteBot() error { return orm.DB.Delete(b, "token = ?", b.Token).Error } diff --git a/src/routing.go b/src/routing.go index 046cd74..468ebcc 100644 --- a/src/routing.go +++ b/src/routing.go @@ -58,36 +58,10 @@ func addBotHandler(c *gin.Context) { } 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) + client := v1.New(conn.MGURL, conn.MGToken) - var client = v1.New(conn.MGURL, conn.MGToken) - data, status, err := client.ActivateTransportChannel(ch) + data, status, err := client.ActivateTransportChannel(getChannelSettings()) if status != http.StatusCreated { c.AbortWithStatusJSON(BadRequest("error_activating_channel")) 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) { token := c.Param("token") b, err := getBotByToken(token) diff --git a/src/run.go b/src/run.go index 3032243..577769a 100644 --- a/src/run.go +++ b/src/run.go @@ -53,6 +53,7 @@ func start() { func setup() *gin.Engine { loadTranslateFile() setValidation() + updateChannelsSettings() if config.Debug == false { gin.SetMode(gin.ReleaseMode) diff --git a/src/utils.go b/src/utils.go index 9da0790..3c8177c 100644 --- a/src/utils.go +++ b/src/utils.go @@ -1,7 +1,9 @@ package main import ( + "crypto/sha1" "crypto/sha256" + "encoding/json" "errors" "fmt" "net/http" @@ -119,3 +121,13 @@ func UploadUserAvatar(url string) (picURLs3 string, err error) { 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 +}