Fix avatars losing && take care of avatar changes

calls
Bohdan Horbeshko 2 years ago
parent bc53a66b2f
commit e38f3897e6

@ -183,8 +183,10 @@ func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) {
// message received // message received
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
go func() { go func() {
chatId := update.Message.ChatId
// guarantee sequential message delivering per chat // guarantee sequential message delivering per chat
lock := c.getChatMessageLock(update.Message.ChatId) lock := c.getChatMessageLock(chatId)
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
@ -196,45 +198,58 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"chat_id": update.Message.ChatId, "chat_id": chatId,
}).Warn("New message from chat") }).Warn("New message from chat")
text := c.messageToText(update.Message, false) var text string
file, filename := c.contentToFilename(update.Message.Content) content := update.Message.Content
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
// download file(s) chat, err := c.client.GetChat(&client.GetChatRequest{
if file != nil && !file.Local.IsDownloadingCompleted { ChatId: chatId,
newFile, err := c.DownloadFile(file.Id, 1, true) })
if err == nil { if err == nil {
file = newFile c.cache.SetChat(chatId, chat)
go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true))
text = "<Chat photo has changed>"
} }
} } else {
// OTR support (I do not know why would you need it, seriously) text = c.messageToText(update.Message, false)
if !(strings.HasPrefix(text, "?OTR") || c.Session.RawMessages) { file, filename := c.contentToFilename(content)
var prefix strings.Builder
prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename))) // download file(s)
if text != "" { if file != nil && !file.Local.IsDownloadingCompleted {
// \n if it is groupchat and message is not empty newFile, err := c.DownloadFile(file.Id, 1, true)
if update.Message.ChatId < 0 { if err == nil {
prefix.WriteString("\n") file = newFile
} else if update.Message.ChatId > 0 {
prefix.WriteString(" | ")
} }
prefix.WriteString(text)
} }
// OTR support (I do not know why would you need it, seriously)
if !(strings.HasPrefix(text, "?OTR") || c.Session.RawMessages) {
var prefix strings.Builder
prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename)))
if text != "" {
// \n if it is groupchat and message is not empty
if chatId < 0 {
prefix.WriteString("\n")
} else if chatId > 0 {
prefix.WriteString(" | ")
}
prefix.WriteString(text)
}
text = prefix.String() text = prefix.String()
}
} }
// mark message as read // mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{ c.client.ViewMessages(&client.ViewMessagesRequest{
ChatId: update.Message.ChatId, ChatId: chatId,
MessageIds: []int64{update.Message.Id}, MessageIds: []int64{update.Message.Id},
ForceRead: true, ForceRead: true,
}) })
// forward message to XMPP // 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)
}() }()
} }

@ -178,6 +178,12 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
var photo string var photo string
if chat != nil && chat.Photo != nil { if chat != nil && chat.Photo != nil {
path := chat.Photo.Small.Local.Path 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) file, err := os.Open(path)
if err == nil { if err == nil {
defer file.Close() defer file.Close()

Loading…
Cancel
Save