From d42277979abcefe885bb068cad989eaf71460872 Mon Sep 17 00:00:00 2001 From: Joseph Mansy <36427684+yousefmansy1@users.noreply.github.com> Date: Tue, 14 Mar 2023 15:12:45 -0700 Subject: [PATCH] Support topic changes and Join/Leave messages (whatsapp) (#2017) --- bridge/whatsappmulti/handlers.go | 73 ++++++++++++++++++++++++++++++++ bridge/whatsappmulti/helpers.go | 23 ++++++++++ 2 files changed, 96 insertions(+) diff --git a/bridge/whatsappmulti/handlers.go b/bridge/whatsappmulti/handlers.go index ae825f75..8791df5e 100644 --- a/bridge/whatsappmulti/handlers.go +++ b/bridge/whatsappmulti/handlers.go @@ -21,9 +21,82 @@ func (b *Bwhatsapp) eventHandler(evt interface{}) { switch e := evt.(type) { case *events.Message: b.handleMessage(e) + case *events.GroupInfo: + b.handleGroupInfo(e) } } +func (b *Bwhatsapp) handleGroupInfo(event *events.GroupInfo) { + + b.Log.Debugf("Receiving event %#v", event) + + switch { + case event.Join != nil: + b.handleUserJoin(event) + case event.Leave != nil: + b.handleUserLeave(event) + case event.Topic != nil: + b.handleTopicChange(event) + } +} + +func (b *Bwhatsapp) handleUserJoin(event *events.GroupInfo) { + for _, joinedJid := range event.Join { + senderName := b.getSenderNameFromJID(joinedJid) + + rmsg := config.Message{ + UserID: joinedJid.String(), + Username: senderName, + Channel: event.JID.String(), + Account: b.Account, + Protocol: b.Protocol, + Event: config.EventJoinLeave, + Text: "joined chat", + } + + b.Remote <- rmsg + } +} +func (b *Bwhatsapp) handleUserLeave(event *events.GroupInfo) { + for _, leftJid := range event.Leave { + senderName := b.getSenderNameFromJID(leftJid) + + rmsg := config.Message{ + UserID: leftJid.String(), + Username: senderName, + Channel: event.JID.String(), + Account: b.Account, + Protocol: b.Protocol, + Event: config.EventJoinLeave, + Text: "left chat", + } + + b.Remote <- rmsg + } +} +func (b *Bwhatsapp) handleTopicChange(event *events.GroupInfo) { + msg := event.Topic + senderJid := msg.TopicSetBy + senderName := b.getSenderNameFromJID(senderJid) + + text := msg.Topic + if text == "" { + text = "removed topic" + } + + rmsg := config.Message{ + UserID: senderJid.String(), + Username: senderName, + Channel: event.JID.String(), + Account: b.Account, + Protocol: b.Protocol, + Event: config.EventTopicChange, + Text: "Topic changed: " + text, + } + + b.Remote <- rmsg +} + func (b *Bwhatsapp) handleMessage(message *events.Message) { msg := message.Message switch { diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index 963eafa1..b0686190 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -64,6 +64,29 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { return "Someone" } +func (b *Bwhatsapp) getSenderNameFromJID(senderJid types.JID) string { + sender, exists := b.contacts[senderJid] + + if !exists || (sender.FullName == "" && sender.FirstName == "") { + b.reloadContacts() // Contacts may need to be reloaded + sender, exists = b.contacts[senderJid] + } + + if exists && sender.FullName != "" { + return sender.FullName + } + + if exists && sender.FirstName != "" { + return sender.FirstName + } + + if sender.PushName != "" { + return sender.PushName + } + + return "Someone" +} + func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { sender, exists := b.contacts[senderJid]