aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2022-06-13 10:16:30 +0100
committerNeil Alexander <neilalexander@users.noreply.github.com>2022-06-13 10:16:30 +0100
commit0a7f7dc716a25b1071bacc16cb91b31441174453 (patch)
tree27f3095e1723a895e497363b8c12f59415d1b75f /cmd
parent89d2adadbdef4ce62338d55d56b58fa58055d495 (diff)
Add `--difference` to `resolve-state` tool
Diffstat (limited to 'cmd')
-rw-r--r--cmd/resolve-state/main.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/cmd/resolve-state/main.go b/cmd/resolve-state/main.go
index 6ed6ebdb..da0f70f0 100644
--- a/cmd/resolve-state/main.go
+++ b/cmd/resolve-state/main.go
@@ -28,7 +28,9 @@ import (
var roomVersion = flag.String("roomversion", "5", "the room version to parse events as")
var filterType = flag.String("filtertype", "", "the event types to filter on")
+var difference = flag.Bool("difference", false, "whether to calculate the difference between snapshots")
+// nolint:gocyclo
func main() {
ctx := context.Background()
cfg := setup.ParseFlags(true)
@@ -64,6 +66,58 @@ func main() {
RoomVersion: gomatrixserverlib.RoomVersion(*roomVersion),
})
+ if *difference {
+ if len(snapshotNIDs) != 2 {
+ panic("need exactly two state snapshot NIDs to calculate difference")
+ }
+
+ removed, added, err := stateres.DifferenceBetweeenStateSnapshots(ctx, snapshotNIDs[0], snapshotNIDs[1])
+ if err != nil {
+ panic(err)
+ }
+
+ var eventNIDs []types.EventNID
+ for _, entry := range append(removed, added...) {
+ eventNIDs = append(eventNIDs, entry.EventNID)
+ }
+
+ eventEntries, err := roomserverDB.Events(ctx, eventNIDs)
+ if err != nil {
+ panic(err)
+ }
+
+ events := make(map[types.EventNID]*gomatrixserverlib.Event, len(eventEntries))
+ for _, entry := range eventEntries {
+ events[entry.EventNID] = entry.Event
+ }
+
+ if len(removed) > 0 {
+ fmt.Println("Removed:")
+ for _, r := range removed {
+ event := events[r.EventNID]
+ fmt.Println()
+ fmt.Printf("* %s %s %q\n", event.EventID(), event.Type(), *event.StateKey())
+ fmt.Printf(" %s\n", string(event.Content()))
+ }
+ }
+
+ if len(removed) > 0 && len(added) > 0 {
+ fmt.Println()
+ }
+
+ if len(added) > 0 {
+ fmt.Println("Added:")
+ for _, a := range added {
+ event := events[a.EventNID]
+ fmt.Println()
+ fmt.Printf("* %s %s %q\n", event.EventID(), event.Type(), *event.StateKey())
+ fmt.Printf(" %s\n", string(event.Content()))
+ }
+ }
+
+ return
+ }
+
var stateEntries []types.StateEntry
for _, snapshotNID := range snapshotNIDs {
var entries []types.StateEntry