Start implementation of Invite method

This commit is contained in:
nxshock 2019-08-04 13:47:58 +05:00
parent 3185bda38e
commit a513e35953
5 changed files with 61 additions and 1 deletions

View File

@ -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) - [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.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) - [ ] [10.4.2.3 POST /_matrix/client/r0/join/{roomIdOrAlias}](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-join-roomidoralias)

View File

@ -49,4 +49,5 @@ type User interface {
Logout(token string) Logout(token string)
LogoutAll() LogoutAll()
JoinRoom(Room) models.ApiError JoinRoom(Room) models.ApiError
Invite(Room, User) models.ApiError
} }

View File

@ -21,6 +21,7 @@ type Room struct {
creator internal.User creator internal.User
joined []internal.User joined []internal.User
invites []internal.User
events []RoomEvent events []RoomEvent

View File

@ -122,6 +122,42 @@ func (user *User) SetTopic(room internal.Room, topic string) models.ApiError {
return nil 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 { func (user *User) LeaveRoom(room internal.Room) models.ApiError {
room.(*Room).mutex.Lock() room.(*Room).mutex.Lock()
defer room.(*Room).mutex.Unlock() defer room.(*Room).mutex.Unlock()

View File

@ -192,3 +192,25 @@ func TestLogoutAll(t *testing.T) {
assert.Len(t, user.Devices(), 0) 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)
}