Merge branch 'master' into muc

This commit is contained in:
Bohdan Horbeshko 2025-09-16 10:08:47 -04:00
commit a6d848194d
7 changed files with 116 additions and 83 deletions

View file

@ -16,7 +16,8 @@ type Status struct {
// Cache allows operating the chats and users cache in // Cache allows operating the chats and users cache in
// a thread-safe manner // a thread-safe manner
type Cache struct { type Cache struct {
chats map[int64]*client.Chat ownChats map[int64]*client.Chat
auxChats map[int64]*client.Chat
users map[int64]*client.User users map[int64]*client.User
statuses map[int64]*Status statuses map[int64]*Status
chatsLock sync.Mutex chatsLock sync.Mutex
@ -27,7 +28,8 @@ type Cache struct {
// NewCache initializes a cache // NewCache initializes a cache
func NewCache() *Cache { func NewCache() *Cache {
return &Cache{ return &Cache{
chats: map[int64]*client.Chat{}, ownChats: map[int64]*client.Chat{},
auxChats: map[int64]*client.Chat{},
users: map[int64]*client.User{}, users: map[int64]*client.User{},
statuses: map[int64]*Status{}, statuses: map[int64]*Status{},
} }
@ -40,7 +42,23 @@ func (cache *Cache) ChatsKeys() []int64 {
defer cache.chatsLock.Unlock() defer cache.chatsLock.Unlock()
var keys []int64 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) keys = append(keys, id)
} }
return keys return keys
@ -84,7 +102,10 @@ func (cache *Cache) GetChat(id int64) (*client.Chat, bool) {
cache.chatsLock.Lock() cache.chatsLock.Lock()
defer cache.chatsLock.Unlock() defer cache.chatsLock.Unlock()
chat, ok := cache.chats[id] chat, ok := cache.ownChats[id]
if !ok {
chat, ok = cache.auxChats[id]
}
return chat, ok return chat, ok
} }
@ -107,11 +128,21 @@ func (cache *Cache) GetStatus(id int64) (*Status, bool) {
} }
// SetChat stores a chat in the cache // 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() cache.chatsLock.Lock()
defer cache.chatsLock.Unlock() 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 // SetUser stores a user in the cache

View file

@ -222,7 +222,7 @@ func (c *Client) helpString(typ CommandType, chatId int64) string {
var str strings.Builder var str strings.Builder
commandMap := GetCommands(typ) commandMap := GetCommands(typ)
chatType, _, chatTypeErr := c.GetChatType(chatId) chatType, _, chatTypeErr := c.GetChatType(chatId, true)
str.WriteString("Available commands:\n") str.WriteString("Available commands:\n")
for _, name := range SortedCommandKeys(commandMap) { 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 return errors.Wrap(err, "Logout error").Error(), false
} }
for _, id := range c.cache.ChatsKeys() { for _, id := range c.cache.OwnChatsKeys() {
c.leaveChat(id) c.leaveChat(id)
} }
@ -498,7 +498,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
return strings.Join(entries, "\n"), true return strings.Join(entries, "\n"), true
case "report": case "report":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), false return err.Error(), false
} }
@ -583,7 +583,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
return notEnoughArguments, true, false return notEnoughArguments, true, false
} }
chatType, _, chatTypeErr := c.GetChatType(chatID) chatType, _, chatTypeErr := c.GetChatType(chatID, true)
if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { if chatTypeErr == nil && !IsCommandForChatType(command, chatType) {
return "Not applicable for this chat type", true, false 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 // invite @username to current groupchat
case "invite": case "invite":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }
@ -890,7 +890,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
return link.InviteLink, true, true return link.InviteLink, true, true
// kick @username from current group chat // kick @username from current group chat
case "kick": case "kick":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }
@ -905,7 +905,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
// mute [@username [n hours]] // mute [@username [n hours]]
case "mute": case "mute":
if len(args) > 0 { if len(args) > 0 {
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }
@ -934,7 +934,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
// unmute [@username] // unmute [@username]
case "unmute": case "unmute":
if len(args) > 0 { if len(args) > 0 {
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false 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] // ban @username from current chat [for N hours]
case "ban": case "ban":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }
@ -976,7 +976,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
} }
// unban @username // unban @username
case "unban": case "unban":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }
@ -990,7 +990,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
} }
// promote @username to admin // promote @username to admin
case "promote": case "promote":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0], false)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }
@ -1052,7 +1052,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
} }
// close secret chat // close secret chat
case "close": case "close":
chat, _, err := c.GetContactByID(chatID, nil) chat, _, err := c.GetContactByID(chatID, nil, true)
if err != nil { if err != nil {
return err.Error(), true, false return err.Error(), true, false
} }

