Update the contact nickname when a chat title has changed

This commit is contained in:
Bohdan Horbeshko 2022-06-29 20:10:39 -04:00
parent 38ffdb9e47
commit 5628a15ac4
2 changed files with 59 additions and 0 deletions

View file

@ -135,6 +135,12 @@ func (c *Client) updateHandler() {
uhOh() uhOh()
} }
c.updateMessageSendFailed(typedUpdate) c.updateMessageSendFailed(typedUpdate)
case client.TypeUpdateChatTitle:
typedUpdate, ok := update.(*client.UpdateChatTitle)
if !ok {
uhOh()
}
c.updateChatTitle(typedUpdate)
default: default:
// log only handled types // log only handled types
continue continue
@ -277,3 +283,14 @@ func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed)
c.cleanTempFile(file.Local.Path) c.cleanTempFile(file.Local.Path)
} }
} }
// chat title changed
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)
_, user, _ := c.GetContactByID(update.ChatId, nil)
if user == nil {
c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", gateway.SPImmed(true))
}
}

View file

@ -13,6 +13,8 @@ import (
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
) )
const NSNick string = "http://jabber.org/protocol/nick"
// Queue stores presences to send later // Queue stores presences to send later
var Queue = make(map[string]*stanza.Presence) var Queue = make(map[string]*stanza.Presence)
var QueueLock = sync.Mutex{} var QueueLock = sync.Mutex{}
@ -52,6 +54,46 @@ func SendMessage(to string, from string, body string, component *xmpp.Component)
Body: body, Body: body,
} }
sendMessage(&message, component)
}
// SetNickname sets a new nickname for a contact
func SetNickname(to string, from string, nickname string, component *xmpp.Component) {
componentJid := Jid.Bare()
messageFrom := from + "@" + componentJid
log.WithFields(log.Fields{
"from": from,
"to": to,
}).Warn("Set nickname")
message := stanza.Message{
Attrs: stanza.Attrs{
From: messageFrom,
To: to,
Type: "headline",
},
Extensions: []stanza.MsgExtension{
stanza.PubSubEvent{
EventElement: stanza.ItemsEvent{
Node: NSNick,
Items: []stanza.ItemEvent{
stanza.ItemEvent{
Any: &stanza.Node{
XMLName: xml.Name{Space: NSNick, Local: "nick"},
Content: nickname,
},
},
},
},
},
},
}
sendMessage(&message, component)
}
func sendMessage(message *stanza.Message, component *xmpp.Component) {
// explicit check, as marshalling is expensive // explicit check, as marshalling is expensive
if log.GetLevel() == log.DebugLevel { if log.GetLevel() == log.DebugLevel {
xmlMessage, err := xml.Marshal(message) xmlMessage, err := xml.Marshal(message)