From 8cfb04a5edc80df505d5691e91641f4c610895d7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 26 Jun 2020 00:05:51 +0300 Subject: [PATCH] Handle WhatsApp invites. Fixes #120 --- portal.go | 21 ++++++++++++++++++++- user.go | 2 ++ whatsapp-ext/chat.go | 3 ++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/portal.go b/portal.go index 63e2aad..682fc3e 100644 --- a/portal.go +++ b/portal.go @@ -549,7 +549,7 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) { func (portal *Portal) GetBasePowerLevels() *event.PowerLevelsEventContent { anyone := 0 nope := 99 - invite := 99 + invite := 50 if portal.bridge.Config.Bridge.AllowUserInvite { invite = 0 } @@ -1328,6 +1328,25 @@ func (portal *Portal) HandleWhatsAppKick(senderJID string, jids []string) { } } +func (portal *Portal) HandleWhatsAppInvite(senderJID string, jids []string) { + senderIntent := portal.MainIntent() + if senderJID != "unknown" { + sender := portal.bridge.GetPuppetByJID(senderJID) + senderIntent = sender.IntentFor(portal) + } + for _, jid := range jids { + puppet := portal.bridge.GetPuppetByJID(jid) + _, err := senderIntent.InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: puppet.MXID}) + if err != nil { + portal.log.Warnfln("Failed to invite %s as %s: %v", puppet.MXID, senderIntent.UserID, err) + } + err = puppet.DefaultIntent().EnsureJoined(portal.MXID) + if err != nil { + portal.log.Errorfln("Failed to ensure %s is joined: %v", puppet.MXID, err) + } + } +} + type base struct { download func() ([]byte, error) info whatsapp.MessageInfo diff --git a/user.go b/user.go index 503ba70..fcf28b4 100644 --- a/user.go +++ b/user.go @@ -889,6 +889,8 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) { go portal.RestrictMetadataChanges(cmd.Data.Restrict) case whatsappExt.ChatActionRemove: go portal.HandleWhatsAppKick(cmd.Data.SenderJID, cmd.Data.UserChange.JIDs) + case whatsappExt.ChatActionAdd: + go portal.HandleWhatsAppInvite(cmd.Data.SenderJID, cmd.Data.UserChange.JIDs) case whatsappExt.ChatActionIntroduce: if cmd.Data.SenderJID != "unknown" { go portal.Sync(user, whatsapp.Contact{Jid: portal.Key.JID}) diff --git a/whatsapp-ext/chat.go b/whatsapp-ext/chat.go index 93ca56d..60faa22 100644 --- a/whatsapp-ext/chat.go +++ b/whatsapp-ext/chat.go @@ -47,6 +47,7 @@ const ( ChatActionDemote ChatActionType = "demote" ChatActionIntroduce ChatActionType = "introduce" ChatActionRemove ChatActionType = "remove" + ChatActionAdd ChatActionType = "add" ) type ChatUpdateData struct { @@ -128,7 +129,7 @@ func (cud *ChatUpdateData) UnmarshalJSON(data []byte) error { unmarshalTo = &cud.Restrict case ChatActionAnnounce: unmarshalTo = &cud.Announce - case ChatActionPromote, ChatActionDemote, ChatActionRemove: + case ChatActionPromote, ChatActionDemote, ChatActionRemove, ChatActionAdd: unmarshalTo = &cud.UserChange default: return nil