Fix avatars losing && take care of avatar changes

This commit is contained in:
Bohdan Horbeshko 2022-02-11 20:47:54 -05:00
parent bc53a66b2f
commit e38f3897e6
2 changed files with 46 additions and 25 deletions

View file

@ -183,8 +183,10 @@ func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) {
// message received
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
go func() {
chatId := update.Message.ChatId
// guarantee sequential message delivering per chat
lock := c.getChatMessageLock(update.Message.ChatId)
lock := c.getChatMessageLock(chatId)
lock.Lock()
defer lock.Unlock()
@ -196,11 +198,23 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
}
log.WithFields(log.Fields{
"chat_id": update.Message.ChatId,
"chat_id": chatId,
}).Warn("New message from chat")
text := c.messageToText(update.Message, false)
file, filename := c.contentToFilename(update.Message.Content)
var text string
content := update.Message.Content
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
chat, err := c.client.GetChat(&client.GetChatRequest{
ChatId: chatId,
})
if err == nil {
c.cache.SetChat(chatId, chat)
go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true))
text = "<Chat photo has changed>"
}
} else {
text = c.messageToText(update.Message, false)
file, filename := c.contentToFilename(content)
// download file(s)
if file != nil && !file.Local.IsDownloadingCompleted {
@ -215,9 +229,9 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename)))
if text != "" {
// \n if it is groupchat and message is not empty
if update.Message.ChatId < 0 {
if chatId < 0 {
prefix.WriteString("\n")
} else if update.Message.ChatId > 0 {
} else if chatId > 0 {
prefix.WriteString(" | ")
}
@ -226,15 +240,16 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
text = prefix.String()
}
}
// mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{
ChatId: update.Message.ChatId,
ChatId: chatId,
MessageIds: []int64{update.Message.Id},
ForceRead: true,
})
// forward message to XMPP
gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatId, 10), text, c.xmpp)
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp)
}()
}

View file

@ -178,6 +178,12 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
var photo string
if chat != nil && chat.Photo != nil {
path := chat.Photo.Small.Local.Path
if path == "" {
tgFile, err := c.DownloadFile(chat.Photo.Small.Id, 1, true)
if err == nil {
path = tgFile.Local.Path
}
}
file, err := os.Open(path)
if err == nil {
defer file.Close()