Move changing room topic to user method

This commit is contained in:
nxshock 2019-07-22 18:38:02 +05:00
parent d52022726a
commit 9c98ec1cf3
4 changed files with 21 additions and 24 deletions

View File

@ -21,7 +21,6 @@ type Room interface {
AliasName() string
Name() string
Topic() string
SetTopic(user User, topic string) *models.ApiError
Events() []rooms.Event
}
@ -30,4 +29,5 @@ type User interface {
ID() string
Password() string
CreateRoom(request createroom.Request) (Room, *models.ApiError)
SetTopic(room Room, topic string) *models.ApiError
}

View File

@ -2,9 +2,6 @@ package memory
import (
"sync"
"time"
"github.com/nxshock/signaller/internal/models"
"github.com/nxshock/signaller/internal"
"github.com/nxshock/signaller/internal/models/createroom"
@ -78,21 +75,3 @@ func (room *Room) Creator() internal.User {
return room.creator
}
func (room *Room) SetTopic(user internal.User, topic string) *models.ApiError {
room.mutex.Lock()
defer room.mutex.Unlock()
if room.creator.ID() != user.ID() { // TODO: currently only creator can change topic
return internal.NewError(models.M_FORBIDDEN, "")
}
room.topic = topic
room.events = append(room.events, RoomEvent{
Type: rooms.Topic,
Sender: user,
OriginServerTS: time.Now(),
Room: room})
return nil
}

View File

@ -76,7 +76,7 @@ func TestSetRoomTopic(t *testing.T) {
room, _ := user.CreateRoom(request)
var newTopic = "new topic"
err := room.SetTopic(user, newTopic)
err := user.SetTopic(room, newTopic)
assert.Nil(t, err)
assert.Equal(t, newTopic, room.Topic())
assert.Equal(t, 5, len(room.Events())) // TODO: check start event count
@ -96,6 +96,6 @@ func TestSetRoomTopicWithnprivelegedUser(t *testing.T) {
room, _ := creator.CreateRoom(request)
var newTopic = "new topic"
err := room.SetTopic(user2, newTopic)
err := user2.SetTopic(room, newTopic)
assert.NotNil(t, err)
}

View File

@ -93,3 +93,21 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models
return room, nil
}
func (user *User) SetTopic(room internal.Room, topic string) *models.ApiError {
room.(*Room).mutex.Lock()
defer room.(*Room).mutex.Unlock()
if room.(*Room).creator.ID() != user.ID() { // TODO: currently only creator can change topic
return internal.NewError(models.M_FORBIDDEN, "")
}
room.(*Room).topic = topic
room.(*Room).events = append(room.(*Room).events, RoomEvent{
Type: rooms.Topic,
Sender: user,
OriginServerTS: time.Now(),
Room: room})
return nil
}