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

fix getUserByExternalID

This commit is contained in:
DmitryZagorulko 2018-06-08 17:40:50 +03:00
parent 6257d19164
commit bfeb3f3fbe
3 changed files with 18 additions and 11 deletions

View File

@ -32,8 +32,8 @@ type Bot struct {
type Users struct {
ID int `gorm:"primary_key"`
ExternalID int `gorm:"external_id;not null;unique"`
UserPhotoURL string `gorm:"user_photo_url type:varchar(255);unique"`
UserPhotoID string `gorm:"user_photo_id type:varchar(100);unique"`
UserPhotoURL string `gorm:"user_photo_url type:varchar(255)"`
UserPhotoID string `gorm:"user_photo_id type:varchar(100)"`
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@ -88,8 +88,8 @@ func (u *Users) save() error {
}
func getUserByExternalID(eid int) *Users {
user := Users{ExternalID: eid}
orm.DB.First(&user)
user := Users{}
orm.DB.First(&user, "external_id = ?", eid)
return &user
}

View File

@ -39,15 +39,15 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
}
if b.ID == 0 || !b.Active {
logger.Error(token, "missing or deactivated")
w.WriteHeader(http.StatusBadRequest)
logger.Error(token, "telegramWebhookHandler: missing or deactivated")
w.WriteHeader(http.StatusOK)
return
}
c := getConnectionById(b.ConnectionID)
if !c.Active {
logger.Error(c.ClientID, "connection deativated")
w.WriteHeader(http.StatusBadRequest)
logger.Error(c.ClientID, "telegramWebhookHandler: connection deactivated")
w.WriteHeader(http.StatusOK)
return
}
@ -76,7 +76,6 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
user := getUserByExternalID(update.Message.From.ID)
if user.Expired(config.UpdateInterval) || user.ID == 0 {
fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
@ -111,6 +110,10 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string
}
}
if config.Debug {
logger.Debugf("telegramWebhookHandler user %v", user)
}
var client = v1.New(c.MGURL, c.MGToken)
if update.Message != nil {
@ -208,7 +211,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
b := getBotByChannel(msg.Data.ChannelID)
if b.ID == 0 || !b.Active {
logger.Error(msg.Data.ChannelID, "missing or deactivated")
logger.Error(msg.Data.ChannelID, "mgWebhookHandler: missing or deactivated")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("missing or deactivated"))
return
@ -216,7 +219,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) {
c := getConnectionById(b.ConnectionID)
if !c.Active {
logger.Error(c.ClientID, "connection deativated")
logger.Error(c.ClientID, "mgWebhookHandler: connection deactivated")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Connection deactivated"))
return
@ -312,6 +315,10 @@ func GetFileIDAndURL(token string, userID int) (fileID, fileURL string, err erro
return
}
if config.Debug {
logger.Debugf("GetFileIDAndURL Photos: %v", res.Photos)
}
if len(res.Photos) > 0 {
fileID = res.Photos[0][len(res.Photos[0])-1].FileID
fileURL, err = bot.GetFileDirectURL(fileID)