mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Make all fields optional; set values to nil if we're returning nothing
This commit is contained in:
parent
d4710217f8
commit
283f909b2d
|
|
@ -327,31 +327,59 @@ type PrevEventRef struct {
|
||||||
PrevSender string `json:"prev_sender"`
|
PrevSender string `json:"prev_sender"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response represents a /sync API response. See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync
|
type DeviceLists struct {
|
||||||
type Response struct {
|
Changed []string `json:"changed,omitempty"`
|
||||||
NextBatch StreamingToken `json:"next_batch"`
|
Left []string `json:"left,omitempty"`
|
||||||
AccountData struct {
|
}
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events,omitempty"`
|
|
||||||
} `json:"account_data,omitempty"`
|
type RoomsResponse struct {
|
||||||
Presence struct {
|
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events,omitempty"`
|
|
||||||
} `json:"presence,omitempty"`
|
|
||||||
Rooms struct {
|
|
||||||
Join map[string]JoinResponse `json:"join,omitempty"`
|
Join map[string]JoinResponse `json:"join,omitempty"`
|
||||||
Peek map[string]JoinResponse `json:"peek,omitempty"`
|
Peek map[string]JoinResponse `json:"peek,omitempty"`
|
||||||
Invite map[string]InviteResponse `json:"invite,omitempty"`
|
Invite map[string]InviteResponse `json:"invite,omitempty"`
|
||||||
Leave map[string]LeaveResponse `json:"leave,omitempty"`
|
Leave map[string]LeaveResponse `json:"leave,omitempty"`
|
||||||
} `json:"rooms,omitempty"`
|
}
|
||||||
ToDevice struct {
|
|
||||||
|
type ToDeviceResponse struct {
|
||||||
Events []gomatrixserverlib.SendToDeviceEvent `json:"events,omitempty"`
|
Events []gomatrixserverlib.SendToDeviceEvent `json:"events,omitempty"`
|
||||||
} `json:"to_device,omitempty"`
|
}
|
||||||
DeviceLists struct {
|
|
||||||
Changed []string `json:"changed,omitempty"`
|
// Response represents a /sync API response. See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync
|
||||||
Left []string `json:"left,omitempty"`
|
type Response struct {
|
||||||
} `json:"device_lists,omitempty"`
|
NextBatch StreamingToken `json:"next_batch"`
|
||||||
|
AccountData *ClientEvents `json:"account_data,omitempty"`
|
||||||
|
Presence *ClientEvents `json:"presence,omitempty"`
|
||||||
|
Rooms *RoomsResponse `json:"rooms,omitempty"`
|
||||||
|
ToDevice *ToDeviceResponse `json:"to_device,omitempty"`
|
||||||
|
DeviceLists *DeviceLists `json:"device_lists,omitempty"`
|
||||||
DeviceListsOTKCount map[string]int `json:"device_one_time_keys_count,omitempty"`
|
DeviceListsOTKCount map[string]int `json:"device_one_time_keys_count,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r Response) MarshalJSON() ([]byte, error) {
|
||||||
|
type alias Response
|
||||||
|
a := alias(r)
|
||||||
|
if r.AccountData != nil && len(r.AccountData.Events) == 0 {
|
||||||
|
a.AccountData = nil
|
||||||
|
}
|
||||||
|
if r.Presence != nil && len(r.Presence.Events) == 0 {
|
||||||
|
a.Presence = nil
|
||||||
|
}
|
||||||
|
if r.DeviceLists != nil {
|
||||||
|
if len(r.DeviceLists.Left) == 0 && len(r.DeviceLists.Changed) == 0 {
|
||||||
|
a.DeviceLists = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.Rooms != nil {
|
||||||
|
if len(r.Rooms.Join) == 0 && len(r.Rooms.Peek) == 0 &&
|
||||||
|
len(r.Rooms.Invite) == 0 && len(r.Rooms.Leave) == 0 {
|
||||||
|
a.Rooms = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r.ToDevice != nil && len(r.ToDevice.Events) == 0 {
|
||||||
|
a.ToDevice = nil
|
||||||
|
}
|
||||||
|
return json.Marshal(a)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) HasUpdates() bool {
|
func (r *Response) HasUpdates() bool {
|
||||||
// purposefully exclude DeviceListsOTKCount as we always include them
|
// purposefully exclude DeviceListsOTKCount as we always include them
|
||||||
return (len(r.AccountData.Events) > 0 ||
|
return (len(r.AccountData.Events) > 0 ||
|
||||||
|
|
@ -370,18 +398,21 @@ func NewResponse() *Response {
|
||||||
res := Response{}
|
res := Response{}
|
||||||
// Pre-initialise the maps. Synapse will return {} even if there are no rooms under a specific section,
|
// Pre-initialise the maps. Synapse will return {} even if there are no rooms under a specific section,
|
||||||
// so let's do the same thing. Bonus: this means we can't get dreaded 'assignment to entry in nil map' errors.
|
// so let's do the same thing. Bonus: this means we can't get dreaded 'assignment to entry in nil map' errors.
|
||||||
res.Rooms.Join = map[string]JoinResponse{}
|
res.Rooms = &RoomsResponse{
|
||||||
res.Rooms.Peek = map[string]JoinResponse{}
|
Join: map[string]JoinResponse{},
|
||||||
res.Rooms.Invite = map[string]InviteResponse{}
|
Peek: map[string]JoinResponse{},
|
||||||
res.Rooms.Leave = map[string]LeaveResponse{}
|
Invite: map[string]InviteResponse{},
|
||||||
|
Leave: map[string]LeaveResponse{},
|
||||||
|
}
|
||||||
|
|
||||||
// Also pre-intialise empty slices or else we'll insert 'null' instead of '[]' for the value.
|
// Also pre-intialise empty slices or else we'll insert 'null' instead of '[]' for the value.
|
||||||
// TODO: We really shouldn't have to do all this to coerce encoding/json to Do The Right Thing. We should
|
// TODO: We really shouldn't have to do all this to coerce encoding/json to Do The Right Thing. We should
|
||||||
// really be using our own Marshal/Unmarshal implementations otherwise this may prove to be a CPU bottleneck.
|
// really be using our own Marshal/Unmarshal implementations otherwise this may prove to be a CPU bottleneck.
|
||||||
// This also applies to NewJoinResponse, NewInviteResponse and NewLeaveResponse.
|
// This also applies to NewJoinResponse, NewInviteResponse and NewLeaveResponse.
|
||||||
res.AccountData.Events = []gomatrixserverlib.ClientEvent{}
|
res.AccountData = &ClientEvents{}
|
||||||
res.Presence.Events = []gomatrixserverlib.ClientEvent{}
|
res.Presence = &ClientEvents{}
|
||||||
res.ToDevice.Events = []gomatrixserverlib.SendToDeviceEvent{}
|
res.DeviceLists = &DeviceLists{}
|
||||||
|
res.ToDevice = &ToDeviceResponse{}
|
||||||
res.DeviceListsOTKCount = map[string]int{}
|
res.DeviceListsOTKCount = map[string]int{}
|
||||||
|
|
||||||
return &res
|
return &res
|
||||||
|
|
@ -403,37 +434,67 @@ type UnreadNotifications struct {
|
||||||
NotificationCount int `json:"notification_count"`
|
NotificationCount int `json:"notification_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// JoinResponse represents a /sync response for a room which is under the 'join' or 'peek' key.
|
type ClientEvents struct {
|
||||||
type JoinResponse struct {
|
Events []gomatrixserverlib.ClientEvent `json:"events,omitempty"`
|
||||||
Summary struct {
|
}
|
||||||
Heroes []string `json:"m.heroes,omitempty"`
|
|
||||||
JoinedMemberCount *int `json:"m.joined_member_count,omitempty"`
|
type Timeline struct {
|
||||||
InvitedMemberCount *int `json:"m.invited_member_count,omitempty"`
|
|
||||||
} `json:"summary"`
|
|
||||||
State struct {
|
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
|
||||||
} `json:"state"`
|
|
||||||
Timeline struct {
|
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
||||||
Limited bool `json:"limited"`
|
Limited bool `json:"limited"`
|
||||||
PrevBatch *TopologyToken `json:"prev_batch,omitempty"`
|
PrevBatch *TopologyToken `json:"prev_batch,omitempty"`
|
||||||
} `json:"timeline"`
|
}
|
||||||
Ephemeral struct {
|
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
type Summary struct {
|
||||||
} `json:"ephemeral"`
|
Heroes []string `json:"m.heroes,omitempty"`
|
||||||
AccountData struct {
|
JoinedMemberCount *int `json:"m.joined_member_count,omitempty"`
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
InvitedMemberCount *int `json:"m.invited_member_count,omitempty"`
|
||||||
} `json:"account_data"`
|
}
|
||||||
|
|
||||||
|
// JoinResponse represents a /sync response for a room which is under the 'join' or 'peek' key.
|
||||||
|
type JoinResponse struct {
|
||||||
|
Summary *Summary `json:"summary,omitempty"`
|
||||||
|
State *ClientEvents `json:"state,omitempty"`
|
||||||
|
Timeline *Timeline `json:"timeline,omitempty"`
|
||||||
|
Ephemeral *ClientEvents `json:"ephemeral,omitempty"`
|
||||||
|
AccountData *ClientEvents `json:"account_data,omitempty"`
|
||||||
*UnreadNotifications `json:"unread_notifications,omitempty"`
|
*UnreadNotifications `json:"unread_notifications,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (jr JoinResponse) MarshalJSON() ([]byte, error) {
|
||||||
|
type alias JoinResponse
|
||||||
|
a := alias(jr)
|
||||||
|
if jr.State != nil && len(jr.State.Events) == 0 {
|
||||||
|
a.State = nil
|
||||||
|
}
|
||||||
|
if jr.Ephemeral != nil && len(jr.Ephemeral.Events) == 0 {
|
||||||
|
a.Ephemeral = nil
|
||||||
|
}
|
||||||
|
if jr.AccountData != nil && len(jr.AccountData.Events) == 0 {
|
||||||
|
a.AccountData = nil
|
||||||
|
}
|
||||||
|
if jr.Timeline != nil && len(jr.Timeline.Events) == 0 {
|
||||||
|
a.Timeline = nil
|
||||||
|
}
|
||||||
|
if jr.Summary != nil && len(jr.Summary.Heroes) == 0 {
|
||||||
|
a.Summary = nil
|
||||||
|
}
|
||||||
|
if jr.UnreadNotifications != nil &&
|
||||||
|
jr.UnreadNotifications.NotificationCount == 0 && jr.UnreadNotifications.HighlightCount == 0 {
|
||||||
|
a.UnreadNotifications = nil
|
||||||
|
}
|
||||||
|
return json.Marshal(a)
|
||||||
|
}
|
||||||
|
|
||||||
// NewJoinResponse creates an empty response with initialised arrays.
|
// NewJoinResponse creates an empty response with initialised arrays.
|
||||||
func NewJoinResponse() *JoinResponse {
|
func NewJoinResponse() *JoinResponse {
|
||||||
res := JoinResponse{}
|
res := JoinResponse{
|
||||||
res.State.Events = []gomatrixserverlib.ClientEvent{}
|
Summary: &Summary{},
|
||||||
res.Timeline.Events = []gomatrixserverlib.ClientEvent{}
|
State: &ClientEvents{},
|
||||||
res.Ephemeral.Events = []gomatrixserverlib.ClientEvent{}
|
Timeline: &Timeline{},
|
||||||
res.AccountData.Events = []gomatrixserverlib.ClientEvent{}
|
Ephemeral: &ClientEvents{},
|
||||||
|
AccountData: &ClientEvents{},
|
||||||
|
UnreadNotifications: &UnreadNotifications{},
|
||||||
|
}
|
||||||
return &res
|
return &res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -469,21 +530,16 @@ func NewInviteResponse(event *gomatrixserverlib.HeaderedEvent) *InviteResponse {
|
||||||
|
|
||||||
// LeaveResponse represents a /sync response for a room which is under the 'leave' key.
|
// LeaveResponse represents a /sync response for a room which is under the 'leave' key.
|
||||||
type LeaveResponse struct {
|
type LeaveResponse struct {
|
||||||
State struct {
|
State *ClientEvents `json:"state,omitempty"`
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
Timeline *Timeline `json:"timeline,omitempty"`
|
||||||
} `json:"state"`
|
|
||||||
Timeline struct {
|
|
||||||
Events []gomatrixserverlib.ClientEvent `json:"events"`
|
|
||||||
Limited bool `json:"limited"`
|
|
||||||
PrevBatch *TopologyToken `json:"prev_batch,omitempty"`
|
|
||||||
} `json:"timeline"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLeaveResponse creates an empty response with initialised arrays.
|
// NewLeaveResponse creates an empty response with initialised arrays.
|
||||||
func NewLeaveResponse() *LeaveResponse {
|
func NewLeaveResponse() *LeaveResponse {
|
||||||
res := LeaveResponse{}
|
res := LeaveResponse{
|
||||||
res.State.Events = []gomatrixserverlib.ClientEvent{}
|
State: &ClientEvents{},
|
||||||
res.Timeline.Events = []gomatrixserverlib.ClientEvent{}
|
Timeline: &Timeline{},
|
||||||
|
}
|
||||||
return &res
|
return &res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue