Keep two chat caches to avoid unnecessary presences

This commit is contained in:
Bohdan Horbeshko 2025-09-16 08:15:42 -04:00
parent 326c94973a
commit 140cf7fa4a
9 changed files with 94 additions and 61 deletions

View file

@ -2,7 +2,7 @@
COMMIT := $(shell git rev-parse --short HEAD)
TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551"
VERSION := "v1.12.5"
VERSION := "v1.12.6"
MAKEOPTS := "-j4"
all:

View file

@ -16,7 +16,7 @@ import (
goxmpp "gosrc.io/xmpp"
)
var version string = "1.12.5"
var version string = "1.12.6"
var commit string
var sm *goxmpp.StreamManager

View file

@ -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

View file

@ -218,7 +218,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) {
@ -378,7 +378,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.unsubscribe(id)
}
@ -505,7 +505,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
}
@ -555,7 +555,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
}
@ -835,7 +835,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
}
@ -862,7 +862,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
}
@ -881,7 +881,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
}
@ -918,7 +918,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
}
@ -946,7 +946,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
}
@ -974,7 +974,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
}
@ -992,7 +992,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
}
@ -1064,7 +1064,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
}

View file

@ -229,7 +229,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...)
}

View file

@ -186,13 +186,13 @@ func (c *Client) updateHandler() {
func (c *Client) updateUser(update *client.UpdateUser) {
c.cache.SetUser(update.User.Id, update.User)
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
@ -206,14 +206,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)
}
if update.Chat.Id < 0 {
c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat")
c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat", true)
}
}()
}
@ -377,7 +377,7 @@ 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
}
@ -432,9 +432,9 @@ func (c *Client) updateChatTitle(update *client.UpdateChatTitle) {
gateway.SetNickname(c.jid, strconv.FormatInt(update.ChatId, 10), update.Title, c.xmpp)
// set also the status (for group chats only)
chat, user, _ := c.GetContactByID(update.ChatId, nil)
chat, user, _ := c.GetContactByID(update.ChatId, nil, false)
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

View file

@ -94,7 +94,7 @@ const (
const AVATAR_SIZE_LIMIT int64 = 128 * 1024
// 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
}
@ -119,11 +119,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
}
@ -158,9 +158,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 {
@ -171,7 +171,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
}
// GetChatType obtains chat type from its information
func (c *Client) GetChatType(id int64) (ChatType, error) {
func (c *Client) GetChatType(id int64, own bool) (ChatType, error) {
if !c.Online() || id == 0 {
return ChatTypeUnknown, errOffline
}
@ -187,7 +187,7 @@ func (c *Client) GetChatType(id int64) (ChatType, error) {
return ChatTypeUnknown, err
}
c.cache.SetChat(id, chat)
c.cache.SetChat(id, chat, own)
}
chatType := chat.Type.ChatTypeType()
@ -209,8 +209,8 @@ func (c *Client) GetChatType(id int64) (ChatType, error) {
}
// IsPM checks if a chat is PM
func (c *Client) IsPM(id int64) (bool, error) {
typ, err := c.GetChatType(id)
func (c *Client) IsPM(id int64, own bool) (bool, error) {
typ, err := c.GetChatType(id, own)
if err != nil {
return false, err
}
@ -222,8 +222,8 @@ func (c *Client) IsPM(id int64) (bool, 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
}
@ -395,7 +395,7 @@ func (c *Client) GetPhotoBase64(photo *client.File) string {
}
// 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
}
@ -404,7 +404,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
}
@ -470,7 +470,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()
}
@ -1091,7 +1091,7 @@ func (c *Client) isCarbonsEnabled() bool {
}
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, suppressReply bool) (string, *gateway.Reply) {
isPM, err := c.IsPM(message.ChatId)
isPM, err := c.IsPM(message.ChatId, true)
if err != nil {
log.Errorf("Could not determine if chat is PM: %v", err)
}
@ -1215,8 +1215,8 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
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 = "<Chat photo has changed>"
if chat.Photo == nil {
@ -1258,7 +1258,7 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
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()
}
}
@ -1456,9 +1456,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
@ -1515,8 +1515,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))
@ -1630,7 +1630,7 @@ func (c *Client) subscribeToID(id int64, chat *client.Chat) {
args := gateway.SimplePresence(id, "subscribe")
if chat == nil {
chat, _, _ = c.GetContactByID(id, nil)
chat, _, _ = c.GetContactByID(id, nil, true)
}
if chat != nil {
args = append(args, gateway.SPNickname(chat.Title))
@ -1660,7 +1660,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
}
@ -1687,7 +1687,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 {
newArgs := []args.V{
@ -1810,7 +1810,7 @@ func (c *Client) GetChatMembers(chatID int64, limited bool, query string, member
if limited {
limit = 20
chat, _, err := c.GetContactByID(chatID, nil)
chat, _, err := c.GetContactByID(chatID, nil, true)
if err != nil {
return nil, err
} else if chat == nil {

View file

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

View file

@ -363,7 +363,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) {
@ -434,6 +434,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
status.ID,
description,
show,
true,
newArgs...,
)
}
@ -548,7 +549,7 @@ func handleGetAvatarDataIq(s xmpp.Sender, iq *stanza.IQ, pubsub *stanza.PubSubGe
if !ok {
log.Info("Could not find avatar in cache, fetching immediately")
chat, _, err := session.GetContactByID(chatId, nil)
chat, _, err := session.GetContactByID(chatId, nil, true)
if err != nil || chat == nil || chat.Photo == nil {
return
}
@ -609,7 +610,7 @@ func getTelegramChatType(from string, to string) (telegram.ChatType, error) {
if ok {
session, ok := sessions[bare]
if ok {
return session.GetChatType(toId)
return session.GetChatType(toId, true)
}
}
}
@ -713,7 +714,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 {
di.AddItem(iq.To, "botmenu", "Bot Menu")
}
@ -1377,8 +1378,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
}