Browse Source

Update child portal m.bridge info when updating parent

Tulir Asokan 2 years ago
parent
commit
54c61c63e6
3 changed files with 31 additions and 2 deletions
  1. 4 0
      database/portal.go
  2. 2 0
      database/upgrades/53-community-index.sql
  3. 25 2
      portal.go

+ 4 - 0
database/portal.go

@@ -83,6 +83,10 @@ func (pq *PortalQuery) GetAllByJID(jid types.JID) []*Portal {
 	return pq.getAll(fmt.Sprintf("SELECT %s FROM portal WHERE jid=$1", portalColumns), jid.ToNonAD())
 }
 
+func (pq *PortalQuery) GetAllByParentGroup(jid types.JID) []*Portal {
+	return pq.getAll(fmt.Sprintf("SELECT %s FROM portal WHERE parent_group=$1", portalColumns), jid)
+}
+
 func (pq *PortalQuery) FindPrivateChats(receiver types.JID) []*Portal {
 	return pq.getAll(fmt.Sprintf("SELECT %s FROM portal WHERE receiver=$1 AND jid LIKE '%%@s.whatsapp.net'", portalColumns), receiver.ToNonAD())
 }

+ 2 - 0
database/upgrades/53-community-index.sql

@@ -0,0 +1,2 @@
+-- v53: Add index to make querying by community faster
+CREATE INDEX portal_parent_group_idx ON portal(parent_group);

+ 25 - 2
portal.go

@@ -136,6 +136,10 @@ func (br *WABridge) GetAllPortalsByJID(jid types.JID) []*Portal {
 	return br.dbPortalsToPortals(br.DB.Portal.GetAllByJID(jid))
 }
 
+func (br *WABridge) GetAllByParentGroup(jid types.JID) []*Portal {
+	return br.dbPortalsToPortals(br.DB.Portal.GetAllByParentGroup(jid))
+}
+
 func (br *WABridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal {
 	br.portalsLock.Lock()
 	defer br.portalsLock.Unlock()
@@ -1129,6 +1133,7 @@ func (portal *Portal) UpdateAvatar(user *User, setBy types.JID, updateInfo bool)
 	if updateInfo {
 		portal.UpdateBridgeInfo()
 		portal.Update(nil)
+		portal.updateChildRooms()
 	}
 	return true
 }
@@ -1158,6 +1163,7 @@ func (portal *Portal) UpdateName(name string, setBy types.JID, updateInfo bool)
 				portal.NameSet = true
 				if updateInfo {
 					portal.UpdateBridgeInfo()
+					portal.updateChildRooms()
 				}
 				return true
 			} else {
@@ -1294,6 +1300,7 @@ func (portal *Portal) UpdateMatrixRoom(user *User, groupInfo *types.GroupInfo) b
 		portal.LastSync = time.Now()
 		portal.Update(nil)
 		portal.UpdateBridgeInfo()
+		portal.updateChildRooms()
 	}
 	return true
 }
@@ -1460,6 +1467,20 @@ func (portal *Portal) UpdateBridgeInfo() {
 	}
 }
 
+func (portal *Portal) updateChildRooms() {
+	if !portal.IsParent {
+		return
+	}
+	children := portal.bridge.GetAllByParentGroup(portal.Key.JID)
+	for _, child := range children {
+		changed := child.updateCommunitySpace(nil, true, false)
+		child.UpdateBridgeInfo()
+		if changed {
+			portal.Update(nil)
+		}
+	}
+}
+
 func (portal *Portal) GetEncryptionEventContent() (evt *event.EncryptionEventContent) {
 	evt = &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}
 	if rot := portal.bridge.Config.Bridge.Encryption.Rotation; rot.EnableCustom {
@@ -1652,7 +1673,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
 	if err != nil {
 		return err
 	}
-	portal.log.Infoln("Matrix room created:", portal.MXID)
+	portal.log.Infoln("Matrix room created:", resp.RoomID)
 	portal.InSpace = false
 	portal.NameSet = len(portal.Name) > 0
 	portal.TopicSet = len(portal.Topic) > 0
@@ -1696,6 +1717,8 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
 		}
 
 		user.UpdateDirectChats(map[id.UserID][]id.RoomID{puppet.MXID: {portal.MXID}})
+	} else if portal.IsParent {
+		portal.updateChildRooms()
 	}
 
 	firstEventResp, err := portal.MainIntent().SendMessageEvent(portal.MXID, PortalCreationDummyEvent, struct{}{})
@@ -1746,7 +1769,7 @@ func (portal *Portal) updateCommunitySpace(user *User, add, updateInfo bool) boo
 	if space == nil {
 		return false
 	} else if space.MXID == "" {
-		if !add {
+		if !add || user == nil {
 			return false
 		}
 		portal.log.Debugfln("Creating portal for parent group %v", space.Key.JID)