diff --git a/telegram/commands.go b/telegram/commands.go index fbed7cd..e8586d8 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -461,12 +461,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return "No error, but chat is nil", true } - gateway.SendPresence( - c.xmpp, - c.jid, - gateway.SPFrom(strconv.FormatInt(chat.Id, 10)), - gateway.SPType("subscribe"), - ) + c.subscribeToID(chat.Id, chat) // join https://t.me/publichat case "join": if len(args) < 1 { diff --git a/telegram/handlers.go b/telegram/handlers.go index 1313871..face5dd 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -73,6 +73,20 @@ func (c *Client) updateHandler() { } c.updateNewChat(typedUpdate) log.Debugf("%#v", typedUpdate.Chat) + case client.TypeUpdateChatPosition: + typedUpdate, ok := update.(*client.UpdateChatPosition) + if !ok { + uhOh() + } + c.updateChatPosition(typedUpdate) + log.Debugf("%#v", typedUpdate) + case client.TypeUpdateChatLastMessage: + typedUpdate, ok := update.(*client.UpdateChatLastMessage) + if !ok { + uhOh() + } + c.updateChatLastMessage(typedUpdate) + log.Debugf("%#v", typedUpdate) case client.TypeUpdateNewMessage: typedUpdate, ok := update.(*client.UpdateNewMessage) if !ok { @@ -142,30 +156,8 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { c.cache.SetChat(update.Chat.Id, update.Chat) - var isChannel = false - chatType := update.Chat.Type.ChatTypeType() - if chatType == client.TypeChatTypeSupergroup { - typeSupergroup, ok := update.Chat.Type.(*client.ChatTypeSupergroup) - if !ok { - uhOh() - } - isChannel = typeSupergroup.IsChannel - } - - // don't subscribe to channel posters - if !((isChannel && update.Chat.LastReadInboxMessageId == 0) || - // don't subscribe to chats with no conversation - // (manual adding will trigger a subscribe anyway) - (update.Chat.LastReadInboxMessageId == 0 && - update.Chat.LastReadOutboxMessageId == 0 && - chatType == client.TypeChatTypePrivate)) { - gateway.SendPresence( - c.xmpp, - c.jid, - gateway.SPFrom(strconv.FormatInt(update.Chat.Id, 10)), - gateway.SPType("subscribe"), - gateway.SPNickname(update.Chat.Title), - ) + if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 { + c.subscribeToID(update.Chat.Id, update.Chat) } if update.Chat.Id < 0 { @@ -174,6 +166,20 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { }() } +// chat position is updated +func (c *Client) updateChatPosition(update *client.UpdateChatPosition) { + if update.Position != nil && update.Position.Order != 0 { + go c.subscribeToID(update.ChatId, nil) + } +} + +// chat last message is updated +func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) { + if update.Positions != nil && len(update.Positions) > 0 { + go c.subscribeToID(update.ChatId, nil) + } +} + // message received func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { go func() { diff --git a/telegram/utils.go b/telegram/utils.go index 66f217a..d6b112d 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -774,3 +774,23 @@ func (c *Client) DownloadFile(id int32, priority int32, synchronous bool) (*clie Synchronous: synchronous, }) } + +// subscribe to a Telegram ID +func (c *Client) subscribeToID(id int64, chat *client.Chat) { + var args []args.V + args = append(args, gateway.SPFrom(strconv.FormatInt(id, 10))) + args = append(args, gateway.SPType("subscribe")) + + if chat == nil { + chat, _, _ = c.GetContactByID(id, nil) + } + if chat != nil { + args = append(args, gateway.SPNickname(chat.Title)) + } + + gateway.SendPresence( + c.xmpp, + c.jid, + args..., + ) +}