From 727b11e7efe82bb43f66e1cff81db11c3dd7cc58 Mon Sep 17 00:00:00 2001 From: Helder Ferreira Date: Tue, 28 Dec 2021 20:14:09 +0000 Subject: [PATCH] add newly created channels to the space Co-authored-by: clmnin --- main.go | 3 ++- portal.go | 11 +++++++++++ user.go | 15 +++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index f5a98a7..2bd520d 100644 --- a/main.go +++ b/main.go @@ -179,8 +179,9 @@ type Bridge struct { usersByMXID map[id.UserID]*User usersByUsername map[string]*User usersLock sync.Mutex - managementRooms map[id.RoomID]*User + spaceRooms map[id.RoomID]*User managementRoomsLock sync.Mutex + managementRooms map[id.RoomID]*User portalsByMXID map[id.RoomID]*Portal portalsByJID map[database.PortalKey]*Portal portalsLock sync.Mutex diff --git a/portal.go b/portal.go index 6ccf8c4..c6811ec 100644 --- a/portal.go +++ b/portal.go @@ -1141,6 +1141,8 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i portal.ensureUserInvited(user) user.syncChatDoublePuppetDetails(portal, true) + portal.addToSpace(user.getSpaceRoom(), portal.MXID) + if groupInfo != nil { portal.SyncParticipants(user, groupInfo) if groupInfo.IsAnnounce { @@ -1176,6 +1178,15 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i return nil } +func (portal *Portal) addToSpace(spaceID id.RoomID, portalID id.RoomID) { + + parentSpaceContent := make(map[string]interface{}) + parentSpaceContent["via"] = []string{"matrix.wounn.xyz"} + + portal.log.Errorln("adding room " + portalID + " to the space " + spaceID) + portal.MainIntent().SendStateEvent(spaceID, event.Type{Type: "m.space.child", Class: event.StateEventType}, portalID.String(), parentSpaceContent) +} + func (portal *Portal) IsPrivateChat() bool { return portal.Key.JID.Server == types.DefaultUserServer } diff --git a/user.go b/user.go index dd8c0f7..eae77cc 100644 --- a/user.go +++ b/user.go @@ -196,6 +196,8 @@ func (user *User) getSpaceRoom() id.RoomID { invite = append(invite, user.MXID) resp, err := user.bridge.Bot.CreateRoom(&mautrix.ReqCreateRoom{ + Visibility: "private", + Name: "WhatsApp", Topic: "WhatsApp bridge Space", Invite: invite, CreationContent: creationContent, @@ -207,12 +209,21 @@ func (user *User) getSpaceRoom() id.RoomID { } } else { user.log.Debugln("Space found" + user.SpaceRoom) + roomID = user.SpaceRoom } return roomID } -func (user *User) setSpaceRoom(roomID id.RoomID) { - user.log.Debugln("Space ID to set" + roomID) +func (user *User) setSpaceRoom(spaceID id.RoomID) { + existingUser, ok := user.bridge.spaceRooms[spaceID] + if ok { + existingUser.SpaceRoom = "" + existingUser.Update() + } + + user.SpaceRoom = spaceID + user.bridge.spaceRooms[user.SpaceRoom] = user + user.Update() } func (user *User) GetManagementRoom() id.RoomID {