Use gomatrixserverlib.ClientEvent

This commit is contained in:
Kegan Dougal 2017-04-13 15:02:19 +01:00
parent 15ee4a374c
commit 97d7a0bc34
3 changed files with 26 additions and 67 deletions

View file

@ -1,38 +0,0 @@
package events
import (
"encoding/json"
"github.com/matrix-org/gomatrixserverlib"
)
// ClientEvent is an event which is fit for consumption by clients, in accordance with the specification.
type ClientEvent struct {
Content json.RawMessage `json:"content"`
Sender string `json:"sender"`
Type string `json:"type"`
StateKey *string `json:"state_key,omitempty"`
Unsigned json.RawMessage `json:"unsigned,omitempty"`
OriginServerTS int64 `json:"origin_server_ts"`
EventID string `json:"event_id"`
}
// ToClientEvents converts server events to client events
func ToClientEvents(serverEvs []gomatrixserverlib.Event) []ClientEvent {
evs := make([]ClientEvent, len(serverEvs))
for i, se := range serverEvs {
evs[i] = ToClientEvent(se)
}
return evs
}
// ToClientEvent converts a single server event to a client event
func ToClientEvent(se gomatrixserverlib.Event) ClientEvent {
return ClientEvent{
Content: json.RawMessage(se.Content()),
Sender: se.Sender(),
Type: se.Type(),
StateKey: se.StateKey(),
// TODO: No unsigned / origin_server_ts fields?
EventID: se.EventID(),
}
}

View file

@ -8,7 +8,6 @@ import (
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth"
"github.com/matrix-org/dendrite/clientapi/events"
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/syncserver/storage" "github.com/matrix-org/dendrite/syncserver/storage"
@ -149,9 +148,9 @@ func (rp *RequestPool) currentSyncForUser(req syncRequest) (*types.Response, err
res.NextBatch = pos.String() res.NextBatch = pos.String()
for roomID, d := range data { for roomID, d := range data {
jr := types.NewJoinResponse() jr := types.NewJoinResponse()
jr.Timeline.Events = events.ToClientEvents(d.RecentEvents) jr.Timeline.Events = gomatrixserverlib.ToClientEvents(d.RecentEvents, gomatrixserverlib.FormatSync)
jr.Timeline.Limited = true jr.Timeline.Limited = true
jr.State.Events = events.ToClientEvents(d.State) jr.State.Events = gomatrixserverlib.ToClientEvents(d.State, gomatrixserverlib.FormatSync)
res.Rooms.Join[roomID] = *jr res.Rooms.Join[roomID] = *jr
} }
return res, nil return res, nil
@ -180,7 +179,7 @@ func (rp *RequestPool) currentSyncForUser(req syncRequest) (*types.Response, err
// for now, dump everything as join timeline events // for now, dump everything as join timeline events
for _, ev := range evs { for _, ev := range evs {
roomData := res.Rooms.Join[ev.RoomID()] roomData := res.Rooms.Join[ev.RoomID()]
roomData.Timeline.Events = append(roomData.Timeline.Events, events.ToClientEvent(ev)) roomData.Timeline.Events = append(roomData.Timeline.Events, gomatrixserverlib.ToClientEvent(ev, gomatrixserverlib.FormatSync))
res.Rooms.Join[ev.RoomID()] = roomData res.Rooms.Join[ev.RoomID()] = roomData
} }

View file

@ -1,10 +1,8 @@
package types package types
import ( import (
"strconv"
"github.com/matrix-org/dendrite/clientapi/events"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"strconv"
) )
// StreamPosition represents the offset in the sync stream a client is at. // StreamPosition represents the offset in the sync stream a client is at.
@ -25,10 +23,10 @@ type RoomData struct {
type Response struct { type Response struct {
NextBatch string `json:"next_batch"` NextBatch string `json:"next_batch"`
AccountData struct { AccountData struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
} `json:"account_data"` } `json:"account_data"`
Presence struct { Presence struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
} `json:"presence"` } `json:"presence"`
Rooms struct { Rooms struct {
Join map[string]JoinResponse `json:"join"` Join map[string]JoinResponse `json:"join"`
@ -50,8 +48,8 @@ func NewResponse() *Response {
// 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 = make([]events.ClientEvent, 0) res.AccountData.Events = make([]gomatrixserverlib.ClientEvent, 0)
res.Presence.Events = make([]events.ClientEvent, 0) res.Presence.Events = make([]gomatrixserverlib.ClientEvent, 0)
return &res return &res
} }
@ -59,52 +57,52 @@ func NewResponse() *Response {
// JoinResponse represents a /sync response for a room which is under the 'join' key. // JoinResponse represents a /sync response for a room which is under the 'join' key.
type JoinResponse struct { type JoinResponse struct {
State struct { State struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
} `json:"state"` } `json:"state"`
Timeline struct { Timeline struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} `json:"timeline"` } `json:"timeline"`
Ephemeral struct { Ephemeral struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
} `json:"ephemeral"` } `json:"ephemeral"`
AccountData struct { AccountData struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
} `json:"account_data"` } `json:"account_data"`
} }
// 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 = make([]events.ClientEvent, 0) res.State.Events = make([]gomatrixserverlib.ClientEvent, 0)
res.Timeline.Events = make([]events.ClientEvent, 0) res.Timeline.Events = make([]gomatrixserverlib.ClientEvent, 0)
res.Ephemeral.Events = make([]events.ClientEvent, 0) res.Ephemeral.Events = make([]gomatrixserverlib.ClientEvent, 0)
res.AccountData.Events = make([]events.ClientEvent, 0) res.AccountData.Events = make([]gomatrixserverlib.ClientEvent, 0)
return &res return &res
} }
// InviteResponse represents a /sync response for a room which is under the 'invite' key. // InviteResponse represents a /sync response for a room which is under the 'invite' key.
type InviteResponse struct { type InviteResponse struct {
InviteState struct { InviteState struct {
Events []events.ClientEvent Events []gomatrixserverlib.ClientEvent
} `json:"invite_state"` } `json:"invite_state"`
} }
// NewInviteResponse creates an empty response with initialised arrays. // NewInviteResponse creates an empty response with initialised arrays.
func NewInviteResponse() *InviteResponse { func NewInviteResponse() *InviteResponse {
res := InviteResponse{} res := InviteResponse{}
res.InviteState.Events = make([]events.ClientEvent, 0) res.InviteState.Events = make([]gomatrixserverlib.ClientEvent, 0)
return &res return &res
} }
// 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 struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
} `json:"state"` } `json:"state"`
Timeline struct { Timeline struct {
Events []events.ClientEvent `json:"events"` Events []gomatrixserverlib.ClientEvent `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"` PrevBatch string `json:"prev_batch"`
} `json:"timeline"` } `json:"timeline"`
@ -113,7 +111,7 @@ type LeaveResponse struct {
// 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 = make([]events.ClientEvent, 0) res.State.Events = make([]gomatrixserverlib.ClientEvent, 0)
res.Timeline.Events = make([]events.ClientEvent, 0) res.Timeline.Events = make([]gomatrixserverlib.ClientEvent, 0)
return &res return &res
} }