diff --git a/community.go b/community.go index 2a2376d..3f841f1 100644 --- a/community.go +++ b/community.go @@ -83,6 +83,35 @@ func (user *User) createCommunity() { } } +func (user *User) addPuppetToCommunity(puppet *Puppet) bool { + if user.IsRelaybot || len(user.CommunityID) == 0 { + return false + } + bot := user.bridge.Bot + url := bot.BuildURL("groups", user.CommunityID, "admin", "users", "invite", puppet.MXID) + blankReqBody := map[string]interface{}{} + _, err := bot.MakeRequest(http.MethodPut, url, &blankReqBody, nil) + if err != nil { + user.log.Warnfln("Failed to invite %s to %s: %v", puppet.MXID, user.CommunityID, err) + return false + } + reqBody := map[string]map[string]string{ + "m.visibility": { + "type": "private", + }, + } + url = bot.BuildURLWithQuery([]string{"groups", user.CommunityID, "self", "accept_invite"}, map[string]string{ + "user_id": puppet.MXID, + }) + _, err = bot.MakeRequest(http.MethodPut, url, &reqBody, nil) + if err != nil { + user.log.Warnfln("Failed to join %s as %s: %v", user.CommunityID, puppet.MXID, err) + return false + } + user.log.Debugln("Added", puppet.MXID, "to", user.CommunityID) + return true +} + func (user *User) addPortalToCommunity(portal *Portal) bool { if user.IsRelaybot || len(user.CommunityID) == 0 || len(portal.MXID) == 0 { return false diff --git a/portal.go b/portal.go index 3122639..c5607c7 100644 --- a/portal.go +++ b/portal.go @@ -791,6 +791,10 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { } } user.addPortalToCommunity(portal) + if portal.IsPrivateChat() { + puppet := user.bridge.GetPuppetByJID(portal.Key.JID) + user.addPuppetToCommunity(puppet) + } err = portal.FillInitialHistory(user) if err != nil { portal.log.Errorln("Failed to fill history:", err) diff --git a/user.go b/user.go index 396a24a..b6faa0f 100644 --- a/user.go +++ b/user.go @@ -449,6 +449,10 @@ func (user *User) syncPortals(chatMap map[string]whatsapp.Chat, createAll bool) var inCommunity, ok bool if inCommunity, ok = existingKeys[portal.Key]; !ok || !inCommunity { inCommunity = user.addPortalToCommunity(portal) + if portal.IsPrivateChat() { + puppet := user.bridge.GetPuppetByJID(portal.Key.JID) + user.addPuppetToCommunity(puppet) + } } portalKeys = append(portalKeys, database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: inCommunity}) }