aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-02-12 04:47:54 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-02-12 04:47:54 +0300
commite38f3897e63265aa497ea2767a662e5e40842301 (patch)
tree3a5bb4240a2ce3a4ebd18bf72502ba75dd624bf6
parentbc53a66b2ffc7863615d71a93ab210f8c3c0857e (diff)
Fix avatars losing && take care of avatar changes
-rw-r--r--telegram/handlers.go65
-rw-r--r--telegram/utils.go6
2 files changed, 46 insertions, 25 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go
index 43a2291..fe4540c 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -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,45 +198,58 @@ 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)
-
- // download file(s)
- if file != nil && !file.Local.IsDownloadingCompleted {
- newFile, err := c.DownloadFile(file.Id, 1, true)
+ 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 {
- file = newFile
+ c.cache.SetChat(chatId, chat)
+ go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true))
+ text = "<Chat photo has changed>"
}
- }
- // 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 update.Message.ChatId < 0 {
- prefix.WriteString("\n")
- } else if update.Message.ChatId > 0 {
- prefix.WriteString(" | ")
+ } else {
+ text = c.messageToText(update.Message, false)
+ file, filename := c.contentToFilename(content)
+
+ // download file(s)
+ if file != nil && !file.Local.IsDownloadingCompleted {
+ newFile, err := c.DownloadFile(file.Id, 1, true)
+ if err == nil {
+ file = newFile
}
-
- 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
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)
}()
}
diff --git a/telegram/utils.go b/telegram/utils.go
index da8d849..086b7ae 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -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()