Skip to content

Commit

Permalink
client: add method to get full state event
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 28, 2025
1 parent 625dbc6 commit 7f20932
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,8 +1460,8 @@ func (cli *Client) updateStoreWithOutgoingEvent(ctx context.Context, roomID id.R
UpdateStateStore(ctx, cli.StateStore, fakeEvt)
}

// StateEvent gets a single state event in a room. It will attempt to JSON unmarshal into the given "outContent" struct with
// the HTTP response body, or return an error.
// StateEvent gets the content of a single state event in a room.
// It will attempt to JSON unmarshal into the given "outContent" struct with the HTTP response body, or return an error.
// See https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3roomsroomidstateeventtypestatekey
func (cli *Client) StateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string, outContent interface{}) (err error) {
u := cli.BuildClientURL("v3", "rooms", roomID, "state", eventType.String(), stateKey)
Expand All @@ -1472,6 +1472,23 @@ func (cli *Client) StateEvent(ctx context.Context, roomID id.RoomID, eventType e
return
}

// FullStateEvent gets a single state event in a room. Unlike [StateEvent], this gets the entire event
// (including details like the sender and timestamp).
// This requires the server to support the ?format=event query parameter, which is currently missing from the spec.
// See https://github.com/matrix-org/matrix-spec/issues/1047 for more info
func (cli *Client) FullStateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string) (evt *event.Event, err error) {
u := cli.BuildURLWithQuery(ClientURLPath{"v3", "rooms", roomID, "state", eventType.String(), stateKey}, map[string]string{
"format": "event",
})
_, err = cli.MakeRequest(ctx, http.MethodGet, u, nil, &evt)
if err == nil && cli.StateStore != nil {
UpdateStateStore(ctx, cli.StateStore, evt)
}
evt.Type.Class = event.StateEventType
_ = evt.Content.ParseRaw(evt.Type)
return
}

// parseRoomStateArray parses a JSON array as a stream and stores the events inside it in a room state map.
func parseRoomStateArray(_ *http.Request, res *http.Response, responseJSON interface{}) ([]byte, error) {
response := make(RoomStateMap)
Expand Down

0 comments on commit 7f20932

Please sign in to comment.