summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorJoseph Mansy <36427684+yousefmansy1@users.noreply.github.com>2023-03-14 15:12:45 -0700
committerGitHub <noreply@github.com>2023-03-14 23:12:45 +0100
commitd42277979abcefe885bb068cad989eaf71460872 (patch)
tree28627bc93cd50851af1c791d496394e379fc4ae9 /bridge
parent24cf007a74bf1e04456ae05c9ccc73b1fb823b88 (diff)
downloadmatterbridge-msglm-d42277979abcefe885bb068cad989eaf71460872.tar.gz
matterbridge-msglm-d42277979abcefe885bb068cad989eaf71460872.tar.bz2
matterbridge-msglm-d42277979abcefe885bb068cad989eaf71460872.zip
Support topic changes and Join/Leave messages (whatsapp) (#2017)
Diffstat (limited to 'bridge')
-rw-r--r--bridge/whatsappmulti/handlers.go73
-rw-r--r--bridge/whatsappmulti/helpers.go23
2 files changed, 96 insertions, 0 deletions
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]