diff --git a/vendor/manifest b/vendor/manifest index cbb026a90..dceb7916c 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -92,7 +92,7 @@ { "importpath": "github.com/matrix-org/gomatrixserverlib", "repository": "https://github.com/matrix-org/gomatrixserverlib", - "revision": "e757e4f7f675c7356d4b2953059718f1b4598a08", + "revision": "8e847d5bdb5cc0dd11e0846188eb403b70ae6bb3", "branch": "master" }, { diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/clientevent.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/clientevent.go new file mode 100644 index 000000000..d4dcac48c --- /dev/null +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/clientevent.go @@ -0,0 +1,66 @@ +/* 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 gomatrixserverlib + +// EventFormat specifies the format of a client event +type EventFormat int + +const ( + // FormatAll will include all client event keys + FormatAll EventFormat = 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 rawJSON `json:"content"` + EventID string `json:"event_id"` + OriginServerTS int64 `json:"origin_server_ts"` + // RoomID is omitted on /sync responses + RoomID string `json:"room_id,omitempty"` + Sender string `json:"sender"` + StateKey *string `json:"state_key,omitempty"` + Type string `json:"type"` + Unsigned rawJSON `json:"unsigned,omitempty"` +} + +// ToClientEvents converts server events to client events. +func ToClientEvents(serverEvs []Event, format EventFormat) []ClientEvent { + evs := make([]ClientEvent, len(serverEvs)) + for i, se := range serverEvs { + evs[i] = ToClientEvent(se, format) + } + return evs +} + +// ToClientEvent converts a single server event to a client event. +func ToClientEvent(se Event, format EventFormat) ClientEvent { + ce := ClientEvent{ + Content: rawJSON(se.Content()), + Sender: se.Sender(), + Type: se.Type(), + StateKey: se.StateKey(), + Unsigned: rawJSON(se.Unsigned()), + OriginServerTS: se.OriginServerTS(), + EventID: se.EventID(), + } + if format == FormatAll { + ce.RoomID = se.RoomID() + } + return ce +} diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/clientevent_test.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/clientevent_test.go new file mode 100644 index 000000000..fdeae5320 --- /dev/null +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/clientevent_test.go @@ -0,0 +1,103 @@ +/* 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 gomatrixserverlib + +import ( + "bytes" + "encoding/json" + "testing" +) + +func TestToClientEvent(t *testing.T) { + ev, err := 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) + 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 := 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) + 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) + } +} diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go index 126830307..778510866 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go @@ -88,16 +88,18 @@ type Event struct { } type eventFields struct { - RoomID string `json:"room_id"` - EventID string `json:"event_id"` - Sender string `json:"sender"` - Type string `json:"type"` - StateKey *string `json:"state_key"` - Content rawJSON `json:"content"` - PrevEvents []EventReference `json:"prev_events"` - AuthEvents []EventReference `json:"auth_events"` - Redacts string `json:"redacts"` - Depth int64 `json:"depth"` + RoomID string `json:"room_id"` + EventID string `json:"event_id"` + Sender string `json:"sender"` + Type string `json:"type"` + StateKey *string `json:"state_key"` + Content rawJSON `json:"content"` + PrevEvents []EventReference `json:"prev_events"` + AuthEvents []EventReference `json:"auth_events"` + Redacts string `json:"redacts"` + Depth int64 `json:"depth"` + Unsigned rawJSON `json:"unsigned"` + OriginServerTS int64 `json:"origin_server_ts"` } var emptyEventReferenceList = []EventReference{} @@ -308,6 +310,16 @@ func (e Event) Type() string { return e.fields.Type } +// OriginServerTS returns the unix timestamp when this event was created on the origin server, with millisecond resolution. +func (e Event) OriginServerTS() int64 { + return e.fields.OriginServerTS +} + +// Unsigned returns the object under the 'unsigned' key of the event. +func (e Event) Unsigned() []byte { + return []byte(e.fields.Unsigned) +} + // Content returns the content JSON of the event. func (e Event) Content() []byte { return []byte(e.fields.Content)