Fix presences sent with no resource

This commit is contained in:
Bohdan Horbeshko 2024-01-24 18:52:40 -05:00
parent 4532748c84
commit b40ccf4a4d
4 changed files with 31 additions and 36 deletions

View file

@ -185,12 +185,8 @@ func keyValueString(key, value string) string {
} }
func (c *Client) unsubscribe(chatID int64) error { func (c *Client) unsubscribe(chatID int64) error {
return gateway.SendPresence( args := gateway.SimplePresence(chatID, "unsubscribed")
c.xmpp, return c.sendPresence(args...)
c.jid,
gateway.SPFrom(strconv.FormatInt(chatID, 10)),
gateway.SPType("unsubscribed"),
)
} }
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) { func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {

View file

@ -2,7 +2,6 @@ package telegram
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"strconv"
"time" "time"
"dev.narayana.im/narayana/telegabber/xmpp/gateway" "dev.narayana.im/narayana/telegabber/xmpp/gateway"
@ -159,7 +158,7 @@ func (c *Client) Connect(resource string) error {
} }
gateway.SubscribeToTransport(c.xmpp, c.jid) gateway.SubscribeToTransport(c.xmpp, c.jid)
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login)) c.sendPresence(gateway.SPStatus("Logged in as: "+c.Session.Login))
}() }()
return nil return nil
@ -228,12 +227,8 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
// we're offline (unsubscribe if logout) // we're offline (unsubscribe if logout)
for _, id := range c.cache.ChatsKeys() { for _, id := range c.cache.ChatsKeys() {
gateway.SendPresence( args := gateway.SimplePresence(id, "unavailable")
c.xmpp, c.sendPresence(args...)
c.jid,
gateway.SPFrom(strconv.FormatInt(id, 10)),
gateway.SPType("unavailable"),
)
} }
c.close() c.close()

View file

@ -281,22 +281,17 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
c.cache.SetStatus(chatID, cacheShow, status) c.cache.SetStatus(chatID, cacheShow, status)
newArgs := []args.V{ newArgs := []args.V{
gateway.SPFrom(strconv.FormatInt(chatID, 10)),
gateway.SPShow(show), gateway.SPShow(show),
gateway.SPStatus(status), gateway.SPStatus(status),
gateway.SPPhoto(photo), gateway.SPPhoto(photo),
gateway.SPResource(gateway.Jid.Resource),
gateway.SPImmed(gateway.SPImmed.Get(oldArgs)), gateway.SPImmed(gateway.SPImmed.Get(oldArgs)),
} }
newArgs = gateway.SPAppendFrom(newArgs, chatID)
if presenceType != "" { if presenceType != "" {
newArgs = append(newArgs, gateway.SPType(presenceType)) newArgs = append(newArgs, gateway.SPType(presenceType))
} }
return gateway.SendPresence( return c.sendPresence(newArgs...)
c.xmpp,
c.jid,
newArgs...,
)
} }
func (c *Client) formatContact(chatID int64) string { func (c *Client) formatContact(chatID int64) string {
@ -1292,7 +1287,7 @@ func (c *Client) roster(resource string) {
c.ProcessStatusUpdate(chat, "", "") c.ProcessStatusUpdate(chat, "", "")
} }
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login)) c.sendPresence(gateway.SPStatus("Logged in as: "+c.Session.Login))
c.addResource(resource) c.addResource(resource)
} }
@ -1393,9 +1388,7 @@ func (c *Client) GetChatDescription(chat *client.Chat) string {
// subscribe to a Telegram ID // subscribe to a Telegram ID
func (c *Client) subscribeToID(id int64, chat *client.Chat) { func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V args := gateway.SimplePresence(id, "subscribe")
args = append(args, gateway.SPFrom(strconv.FormatInt(id, 10)))
args = append(args, gateway.SPType("subscribe"))
if chat == nil { if chat == nil {
chat, _, _ = c.GetContactByID(id, nil) chat, _, _ = c.GetContactByID(id, nil)
@ -1406,11 +1399,11 @@ func (c *Client) subscribeToID(id int64, chat *client.Chat) {
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp) gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
} }
gateway.SendPresence( c.sendPresence(args...)
c.xmpp, }
c.jid,
args..., func (c *Client) sendPresence(args ...args.V) error {
) return gateway.SendPresence(c.xmpp, c.jid, args...)
} }
func (c *Client) prepareDiskSpace(size uint64) { func (c *Client) prepareDiskSpace(size uint64) {
@ -1459,9 +1452,9 @@ func (c *Client) UpdateChatNicknames() {
chat, ok := c.cache.GetChat(id) chat, ok := c.cache.GetChat(id)
if ok { if ok {
newArgs := []args.V{ newArgs := []args.V{
gateway.SPFrom(strconv.FormatInt(id, 10)),
gateway.SPNickname(chat.Title), gateway.SPNickname(chat.Title),
} }
newArgs = gateway.SPAppendFrom(newArgs, id)
cachedStatus, ok := c.cache.GetStatus(id) cachedStatus, ok := c.cache.GetStatus(id)
if ok { if ok {
@ -1472,11 +1465,7 @@ func (c *Client) UpdateChatNicknames() {
} }
} }
gateway.SendPresence( c.sendPresence(newArgs...)
c.xmpp,
c.jid,
newArgs...,
)
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp) gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
} }

View file

@ -3,6 +3,7 @@ package gateway
import ( import (
"encoding/xml" "encoding/xml"
"github.com/pkg/errors" "github.com/pkg/errors"
"strconv"
"strings" "strings"
"sync" "sync"
@ -343,6 +344,20 @@ func SendPresence(component *xmpp.Component, to string, args ...args.V) error {
return nil return nil
} }
// SPAppendFrom appends numeric from and resource to varargs
func SPAppendFrom(oldArgs []args.V, id int64) []args.V {
newArgs := append(oldArgs, SPFrom(strconv.FormatInt(id, 10)))
newArgs = append(newArgs, SPResource(Jid.Resource))
return newArgs
}
// SimplePresence crafts simple presence varargs
func SimplePresence(from int64, typ string) []args.V {
args := []args.V{SPType(typ)}
args = SPAppendFrom(args, from)
return args
}
// ResumableSend tries to resume the connection once and sends the packet again // ResumableSend tries to resume the connection once and sends the packet again
func ResumableSend(component *xmpp.Component, packet stanza.Packet) error { func ResumableSend(component *xmpp.Component, packet stanza.Packet) error {
err := component.Send(packet) err := component.Send(packet)