From 001a280973d56c09487d4d9c6cb09b89448661a9 Mon Sep 17 00:00:00 2001 From: Sean Quah Date: Wed, 10 Aug 2022 16:45:02 +0100 Subject: [PATCH 1/2] Add SyncStateHas* check functions --- internal/client/client.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/internal/client/client.go b/internal/client/client.go index cae7cac6..6fe17efb 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -682,6 +682,34 @@ func SyncTimelineHasEventID(roomID string, eventID string) SyncCheckOpt { }) } +// Check that the state for `roomID` has an event which passes the check function. +func SyncStateHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt { + return func(clientUserID string, topLevelSyncJSON gjson.Result) error { + err := loopArray( + topLevelSyncJSON, "rooms.join."+GjsonEscape(roomID)+".state.events", check, + ) + if err == nil { + return nil + } + return fmt.Errorf("SyncStateHas(%s): %s", roomID, err) + } +} + +// Check that the state for `roomID` has an event which matches the event ID. +func SyncStateHasEventID(roomID string, eventID string) SyncCheckOpt { + return SyncStateHas(roomID, func(ev gjson.Result) bool { + return ev.Get("event_id").Str == eventID + }) +} + +// Check that the state for `roomID` has an event which matches the state key. +func SyncStateHasStateKey(roomID string, eventType string, stateKey string) SyncCheckOpt { + return SyncStateHas(roomID, func(ev gjson.Result) bool { + return ev.Get("type").Str == eventType && + ev.Get("state_key").Str == stateKey + }) +} + // Checks that `userID` gets invited to `roomID`. // // This checks different parts of the /sync response depending on the client making the request. From 4b1b980b90d7e423746ed8e16c87d28c5d9a18fd Mon Sep 17 00:00:00 2001 From: Sean Quah Date: Fri, 2 Sep 2022 13:34:16 +0100 Subject: [PATCH 2/2] Remove additional SyncStateHas* functions and update comment --- internal/client/client.go | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index c04a161e..6800abae 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -682,7 +682,10 @@ func SyncTimelineHasEventID(roomID string, eventID string) SyncCheckOpt { }) } -// Check that the state for `roomID` has an event which passes the check function. +// Check that the state section for `roomID` has an event which passes the check function. +// Note that the state section of a sync response only contains the change in state up to the start +// of the timeline and will not contain the entire state of the room for incremental or +// `lazy_load_members` syncs. func SyncStateHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt { return func(clientUserID string, topLevelSyncJSON gjson.Result) error { err := loopArray( @@ -695,21 +698,6 @@ func SyncStateHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt { } } -// Check that the state for `roomID` has an event which matches the event ID. -func SyncStateHasEventID(roomID string, eventID string) SyncCheckOpt { - return SyncStateHas(roomID, func(ev gjson.Result) bool { - return ev.Get("event_id").Str == eventID - }) -} - -// Check that the state for `roomID` has an event which matches the state key. -func SyncStateHasStateKey(roomID string, eventType string, stateKey string) SyncCheckOpt { - return SyncStateHas(roomID, func(ev gjson.Result) bool { - return ev.Get("type").Str == eventType && - ev.Get("state_key").Str == stateKey - }) -} - func SyncEphemeralHas(roomID string, check func(gjson.Result) bool) SyncCheckOpt { return func(clientUserID string, topLevelSyncJSON gjson.Result) error { err := loopArray(