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

minor fixes

This commit is contained in:
DmitryZagorulko 2018-06-08 12:13:02 +03:00
parent a391a01c0f
commit 9636237212
6 changed files with 29 additions and 42 deletions

View File

@ -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

View File

@ -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,
}

View File

@ -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)
);

View File

@ -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

View File

@ -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

View File

@ -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(