aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-06-30 03:10:39 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-06-30 03:10:39 +0300
commit5628a15ac496d72c840a265c3dace720e2547728 (patch)
tree7a4a1c68cb320fc58738bb5da23620b8d790c143
parent38ffdb9e4791652b09d59643c8e0b200c2566e1c (diff)
Update the contact nickname when a chat title has changed
-rw-r--r--telegram/handlers.go17
-rw-r--r--xmpp/gateway/gateway.go42
2 files changed, 59 insertions, 0 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go
index 919c173..126d858 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -135,6 +135,12 @@ func (c *Client) updateHandler() {
uhOh()
}
c.updateMessageSendFailed(typedUpdate)
+ case client.TypeUpdateChatTitle:
+ typedUpdate, ok := update.(*client.UpdateChatTitle)
+ if !ok {
+ uhOh()
+ }
+ c.updateChatTitle(typedUpdate)
default:
// log only handled types
continue
@@ -277,3 +283,14 @@ func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed)
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))
+ }
+}
diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go
index 4edaec4..367a406 100644
--- a/xmpp/gateway/gateway.go
+++ b/xmpp/gateway/gateway.go
@@ -13,6 +13,8 @@ import (
"gosrc.io/xmpp/stanza"
)
+const NSNick string = "http://jabber.org/protocol/nick"
+
// Queue stores presences to send later
var Queue = make(map[string]*stanza.Presence)
var QueueLock = sync.Mutex{}
@@ -52,6 +54,46 @@ func SendMessage(to string, from string, body string, component *xmpp.Component)
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
if log.GetLevel() == log.DebugLevel {
xmlMessage, err := xml.Marshal(message)