Use synctypes not GMSL for ClientEvent

This commit is contained in:
Kegan Dougal 2023-04-04 16:51:01 +01:00
parent 985298cfc4
commit b722f14c30
28 changed files with 331 additions and 108 deletions

View file

@ -32,10 +32,17 @@ import (
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/dendrite/setup/process"
"github.com/matrix-org/dendrite/syncapi/synctypes"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// ApplicationServiceTransaction is the transaction that is sent off to an
// application service.
type ApplicationServiceTransaction struct {
Events []synctypes.ClientEvent `json:"events"`
}
// OutputRoomEventConsumer consumes events that originated in the room server. // OutputRoomEventConsumer consumes events that originated in the room server.
type OutputRoomEventConsumer struct { type OutputRoomEventConsumer struct {
ctx context.Context ctx context.Context
@ -171,8 +178,8 @@ func (s *OutputRoomEventConsumer) sendEvents(
) error { ) error {
// Create the transaction body. // Create the transaction body.
transaction, err := json.Marshal( transaction, err := json.Marshal(
gomatrixserverlib.ApplicationServiceTransaction{ ApplicationServiceTransaction{
Events: gomatrixserverlib.HeaderedToClientEvents(events, gomatrixserverlib.FormatAll), Events: synctypes.HeaderedToClientEvents(events, synctypes.FormatAll),
}, },
) )
if err != nil { if err != nil {

View file

@ -22,6 +22,7 @@ import (
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/synctypes"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
@ -29,7 +30,7 @@ import (
) )
type stateEventInStateResp struct { type stateEventInStateResp struct {
gomatrixserverlib.ClientEvent synctypes.ClientEvent
PrevContent json.RawMessage `json:"prev_content,omitempty"` PrevContent json.RawMessage `json:"prev_content,omitempty"`
ReplacesState string `json:"replaces_state,omitempty"` ReplacesState string `json:"replaces_state,omitempty"`
} }
@ -122,7 +123,7 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
"state_at_event": !wantLatestState, "state_at_event": !wantLatestState,
}).Info("Fetching all state") }).Info("Fetching all state")
stateEvents := []gomatrixserverlib.ClientEvent{} stateEvents := []synctypes.ClientEvent{}
if wantLatestState { if wantLatestState {
// If we are happy to use the latest state, either because the user is // If we are happy to use the latest state, either because the user is
// still in the room, or because the room is world-readable, then just // still in the room, or because the room is world-readable, then just
@ -131,7 +132,7 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
for _, ev := range stateRes.StateEvents { for _, ev := range stateRes.StateEvents {
stateEvents = append( stateEvents = append(
stateEvents, stateEvents,
gomatrixserverlib.HeaderedToClientEvent(ev, gomatrixserverlib.FormatAll), synctypes.HeaderedToClientEvent(ev, synctypes.FormatAll),
) )
} }
} else { } else {
@ -150,7 +151,7 @@ func OnIncomingStateRequest(ctx context.Context, device *userapi.Device, rsAPI a
for _, ev := range stateAfterRes.StateEvents { for _, ev := range stateAfterRes.StateEvents {
stateEvents = append( stateEvents = append(
stateEvents, stateEvents,
gomatrixserverlib.HeaderedToClientEvent(ev, gomatrixserverlib.FormatAll), synctypes.HeaderedToClientEvent(ev, synctypes.FormatAll),
) )
} }
} }
@ -309,7 +310,7 @@ func OnIncomingStateTypeRequest(
} }
stateEvent := stateEventInStateResp{ stateEvent := stateEventInStateResp{
ClientEvent: gomatrixserverlib.HeaderedToClientEvent(event, gomatrixserverlib.FormatAll), ClientEvent: synctypes.HeaderedToClientEvent(event, synctypes.FormatAll),
} }
var res interface{} var res interface{}

View file

@ -24,6 +24,7 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/syncapi/synctypes"
) )
// QueryLatestEventsAndStateRequest is a request to QueryLatestEventsAndState // QueryLatestEventsAndStateRequest is a request to QueryLatestEventsAndState
@ -146,7 +147,7 @@ type QueryMembershipsForRoomRequest struct {
// QueryMembershipsForRoomResponse is a response to QueryMembershipsForRoom // QueryMembershipsForRoomResponse is a response to QueryMembershipsForRoom
type QueryMembershipsForRoomResponse struct { type QueryMembershipsForRoomResponse struct {
// The "m.room.member" events (of "join" membership) in the client format // The "m.room.member" events (of "join" membership) in the client format
JoinEvents []gomatrixserverlib.ClientEvent `json:"join_events"` JoinEvents []synctypes.ClientEvent `json:"join_events"`
// True if the user has been in room before and has either stayed in it or // True if the user has been in room before and has either stayed in it or
// left it. // left it.
HasBeenInRoom bool `json:"has_been_in_room"` HasBeenInRoom bool `json:"has_been_in_room"`

View file

@ -26,6 +26,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/matrix-org/dendrite/roomserver/storage/tables" "github.com/matrix-org/dendrite/roomserver/storage/tables"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/caching"
@ -345,7 +346,7 @@ func (r *Queryer) QueryMembershipsForRoom(
return fmt.Errorf("r.DB.Events: %w", err) return fmt.Errorf("r.DB.Events: %w", err)
} }
for _, event := range events { for _, event := range events {
clientEvent := gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll) clientEvent := synctypes.ToClientEvent(event.Event, synctypes.FormatAll)
response.JoinEvents = append(response.JoinEvents, clientEvent) response.JoinEvents = append(response.JoinEvents, clientEvent)
} }
return nil return nil
@ -365,7 +366,7 @@ func (r *Queryer) QueryMembershipsForRoom(
} }
response.HasBeenInRoom = true response.HasBeenInRoom = true
response.JoinEvents = []gomatrixserverlib.ClientEvent{} response.JoinEvents = []synctypes.ClientEvent{}
var events []types.Event var events []types.Event
var stateEntries []types.StateEntry var stateEntries []types.StateEntry
@ -394,7 +395,7 @@ func (r *Queryer) QueryMembershipsForRoom(
} }
for _, event := range events { for _, event := range events {
clientEvent := gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll) clientEvent := synctypes.ToClientEvent(event.Event, synctypes.FormatAll)
response.JoinEvents = append(response.JoinEvents, clientEvent) response.JoinEvents = append(response.JoinEvents, clientEvent)
} }

View file

