Cleanup & fix invite response creation

This commit is contained in:
Devon Hudson 2023-09-13 13:52:54 -06:00
parent b593f29fce
commit 8ee7b9de3c
No known key found for this signature in database
GPG key ID: CD06B18E77F6A628

View file

@ -534,26 +534,14 @@ type InviteResponse struct {
// NewInviteResponse creates an empty response with initialised arrays.
func NewInviteResponse(ctx context.Context, rsAPI api.QuerySenderIDAPI, event *types.HeaderedEvent, userID spec.UserID, stateKey *string, eventFormat synctypes.ClientEventFormat) (*InviteResponse, error) {
res, err := updateInviteRoomState(ctx, rsAPI, event, userID, stateKey, eventFormat)
if err != nil {
return nil, err
}
return res, nil
}
func updateInviteRoomState(ctx context.Context, rsAPI api.QuerySenderIDAPI, event *types.HeaderedEvent, inviterUserID spec.UserID, stateKey *string, eventFormat synctypes.ClientEventFormat) (*InviteResponse, error) {
inv := InviteResponse{}
inv.InviteState.Events = []json.RawMessage{}
res := InviteResponse{}
res.InviteState.Events = []json.RawMessage{}
// First see if there's invite_room_state in the unsigned key of the invite.
// If there is then unmarshal it into the response. This will contain the
// partial room state such as join rules, room name etc.
if inviteRoomState := gjson.GetBytes(event.Unsigned(), "invite_room_state"); inviteRoomState.Exists() {
err := json.Unmarshal([]byte(inviteRoomState.Raw), &inv.InviteState.Events)
if err != nil {
return nil, err
}
_ = json.Unmarshal([]byte(inviteRoomState.Raw), &res.InviteState.Events)
}
// Clear unsigned so it doesn't have pseudoIDs converted during ToClientEvent
@ -566,7 +554,7 @@ func updateInviteRoomState(ctx context.Context, rsAPI api.QuerySenderIDAPI, even
// This is needed for clients to work out *who* sent the invite.
inviteEvent, err := synctypes.ToClientEvent(eventNoUnsigned, eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
return rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
}, inviterUserID.String(), stateKey)
}, userID.String(), stateKey)
if err != nil {
return nil, err
}
@ -575,10 +563,10 @@ func updateInviteRoomState(ctx context.Context, rsAPI api.QuerySenderIDAPI, even
inviteEvent.Unsigned = nil
if ev, err := json.Marshal(*inviteEvent); err == nil {
inv.InviteState.Events = append(inv.InviteState.Events, ev)
res.InviteState.Events = append(res.InviteState.Events, ev)
}
return &inv, nil
return &res, nil
}
// LeaveResponse represents a /sync response for a room which is under the 'leave' key.