diff --git a/telegram/cache/cache.go b/telegram/cache/cache.go index 6847d3e..1261497 100644 --- a/telegram/cache/cache.go +++ b/telegram/cache/cache.go @@ -16,7 +16,8 @@ type Status struct { // Cache allows operating the chats and users cache in // a thread-safe manner type Cache struct { - chats map[int64]*client.Chat + ownChats map[int64]*client.Chat + auxChats map[int64]*client.Chat users map[int64]*client.User statuses map[int64]*Status chatsLock sync.Mutex @@ -27,7 +28,8 @@ type Cache struct { // NewCache initializes a cache func NewCache() *Cache { return &Cache{ - chats: map[int64]*client.Chat{}, + ownChats: map[int64]*client.Chat{}, + auxChats: map[int64]*client.Chat{}, users: map[int64]*client.User{}, statuses: map[int64]*Status{}, } @@ -40,7 +42,23 @@ func (cache *Cache) ChatsKeys() []int64 { defer cache.chatsLock.Unlock() var keys []int64 - for id := range cache.chats { + for id := range cache.ownChats { + keys = append(keys, id) + } + for id := range cache.auxChats { + keys = append(keys, id) + } + return keys +} + +// OwnChatsKeys grabs only own chat ids synchronously to avoid lockups +// while they are used +func (cache *Cache) OwnChatsKeys() []int64 { + cache.chatsLock.Lock() + defer cache.chatsLock.Unlock() + + var keys []int64 + for id := range cache.ownChats { keys = append(keys, id) } return keys @@ -84,7 +102,10 @@ func (cache *Cache) GetChat(id int64) (*client.Chat, bool) { cache.chatsLock.Lock() defer cache.chatsLock.Unlock() - chat, ok := cache.chats[id] + chat, ok := cache.ownChats[id] + if !ok { + chat, ok = cache.auxChats[id] + } return chat, ok } @@ -107,11 +128,21 @@ func (cache *Cache) GetStatus(id int64) (*Status, bool) { } // SetChat stores a chat in the cache -func (cache *Cache) SetChat(id int64, chat *client.Chat) { +func (cache *Cache) SetChat(id int64, chat *client.Chat, own bool) { cache.chatsLock.Lock() defer cache.chatsLock.Unlock() - cache.chats[id] = chat + if own { + cache.ownChats[id] = chat + // move from aux to own, but not vice versa + // (own: true means that presences for the chat are needed + // for sure, false means just "not necessarily") + if _, ok := cache.auxChats[id]; ok { + delete(cache.auxChats, id) + } + } else { + cache.auxChats[id] = chat + } } // SetUser stores a user in the cache diff --git a/telegram/commands.go b/telegram/commands.go index a174009..c271977 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -222,7 +222,7 @@ func (c *Client) helpString(typ CommandType, chatId int64) string { var str strings.Builder commandMap := GetCommands(typ) - chatType, _, chatTypeErr := c.GetChatType(chatId) + chatType, _, chatTypeErr := c.GetChatType(chatId, true) str.WriteString("Available commands:\n") for _, name := range SortedCommandKeys(commandMap) { @@ -358,7 +358,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin return errors.Wrap(err, "Logout error").Error(), false } - for _, id := range c.cache.ChatsKeys() { + for _, id := range c.cache.OwnChatsKeys() { c.leaveChat(id) } @@ -498,7 +498,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin return strings.Join(entries, "\n"), true case "report": - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), false } @@ -583,7 +583,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, return notEnoughArguments, true, false } - chatType, _, chatTypeErr := c.GetChatType(chatID) + chatType, _, chatTypeErr := c.GetChatType(chatID, true) if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { return "Not applicable for this chat type", true, false } @@ -863,7 +863,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } // invite @username to current groupchat case "invite": - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -890,7 +890,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, return link.InviteLink, true, true // kick @username from current group chat case "kick": - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -905,7 +905,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, // mute [@username [n hours]] case "mute": if len(args) > 0 { - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -934,7 +934,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, // unmute [@username] case "unmute": if len(args) > 0 { - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -954,7 +954,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } // ban @username from current chat [for N hours] case "ban": - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -976,7 +976,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } // unban @username case "unban": - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -990,7 +990,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } // promote @username to admin case "promote": - contact, _, err := c.GetContactByUsername(args[0]) + contact, _, err := c.GetContactByUsername(args[0], false) if err != nil { return err.Error(), true, false } @@ -1052,7 +1052,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } // close secret chat case "close": - chat, _, err := c.GetContactByID(chatID, nil) + chat, _, err := c.GetContactByID(chatID, nil, true) if err != nil { return err.Error(), true, false } diff --git a/telegram/connect.go b/telegram/connect.go index bee3366..86b8d4a 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -232,7 +232,7 @@ func (c *Client) Disconnect(resource string, quit bool) bool { log.Warn("Disconnecting from Telegram network...") // we're offline (unsubscribe if logout) - for _, id := range c.cache.ChatsKeys() { + for _, id := range c.cache.OwnChatsKeys() { args := gateway.SimplePresence(id, "unavailable") c.sendPresence(args...) } diff --git a/telegram/handlers.go b/telegram/handlers.go index 66582a2..35be03c 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -157,13 +157,13 @@ func (c *Client) updateUser(update *client.UpdateUser) { } show, status, presenceType := c.userStatusToText(update.User.Status, update.User.Id) - go c.ProcessStatusUpdate(update.User.Id, status, show, gateway.SPType(presenceType)) + go c.ProcessStatusUpdate(update.User.Id, status, show, false, gateway.SPType(presenceType)) } // user status changed func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { show, status, presenceType := c.userStatusToText(update.Status, update.UserId) - go c.ProcessStatusUpdate(update.UserId, status, show, gateway.SPImmed(false), gateway.SPType(presenceType)) + go c.ProcessStatusUpdate(update.UserId, status, show, false, gateway.SPImmed(false), gateway.SPType(presenceType)) } // new chat discovered @@ -177,14 +177,14 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { } } - c.cache.SetChat(update.Chat.Id, update.Chat) + c.cache.SetChat(update.Chat.Id, update.Chat, true) if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 { c.subscribeToID(update.Chat.Id, update.Chat, false) } if update.Chat.Id < 0 { - c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat") + c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat", true) } }() } @@ -269,7 +269,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { } log.Infof("ignoredResource: %v", ignoredResource) - chat, _, _ := c.GetContactByID(update.ChatId, nil) + chat, _, _ := c.GetContactByID(update.ChatId, nil, true) isMUC := c.Session.MUC && c.IsGroup(chat) var jids []string @@ -398,14 +398,14 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { return } if c.Session.IgnoreGroupDeletions { - chatType, _, chatTypeErr := c.GetChatType(update.ChatId) + chatType, _, chatTypeErr := c.GetChatType(update.ChatId, false) if chatTypeErr == nil && (chatType == ChatTypeBasicGroup || chatType == ChatTypeSupergroup) { return } } var isGroupchat bool - chat, _, _ := c.GetContactByID(update.ChatId, nil) + chat, _, _ := c.GetContactByID(update.ChatId, nil, false) if c.Session.MUC && c.IsGroup(chat) { isGroupchat = true } @@ -489,7 +489,7 @@ func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) // chat title changed func (c *Client) updateChatTitle(update *client.UpdateChatTitle) { - chat, user, _ := c.GetContactByID(update.ChatId, nil) + chat, user, _ := c.GetContactByID(update.ChatId, nil, false) if c.Session.MUC && c.IsGroup(chat) { return } @@ -498,7 +498,7 @@ func (c *Client) updateChatTitle(update *client.UpdateChatTitle) { // set also the status (for group chats only) if user == nil { - c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", gateway.SPImmed(true)) + c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", false, gateway.SPImmed(true)) } // update chat title in the cache @@ -528,7 +528,7 @@ func (c *Client) updateBasicGroupFullInfo(update *client.UpdateBasicGroupFullInf } func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) { - chat, _, _ := c.GetContactByID(update.ChatId, nil) + chat, _, _ := c.GetContactByID(update.ChatId, nil, false) // update chat permissions in the cache if chat != nil { diff --git a/telegram/utils.go b/telegram/utils.go index 7bdf588..a7d9efa 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -149,7 +149,7 @@ const ( type ChatMemberStatus int // GetContactByUsername resolves username to user id retrieves user and chat information -func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) { +func (c *Client) GetContactByUsername(username string, own bool) (*client.Chat, *client.User, error) { if !c.Online() { return nil, nil, errOffline } @@ -174,11 +174,11 @@ func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.Us } } - return c.GetContactByID(userID, chat) + return c.GetContactByID(userID, chat, own) } // GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing) -func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *client.User, error) { +func (c *Client) GetContactByID(id int64, chat *client.Chat, own bool) (*client.Chat, *client.User, error) { if !c.Online() || id == 0 { return nil, nil, errOffline } @@ -213,9 +213,9 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli return nil, nil, err } - c.cache.SetChat(id, cacheChat) + c.cache.SetChat(id, cacheChat, own) } else { - c.cache.SetChat(id, chat) + c.cache.SetChat(id, chat, own) } } if chat == nil { @@ -226,8 +226,8 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli } // GetChatByID gets exactly a chat from a cache, or error if chat is not found -func (c *Client) GetChatByID(id int64, chat *client.Chat) (*client.Chat, error) { - chat, _, err := c.GetContactByID(id, nil) +func (c *Client) GetChatByID(id int64, chat *client.Chat, own bool) (*client.Chat, error) { + chat, _, err := c.GetContactByID(id, nil, own) if err != nil { return nil, err } else if chat == nil { @@ -238,7 +238,7 @@ func (c *Client) GetChatByID(id int64, chat *client.Chat) (*client.Chat, error) } // GetChatType obtains chat type from its information -func (c *Client) GetChatType(id int64) (ChatType, *client.Chat, error) { +func (c *Client) GetChatType(id int64, own bool) (ChatType, *client.Chat, error) { if !c.Online() || id == 0 { return ChatTypeUnknown, nil, errOffline } @@ -254,7 +254,7 @@ func (c *Client) GetChatType(id int64) (ChatType, *client.Chat, error) { return ChatTypeUnknown, nil, err } - c.cache.SetChat(id, chat) + c.cache.SetChat(id, chat, own) } chatType := chat.Type.ChatTypeType() @@ -276,8 +276,8 @@ func (c *Client) GetChatType(id int64) (ChatType, *client.Chat, error) { } // IsPM checks if a chat is PM -func (c *Client) IsPM(id int64) (bool, *client.Chat, error) { - typ, chat, err := c.GetChatType(id) +func (c *Client) IsPM(id int64, own bool) (bool, *client.Chat, error) { + typ, chat, err := c.GetChatType(id, own) if err != nil { return false, chat, err } @@ -289,8 +289,8 @@ func (c *Client) IsPM(id int64) (bool, *client.Chat, error) { } // IsBot checks if a chat is a bot -func (c *Client) IsBot(id int64) (bool, error) { - _, user, err := c.GetContactByID(id, nil) +func (c *Client) IsBot(id int64, own bool) (bool, error) { + _, user, err := c.GetContactByID(id, nil, own) if err != nil { return false, err } @@ -470,7 +470,7 @@ func (c *Client) GetHashedAvatar(chatId int64) *HashedAvatar { if !ok { log.Info("Could not find avatar in cache, fetching immediately") - chat, _, err := c.GetContactByID(chatId, nil) + chat, _, err := c.GetContactByID(chatId, nil, true) if err != nil || chat == nil || chat.Photo == nil { return nil } @@ -488,7 +488,7 @@ func (c *Client) GetHashedAvatar(chatId int64) *HashedAvatar { } // ProcessStatusUpdate sets contact status -func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, oldArgs ...args.V) error { +func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, own bool, oldArgs ...args.V) error { if !c.Online() { return nil } @@ -497,7 +497,7 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o "chat_id": chatID, }).Info("Status update for") - chat, user, err := c.GetContactByID(chatID, nil) + chat, user, err := c.GetContactByID(chatID, nil, own) if err != nil { return err } @@ -713,7 +713,7 @@ func (c *Client) updateMUCOccupants(mucState *MUCState, chatID int64, members [] myAffiliation := "member" myRole := "participant" - chat, _, _ := c.GetContactByID(chatID, nil) + chat, _, _ := c.GetContactByID(chatID, nil, true) _, toJids := c.getMUCJoinedJIDs(chatID, mucState, false) @@ -874,7 +874,7 @@ func (c *Client) GetMUCNickname(chatID int64) string { log.Warnf("Resourceprep for %v failed, falling back to chat ID", fc) var usernames string - _, user, _ := c.GetContactByID(chatID, nil) + _, user, _ := c.GetContactByID(chatID, nil, false) if user != nil && user.Usernames != nil { usernames = c.usernamesToString(user.Usernames.ActiveUsernames) } @@ -1066,7 +1066,7 @@ func (c *Client) FormatContact(chatID int64) string { return "" } - chat, user, err := c.GetContactByID(chatID, nil) + chat, user, err := c.GetContactByID(chatID, nil, false) if err != nil { return "unknown contact: " + err.Error() } @@ -1698,7 +1698,7 @@ func (c *Client) isCarbonsEnabled() bool { } func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, suppressReply bool) (string, *gateway.Reply) { - isPM, chat, err := c.IsPM(message.ChatId) + isPM, chat, err := c.IsPM(message.ChatId, true) if err != nil { log.Errorf("Could not determine chat type: %v", err) } @@ -1811,7 +1811,7 @@ func (c *Client) getPrefixSeparator(chatId int64) string { // ProcessIncomingMessage is a legacy wrapper for SendMessageToGateway aiming only PM messages func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { - chat, _, _ := c.GetContactByID(chatId, nil) + chat, _, _ := c.GetContactByID(chatId, nil, true) safeToSend := true groupChatFrom := "" groupChatTos := []string{} @@ -1898,8 +1898,8 @@ func (c *Client) SendMessageToGateway(chatId int64, message *client.Message, id ChatId: chatId, }) if err == nil { - c.cache.SetChat(chatId, chat) - go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true)) + c.cache.SetChat(chatId, chat, true) + go c.ProcessStatusUpdate(chatId, "", "", true, gateway.SPImmed(true)) text = "" if chat.Photo == nil { @@ -1953,7 +1953,7 @@ func (c *Client) SendMessageToGateway(chatId int64, message *client.Message, id var ignorePrefix bool if oobSwap { if text == "" || message.Content.MessageContentType() == client.TypeMessageSticker { - chatType, _, err := c.GetChatType(chatId) + chatType, _, err := c.GetChatType(chatId, true) ignorePrefix = err == nil && (chatType != ChatTypeBasicGroup && chatType != ChatTypeSupergroup) && c.isCarbonsEnabled() } } @@ -2030,7 +2030,7 @@ func (c *Client) SendMessageToGateway(chatId int64, message *client.Message, id if err == nil { status = chatMember.Status } - chat, err := c.GetChatByID(chatId, nil) + chat, err := c.GetChatByID(chatId, nil, true) if err == nil { affiliation, role := c.memberStatusToAffiliationAndRole(status, chat) mucUserItem = &gateway.MUCUserItem{ @@ -2192,7 +2192,7 @@ func (c *Client) returnMessage(returnJid string, chatID int64, text string, code gateway.SendErrorMessage(returnJid, gateway.MUCJID(chatID), text, code, isGroupchat, c.xmpp) } else { var nickname string - chat, err := c.GetChatByID(chatID, nil) + chat, err := c.GetChatByID(chatID, nil, true) if err == nil { nickname = chat.Title } @@ -2235,9 +2235,9 @@ func (c *Client) prepareOutgoingMessageContent(text string, file *client.InputFi return content } -// ChatsKeys proxies the following function from unexported cache -func (c *Client) ChatsKeys() []int64 { - return c.cache.ChatsKeys() +// OwnChatsKeys proxies the following function from unexported cache +func (c *Client) OwnChatsKeys() []int64 { + return c.cache.OwnChatsKeys() } // StatusesRange proxies the following function from unexported cache @@ -2294,8 +2294,8 @@ func (c *Client) roster(resource string) { log.Warnf("Sending roster for %v", resource) - for _, chat := range c.cache.ChatsKeys() { - c.ProcessStatusUpdate(chat, "", "") + for _, chat := range c.cache.OwnChatsKeys() { + c.ProcessStatusUpdate(chat, "", "", true) } c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login)) @@ -2617,7 +2617,7 @@ func (c *Client) GetGroupChats() []*client.Chat { }) if err == nil { for _, id := range chats.ChatIds { - chat, _, _ := c.GetContactByID(id, nil) + chat, _, _ := c.GetContactByID(id, nil, true) if chat != nil && c.IsGroup(chat) { groupChats = append(groupChats, chat) } @@ -2643,7 +2643,7 @@ func (c *Client) subscribeToID(id int64, chat *client.Chat, firstTime bool) { args := gateway.SimplePresence(id, "subscribe") if chat == nil { - chat, _, _ = c.GetContactByID(id, nil) + chat, _, _ = c.GetContactByID(id, nil, true) } if chat != nil { if c.Session.MUC && c.IsGroup(chat) { @@ -2682,7 +2682,7 @@ func (c *Client) prepareDiskSpace(size uint64) { func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) { var info VCardInfo - chat, user, err := c.GetContactByID(toID, nil) + chat, user, err := c.GetContactByID(toID, nil, false) if err != nil { return info, err } @@ -2709,7 +2709,7 @@ func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) { } func (c *Client) UpdateChatNicknames() { - for _, id := range c.cache.ChatsKeys() { + for _, id := range c.cache.OwnChatsKeys() { chat, ok := c.cache.GetChat(id) if ok { if c.Session.MUC && c.IsGroup(chat) { @@ -2864,7 +2864,7 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message, p var isMUC bool if plain { - chat, err := c.GetChatByID(chatID, nil) + chat, err := c.GetChatByID(chatID, nil, true) if err == nil { isMUC = c.Session.MUC && c.IsGroup(chat) } @@ -2923,7 +2923,7 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message, p // GetChatMembers retrieves a list of chat members. "Limited" mode works only if there are no more than 20 members at all func (c *Client) GetChatMembers(chatID int64, limited bool, query string, membersList MembersList) ([]*client.ChatMember, error) { if membersList == MembersListCreators { - chat, err := c.GetChatByID(chatID, nil) + chat, err := c.GetChatByID(chatID, nil, true) if err != nil { return nil, err } @@ -2976,7 +2976,7 @@ func (c *Client) GetChatMembers(chatID int64, limited bool, query string, member if limited { limit = 20 - chat, err := c.GetChatByID(chatID, nil) + chat, err := c.GetChatByID(chatID, nil, true) if err != nil { return nil, err } @@ -3057,7 +3057,7 @@ func (c *Client) unsubscribe(chatID int64) error { } func (c *Client) leaveChat(chatID int64) error { - chat, err := c.GetChatByID(chatID, nil) + chat, err := c.GetChatByID(chatID, nil, true) if err == nil && c.Session.MUC && c.IsGroup(chat) { return c.kickMeFromMUC(chatID, []uint16{110, 307}, false, nil) diff --git a/xmpp/component.go b/xmpp/component.go index 45c5440..20ae0a6 100644 --- a/xmpp/component.go +++ b/xmpp/component.go @@ -143,6 +143,7 @@ func heartbeat(component *xmpp.Component) { chatID, session.LastSeenStatus(delayedStatus.TimestampOnline), "away", + true, ) delete(session.DelayedStatuses, chatID) } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 365ab20..7ad4d95 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -202,7 +202,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { isGroupchat := msg.Type == "groupchat" if session.Session.MUC { - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err == nil && session.IsGroup(chat) { if !toIsGroup { gateway.SendErrorMessage(msg.From, toJid.Node, "KHVATIT SYUDA ZVONITb", 403, false, component) @@ -502,7 +502,7 @@ func handleSubscription(s xmpp.Sender, p stanza.Presence) { if !ok { return } - go session.ProcessStatusUpdate(toID, "", "", gateway.SPImmed(false)) + go session.ProcessStatusUpdate(toID, "", "", true, gateway.SPImmed(false)) } func handlePresence(s xmpp.Sender, p stanza.Presence) { @@ -573,6 +573,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) { status.ID, description, show, + true, newArgs..., ) } @@ -633,7 +634,7 @@ func handleMUCPresence(s xmpp.Sender, p stanza.Presence, mucExt stanza.MucPresen return } - chat, _, err := session.GetContactByID(chatId, nil) + chat, _, err := session.GetContactByID(chatId, nil, true) if err != nil || !session.IsGroup(chat) { presenceReplySetError(reply, 404) return @@ -697,7 +698,7 @@ func tryHandleMUCPresence(s xmpp.Sender, p stanza.Presence) { return } - chat, _, err := session.GetContactByID(chatId, nil) + chat, _, err := session.GetContactByID(chatId, nil, true) if err != nil || !session.IsGroup(chat) { return } @@ -938,7 +939,7 @@ func getTelegramChatType(from string, to string) (telegram.ChatType, error) { if ok { session, ok := sessions[bare] if ok { - chatType, _, chatTypeErr := session.GetChatType(toId) + chatType, _, chatTypeErr := session.GetChatType(toId, true) return chatType, chatTypeErr } } @@ -995,7 +996,7 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) { session, sessionOk := sessions[bare] if sessionOk && session.Session.MUC { if toOk && toIsGroup { - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err == nil && session.IsGroup(chat) { isMuc = true disco.AddIdentity(chat.Title, "conference", "text") @@ -1077,7 +1078,7 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) { session, sessionOk := sessions[bare] if sessionOk && session.Session.MUC { if toOk && toIsGroup { - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err == nil && session.IsGroup(chat) { disco.SetNode(di.Node) disco.AddIdentity(session.GetMUCNickname(0), "conference", "text") @@ -1149,7 +1150,7 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) { isOnline = session.Online() if toOk { - isBot, err := session.IsBot(toID) + isBot, err := session.IsBot(toID, true) if err == nil && isBot { disco.AddItem(iq.To, "botmenu", "Bot Menu") } @@ -1278,7 +1279,7 @@ func handleGetQueryMucAdmin(s xmpp.Sender, iq *stanza.IQ, query *extensions.Quer return } - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err != nil || !session.IsGroup(chat) { iqAnswerSetError(answer, 405) return @@ -1372,7 +1373,7 @@ func handleGetQueryMucOwner(s xmpp.Sender, iq *stanza.IQ) { return } - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err != nil || chat == nil || !session.IsGroup(chat) { iqAnswerSetError(answer, 405) return @@ -1502,7 +1503,7 @@ func handleGetMetadataMAM2(s xmpp.Sender, iq *stanza.IQ) { return } - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err != nil || chat == nil || !session.IsGroup(chat) { iqAnswerSetError(answer, 405) return @@ -2116,7 +2117,7 @@ func handleSetQueryMucOwner(s xmpp.Sender, iq *stanza.IQ, query *extensions.Quer return } - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err != nil || chat == nil || !session.IsGroup(chat) { iqAnswerSetError(answer, 405) return @@ -2227,7 +2228,7 @@ func handleSetQueryMAM(s xmpp.Sender, iq *stanza.IQ, query extensions.MAMQuery) return } - chat, _, err := session.GetContactByID(toID, nil) + chat, _, err := session.GetContactByID(toID, nil, true) if err != nil || chat == nil || !session.IsGroup(chat) { iqAnswerSetError(answer, 405) return @@ -2870,8 +2871,8 @@ func sendPubSubAvatarNotifications(s xmpp.Sender, jid string, session *telegram. return } - for _, chatId := range session.ChatsKeys() { - chat, _, err := session.GetContactByID(chatId, nil) + for _, chatId := range session.OwnChatsKeys() { + chat, _, err := session.GetContactByID(chatId, nil, true) if err != nil || chat == nil { continue }