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 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) show, status = c.userStatusToText(user.Status, chatID)
} else { } else {
show, status = "chat", chat.Title show, status = "chat", chat.Title

View file

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