Эх сурвалжийг харах

Create parent portal if necessary when updating space

Tulir Asokan 2 жил өмнө
parent
commit
738ea1b943
2 өөрчлөгдсөн 26 нэмэгдсэн , 12 устгасан
  1. 24 10
      portal.go
  2. 2 2
      user.go

+ 24 - 10
portal.go

@@ -1196,23 +1196,23 @@ func (portal *Portal) UpdateTopic(topic string, setBy types.JID, updateInfo bool
 	return false
 	return false
 }
 }
 
 
-func (portal *Portal) UpdateParentGroup(parent types.JID, updateInfo bool) bool {
+func (portal *Portal) UpdateParentGroup(source *User, parent types.JID, updateInfo bool) bool {
 	portal.parentGroupUpdateLock.Lock()
 	portal.parentGroupUpdateLock.Lock()
 	defer portal.parentGroupUpdateLock.Unlock()
 	defer portal.parentGroupUpdateLock.Unlock()
 	if portal.ParentGroup != parent {
 	if portal.ParentGroup != parent {
 		portal.log.Debugfln("Updating parent group %v -> %v", portal.ParentGroup, parent)
 		portal.log.Debugfln("Updating parent group %v -> %v", portal.ParentGroup, parent)
-		portal.updateCommunitySpace(false)
+		portal.updateCommunitySpace(source, false, false)
 		portal.ParentGroup = parent
 		portal.ParentGroup = parent
 		portal.parentPortal = nil
 		portal.parentPortal = nil
 		portal.InSpace = false
 		portal.InSpace = false
-		portal.updateCommunitySpace(true)
+		portal.updateCommunitySpace(source, true, false)
 		if updateInfo {
 		if updateInfo {
 			portal.UpdateBridgeInfo()
 			portal.UpdateBridgeInfo()
 			portal.Update(nil)
 			portal.Update(nil)
 		}
 		}
 		return true
 		return true
 	} else if !portal.ParentGroup.IsEmpty() && !portal.InSpace {
 	} else if !portal.ParentGroup.IsEmpty() && !portal.InSpace {
-		return portal.updateCommunitySpace(true)
+		return portal.updateCommunitySpace(source, true, updateInfo)
 	}
 	}
 	return false
 	return false
 }
 }
@@ -1253,7 +1253,7 @@ func (portal *Portal) UpdateMetadata(user *User, groupInfo *types.GroupInfo) boo
 	update := false
 	update := false
 	update = portal.UpdateName(groupInfo.Name, groupInfo.NameSetBy, false) || update
 	update = portal.UpdateName(groupInfo.Name, groupInfo.NameSetBy, false) || update
 	update = portal.UpdateTopic(groupInfo.Topic, groupInfo.TopicSetBy, false) || update
 	update = portal.UpdateTopic(groupInfo.Topic, groupInfo.TopicSetBy, false) || update
-	update = portal.UpdateParentGroup(groupInfo.LinkedParentJID, false) || update
+	update = portal.UpdateParentGroup(user, groupInfo.LinkedParentJID, false) || update
 	if portal.ExpirationTime != groupInfo.DisappearingTimer {
 	if portal.ExpirationTime != groupInfo.DisappearingTimer {
 		update = true
 		update = true
 		portal.ExpirationTime = groupInfo.DisappearingTimer
 		portal.ExpirationTime = groupInfo.DisappearingTimer
@@ -1610,9 +1610,10 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
 	}
 	}
 	if portal.IsParent {
 	if portal.IsParent {
 		creationContent["type"] = event.RoomTypeSpace
 		creationContent["type"] = event.RoomTypeSpace
-	} else if parent := portal.GetParentPortal(); parent != nil {
+	} else if parent := portal.GetParentPortal(); parent != nil && parent.MXID != "" {
 		initialState = append(initialState, &event.Event{
 		initialState = append(initialState, &event.Event{
-			Type: event.StateSpaceParent,
+			Type:     event.StateSpaceParent,
+			StateKey: proto.String(parent.MXID.String()),
 			Content: event.Content{
 			Content: event.Content{
 				Parsed: &event.SpaceParentEventContent{
 				Parsed: &event.SpaceParentEventContent{
 					Via:       []string{portal.bridge.Config.Homeserver.Domain},
 					Via:       []string{portal.bridge.Config.Homeserver.Domain},
@@ -1659,7 +1660,6 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
 	portal.bridge.portalsLock.Lock()
 	portal.bridge.portalsLock.Lock()
 	portal.bridge.portalsByMXID[portal.MXID] = portal
 	portal.bridge.portalsByMXID[portal.MXID] = portal
 	portal.bridge.portalsLock.Unlock()
 	portal.bridge.portalsLock.Unlock()
-	portal.updateCommunitySpace(true)
 	portal.Update(nil)
 	portal.Update(nil)
 
 
 	// We set the memberships beforehand to make sure the encryption key exchange in initial backfill knows the users are here.
 	// We set the memberships beforehand to make sure the encryption key exchange in initial backfill knows the users are here.
@@ -1676,6 +1676,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
 	}
 	}
 	user.syncChatDoublePuppetDetails(portal, true)
 	user.syncChatDoublePuppetDetails(portal, true)
 
 
+	go portal.updateCommunitySpace(user, true, true)
 	go portal.addToPersonalSpace(user)
 	go portal.addToPersonalSpace(user)
 
 
 	if groupInfo != nil && !autoJoinInvites {
 	if groupInfo != nil && !autoJoinInvites {
@@ -1730,13 +1731,23 @@ func (portal *Portal) addToPersonalSpace(user *User) {
 	}
 	}
 }
 }
 
 
-func (portal *Portal) updateCommunitySpace(add bool) bool {
+func (portal *Portal) updateCommunitySpace(user *User, add, updateDB bool) bool {
 	if add == portal.InSpace {
 	if add == portal.InSpace {
 		return false
 		return false
 	}
 	}
 	space := portal.GetParentPortal()
 	space := portal.GetParentPortal()
-	if space == nil || space.MXID == "" {
+	if space == nil {
 		return false
 		return false
+	} else if space.MXID == "" {
+		if !add {
+			return false
+		}
+		portal.log.Debugfln("Creating portal for parent group %v", space.Key.JID)
+		err := space.CreateMatrixRoom(user, nil, false, false)
+		if err != nil {
+			portal.log.Debugfln("Failed to create portal for parent group: %v", err)
+			return false
+		}
 	}
 	}
 
 
 	var action string
 	var action string
@@ -1763,6 +1774,9 @@ func (portal *Portal) updateCommunitySpace(add bool) bool {
 		portal.log.Warnfln("Failed to send m.space.parent event to %s %s: %v", action, space.MXID, err)
 		portal.log.Warnfln("Failed to send m.space.parent event to %s %s: %v", action, space.MXID, err)
 	}
 	}
 	portal.InSpace = add
 	portal.InSpace = add
+	if updateDB {
+		portal.Update(nil)
+	}
 	return true
 	return true
 }
 }
 
 

+ 2 - 2
user.go

@@ -1324,11 +1324,11 @@ func (user *User) handleGroupUpdate(evt *events.GroupInfo) {
 		portal.UpdateGroupDisappearingMessages(evt.Sender, evt.Timestamp, evt.Ephemeral.DisappearingTimer)
 		portal.UpdateGroupDisappearingMessages(evt.Sender, evt.Timestamp, evt.Ephemeral.DisappearingTimer)
 	case evt.Link != nil:
 	case evt.Link != nil:
 		if evt.Link.Type == types.GroupLinkChangeTypeParent {
 		if evt.Link.Type == types.GroupLinkChangeTypeParent {
-			portal.UpdateParentGroup(evt.Link.Group.JID, true)
+			portal.UpdateParentGroup(user, evt.Link.Group.JID, true)
 		}
 		}
 	case evt.Unlink != nil:
 	case evt.Unlink != nil:
 		if evt.Unlink.Type == types.GroupLinkChangeTypeParent && portal.ParentGroup == evt.Unlink.Group.JID {
 		if evt.Unlink.Type == types.GroupLinkChangeTypeParent && portal.ParentGroup == evt.Unlink.Group.JID {
-			portal.UpdateParentGroup(types.EmptyJID, true)
+			portal.UpdateParentGroup(user, types.EmptyJID, true)
 		}
 		}
 	}
 	}
 }
 }