From a513e35953854adfbff248ff56cc76ddb839bbc6 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sun, 4 Aug 2019 13:47:58 +0500 Subject: [PATCH] Start implementation of Invite method --- STATUS.md | 2 +- internal/backend.go | 1 + internal/backends/memory/rooms.go | 1 + internal/backends/memory/user.go | 36 +++++++++++++++++++++++++++ internal/backends/memory/user_test.go | 22 ++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/STATUS.md b/STATUS.md index f787742..bf77ee5 100644 --- a/STATUS.md +++ b/STATUS.md @@ -97,7 +97,7 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [x] [10.4.1 GET /_matrix/client/r0/joined_rooms](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms) -- [ ] [10.4.2.1 POST /_matrix/client/r0/rooms/{roomId}/invite](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-invite) +- [ ] **[10.4.2.1 POST /_matrix/client/r0/rooms/{roomId}/invite](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-invite)** - [ ] [10.4.2.2 POST /_matrix/client/r0/rooms/{roomId}/join](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-join) - [ ] [10.4.2.3 POST /_matrix/client/r0/join/{roomIdOrAlias}](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-join-roomidoralias) diff --git a/internal/backend.go b/internal/backend.go index a89a7db..86a5431 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -49,4 +49,5 @@ type User interface { Logout(token string) LogoutAll() JoinRoom(Room) models.ApiError + Invite(Room, User) models.ApiError } diff --git a/internal/backends/memory/rooms.go b/internal/backends/memory/rooms.go index 7131f37..63c5057 100644 --- a/internal/backends/memory/rooms.go +++ b/internal/backends/memory/rooms.go @@ -21,6 +21,7 @@ type Room struct { creator internal.User joined []internal.User + invites []internal.User events []RoomEvent diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index 9766654..4a2e90f 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -122,6 +122,42 @@ func (user *User) SetTopic(room internal.Room, topic string) models.ApiError { return nil } +func (user *User) Invite(room internal.Room, invitee internal.User) models.ApiError { + memRoom := room.(*Room) + + memRoom.mutex.Lock() + defer memRoom.mutex.Unlock() + + userInRoom := false + + for _, roomUser := range memRoom.joined { + if user.ID() == roomUser.ID() { + userInRoom = true + } + } + + if !userInRoom { + return models.NewError(models.M_FORBIDDEN, "the inviter is not currently in the room") // TODO: check code + } + + // TODO: remove repeated cycle + for _, roomUser := range memRoom.joined { + if roomUser.ID() == invitee.ID() { + return models.NewError(models.M_FORBIDDEN, "the invitee is already a member of the room.") // TODO: check code + } + } + + for _, inviteeUser := range memRoom.invites { + if inviteeUser.ID() == invitee.ID() { + return models.NewError(models.M_FORBIDDEN, "user already has been invited") // TODO: check code + } + } + + memRoom.invites = append(memRoom.invites, invitee) // TODO: add invite event + info about inviter + + return nil +} + func (user *User) LeaveRoom(room internal.Room) models.ApiError { room.(*Room).mutex.Lock() defer room.(*Room).mutex.Unlock() diff --git a/internal/backends/memory/user_test.go b/internal/backends/memory/user_test.go index 27ac421..55026aa 100644 --- a/internal/backends/memory/user_test.go +++ b/internal/backends/memory/user_test.go @@ -192,3 +192,25 @@ func TestLogoutAll(t *testing.T) { assert.Len(t, user.Devices(), 0) } + +func TestInviteUser(t *testing.T) { + backend := NewBackend("localhost") + + user1, _, err := backend.Register("username1", "", "") + assert.NoError(t, err) + + user2, _, err := backend.Register("username2", "", "") + assert.NoError(t, err) + + request := createroom.Request{ + RoomAliasName: "room1", + Name: "room1"} + + room, err := user1.CreateRoom(request) + assert.NoError(t, err) + assert.Len(t, room.(*Room).invites, 0) + + err = user1.Invite(room, user2) + assert.NoError(t, err) + assert.Len(t, room.(*Room).invites, 1) +}