Fix status caching and send status on subscription

This commit is contained in:
Bohdan Horbeshko 2022-02-05 05:21:56 -05:00
parent 39d38fb487
commit 4b2969925b
2 changed files with 60 additions and 27 deletions

View file

@ -194,8 +194,11 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, a
}
}
cachedStatus, ok := c.cache.GetStatus(chatID)
if status == "" {
if user != nil {
if ok {
show, status = cachedStatus.XMPP, cachedStatus.Description
} else if user != nil && user.Status != nil {
show, status = c.userStatusToText(user.Status, chatID)
} else {
show, status = "chat", chat.Title

View file

@ -60,32 +60,24 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
}).Warn("Message")
log.Debugf("%#v", msg)
fromJid, err := stanza.NewJid(msg.From)
if err != nil {
log.Error("Invalid from JID!")
bare, resource, ok := splitFrom(msg.From)
if !ok {
return
}
session, ok := sessions[fromJid.Bare()]
session, ok := sessions[bare]
if !ok {
log.Error("Message from stranger")
return
}
toParts := strings.Split(msg.To, "@")
toID := toParts[0]
if len(toParts) > 1 {
toIDInt, err := strconv.ParseInt(toID, 10, 64)
if err == nil {
session.ProcessOutgoingMessage(toIDInt, msg.Body, msg.From)
return
}
log.WithFields(log.Fields{
"toID": toID,
}).Error(errors.Wrap(err, "Invalid to JID!"))
} else if toID == gateway.Jid.Bare() {
toID, ok := toToID(msg.To)
if ok {
session.ProcessOutgoingMessage(toID, msg.Body, msg.From)
return
} else if msg.To == gateway.Jid.Bare() {
if strings.HasPrefix(msg.Body, "/") {
response := session.ProcessTransportCommand(msg.Body, fromJid.Resource)
response := session.ProcessTransportCommand(msg.Body, resource)
if response != "" {
gateway.SendMessage(msg.From, "", response, component)
}
@ -133,6 +125,20 @@ func handleSubscription(s xmpp.Sender, p stanza.Presence) {
}
_ = gateway.ResumableSend(component, reply)
toID, ok := toToID(p.To)
if !ok {
return
}
bare, _, ok := splitFrom(p.From)
if !ok {
return
}
session, ok := getTelegramInstance(bare, &persistence.Session{}, component)
if !ok {
return
}
go session.ProcessStatusUpdate(toID, "", "", gateway.SPImmed(false))
}
func handlePresence(s xmpp.Sender, p stanza.Presence) {
@ -155,13 +161,11 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
log.Debugf("%#v", p)
// create session
fromJid, err := stanza.NewJid(p.From)
if err != nil {
log.Error("Invalid from JID!")
bare, resource, ok := splitFrom(p.From)
if !ok {
return
}
bareFromJid := fromJid.Bare()
session, ok := getTelegramInstance(bareFromJid, &persistence.Session{}, component)
session, ok := getTelegramInstance(bare, &persistence.Session{}, component)
if !ok {
return
}
@ -169,20 +173,20 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
switch p.Type {
// destroy session
case "unsubscribed", "unsubscribe":
if session.Disconnect(fromJid.Resource, false) {
if session.Disconnect(resource, false) {
sessionLock.Lock()
delete(sessions, bareFromJid)
delete(sessions, bare)
sessionLock.Unlock()
}
// go offline
case "unavailable", "error":
session.Disconnect(fromJid.Resource, false)
session.Disconnect(resource, false)
// go online
case "probe", "", "online":
// due to the weird implementation of go-tdlib wrapper, it won't
// return the client instance until successful authorization
go func() {
err = session.Connect(fromJid.Resource)
err := session.Connect(resource)
if err != nil {
log.Error(errors.Wrap(err, "TDlib connection failure"))
} else {
@ -290,3 +294,29 @@ func handleGetVcardTempIq(s xmpp.Sender, iq *stanza.IQ) {
_ = gateway.ResumableSend(component, &answer)
}
func splitFrom(from string) (string, string, bool) {
fromJid, err := stanza.NewJid(from)
if err != nil {
log.WithFields(log.Fields{
"from": from,
}).Error(errors.Wrap(err, "Invalid from JID!"))
return "", "", false
}
return fromJid.Bare(), fromJid.Resource, true
}
func toToID(to string) (int64, bool) {
toParts := strings.Split(to, "@")
if len(toParts) < 2 {
return 0, false
}
toID, err := strconv.ParseInt(toParts[0], 10, 64)
if err != nil {
log.WithFields(log.Fields{
"to": to,
}).Error(errors.Wrap(err, "Invalid to JID!"))
return 0, false
}
return toID, true
}