View file

@ -232,7 +232,7 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
log.Warn("Disconnecting from Telegram network...") log.Warn("Disconnecting from Telegram network...")
// we're offline (unsubscribe if logout) // we're offline (unsubscribe if logout)
for _, id := range c.cache.ChatsKeys() { for _, id := range c.cache.OwnChatsKeys() {
args := gateway.SimplePresence(id, "unavailable") args := gateway.SimplePresence(id, "unavailable")
c.sendPresence(args...) c.sendPresence(args...)
} }

View file

@ -157,13 +157,13 @@ func (c *Client) updateUser(update *client.UpdateUser) {
} }
show, status, presenceType := c.userStatusToText(update.User.Status, update.User.Id) 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 // user status changed
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
show, status, presenceType := c.userStatusToText(update.Status, update.UserId) 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 // 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 { if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 {
c.subscribeToID(update.Chat.Id, update.Chat, false) c.subscribeToID(update.Chat.Id, update.Chat, false)
} }
if update.Chat.Id < 0 { 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) 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) isMUC := c.Session.MUC && c.IsGroup(chat)
var jids []string var jids []string
@ -398,14 +398,14 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
return return
} }
if c.Session.IgnoreGroupDeletions { if c.Session.IgnoreGroupDeletions {
chatType, _, chatTypeErr := c.GetChatType(update.ChatId) chatType, _, chatTypeErr := c.GetChatType(update.ChatId, false)
if chatTypeErr == nil && (chatType == ChatTypeBasicGroup || chatType == ChatTypeSupergroup) { if chatTypeErr == nil && (chatType == ChatTypeBasicGroup || chatType == ChatTypeSupergroup) {
return return
} }
} }
var isGroupchat bool var isGroupchat bool
chat, _, _ := c.GetContactByID(update.ChatId, nil) chat, _, _ := c.GetContactByID(update.ChatId, nil, false)
if c.Session.MUC && c.IsGroup(chat) { if c.Session.MUC && c.IsGroup(chat) {
isGroupchat = true isGroupchat = true
} }
@ -489,7 +489,7 @@ func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed)
// chat title changed // chat title changed
func (c *Client) updateChatTitle(update *client.UpdateChatTitle) { 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) { if c.Session.MUC && c.IsGroup(chat) {
return return
} }
@ -498,7 +498,7 @@ func (c *Client) updateChatTitle(update *client.UpdateChatTitle) {
// set also the status (for group chats only) // set also the status (for group chats only)
if user == nil { 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 // update chat title in the cache
@ -528,7 +528,7 @@ func (c *Client) updateBasicGroupFullInfo(update *client.UpdateBasicGroupFullInf
} }
func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) { 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 // update chat permissions in the cache
if chat != nil { if chat != nil {

View file

@ -149,7 +149,7 @@ const (
type ChatMemberStatus int type ChatMemberStatus int
// GetContactByUsername resolves username to user id retrieves user and chat information // 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() { if !c.Online() {
return nil, nil, errOffline 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) // 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 { if !c.Online() || id == 0 {
return nil, nil, errOffline return nil, nil, errOffline
} }
@ -213,9 +213,9 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
return nil, nil, err return nil, nil, err
} }
c.cache.SetChat(id, cacheChat) c.cache.SetChat(id, cacheChat, own)
} else { } else {
c.cache.SetChat(id, chat) c.cache.SetChat(id, chat, own)
} }
} }
if chat == nil { 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 // 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) { func (c *Client) GetChatByID(id int64, chat *client.Chat, own bool) (*client.Chat, error) {
chat, _, err := c.GetContactByID(id, nil) chat, _, err := c.GetContactByID(id, nil, own)
if err != nil { if err != nil {
return nil, err return nil, err
} else if chat == nil { } 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 // 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 { if !c.Online() || id == 0 {
return ChatTypeUnknown, nil, errOffline return ChatTypeUnknown, nil, errOffline
} }
@ -254,7 +254,7 @@ func (c *Client) GetChatType(id int64) (ChatType, *client.Chat, error) {
return ChatTypeUnknown, nil, err return ChatTypeUnknown, nil, err
} }
c.cache.SetChat(id, chat) c.cache.SetChat(id, chat, own)
} }
chatType := chat.Type.ChatTypeType() 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 // IsPM checks if a chat is PM
func (c *Client) IsPM(id int64) (bool, *client.Chat, error) { func (c *Client) IsPM(id int64, own bool) (bool, *client.Chat, error) {
typ, chat, err := c.GetChatType(id) typ, chat, err := c.GetChatType(id, own)
if err != nil { if err != nil {
return false, chat, err 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 // IsBot checks if a chat is a bot
func (c *Client) IsBot(id int64) (bool, error) { func (c *Client) IsBot(id int64, own bool) (bool, error) {
_, user, err := c.GetContactByID(id, nil) _, user, err := c.GetContactByID(id, nil, own)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -470,7 +470,7 @@ func (c *Client) GetHashedAvatar(chatId int64) *HashedAvatar {
if !ok { if !ok {
log.Info("Could not find avatar in cache, fetching immediately") 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 { if err != nil || chat == nil || chat.Photo == nil {
return nil return nil
} }
@ -488,7 +488,7 @@ func (c *Client) GetHashedAvatar(chatId int64) *HashedAvatar {
} }
// ProcessStatusUpdate sets contact status // 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() { if !c.Online() {
return nil return nil
} }
@ -497,7 +497,7 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
"chat_id": chatID, "chat_id": chatID,
}).Info("Status update for") }).Info("Status update for")
chat, user, err := c.GetContactByID(chatID, nil) chat, user, err := c.GetContactByID(chatID, nil, own)
if err != nil { if err != nil {
return err return err
} }
@ -713,7 +713,7 @@ func (c *Client) updateMUCOccupants(mucState *MUCState, chatID int64, members []
myAffiliation := "member" myAffiliation := "member"
myRole := "participant" myRole := "participant"
chat, _, _ := c.GetContactByID(chatID, nil) chat, _, _ := c.GetContactByID(chatID, nil, true)
_, toJids := c.getMUCJoinedJIDs(chatID, mucState, false) _, 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) log.Warnf("Resourceprep for %v failed, falling back to chat ID", fc)
var usernames string var usernames string
_, user, _ := c.GetContactByID(chatID, nil) _, user, _ := c.GetContactByID(chatID, nil, false)
if user != nil && user.Usernames != nil { if user != nil && user.Usernames != nil {
usernames = c.usernamesToString(user.Usernames.ActiveUsernames) usernames = c.usernamesToString(user.Usernames.ActiveUsernames)
} }
@ -1066,7 +1066,7 @@ func (c *Client) FormatContact(chatID int64) string {
return "" return ""
} }
chat, user, err := c.GetContactByID(chatID, nil) chat, user, err := c.GetContactByID(chatID, nil, false)
if err != nil { if err != nil {
return "unknown contact: " + err.Error() 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) { 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 { if err != nil {
log.Errorf("Could not determine chat type: %v", err) 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 // ProcessIncomingMessage is a legacy wrapper for SendMessageToGateway aiming only PM messages
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
chat, _, _ := c.GetContactByID(chatId, nil) chat, _, _ := c.GetContactByID(chatId, nil, true)
safeToSend := true safeToSend := true
groupChatFrom := "" groupChatFrom := ""
groupChatTos := []string{} groupChatTos := []string{}
@ -1898,8 +1898,8 @@ func (c *Client) SendMessageToGateway(chatId int64, message *client.Message, id
ChatId: chatId, ChatId: chatId,
}) })
if err == nil { if err == nil {
c.cache.SetChat(chatId, chat) c.cache.SetChat(chatId, chat, true)
go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true)) go c.ProcessStatusUpdate(chatId, "", "", true, gateway.SPImmed(true))
text = "<Chat photo has changed>" text = "<Chat photo has changed>"
if chat.Photo == nil { if chat.Photo == nil {
@ -1953,7 +1953,7 @@ func (c *Client) SendMessageToGateway(chatId int64, message *client.Message, id
var ignorePrefix bool var ignorePrefix bool
if oobSwap { if oobSwap {
if text == "" || message.Content.MessageContentType() == client.TypeMessageSticker { 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() 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 { if err == nil {
status = chatMember.Status status = chatMember.Status
} }
chat, err := c.GetChatByID(chatId, nil) chat, err := c.GetChatByID(chatId, nil, true)
if err == nil { if err == nil {
affiliation, role := c.memberStatusToAffiliationAndRole(status, chat) affiliation, role := c.memberStatusToAffiliationAndRole(status, chat)
mucUserItem = &gateway.MUCUserItem{ 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) gateway.SendErrorMessage(returnJid, gateway.MUCJID(chatID), text, code, isGroupchat, c.xmpp)
} else { } else {
var nickname string var nickname string
chat, err := c.GetChatByID(chatID, nil) chat, err := c.GetChatByID(chatID, nil, true)
if err == nil { if err == nil {
nickname = chat.Title nickname = chat.Title
} }
@ -2235,9 +2235,9 @@ func (c *Client) prepareOutgoingMessageContent(text string, file *client.InputFi
return content return content
} }
// ChatsKeys proxies the following function from unexported cache // OwnChatsKeys proxies the following function from unexported cache
func (c *Client) ChatsKeys() []int64 { func (c *Client) OwnChatsKeys() []int64 {
return c.cache.ChatsKeys() return c.cache.OwnChatsKeys()
} }
// StatusesRange proxies the following function from unexported cache // 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) log.Warnf("Sending roster for %v", resource)
for _, chat := range c.cache.ChatsKeys() { for _, chat := range c.cache.OwnChatsKeys() {
c.ProcessStatusUpdate(chat, "", "") c.ProcessStatusUpdate(chat, "", "", true)
} }
c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login)) c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login))
@ -2617,7 +2617,7 @@ func (c *Client) GetGroupChats() []*client.Chat {
}) })
if err == nil { if err == nil {
for _, id := range chats.ChatIds { for _, id := range chats.ChatIds {
chat, _, _ := c.GetContactByID(id, nil) chat, _, _ := c.GetContactByID(id, nil, true)
if chat != nil && c.IsGroup(chat) { if chat != nil && c.IsGroup(chat) {
groupChats = append(groupChats, 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") args := gateway.SimplePresence(id, "subscribe")
if chat == nil { if chat == nil {
chat, _, _ = c.GetContactByID(id, nil) chat, _, _ = c.GetContactByID(id, nil, true)
} }
if chat != nil { if chat != nil {
if c.Session.MUC && c.IsGroup(chat) { 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) { func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) {
var info VCardInfo var info VCardInfo
chat, user, err := c.GetContactByID(toID, nil) chat, user, err := c.GetContactByID(toID, nil, false)
if err != nil { if err != nil {
return info, err return info, err
} }
@ -2709,7 +2709,7 @@ func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) {
} }
func (c *Client) UpdateChatNicknames() { func (c *Client) UpdateChatNicknames() {
for _, id := range c.cache.ChatsKeys() { for _, id := range c.cache.OwnChatsKeys() {
chat, ok := c.cache.GetChat(id) chat, ok := c.cache.GetChat(id)
if ok { if ok {
if c.Session.MUC && c.IsGroup(chat) { if c.Session.MUC && c.IsGroup(chat) {
@ -2864,7 +2864,7 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message, p
var isMUC bool var isMUC bool
if plain { if plain {
chat, err := c.GetChatByID(chatID, nil) chat, err := c.GetChatByID(chatID, nil, true)
if err == nil { if err == nil {
isMUC = c.Session.MUC && c.IsGroup(chat) 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 // 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) { func (c *Client) GetChatMembers(chatID int64, limited bool, query string, membersList MembersList) ([]*client.ChatMember, error) {
if membersList == MembersListCreators { if membersList == MembersListCreators {
chat, err := c.GetChatByID(chatID, nil) chat, err := c.GetChatByID(chatID, nil, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2976,7 +2976,7 @@ func (c *Client) GetChatMembers(chatID int64, limited bool, query string, member
if limited { if limited {
limit = 20 limit = 20
chat, err := c.GetChatByID(chatID, nil) chat, err := c.GetChatByID(chatID, nil, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3057,7 +3057,7 @@ func (c *Client) unsubscribe(chatID int64) error {
} }
func (c *Client) leaveChat(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) { if err == nil && c.Session.MUC && c.IsGroup(chat) {
return c.kickMeFromMUC(chatID, []uint16{110, 307}, false, nil) return c.kickMeFromMUC(chatID, []uint16{110, 307}, false, nil)

View file

@ -143,6 +143,7 @@ func heartbeat(component *xmpp.Component) {
chatID, chatID,
session.LastSeenStatus(delayedStatus.TimestampOnline), session.LastSeenStatus(delayedStatus.TimestampOnline),
"away", "away",
true,
) )
delete(session.DelayedStatuses, chatID) delete(session.DelayedStatuses, chatID)
} }

View file

@ -202,7 +202,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
isGroupchat := msg.Type == "groupchat" isGroupchat := msg.Type == "groupchat"
if session.Session.MUC { if session.Session.MUC {
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err == nil && session.IsGroup(chat) { if err == nil && session.IsGroup(chat) {
if !toIsGroup { if !toIsGroup {
gateway.SendErrorMessage(msg.From, toJid.Node, "KHVATIT SYUDA ZVONITb", 403, false, component) 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 { if !ok {
return return
} }
go session.ProcessStatusUpdate(toID, "", "", gateway.SPImmed(false)) go session.ProcessStatusUpdate(toID, "", "", true, gateway.SPImmed(false))
} }
func handlePresence(s xmpp.Sender, p stanza.Presence) { func handlePresence(s xmpp.Sender, p stanza.Presence) {
@ -573,6 +573,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
status.ID, status.ID,
description, description,
show, show,
true,
newArgs..., newArgs...,
) )
} }
@ -633,7 +634,7 @@ func handleMUCPresence(s xmpp.Sender, p stanza.Presence, mucExt stanza.MucPresen
return return
} }
chat, _, err := session.GetContactByID(chatId, nil) chat, _, err := session.GetContactByID(chatId, nil, true)
if err != nil || !session.IsGroup(chat) { if err != nil || !session.IsGroup(chat) {
presenceReplySetError(reply, 404) presenceReplySetError(reply, 404)
return return
@ -697,7 +698,7 @@ func tryHandleMUCPresence(s xmpp.Sender, p stanza.Presence) {
return return
} }
chat, _, err := session.GetContactByID(chatId, nil) chat, _, err := session.GetContactByID(chatId, nil, true)
if err != nil || !session.IsGroup(chat) { if err != nil || !session.IsGroup(chat) {
return return
} }
@ -938,7 +939,7 @@ func getTelegramChatType(from string, to string) (telegram.ChatType, error) {
if ok { if ok {
session, ok := sessions[bare] session, ok := sessions[bare]
if ok { if ok {
chatType, _, chatTypeErr := session.GetChatType(toId) chatType, _, chatTypeErr := session.GetChatType(toId, true)
return chatType, chatTypeErr return chatType, chatTypeErr
} }
} }
@ -995,7 +996,7 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) {
session, sessionOk := sessions[bare] session, sessionOk := sessions[bare]
if sessionOk && session.Session.MUC { if sessionOk && session.Session.MUC {
if toOk && toIsGroup { if toOk && toIsGroup {
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err == nil && session.IsGroup(chat) { if err == nil && session.IsGroup(chat) {
isMuc = true isMuc = true
disco.AddIdentity(chat.Title, "conference", "text") 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] session, sessionOk := sessions[bare]
if sessionOk && session.Session.MUC { if sessionOk && session.Session.MUC {
if toOk && toIsGroup { if toOk && toIsGroup {
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err == nil && session.IsGroup(chat) { if err == nil && session.IsGroup(chat) {
disco.SetNode(di.Node) disco.SetNode(di.Node)
disco.AddIdentity(session.GetMUCNickname(0), "conference", "text") 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() isOnline = session.Online()
if toOk { if toOk {
isBot, err := session.IsBot(toID) isBot, err := session.IsBot(toID, true)
if err == nil && isBot { if err == nil && isBot {
disco.AddItem(iq.To, "botmenu", "Bot Menu") disco.AddItem(iq.To, "botmenu", "Bot Menu")
} }
@ -1278,7 +1279,7 @@ func handleGetQueryMucAdmin(s xmpp.Sender, iq *stanza.IQ, query *extensions.Quer
return return
} }
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err != nil || !session.IsGroup(chat) { if err != nil || !session.IsGroup(chat) {
iqAnswerSetError(answer, 405) iqAnswerSetError(answer, 405)
return return
@ -1372,7 +1373,7 @@ func handleGetQueryMucOwner(s xmpp.Sender, iq *stanza.IQ) {
return return
} }
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err != nil || chat == nil || !session.IsGroup(chat) { if err != nil || chat == nil || !session.IsGroup(chat) {
iqAnswerSetError(answer, 405) iqAnswerSetError(answer, 405)
return return
@ -1502,7 +1503,7 @@ func handleGetMetadataMAM2(s xmpp.Sender, iq *stanza.IQ) {
return return
} }
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err != nil || chat == nil || !session.IsGroup(chat) { if err != nil || chat == nil || !session.IsGroup(chat) {
iqAnswerSetError(answer, 405) iqAnswerSetError(answer, 405)
return return
@ -2116,7 +2117,7 @@ func handleSetQueryMucOwner(s xmpp.Sender, iq *stanza.IQ, query *extensions.Quer
return return
} }
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err != nil || chat == nil || !session.IsGroup(chat) { if err != nil || chat == nil || !session.IsGroup(chat) {
iqAnswerSetError(answer, 405) iqAnswerSetError(answer, 405)
return return
@ -2227,7 +2228,7 @@ func handleSetQueryMAM(s xmpp.Sender, iq *stanza.IQ, query extensions.MAMQuery)
return return
} }
chat, _, err := session.GetContactByID(toID, nil) chat, _, err := session.GetContactByID(toID, nil, true)
if err != nil || chat == nil || !session.IsGroup(chat) { if err != nil || chat == nil || !session.IsGroup(chat) {
iqAnswerSetError(answer, 405) iqAnswerSetError(answer, 405)
return return
@ -2870,8 +2871,8 @@ func sendPubSubAvatarNotifications(s xmpp.Sender, jid string, session *telegram.
return return
} }
for _, chatId := range session.ChatsKeys() { for _, chatId := range session.OwnChatsKeys() {
chat, _, err := session.GetContactByID(chatId, nil) chat, _, err := session.GetContactByID(chatId, nil, true)
if err != nil || chat == nil { if err != nil || chat == nil {
continue continue
} }