@ -34,6 +34,7 @@ import (
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
roomserver "github.com/matrix-org/dendrite/roomserver/api" roomserver "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/syncapi/synctypes"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
@ -78,9 +79,9 @@ func (r *EventRelationshipRequest) Defaults() {
} }
type EventRelationshipResponse struct { type EventRelationshipResponse struct {
Events []gomatrixserverlib.ClientEvent `json:"events"` Events []synctypes.ClientEvent `json:"events"`
NextBatch string `json:"next_batch"` NextBatch string `json:"next_batch"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
} }
type MSC2836EventRelationshipsResponse struct { type MSC2836EventRelationshipsResponse struct {
@ -91,7 +92,7 @@ type MSC2836EventRelationshipsResponse struct {
func toClientResponse(res *MSC2836EventRelationshipsResponse) *EventRelationshipResponse { func toClientResponse(res *MSC2836EventRelationshipsResponse) *EventRelationshipResponse {
out := &EventRelationshipResponse{ out := &EventRelationshipResponse{
Events: gomatrixserverlib.ToClientEvents(res.ParsedEvents, gomatrixserverlib.FormatAll), Events: synctypes.ToClientEvents(res.ParsedEvents, synctypes.FormatAll),
Limited: res.Limited, Limited: res.Limited,
NextBatch: res.NextBatch, NextBatch: res.NextBatch,
} }

View file

@ -16,6 +16,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/dendrite/setup/process"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/dendrite/internal/hooks" "github.com/matrix-org/dendrite/internal/hooks"
@ -462,7 +463,7 @@ func assertContains(t *testing.T, result *msc2836.EventRelationshipResponse, wan
} }
} }
func assertUnsignedChildren(t *testing.T, ev gomatrixserverlib.ClientEvent, relType string, wantCount int, childrenEventIDs []string) { func assertUnsignedChildren(t *testing.T, ev synctypes.ClientEvent, relType string, wantCount int, childrenEventIDs []string) {
t.Helper() t.Helper()
unsigned := struct { unsigned := struct {
Children map[string]int `json:"children"` Children map[string]int `json:"children"`

View file

@ -26,6 +26,7 @@ import (
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/api"
) )
@ -278,7 +279,7 @@ func leftRooms(res *types.Response) []string {
return roomIDs return roomIDs
} }
func membershipEventPresent(events []gomatrixserverlib.ClientEvent, userID string) bool { func membershipEventPresent(events []synctypes.ClientEvent, userID string) bool {
for _, ev := range events { for _, ev := range events {
// it's enough to know that we have our member event here, don't need to check membership content // it's enough to know that we have our member event here, don't need to check membership content
// as it's implied by being in the respective section of the sync response. // as it's implied by being in the respective section of the sync response.

View file

@ -10,6 +10,7 @@ import (
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
) )
@ -159,7 +160,7 @@ func assertCatchup(t *testing.T, hasNew bool, syncResponse *types.Response, want
func joinResponseWithRooms(syncResponse *types.Response, userID string, roomIDs []string) *types.Response { func joinResponseWithRooms(syncResponse *types.Response, userID string, roomIDs []string) *types.Response {
for _, roomID := range roomIDs { for _, roomID := range roomIDs {
roomEvents := []gomatrixserverlib.ClientEvent{ roomEvents := []synctypes.ClientEvent{
{ {
Type: "m.room.member", Type: "m.room.member",
StateKey: &userID, StateKey: &userID,
@ -182,7 +183,7 @@ func joinResponseWithRooms(syncResponse *types.Response, userID string, roomIDs
func leaveResponseWithRooms(syncResponse *types.Response, userID string, roomIDs []string) *types.Response { func leaveResponseWithRooms(syncResponse *types.Response, userID string, roomIDs []string) *types.Response {
for _, roomID := range roomIDs { for _, roomID := range roomIDs {
roomEvents := []gomatrixserverlib.ClientEvent{ roomEvents := []synctypes.ClientEvent{
{ {
Type: "m.room.member", Type: "m.room.member",
StateKey: &userID, StateKey: &userID,
@ -299,7 +300,7 @@ func TestKeyChangeCatchupNoNewJoinsButMessages(t *testing.T) {
roomID := "!TestKeyChangeCatchupNoNewJoinsButMessages:bar" roomID := "!TestKeyChangeCatchupNoNewJoinsButMessages:bar"
syncResponse := types.NewResponse() syncResponse := types.NewResponse()
empty := "" empty := ""
roomStateEvents := []gomatrixserverlib.ClientEvent{ roomStateEvents := []synctypes.ClientEvent{
{ {
Type: "m.room.name", Type: "m.room.name",
StateKey: &empty, StateKey: &empty,
@ -309,7 +310,7 @@ func TestKeyChangeCatchupNoNewJoinsButMessages(t *testing.T) {
Content: []byte(`{"name":"The Room Name"}`), Content: []byte(`{"name":"The Room Name"}`),
}, },
} }
roomTimelineEvents := []gomatrixserverlib.ClientEvent{ roomTimelineEvents := []synctypes.ClientEvent{
{ {
Type: "m.room.message", Type: "m.room.message",
EventID: "$something1:here", EventID: "$something1:here",
@ -402,7 +403,7 @@ func TestKeyChangeCatchupChangeAndLeftSameRoom(t *testing.T) {
newShareUser2 := "@bobby:localhost" newShareUser2 := "@bobby:localhost"
roomID := "!join:bar" roomID := "!join:bar"
syncResponse := types.NewResponse() syncResponse := types.NewResponse()
roomEvents := []gomatrixserverlib.ClientEvent{ roomEvents := []synctypes.ClientEvent{
{ {
Type: "m.room.member", Type: "m.room.member",
StateKey: &syncingUser, StateKey: &syncingUser,

View file

@ -29,6 +29,7 @@ import (
roomserver "github.com/matrix-org/dendrite/roomserver/api" roomserver "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/internal" "github.com/matrix-org/dendrite/syncapi/internal"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -37,12 +38,12 @@ import (
) )
type ContextRespsonse struct { type ContextRespsonse struct {
End string `json:"end"` End string `json:"end"`
Event *gomatrixserverlib.ClientEvent `json:"event,omitempty"` Event *synctypes.ClientEvent `json:"event,omitempty"`
EventsAfter []gomatrixserverlib.ClientEvent `json:"events_after,omitempty"` EventsAfter []synctypes.ClientEvent `json:"events_after,omitempty"`
EventsBefore []gomatrixserverlib.ClientEvent `json:"events_before,omitempty"` EventsBefore []synctypes.ClientEvent `json:"events_before,omitempty"`
Start string `json:"start"` Start string `json:"start"`
State []gomatrixserverlib.ClientEvent `json:"state,omitempty"` State []synctypes.ClientEvent `json:"state,omitempty"`
} }
func Context( func Context(
@ -167,14 +168,14 @@ func Context(
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
eventsBeforeClient := gomatrixserverlib.HeaderedToClientEvents(eventsBeforeFiltered, gomatrixserverlib.FormatAll) eventsBeforeClient := synctypes.HeaderedToClientEvents(eventsBeforeFiltered, synctypes.FormatAll)
eventsAfterClient := gomatrixserverlib.HeaderedToClientEvents(eventsAfterFiltered, gomatrixserverlib.FormatAll) eventsAfterClient := synctypes.HeaderedToClientEvents(eventsAfterFiltered, synctypes.FormatAll)
newState := state newState := state
if filter.LazyLoadMembers { if filter.LazyLoadMembers {
allEvents := append(eventsBeforeFiltered, eventsAfterFiltered...) allEvents := append(eventsBeforeFiltered, eventsAfterFiltered...)
allEvents = append(allEvents, &requestedEvent) allEvents = append(allEvents, &requestedEvent)
evs := gomatrixserverlib.HeaderedToClientEvents(allEvents, gomatrixserverlib.FormatAll) evs := synctypes.HeaderedToClientEvents(allEvents, synctypes.FormatAll)
newState, err = applyLazyLoadMembers(ctx, device, snapshot, roomID, evs, lazyLoadCache) newState, err = applyLazyLoadMembers(ctx, device, snapshot, roomID, evs, lazyLoadCache)
if err != nil { if err != nil {
logrus.WithError(err).Error("unable to load membership events") logrus.WithError(err).Error("unable to load membership events")
@ -182,12 +183,12 @@ func Context(
} }
} }
ev := gomatrixserverlib.HeaderedToClientEvent(&requestedEvent, gomatrixserverlib.FormatAll) ev := synctypes.HeaderedToClientEvent(&requestedEvent, synctypes.FormatAll)
response := ContextRespsonse{ response := ContextRespsonse{
Event: &ev, Event: &ev,
EventsAfter: eventsAfterClient, EventsAfter: eventsAfterClient,
EventsBefore: eventsBeforeClient, EventsBefore: eventsBeforeClient,
State: gomatrixserverlib.HeaderedToClientEvents(newState, gomatrixserverlib.FormatAll), State: synctypes.HeaderedToClientEvents(newState, synctypes.FormatAll),
} }
if len(response.State) > filter.Limit { if len(response.State) > filter.Limit {
@ -261,7 +262,7 @@ func applyLazyLoadMembers(
device *userapi.Device, device *userapi.Device,
snapshot storage.DatabaseTransaction, snapshot storage.DatabaseTransaction,
roomID string, roomID string,
events []gomatrixserverlib.ClientEvent, events []synctypes.ClientEvent,
lazyLoadCache caching.LazyLoadCache, lazyLoadCache caching.LazyLoadCache,
) ([]*gomatrixserverlib.HeaderedEvent, error) { ) ([]*gomatrixserverlib.HeaderedEvent, error) {
eventSenders := make(map[string]struct{}) eventSenders := make(map[string]struct{})

View file

@ -17,7 +17,6 @@ package routing
import ( import (
"net/http" "net/http"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -26,6 +25,7 @@ import (
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/syncapi/internal" "github.com/matrix-org/dendrite/syncapi/internal"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
) )
@ -97,6 +97,6 @@ func GetEvent(
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: gomatrixserverlib.HeaderedToClientEvent(events[0], gomatrixserverlib.FormatAll), JSON: synctypes.HeaderedToClientEvent(events[0], synctypes.FormatAll),
} }
} }

View file

@ -22,14 +22,14 @@ import (
"github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
type getMembershipResponse struct { type getMembershipResponse struct {
Chunk []gomatrixserverlib.ClientEvent `json:"chunk"` Chunk []synctypes.ClientEvent `json:"chunk"`
} }
// https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-rooms-roomid-joined-members // https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-rooms-roomid-joined-members
@ -134,6 +134,6 @@ func GetMemberships(
} }
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: getMembershipResponse{gomatrixserverlib.HeaderedToClientEvents(result, gomatrixserverlib.FormatAll)}, JSON: getMembershipResponse{synctypes.HeaderedToClientEvents(result, synctypes.FormatAll)},
} }
} }

View file

@ -34,6 +34,7 @@ import (
"github.com/matrix-org/dendrite/syncapi/internal" "github.com/matrix-org/dendrite/syncapi/internal"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/sync" "github.com/matrix-org/dendrite/syncapi/sync"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
) )
@ -54,11 +55,11 @@ type messagesReq struct {
} }
type messagesResp struct { type messagesResp struct {
Start string `json:"start"` Start string `json:"start"`
StartStream string `json:"start_stream,omitempty"` // NOTSPEC: used by Cerulean, so clients can hit /messages then immediately /sync with a latest sync token StartStream string `json:"start_stream,omitempty"` // NOTSPEC: used by Cerulean, so clients can hit /messages then immediately /sync with a latest sync token
End string `json:"end,omitempty"` End string `json:"end,omitempty"`
Chunk []gomatrixserverlib.ClientEvent `json:"chunk"` Chunk []synctypes.ClientEvent `json:"chunk"`
State []gomatrixserverlib.ClientEvent `json:"state,omitempty"` State []synctypes.ClientEvent `json:"state,omitempty"`
} }
// OnIncomingMessagesRequest implements the /messages endpoint from the // OnIncomingMessagesRequest implements the /messages endpoint from the
@ -253,7 +254,7 @@ func OnIncomingMessagesRequest(
util.GetLogger(req.Context()).WithError(err).Error("failed to apply lazy loading") util.GetLogger(req.Context()).WithError(err).Error("failed to apply lazy loading")
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
res.State = append(res.State, gomatrixserverlib.HeaderedToClientEvents(membershipEvents, gomatrixserverlib.FormatAll)...) res.State = append(res.State, synctypes.HeaderedToClientEvents(membershipEvents, synctypes.FormatAll)...)
} }
// If we didn't return any events, set the end to an empty string, so it will be omitted // If we didn't return any events, set the end to an empty string, so it will be omitted
@ -291,7 +292,7 @@ func getMembershipForUser(ctx context.Context, roomID, userID string, rsAPI api.
// Returns an error if there was an issue talking to the database or with the // Returns an error if there was an issue talking to the database or with the
// remote homeserver. // remote homeserver.
func (r *messagesReq) retrieveEvents() ( func (r *messagesReq) retrieveEvents() (
clientEvents []gomatrixserverlib.ClientEvent, start, clientEvents []synctypes.ClientEvent, start,
end types.TopologyToken, err error, end types.TopologyToken, err error,
) { ) {
// Retrieve the events from the local database. // Retrieve the events from the local database.
@ -323,7 +324,7 @@ func (r *messagesReq) retrieveEvents() (
// If we didn't get any event, we don't need to proceed any further. // If we didn't get any event, we don't need to proceed any further.
if len(events) == 0 { if len(events) == 0 {
return []gomatrixserverlib.ClientEvent{}, *r.from, *r.to, nil return []synctypes.ClientEvent{}, *r.from, *r.to, nil
} }
// Get the position of the first and the last event in the room's topology. // Get the position of the first and the last event in the room's topology.
@ -334,7 +335,7 @@ func (r *messagesReq) retrieveEvents() (
// only have to change it in one place, i.e. the database. // only have to change it in one place, i.e. the database.
start, end, err = r.getStartEnd(events) start, end, err = r.getStartEnd(events)
if err != nil { if err != nil {
return []gomatrixserverlib.ClientEvent{}, *r.from, *r.to, err return []synctypes.ClientEvent{}, *r.from, *r.to, err
} }
// Sort the events to ensure we send them in the right order. // Sort the events to ensure we send them in the right order.
@ -350,7 +351,7 @@ func (r *messagesReq) retrieveEvents() (
events = reversed(events) events = reversed(events)
} }
if len(events) == 0 { if len(events) == 0 {
return []gomatrixserverlib.ClientEvent{}, *r.from, *r.to, nil return []synctypes.ClientEvent{}, *r.from, *r.to, nil
} }
// Apply room history visibility filter // Apply room history visibility filter
@ -362,7 +363,7 @@ func (r *messagesReq) retrieveEvents() (
"events_before": len(events), "events_before": len(events),
"events_after": len(filteredEvents), "events_after": len(filteredEvents),
}).Debug("applied history visibility (messages)") }).Debug("applied history visibility (messages)")
return gomatrixserverlib.HeaderedToClientEvents(filteredEvents, gomatrixserverlib.FormatAll), start, end, err return synctypes.HeaderedToClientEvents(filteredEvents, synctypes.FormatAll), start, end, err
} }
func (r *messagesReq) getStartEnd(events []*gomatrixserverlib.HeaderedEvent) (start, end types.TopologyToken, err error) { func (r *messagesReq) getStartEnd(events []*gomatrixserverlib.HeaderedEvent) (start, end types.TopologyToken, err error) {

View file

@ -27,14 +27,15 @@ import (
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/internal" "github.com/matrix-org/dendrite/syncapi/internal"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
) )
type RelationsResponse struct { type RelationsResponse struct {
Chunk []gomatrixserverlib.ClientEvent `json:"chunk"` Chunk []synctypes.ClientEvent `json:"chunk"`
NextBatch string `json:"next_batch,omitempty"` NextBatch string `json:"next_batch,omitempty"`
PrevBatch string `json:"prev_batch,omitempty"` PrevBatch string `json:"prev_batch,omitempty"`
} }
// nolint:gocyclo // nolint:gocyclo
@ -85,7 +86,7 @@ func Relations(
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err) defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
res := &RelationsResponse{ res := &RelationsResponse{
Chunk: []gomatrixserverlib.ClientEvent{}, Chunk: []synctypes.ClientEvent{},
} }
var events []types.StreamEvent var events []types.StreamEvent
events, res.PrevBatch, res.NextBatch, err = snapshot.RelationsFor( events, res.PrevBatch, res.NextBatch, err = snapshot.RelationsFor(
@ -108,11 +109,11 @@ func Relations(
// Convert the events into client events, and optionally filter based on the event // Convert the events into client events, and optionally filter based on the event
// type if it was specified. // type if it was specified.
res.Chunk = make([]gomatrixserverlib.ClientEvent, 0, len(filteredEvents)) res.Chunk = make([]synctypes.ClientEvent, 0, len(filteredEvents))
for _, event := range filteredEvents { for _, event := range filteredEvents {
res.Chunk = append( res.Chunk = append(
res.Chunk, res.Chunk,
gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll), synctypes.ToClientEvent(event.Event, synctypes.FormatAll),
) )
} }

View file

@ -32,6 +32,7 @@ import (
"github.com/matrix-org/dendrite/internal/fulltext" "github.com/matrix-org/dendrite/internal/fulltext"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/api"
) )
@ -166,7 +167,7 @@ func Search(req *http.Request, device *api.Device, syncDB storage.Database, fts
}) })
} }
stateForRooms := make(map[string][]gomatrixserverlib.ClientEvent) stateForRooms := make(map[string][]synctypes.ClientEvent)
for _, event := range evs { for _, event := range evs {
eventsBefore, eventsAfter, err := contextEvents(ctx, snapshot, event, roomFilter, searchReq) eventsBefore, eventsAfter, err := contextEvents(ctx, snapshot, event, roomFilter, searchReq)
if err != nil { if err != nil {
@ -204,12 +205,12 @@ func Search(req *http.Request, device *api.Device, syncDB storage.Database, fts
Context: SearchContextResponse{ Context: SearchContextResponse{
Start: startToken.String(), Start: startToken.String(),
End: endToken.String(), End: endToken.String(),
EventsAfter: gomatrixserverlib.HeaderedToClientEvents(eventsAfter, gomatrixserverlib.FormatSync), EventsAfter: synctypes.HeaderedToClientEvents(eventsAfter, synctypes.FormatSync),
EventsBefore: gomatrixserverlib.HeaderedToClientEvents(eventsBefore, gomatrixserverlib.FormatSync), EventsBefore: synctypes.HeaderedToClientEvents(eventsBefore, synctypes.FormatSync),
ProfileInfo: profileInfos, ProfileInfo: profileInfos,
}, },
Rank: eventScore[event.EventID()].Score, Rank: eventScore[event.EventID()].Score,
Result: gomatrixserverlib.HeaderedToClientEvent(event, gomatrixserverlib.FormatAll), Result: synctypes.HeaderedToClientEvent(event, synctypes.FormatAll),
}) })
roomGroup := groups[event.RoomID()] roomGroup := groups[event.RoomID()]
roomGroup.Results = append(roomGroup.Results, event.EventID()) roomGroup.Results = append(roomGroup.Results, event.EventID())
@ -221,7 +222,7 @@ func Search(req *http.Request, device *api.Device, syncDB storage.Database, fts
logrus.WithError(err).Error("unable to get current state") logrus.WithError(err).Error("unable to get current state")
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
stateForRooms[event.RoomID()] = gomatrixserverlib.HeaderedToClientEvents(state, gomatrixserverlib.FormatSync) stateForRooms[event.RoomID()] = synctypes.HeaderedToClientEvents(state, synctypes.FormatSync)
} }
} }
@ -331,17 +332,17 @@ type Groups struct {
} }
type Result struct { type Result struct {
Context SearchContextResponse `json:"context"` Context SearchContextResponse `json:"context"`
Rank float64 `json:"rank"` Rank float64 `json:"rank"`
Result gomatrixserverlib.ClientEvent `json:"result"` Result synctypes.ClientEvent `json:"result"`
} }
type SearchContextResponse struct { type SearchContextResponse struct {
End string `json:"end"` End string `json:"end"`
EventsAfter []gomatrixserverlib.ClientEvent `json:"events_after"` EventsAfter []synctypes.ClientEvent `json:"events_after"`
EventsBefore []gomatrixserverlib.ClientEvent `json:"events_before"` EventsBefore []synctypes.ClientEvent `json:"events_before"`
Start string `json:"start"` Start string `json:"start"`
ProfileInfo map[string]ProfileInfoResponse `json:"profile_info"` ProfileInfo map[string]ProfileInfoResponse `json:"profile_info"`
} }
type ProfileInfoResponse struct { type ProfileInfoResponse struct {
@ -350,12 +351,12 @@ type ProfileInfoResponse struct {
} }
type RoomEventsResponse struct { type RoomEventsResponse struct {
Count int `json:"count"` Count int `json:"count"`
Groups Groups `json:"groups"` Groups Groups `json:"groups"`
Highlights []string `json:"highlights"` Highlights []string `json:"highlights"`
NextBatch *string `json:"next_batch,omitempty"` NextBatch *string `json:"next_batch,omitempty"`
Results []Result `json:"results"` Results []Result `json:"results"`
State map[string][]gomatrixserverlib.ClientEvent `json:"state,omitempty"` State map[string][]synctypes.ClientEvent `json:"state,omitempty"`
} }
type SearchCategoriesResponse struct { type SearchCategoriesResponse struct {
RoomEvents RoomEventsResponse `json:"room_events"` RoomEvents RoomEventsResponse `json:"room_events"`

View file

@ -6,6 +6,7 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
) )
@ -82,7 +83,7 @@ func (p *AccountDataStreamProvider) IncrementalSync(
if globalData, ok := dataRes.GlobalAccountData[dataType]; ok { if globalData, ok := dataRes.GlobalAccountData[dataType]; ok {
req.Response.AccountData.Events = append( req.Response.AccountData.Events = append(
req.Response.AccountData.Events, req.Response.AccountData.Events,
gomatrixserverlib.ClientEvent{ synctypes.ClientEvent{
Type: dataType, Type: dataType,
Content: gomatrixserverlib.RawJSON(globalData), Content: gomatrixserverlib.RawJSON(globalData),
}, },
@ -96,7 +97,7 @@ func (p *AccountDataStreamProvider) IncrementalSync(
} }
joinData.AccountData.Events = append( joinData.AccountData.Events = append(
joinData.AccountData.Events, joinData.AccountData.Events,
gomatrixserverlib.ClientEvent{ synctypes.ClientEvent{
Type: dataType, Type: dataType,
Content: gomatrixserverlib.RawJSON(roomData), Content: gomatrixserverlib.RawJSON(roomData),
}, },

View file

@ -11,6 +11,7 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
) )
@ -85,7 +86,7 @@ func (p *InviteStreamProvider) IncrementalSync(
lr := types.NewLeaveResponse() lr := types.NewLeaveResponse()
h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...)) h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...))
lr.Timeline.Events = append(lr.Timeline.Events, gomatrixserverlib.ClientEvent{ lr.Timeline.Events = append(lr.Timeline.Events, synctypes.ClientEvent{
// fake event ID which muxes in the to position // fake event ID which muxes in the to position
EventID: "$" + base64.RawURLEncoding.EncodeToString(h[:]), EventID: "$" + base64.RawURLEncoding.EncodeToString(h[:]),
OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()), OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()),

View file

@ -10,6 +10,7 @@ import (
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/internal" "github.com/matrix-org/dendrite/syncapi/internal"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
@ -365,20 +366,20 @@ func (p *PDUStreamProvider) addRoomDeltaToResponse(
} }
} }
jr.Timeline.PrevBatch = &prevBatch jr.Timeline.PrevBatch = &prevBatch
jr.Timeline.Events = gomatrixserverlib.HeaderedToClientEvents(events, gomatrixserverlib.FormatSync) jr.Timeline.Events = synctypes.HeaderedToClientEvents(events, synctypes.FormatSync)
// If we are limited by the filter AND the history visibility filter // If we are limited by the filter AND the history visibility filter
// didn't "remove" events, return that the response is limited. // didn't "remove" events, return that the response is limited.
jr.Timeline.Limited = (limited && len(events) == len(recentEvents)) || delta.NewlyJoined jr.Timeline.Limited = (limited && len(events) == len(recentEvents)) || delta.NewlyJoined
jr.State.Events = gomatrixserverlib.HeaderedToClientEvents(delta.StateEvents, gomatrixserverlib.FormatSync) jr.State.Events = synctypes.HeaderedToClientEvents(delta.StateEvents, synctypes.FormatSync)
req.Response.Rooms.Join[delta.RoomID] = jr req.Response.Rooms.Join[delta.RoomID] = jr
case gomatrixserverlib.Peek: case gomatrixserverlib.Peek:
jr := types.NewJoinResponse() jr := types.NewJoinResponse()
jr.Timeline.PrevBatch = &prevBatch jr.Timeline.PrevBatch = &prevBatch
// TODO: Apply history visibility on peeked rooms // TODO: Apply history visibility on peeked rooms
jr.Timeline.Events = gomatrixserverlib.HeaderedToClientEvents(recentEvents, gomatrixserverlib.FormatSync) jr.Timeline.Events = synctypes.HeaderedToClientEvents(recentEvents, synctypes.FormatSync)
jr.Timeline.Limited = limited jr.Timeline.Limited = limited
jr.State.Events = gomatrixserverlib.HeaderedToClientEvents(delta.StateEvents, gomatrixserverlib.FormatSync) jr.State.Events = synctypes.HeaderedToClientEvents(delta.StateEvents, synctypes.FormatSync)
req.Response.Rooms.Peek[delta.RoomID] = jr req.Response.Rooms.Peek[delta.RoomID] = jr
case gomatrixserverlib.Leave: case gomatrixserverlib.Leave:
@ -387,11 +388,11 @@ func (p *PDUStreamProvider) addRoomDeltaToResponse(
case gomatrixserverlib.Ban: case gomatrixserverlib.Ban:
lr := types.NewLeaveResponse() lr := types.NewLeaveResponse()
lr.Timeline.PrevBatch = &prevBatch lr.Timeline.PrevBatch = &prevBatch
lr.Timeline.Events = gomatrixserverlib.HeaderedToClientEvents(events, gomatrixserverlib.FormatSync) lr.Timeline.Events = synctypes.HeaderedToClientEvents(events, synctypes.FormatSync)
// If we are limited by the filter AND the history visibility filter // If we are limited by the filter AND the history visibility filter
// didn't "remove" events, return that the response is limited. // didn't "remove" events, return that the response is limited.
lr.Timeline.Limited = limited && len(events) == len(recentEvents) lr.Timeline.Limited = limited && len(events) == len(recentEvents)
lr.State.Events = gomatrixserverlib.HeaderedToClientEvents(delta.StateEvents, gomatrixserverlib.FormatSync) lr.State.Events = synctypes.HeaderedToClientEvents(delta.StateEvents, synctypes.FormatSync)
req.Response.Rooms.Leave[delta.RoomID] = lr req.Response.Rooms.Leave[delta.RoomID] = lr
} }
@ -541,11 +542,11 @@ func (p *PDUStreamProvider) getJoinResponseForCompleteSync(
} }
jr.Timeline.PrevBatch = prevBatch jr.Timeline.PrevBatch = prevBatch
jr.Timeline.Events = gomatrixserverlib.HeaderedToClientEvents(events, gomatrixserverlib.FormatSync) jr.Timeline.Events = synctypes.HeaderedToClientEvents(events, synctypes.FormatSync)
// If we are limited by the filter AND the history visibility filter // If we are limited by the filter AND the history visibility filter
// didn't "remove" events, return that the response is limited. // didn't "remove" events, return that the response is limited.
jr.Timeline.Limited = limited && len(events) == len(recentEvents) jr.Timeline.Limited = limited && len(events) == len(recentEvents)
jr.State.Events = gomatrixserverlib.HeaderedToClientEvents(stateEvents, gomatrixserverlib.FormatSync) jr.State.Events = synctypes.HeaderedToClientEvents(stateEvents, synctypes.FormatSync)
return jr, nil return jr, nil
} }

View file

@ -25,6 +25,7 @@ import (
"github.com/matrix-org/dendrite/syncapi/notifier" "github.com/matrix-org/dendrite/syncapi/notifier"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
) )
@ -130,7 +131,7 @@ func (p *PresenceStreamProvider) IncrementalSync(
return from return from
} }
req.Response.Presence.Events = append(req.Response.Presence.Events, gomatrixserverlib.ClientEvent{ req.Response.Presence.Events = append(req.Response.Presence.Events, synctypes.ClientEvent{
Content: content, Content: content,
Sender: presence.UserID, Sender: presence.UserID,
Type: gomatrixserverlib.MPresence, Type: gomatrixserverlib.MPresence,
@ -202,7 +203,7 @@ func joinedRooms(res *types.Response, userID string) []string {
return roomIDs return roomIDs
} }
func membershipEventPresent(events []gomatrixserverlib.ClientEvent, userID string) bool { func membershipEventPresent(events []synctypes.ClientEvent, userID string) bool {
for _, ev := range events { for _, ev := range events {
// it's enough to know that we have our member event here, don't need to check membership content // it's enough to know that we have our member event here, don't need to check membership content
// as it's implied by being in the respective section of the sync response. // as it's implied by being in the respective section of the sync response.

View file

@ -7,6 +7,7 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
) )
@ -86,7 +87,7 @@ func (p *ReceiptStreamProvider) IncrementalSync(
jr = types.NewJoinResponse() jr = types.NewJoinResponse()
} }
ev := gomatrixserverlib.ClientEvent{ ev := synctypes.ClientEvent{
Type: gomatrixserverlib.MReceipt, Type: gomatrixserverlib.MReceipt,
} }
content := make(map[string]ReceiptMRead) content := make(map[string]ReceiptMRead)

View file

@ -8,6 +8,7 @@ import (
"github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/storage"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
) )
@ -51,7 +52,7 @@ func (p *TypingStreamProvider) IncrementalSync(
typingUsers = append(typingUsers, users[i]) typingUsers = append(typingUsers, users[i])
} }
} }
ev := gomatrixserverlib.ClientEvent{ ev := synctypes.ClientEvent{
Type: gomatrixserverlib.MTyping, Type: gomatrixserverlib.MTyping,
} }
ev.Content, err = json.Marshal(map[string]interface{}{ ev.Content, err = json.Marshal(map[string]interface{}{

View file

@ -0,0 +1,88 @@
/* Copyright 2017 Vector Creations Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package synctypes
import "github.com/matrix-org/gomatrixserverlib"
type ClientEventFormat int
const (
// FormatAll will include all client event keys
FormatAll ClientEventFormat = iota
// FormatSync will include only the event keys required by the /sync API. Notably, this
// means the 'room_id' will be missing from the events.
FormatSync
)
// ClientEvent is an event which is fit for consumption by clients, in accordance with the specification.
type ClientEvent struct {
Content gomatrixserverlib.RawJSON `json:"content"`
EventID string `json:"event_id,omitempty"` // EventID is omitted on receipt events
OriginServerTS gomatrixserverlib.Timestamp `json:"origin_server_ts,omitempty"` // OriginServerTS is omitted on receipt events
RoomID string `json:"room_id,omitempty"` // RoomID is omitted on /sync responses
Sender string `json:"sender,omitempty"` // Sender is omitted on receipt events
StateKey *string `json:"state_key,omitempty"`
Type string `json:"type"`
Unsigned gomatrixserverlib.RawJSON `json:"unsigned,omitempty"`
Redacts string `json:"redacts,omitempty"`
}
// ToClientEvents converts server events to client events.
func ToClientEvents(serverEvs []*gomatrixserverlib.Event, format ClientEventFormat) []ClientEvent {
evs := make([]ClientEvent, 0, len(serverEvs))
for _, se := range serverEvs {
if se == nil {
continue // TODO: shouldn't happen?
}
evs = append(evs, ToClientEvent(se, format))
}
return evs
}
// HeaderedToClientEvents converts headered server events to client events.
func HeaderedToClientEvents(serverEvs []*gomatrixserverlib.HeaderedEvent, format ClientEventFormat) []ClientEvent {
evs := make([]ClientEvent, 0, len(serverEvs))
for _, se := range serverEvs {
if se == nil {
continue // TODO: shouldn't happen?
}
evs = append(evs, HeaderedToClientEvent(se, format))
}
return evs
}
// ToClientEvent converts a single server event to a client event.
func ToClientEvent(se *gomatrixserverlib.Event, format ClientEventFormat) ClientEvent {
ce := ClientEvent{
Content: gomatrixserverlib.RawJSON(se.Content()),
Sender: se.Sender(),
Type: se.Type(),
StateKey: se.StateKey(),
Unsigned: gomatrixserverlib.RawJSON(se.Unsigned()),
OriginServerTS: se.OriginServerTS(),
EventID: se.EventID(),
Redacts: se.Redacts(),
}
if format == FormatAll {
ce.RoomID = se.RoomID()
}
return ce
}
// HeaderedToClientEvent converts a single headered server event to a client event.
func HeaderedToClientEvent(se *gomatrixserverlib.HeaderedEvent, format ClientEventFormat) ClientEvent {
return ToClientEvent(se.Event, format)
}

View file

@ -0,0 +1,105 @@
/* Copyright 2017 Vector Creations Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package synctypes
import (
"bytes"
"encoding/json"
"testing"
"github.com/matrix-org/gomatrixserverlib"
)
func TestToClientEvent(t *testing.T) { // nolint: gocyclo
ev, err := gomatrixserverlib.NewEventFromTrustedJSON([]byte(`{
"type": "m.room.name",
"state_key": "",
"event_id": "$test:localhost",
"room_id": "!test:localhost",
"sender": "@test:localhost",
"content": {
"name": "Hello World"
},
"origin_server_ts": 123456,
"unsigned": {
"prev_content": {
"name": "Goodbye World"
}
}
}`), false, gomatrixserverlib.RoomVersionV1)
if err != nil {
t.Fatalf("failed to create Event: %s", err)
}
ce := ToClientEvent(ev, FormatAll)
if ce.EventID != ev.EventID() {
t.Errorf("ClientEvent.EventID: wanted %s, got %s", ev.EventID(), ce.EventID)
}
if ce.OriginServerTS != ev.OriginServerTS() {
t.Errorf("ClientEvent.OriginServerTS: wanted %d, got %d", ev.OriginServerTS(), ce.OriginServerTS)
}
if ce.StateKey == nil || *ce.StateKey != "" {
t.Errorf("ClientEvent.StateKey: wanted '', got %v", ce.StateKey)
}
if ce.Type != ev.Type() {
t.Errorf("ClientEvent.Type: wanted %s, got %s", ev.Type(), ce.Type)
}
if !bytes.Equal(ce.Content, ev.Content()) {
t.Errorf("ClientEvent.Content: wanted %s, got %s", string(ev.Content()), string(ce.Content))
}
if !bytes.Equal(ce.Unsigned, ev.Unsigned()) {
t.Errorf("ClientEvent.Unsigned: wanted %s, got %s", string(ev.Unsigned()), string(ce.Unsigned))
}
if ce.Sender != ev.Sender() {
t.Errorf("ClientEvent.Sender: wanted %s, got %s", ev.Sender(), ce.Sender)
}
j, err := json.Marshal(ce)
if err != nil {
t.Fatalf("failed to Marshal ClientEvent: %s", err)
}
// Marshal sorts keys in structs by the order they are defined in the struct, which is alphabetical
out := `{"content":{"name":"Hello World"},"event_id":"$test:localhost","origin_server_ts":123456,` +
`"room_id":"!test:localhost","sender":"@test:localhost","state_key":"","type":"m.room.name",` +
`"unsigned":{"prev_content":{"name":"Goodbye World"}}}`
if !bytes.Equal([]byte(out), j) {
t.Errorf("ClientEvent marshalled to wrong bytes: wanted %s, got %s", out, string(j))
}
}
func TestToClientFormatSync(t *testing.T) {
ev, err := gomatrixserverlib.NewEventFromTrustedJSON([]byte(`{
"type": "m.room.name",
"state_key": "",
"event_id": "$test:localhost",
"room_id": "!test:localhost",
"sender": "@test:localhost",
"content": {
"name": "Hello World"
},
"origin_server_ts": 123456,
"unsigned": {
"prev_content": {
"name": "Goodbye World"
}
}
}`), false, gomatrixserverlib.RoomVersionV1)
if err != nil {
t.Fatalf("failed to create Event: %s", err)
}
ce := ToClientEvent(ev, FormatSync)
if ce.RoomID != "" {
t.Errorf("ClientEvent.RoomID: wanted '', got %s", ce.RoomID)
}
}

View file

@ -25,6 +25,7 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/syncapi/synctypes"
) )
var ( var (
@ -451,13 +452,13 @@ type UnreadNotifications struct {
} }
type ClientEvents struct { type ClientEvents struct {
Events []gomatrixserverlib.ClientEvent `json:"events,omitempty"` Events []synctypes.ClientEvent `json:"events,omitempty"`
} }
type Timeline struct { type Timeline struct {
Events []gomatrixserverlib.ClientEvent `json:"events"` Events []synctypes.ClientEvent `json:"events"`
Limited bool `json:"limited"` Limited bool `json:"limited"`
PrevBatch *TopologyToken `json:"prev_batch,omitempty"` PrevBatch *TopologyToken `json:"prev_batch,omitempty"`
} }
type Summary struct { type Summary struct {
@ -549,7 +550,7 @@ func NewInviteResponse(event *gomatrixserverlib.HeaderedEvent) *InviteResponse {
// Then we'll see if we can create a partial of the invite event itself. // Then we'll see if we can create a partial of the invite event itself.
// This is needed for clients to work out *who* sent the invite. // This is needed for clients to work out *who* sent the invite.
inviteEvent := gomatrixserverlib.ToClientEvent(event.Unwrap(), gomatrixserverlib.FormatSync) inviteEvent := synctypes.ToClientEvent(event.Unwrap(), synctypes.FormatSync)
inviteEvent.Unsigned = nil inviteEvent.Unsigned = nil
if ev, err := json.Marshal(inviteEvent); err == nil { if ev, err := json.Marshal(inviteEvent); err == nil {
res.InviteState.Events = append(res.InviteState.Events, ev) res.InviteState.Events = append(res.InviteState.Events, ev)

View file

@ -5,6 +5,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -125,7 +126,7 @@ func TestJoinResponse_MarshalJSON(t *testing.T) {
{ {
name: "unread notifications are NOT removed, if state is set", name: "unread notifications are NOT removed, if state is set",
fields: fields{ fields: fields{
State: &ClientEvents{Events: []gomatrixserverlib.ClientEvent{{Content: []byte("{}")}}}, State: &ClientEvents{Events: []synctypes.ClientEvent{{Content: []byte("{}")}}},
UnreadNotifications: &UnreadNotifications{NotificationCount: 1}, UnreadNotifications: &UnreadNotifications{NotificationCount: 1},
}, },
want: []byte(`{"state":{"events":[{"content":{},"type":""}]},"unread_notifications":{"highlight_count":0,"notification_count":1}}`), want: []byte(`{"state":{"events":[{"content":{},"type":""}]},"unread_notifications":{"highlight_count":0,"notification_count":1}}`),
@ -134,7 +135,7 @@ func TestJoinResponse_MarshalJSON(t *testing.T) {
name: "roomID is removed from EDUs", name: "roomID is removed from EDUs",
fields: fields{ fields: fields{
Ephemeral: &ClientEvents{ Ephemeral: &ClientEvents{
Events: []gomatrixserverlib.ClientEvent{ Events: []synctypes.ClientEvent{
{RoomID: "!someRandomRoomID:test", Content: []byte("{}")}, {RoomID: "!someRandomRoomID:test", Content: []byte("{}")},
}, },
}, },

View file

@ -21,6 +21,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/userapi/types" "github.com/matrix-org/dendrite/userapi/types"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -580,12 +581,12 @@ type QueryNotificationsResponse struct {
} }
type Notification struct { type Notification struct {
Actions []*pushrules.Action `json:"actions"` // Required. Actions []*pushrules.Action `json:"actions"` // Required.
Event gomatrixserverlib.ClientEvent `json:"event"` // Required. Event synctypes.ClientEvent `json:"event"` // Required.
ProfileTag string `json:"profile_tag"` // Required by Sytest, but actually optional. ProfileTag string `json:"profile_tag"` // Required by Sytest, but actually optional.
Read bool `json:"read"` // Required. Read bool `json:"read"` // Required.
RoomID string `json:"room_id"` // Required. RoomID string `json:"room_id"` // Required.
TS gomatrixserverlib.Timestamp `json:"ts"` // Required. TS gomatrixserverlib.Timestamp `json:"ts"` // Required.
} }
type QueryNumericLocalpartRequest struct { type QueryNumericLocalpartRequest struct {

View file

@ -23,6 +23,7 @@ import (
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/dendrite/setup/process"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/dendrite/userapi/producers" "github.com/matrix-org/dendrite/userapi/producers"
@ -298,7 +299,7 @@ func (s *OutputRoomEventConsumer) processMessage(ctx context.Context, event *gom
switch { switch {
case event.Type() == gomatrixserverlib.MRoomMember: case event.Type() == gomatrixserverlib.MRoomMember:
cevent := gomatrixserverlib.HeaderedToClientEvent(event, gomatrixserverlib.FormatAll) cevent := synctypes.HeaderedToClientEvent(event, synctypes.FormatAll)
var member *localMembership var member *localMembership
member, err = newLocalMembership(&cevent) member, err = newLocalMembership(&cevent)
if err != nil { if err != nil {
@ -358,7 +359,7 @@ type localMembership struct {
Domain gomatrixserverlib.ServerName Domain gomatrixserverlib.ServerName
} }
func newLocalMembership(event *gomatrixserverlib.ClientEvent) (*localMembership, error) { func newLocalMembership(event *synctypes.ClientEvent) (*localMembership, error) {
if event.StateKey == nil { if event.StateKey == nil {
return nil, fmt.Errorf("missing state_key") return nil, fmt.Errorf("missing state_key")
} }
@ -531,7 +532,7 @@ func (s *OutputRoomEventConsumer) notifyLocal(ctx context.Context, event *gomatr
// UNSPEC: the spec doesn't say this is a ClientEvent, but the // UNSPEC: the spec doesn't say this is a ClientEvent, but the
// fields seem to match. room_id should be missing, which // fields seem to match. room_id should be missing, which
// matches the behaviour of FormatSync. // matches the behaviour of FormatSync.
Event: gomatrixserverlib.HeaderedToClientEvent(event, gomatrixserverlib.FormatSync), Event: synctypes.HeaderedToClientEvent(event, synctypes.FormatSync),
// TODO: this is per-device, but it's not part of the primary // TODO: this is per-device, but it's not part of the primary
// key. So inserting one notification per profile tag doesn't // key. So inserting one notification per profile tag doesn't
// make sense. What is this supposed to be? Sytests require it // make sense. What is this supposed to be? Sytests require it

View file

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/dendrite/userapi/types" "github.com/matrix-org/dendrite/userapi/types"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
@ -526,7 +527,7 @@ func Test_Notification(t *testing.T) {
Actions: []*pushrules.Action{ Actions: []*pushrules.Action{
{}, {},
}, },
Event: gomatrixserverlib.ClientEvent{ Event: synctypes.ClientEvent{
Content: gomatrixserverlib.RawJSON("{}"), Content: gomatrixserverlib.RawJSON("{}"),
}, },
Read: false, Read: false,

View file

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/syncapi/synctypes"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -99,7 +100,7 @@ func TestNotifyUserCountsAsync(t *testing.T) {
// Insert a dummy event // Insert a dummy event
if err := db.InsertNotification(ctx, aliceLocalpart, serverName, dummyEvent.EventID(), 0, nil, &api.Notification{ if err := db.InsertNotification(ctx, aliceLocalpart, serverName, dummyEvent.EventID(), 0, nil, &api.Notification{
Event: gomatrixserverlib.HeaderedToClientEvent(dummyEvent, gomatrixserverlib.FormatAll), Event: synctypes.HeaderedToClientEvent(dummyEvent, synctypes.FormatAll),
}); err != nil { }); err != nil {
t.Error(err) t.Error(err)
} }