From 9636237212d1bff5461179871c47ee3461d8db33 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 8 Jun 2018 12:13:02 +0300 Subject: [PATCH] minor fixes --- config_test.yml.dist | 9 +++++++++ database.go | 20 -------------------- migrations/1528208070_app.up.sql | 6 +++--- models.go | 2 +- routing.go | 5 +++++ telegram.go | 29 +++++++++++------------------ 6 files changed, 29 insertions(+), 42 deletions(-) diff --git a/config_test.yml.dist b/config_test.yml.dist index 090e513..113781d 100644 --- a/config_test.yml.dist +++ b/config_test.yml.dist @@ -10,3 +10,12 @@ sentry_dsn: ~ log_level: 5 debug: false + +update_interval: 24 + +config_aws: + access_key_id: ~ + secret_access_key: ~ + region: ~ + bucket: ~ + content_type: image/jpeg diff --git a/database.go b/database.go index bc074a6..db2cc8c 100644 --- a/database.go +++ b/database.go @@ -25,29 +25,9 @@ func NewDb(config *TransportConfig) *Orm { db.DB().SetMaxOpenConns(config.Database.MaxOpenConnections) db.DB().SetMaxIdleConns(config.Database.MaxIdleConnections) - gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string { - return config.Database.TablePrefix + defaultTableName - } - db.SingularTable(true) db.LogMode(config.Database.Logging) - setCreatedAt := func(scope *gorm.Scope) { - if scope.HasColumn("CreatedAt") { - scope.SetColumn("CreatedAt", time.Now()) - scope.SetColumn("UpdatedAt", time.Now()) - } - } - - setUpdatedAt := func(scope *gorm.Scope) { - if scope.HasColumn("UpdatedAt") { - scope.SetColumn("UpdatedAt", time.Now()) - } - } - - db.Callback().Create().Replace("gorm:update_time_stamp", setCreatedAt) - db.Callback().Update().Register("update_time_stamp", setUpdatedAt) - return &Orm{ DB: db, } diff --git a/migrations/1528208070_app.up.sql b/migrations/1528208070_app.up.sql index e9c007f..ebaf04d 100644 --- a/migrations/1528208070_app.up.sql +++ b/migrations/1528208070_app.up.sql @@ -1,12 +1,12 @@ create table users ( id serial not null - constraint user_tg_pkey + constraint users_pkey primary key, external_id integer not null, - user_photo varchar(255), + user_photo_url varchar(255), user_photo_id varchar(100), created_at timestamp with time zone, updated_at timestamp with time zone, - constraint user_tg_key unique(external_id, user_photo, user_photo_id) + constraint users_key unique(external_id, user_photo_url, user_photo_id) ); diff --git a/models.go b/models.go index 841ba0e..79f067f 100644 --- a/models.go +++ b/models.go @@ -32,7 +32,7 @@ type Bot struct { type Users struct { ID int `gorm:"primary_key"` ExternalID int `gorm:"external_id;not null;unique"` - UserPhotoURL string `gorm:"user_photo type:varchar(255);unique"` + UserPhotoURL string `gorm:"user_photo_url type:varchar(255);unique"` UserPhotoID string `gorm:"user_photo_id type:varchar(100);unique"` CreatedAt time.Time UpdatedAt time.Time diff --git a/routing.go b/routing.go index 74e5b96..998f690 100644 --- a/routing.go +++ b/routing.go @@ -123,6 +123,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { logger.Error(err.Error()) return } + defer r.Body.Close() var b Bot @@ -228,6 +229,7 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) { logger.Error(err.Error()) return } + defer r.Body.Close() var b Bot @@ -329,6 +331,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) return } + defer r.Body.Close() var c Connection @@ -374,6 +377,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) { logger.Error(err.Error()) return } + defer r.Body.Close() var c Connection @@ -512,6 +516,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) { w.Write(jsonString) return } + defer r.Body.Close() var rec v5.ActivityCallback diff --git a/telegram.go b/telegram.go index 83833b3..1846469 100644 --- a/telegram.go +++ b/telegram.go @@ -46,7 +46,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string c := getConnectionById(b.ConnectionID) if !c.Active { - logger.Error(c.ClientID, " connection deativated") + logger.Error(c.ClientID, "connection deativated") w.WriteHeader(http.StatusBadRequest) return } @@ -60,6 +60,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string w.WriteHeader(http.StatusInternalServerError) return } + defer r.Body.Close() if config.Debug { logger.Debugf("telegramWebhookHandler: %v", string(bytes)) @@ -77,7 +78,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string if time.Now().After(user.UpdatedAt.Add(time.Hour*time.Duration(config.UpdateInterval))) || user.ID == 0 { - fileID, fileURL, err := getFileIDAndURL(b.Token, update.Message.From.ID) + fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID) if err != nil { raven.CaptureErrorAndWait(err, nil) logger.Error(err) @@ -188,6 +189,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) return } + defer r.Body.Close() if config.Debug { logger.Debugf("mgWebhookHandler request: %v", string(bytes)) @@ -206,23 +208,16 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { cid, _ := strconv.ParseInt(msg.Data.ExternalChatID, 10, 64) b := getBotByChannel(msg.Data.ChannelID) - if b.ID == 0 { - logger.Error(msg.Data.ChannelID, "missing") + if b.ID == 0 || !b.Active { + logger.Error(msg.Data.ChannelID, "missing or deactivated") w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("Bot missing")) - return - } - - if !b.Active { - logger.Error(msg.Data.ChannelID, "deactivated") - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("Bot deactivated")) + w.Write([]byte("missing or deactivated")) return } c := getConnectionById(b.ConnectionID) if !c.Active { - logger.Error(c.ClientID, " connection deativated") + logger.Error(c.ClientID, "connection deativated") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Connection deactivated")) return @@ -299,7 +294,8 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { } } -func getFileIDAndURL(token string, userID int) (fileID, fileURL string, err error) { +//GetFileIDAndURL function +func GetFileIDAndURL(token string, userID int) (fileID, fileURL string, err error) { bot, err := tgbotapi.NewBotAPI(token) if err != nil { return @@ -320,15 +316,12 @@ func getFileIDAndURL(token string, userID int) (fileID, fileURL string, err erro if len(res.Photos) > 0 { fileID = res.Photos[0][len(res.Photos[0])-1].FileID fileURL, err = bot.GetFileDirectURL(fileID) - if err != nil { - return - } - return } return } +//UploadUserAvatar function func UploadUserAvatar(url string) (picURLs3 string, err error) { s3Config := &aws.Config{ Credentials: credentials.NewStaticCredentials(