From feb71433c4b08bf7c1ef6da4a5c97cefb328be63 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sun, 21 Jul 2019 16:30:14 +0500 Subject: [PATCH] Add room event on changint topic --- internal/backends/memory/roomevents.go | 30 ++++++++++++++++ internal/backends/memory/rooms.go | 22 +++++++----- internal/backends/memory/rooms_test.go | 1 + internal/models/events/events.go | 23 ------------ internal/models/rooms/rooms.go | 48 ++++++++++++++++++++++++-- 5 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 internal/backends/memory/roomevents.go diff --git a/internal/backends/memory/roomevents.go b/internal/backends/memory/roomevents.go new file mode 100644 index 0000000..e3b622f --- /dev/null +++ b/internal/backends/memory/roomevents.go @@ -0,0 +1,30 @@ +package memory + +import ( + "encoding/json" + "time" + + "github.com/nxshock/signaller/internal" + "github.com/nxshock/signaller/internal/models/rooms" +) + +type RoomEvent struct { + Content json.RawMessage + Type rooms.Type + EventID string + Sender internal.User + OriginServerTS time.Time + Room internal.Room +} + +func (roomEvent *RoomEvent) ToEvent() rooms.Event { + event := rooms.Event{ + Content: roomEvent.Content, + Type: roomEvent.Type, + EventID: roomEvent.EventID, + Sender: roomEvent.Sender.ID(), + OriginServerTS: roomEvent.OriginServerTS.Unix(), + RoomID: roomEvent.Room.ID()} + + return event +} diff --git a/internal/backends/memory/rooms.go b/internal/backends/memory/rooms.go index 3b21e19..1429ef4 100644 --- a/internal/backends/memory/rooms.go +++ b/internal/backends/memory/rooms.go @@ -2,6 +2,7 @@ package memory import ( "sync" + "time" "github.com/nxshock/signaller/internal/models" @@ -19,7 +20,7 @@ type Room struct { creator internal.User - events []rooms.Event + events []RoomEvent mutex sync.RWMutex } @@ -63,7 +64,12 @@ func (room *Room) Events() []rooms.Event { room.mutex.RLock() defer room.mutex.RUnlock() - return room.events + result := make([]rooms.Event, 0) + for _, v := range room.events { + result = append(result, v.ToEvent()) + } + + return result } func (room *Room) Creator() internal.User { @@ -73,13 +79,6 @@ func (room *Room) Creator() internal.User { return room.creator } -func (room *Room) NewEvent(event rooms.Event) { - room.mutex.Lock() - defer room.mutex.Unlock() - - room.events = append(room.events, event) -} - func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError { room.mutex.Lock() defer room.mutex.Unlock() @@ -89,6 +88,11 @@ func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError { } room.topic = topic + room.events = append(room.events, RoomEvent{ + Type: rooms.Topic, + Sender: user, + OriginServerTS: time.Now(), + Room: room}) return nil } diff --git a/internal/backends/memory/rooms_test.go b/internal/backends/memory/rooms_test.go index 8d9dc33..5ee9135 100644 --- a/internal/backends/memory/rooms_test.go +++ b/internal/backends/memory/rooms_test.go @@ -79,6 +79,7 @@ func TestSetRoomTopic(t *testing.T) { err := room.SetTopic(user, newTopic) assert.Nil(t, err) assert.Equal(t, newTopic, room.Topic()) + assert.Equal(t, 1, len(room.Events())) } func TestSetRoomTopicWithnprivelegedUser(t *testing.T) { diff --git a/internal/models/events/events.go b/internal/models/events/events.go index c0b47a2..6c45562 100644 --- a/internal/models/events/events.go +++ b/internal/models/events/events.go @@ -7,29 +7,6 @@ import ( // Type is type of event type Type string -const ( - // https://matrix.org/docs/spec/client_server/latest#m-room-aliases - RoomAliases Type = "m.room.aliases" - - // https://matrix.org/docs/spec/client_server/latest#m-room-canonical-alias - RoomCanonicalAlias Type = "m.room.canonical_alias" - - // https://matrix.org/docs/spec/client_server/latest#m-room-create - RoomCreate Type = "m.room.create" - - // https://matrix.org/docs/spec/client_server/latest#m-room-join-rules - RoomJoinRules Type = "m.room.join_rules" - - // https://matrix.org/docs/spec/client_server/latest#m-room-member - RoomMember Type = "m.room.member" - - // https://matrix.org/docs/spec/client_server/latest#m-room-power-levels - RoomPowerLevels Type = "m.room.power_levels" - - // https://matrix.org/docs/spec/client_server/latest#m-room-redaction - RoomRedaction Type = "m.room.redaction" -) - // Event is the basic set of fields all events must have // https://matrix.org/docs/spec/client_server/latest#event-fields type Event struct { diff --git a/internal/models/rooms/rooms.go b/internal/models/rooms/rooms.go index c713838..58bcbde 100644 --- a/internal/models/rooms/rooms.go +++ b/internal/models/rooms/rooms.go @@ -6,13 +6,57 @@ import ( "github.com/nxshock/signaller/internal/models/events" ) +// Type is type of event +type Type string + +const ( + // https://matrix.org/docs/spec/client_server/latest#m-room-aliases + Aliases Type = "m.room.aliases" + + // https://matrix.org/docs/spec/client_server/latest#m-room-canonical-alias + CanonicalAlias = "m.room.canonical_alias" + + // https://matrix.org/docs/spec/client_server/latest#m-room-create + Create = "m.room.create" + + // https://matrix.org/docs/spec/client_server/latest#m-room-join-rules + JoinRules = "m.room.join_rules" + + // https://matrix.org/docs/spec/client_server/latest#m-room-member + Member = "m.room.member" + + // https://matrix.org/docs/spec/client_server/latest#m-room-power-levels + PowerLevels = "m.room.power_levels" + + // https://matrix.org/docs/spec/client_server/latest#m-room-redaction + Redaction = "m.room.redaction" + + // https://matrix.org/docs/spec/client_server/latest#m-room-message + Message = "m.room.message" + + // https://matrix.org/docs/spec/client_server/latest#m-room-message-feedback + MessageFeedback = "m.room.message.feedback" + + // https://matrix.org/docs/spec/client_server/latest#m-room-name + Name = "m.room.name" + + // https://matrix.org/docs/spec/client_server/latest#m-room-topic + Topic = "m.room.topic" + + // https://matrix.org/docs/spec/client_server/latest#m-room-avatar + Avatar = "m.room.avatar" + + // https://matrix.org/docs/spec/client_server/latest#m-room-pinned-events + PinnedEvents = "m.room.pinned_events" +) + // https://matrix.org/docs/spec/client_server/latest#room-event-fields type Event struct { Content json.RawMessage `json:"content"` // Required. The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body. - Type string `json:"type"` // Required. The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. 'com.example.subdomain.event.type' + Type Type `json:"type"` // Required. The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. 'com.example.subdomain.event.type' EventID string `json:"event_id"` // Required. The globally unique event identifier. Sender string `json:"sender"` // Required. Contains the fully-qualified ID of the user who sent this event. - OriginServerTS int `json:"origin_server_ts"` // Required. Timestamp in milliseconds on originating homeserver when this event was sent. + OriginServerTS int64 `json:"origin_server_ts"` // Required. Timestamp in milliseconds on originating homeserver when this event was sent. Unsigned UnsignedData `json:"unsigned,omitempty"` // Contains optional extra information about the event. RoomID string `json:"room_id"` // Required. The ID of the room associated with this event. Will not be present on events that arrive through /sync, despite being required everywhere else. }