1
0
mirror of synced 2024-11-22 12:26:02 +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 { type Users struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ExternalID int `gorm:"external_id;not null;unique"` ExternalID int `gorm:"external_id;not null;unique"`
UserPhotoURL string `gorm:"user_photo_url type:varchar(255);unique"` UserPhotoURL string `gorm:"user_photo_url type:varchar(255)"`
UserPhotoID string `gorm:"user_photo_id type:varchar(100);unique"` UserPhotoID string `gorm:"user_photo_id type:varchar(100)"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }

View File

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

View File

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