Commit 83e3beec authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update check new username

parent eea31436
......@@ -34,6 +34,7 @@ func (d *Dao) IncrMessageCount(a *dbModel.Active) (err error) {
func (d *Dao) GetUserActiveMsgCount(userId, chatId, unixDay int) (count int, err error) {
a := dbModel.Active{}
err = d.db.Model(&dbModel.Active{}).
Select("msg_count").
Where("user_id = ? AND chat_id = ? AND unix_day = ?", userId, chatId, unixDay).
First(&a).Error
......@@ -44,15 +45,22 @@ func (d *Dao) GetUserActiveMsgCount(userId, chatId, unixDay int) (count int, err
return a.MsgCount, err
}
func (d *Dao) UserExist(userId, chatId int) (exist bool, err error) {
func (d *Dao) UserExist(userId, chatId int) (exist bool, username string, err error) {
a := dbModel.User{}
err = d.db.Model(&dbModel.User{}).
Select("username").
Where("user_id = ? AND chat_id = ? AND left_at IS NULL", userId, chatId).
First(&a).Error
if err == gorm.ErrRecordNotFound {
return false, nil
return false, "", nil
}
return err == nil, err
return err == nil, a.Username, err
}
func (d *Dao) UpdateUserName(userId, chatId int, username string) (err error) {
return d.db.Model(&dbModel.User{}).
Where("user_id = ? AND chat_id = ? AND left_at IS NULL", userId, chatId).
Update("username", username).Error
}
func (d *Dao) UserLeft(userId, chatId int) (err error) {
......
......@@ -155,9 +155,10 @@ func (m *Messenger) handleActive(msg *tgbotapi.Message) {
return
}
exist, err := m.d.UserExist(int(msg.From.ID), int(msg.Chat.ID))
exist, username, err := m.d.UserExist(int(msg.From.ID), int(msg.Chat.ID))
if err != nil {
log.WithError(err).Error("exist user error")
return
}
if err == nil && !exist {
err = m.d.CreateUser(&dbModel.User{
......@@ -170,6 +171,20 @@ func (m *Messenger) handleActive(msg *tgbotapi.Message) {
}
}
if msg.From.UserName != username {
err = m.d.UpdateUserName(int(msg.From.ID), int(msg.Chat.ID), msg.From.UserName)
if err != nil {
log.WithError(err).Error("update user name error")
return
}
log.WithFields(log.Fields{
"user_id": msg.From.ID,
"chat_id": msg.Chat.ID,
"old_name": username,
"new_name": msg.From.UserName,
}).Debug("update user name")
}
err = m.d.IncrMessageCount(&dbModel.Active{
UserId: int(msg.From.ID),
ChatId: int(msg.Chat.ID),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment