From 10eefaf92897d406949fadd9d13d470f2b4a4583 Mon Sep 17 00:00:00 2001 From: Malte E <97891689+maltee1@users.noreply.github.com> Date: Fri, 28 Jun 2024 23:21:34 +0200 Subject: [PATCH] v1: sync groups command (#490) [skip cd] --- commands.go | 73 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/commands.go b/commands.go index e3021c01..7600d66f 100644 --- a/commands.go +++ b/commands.go @@ -61,7 +61,7 @@ func (br *SignalBridge) RegisterCommands() { cmdSetDeviceName, cmdPM, cmdResolvePhone, - cmdSyncSpace, + cmdSync, cmdDeleteSession, cmdSetRelay, cmdUnsetRelay, @@ -537,18 +537,26 @@ func fnResolvePhone(ce *WrappedCommandEvent) { } } -var cmdSyncSpace = &commands.FullHandler{ - Func: wrapCommand(fnSyncSpace), - Name: "sync-space", +var cmdSync = &commands.FullHandler{ + Func: wrapCommand(fnSync), + Name: "sync", Help: commands.HelpMeta{ Section: HelpSectionMiscellaneous, - Description: "Synchronize your personal filtering space", + Description: "Synchronize Signal bridge data", + Args: "", }, RequiresLogin: true, } -func fnSyncSpace(ce *WrappedCommandEvent) { - if !ce.Bridge.Config.Bridge.PersonalFilteringSpaces { +func fnSync(ce *WrappedCommandEvent) { + args := strings.ToLower(strings.Join(ce.Args, " ")) + space := strings.Contains(args, "space") + groups := strings.Contains(args, "groups") + if !space && !groups { + ce.Reply("**Usage:** `sync `") + return + } + if !ce.Bridge.Config.Bridge.PersonalFilteringSpaces && space { ce.Reply("Personal filtering spaces are not enabled on this instance of the bridge") return } @@ -559,26 +567,49 @@ func fnSyncSpace(ce *WrappedCommandEvent) { ce.Reply("Failed to get private chat IDs from database") return } - count := 0 allPortals := ce.Bridge.GetAllPortalsWithMXID() - for _, portal := range allPortals { - if portal.IsPrivateChat() { - continue + if space { + count := 0 + for _, portal := range allPortals { + if portal.IsPrivateChat() { + continue + } + if ce.Bridge.StateStore.IsInRoom(ctx, portal.MXID, ce.User.MXID) && portal.addToPersonalSpace(ctx, ce.User) { + count++ + } } - if ce.Bridge.StateStore.IsInRoom(ctx, portal.MXID, ce.User.MXID) && portal.addToPersonalSpace(ctx, ce.User) { + for _, key := range dmKeys { + portal := ce.Bridge.GetPortalByChatID(key) + portal.addToPersonalSpace(ctx, ce.User) count++ } + plural := "s" + if count == 1 { + plural = "" + } + ce.Reply("Added %d room%s to space", count, plural) } - for _, key := range dmKeys { - portal := ce.Bridge.GetPortalByChatID(key) - portal.addToPersonalSpace(ctx, ce.User) - count++ - } - plural := "s" - if count == 1 { - plural = "" + if groups { + count := 0 + for _, portal := range allPortals { + if portal.IsPrivateChat() { + continue + } + if ce.Bridge.StateStore.IsInRoom(ctx, portal.MXID, ce.User.MXID) { + groupInfo := portal.UpdateGroupInfo(ce.Ctx, ce.User, nil, 0, true) + if groupInfo != nil { + members := portal.SyncParticipants(ctx, ce.User, groupInfo) + portal.updatePowerLevelsAndJoinRule(ctx, groupInfo, members) + count++ + } + } + } + plural := "s" + if count == 1 { + plural = "" + } + ce.Reply("Synced %d group%s", count, plural) } - ce.Reply("Added %d room%s to space", count, plural) } var cmdLogin = &commands.FullHandler{