From d52022726a7ff40bc10102e9f8405ce86c210fc9 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sun, 21 Jul 2019 17:08:16 +0500 Subject: [PATCH] Add room creation events --- internal/backends/memory/rooms_test.go | 2 +- internal/backends/memory/user.go | 55 +++++++++++++++++++++----- internal/models/rooms/rooms.go | 9 +++++ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/internal/backends/memory/rooms_test.go b/internal/backends/memory/rooms_test.go index 6a8821e..dca5937 100644 --- a/internal/backends/memory/rooms_test.go +++ b/internal/backends/memory/rooms_test.go @@ -79,7 +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, 2, len(room.Events())) // create event + topic event == 2 + assert.Equal(t, 5, len(room.Events())) // TODO: check start event count } func TestSetRoomTopicWithnprivelegedUser(t *testing.T) { diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 5500b90..8f4487e 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -36,21 +36,58 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models } } + t := time.Now() + + events := make([]RoomEvent, 0) + + // Create room event + events = append(events, RoomEvent{ + Content: nil, + Type: rooms.Create, + EventID: internal.NewToken(eventIDSize), + Sender: user, + OriginServerTS: t}) + + // Set join rules event + events = append(events, RoomEvent{ + Content: []byte(request.Visibility), // TODO: check visibility vs join rules + Type: rooms.JoinRules, + EventID: internal.NewToken(eventIDSize), + Sender: user, + OriginServerTS: t}) + + // Set room name event + if request.Name != "" { + events = append(events, RoomEvent{ + Content: nil, // TODO: add + Type: rooms.Name, + EventID: internal.NewToken(eventIDSize), + Sender: user, + OriginServerTS: t}) + } + + // Set room alias event + if request.RoomAliasName != "" { + events = append(events, RoomEvent{ + Content: nil, // TODO: add + Type: rooms.CanonicalAlias, + EventID: internal.NewToken(eventIDSize), + Sender: user, + OriginServerTS: t}) + } + room := &Room{ id: internal.NewToken(groupIDSize), aliasName: request.RoomAliasName, name: request.Name, topic: request.Topic, - events: []RoomEvent{ - RoomEvent{ - Content: nil, - Type: rooms.Create, - EventID: internal.NewToken(eventIDSize), - Sender: user, - OriginServerTS: time.Now()}}, - creator: user} + events: events, + creator: user} - room.events[0].Room = room + for i, _ := range room.events { + room.events[i].Room = room + //v.Room = room + } user.backend.rooms[room.id] = room diff --git a/internal/models/rooms/rooms.go b/internal/models/rooms/rooms.go index 58bcbde..a1a5d34 100644 --- a/internal/models/rooms/rooms.go +++ b/internal/models/rooms/rooms.go @@ -50,6 +50,15 @@ const ( PinnedEvents = "m.room.pinned_events" ) +type JoinRule string + +const ( + Public JoinRule = "public" + Knock = "knock" + Invite = "invite" + Private = "private" +) + // 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.