2019-07-21 09:43:14 +00:00
|
|
|
package memory
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
import (
|
2019-07-25 16:56:31 +00:00
|
|
|
"sync"
|
2019-07-21 11:52:32 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nxshock/signaller/internal"
|
|
|
|
"github.com/nxshock/signaller/internal/models"
|
|
|
|
"github.com/nxshock/signaller/internal/models/createroom"
|
2019-07-29 14:32:38 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/devices"
|
2019-07-21 11:52:32 +00:00
|
|
|
"github.com/nxshock/signaller/internal/models/rooms"
|
|
|
|
)
|
|
|
|
|
2019-07-21 09:43:14 +00:00
|
|
|
type User struct {
|
|
|
|
name string
|
|
|
|
password string
|
|
|
|
Tokens map[string]Token
|
|
|
|
|
|
|
|
backend *Backend
|
2019-07-25 16:56:31 +00:00
|
|
|
|
|
|
|
mutex sync.RWMutex
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) ID() string {
|
2019-07-21 11:04:02 +00:00
|
|
|
return "@" + user.name + ":" + user.backend.hostname
|
2019-07-21 09:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Name() string {
|
|
|
|
return user.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Password() string {
|
|
|
|
return user.password
|
|
|
|
}
|
2019-07-21 11:52:32 +00:00
|
|
|
|
|
|
|
func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models.ApiError) {
|
|
|
|
for _, existingRoom := range user.backend.rooms {
|
|
|
|
if existingRoom.AliasName() == request.RoomAliasName { // TODO: strip and check request room alias name before use
|
|
|
|
return nil, internal.NewError(models.M_ROOM_IN_USE, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:08:16 +00:00
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
events := make([]RoomEvent, 0)
|
|
|
|
|
|
|
|
// Create room event
|
|
|
|
events = append(events, RoomEvent{
|
|
|
|
Content: nil,
|
|
|
|
Type: rooms.Create,
|
2019-07-22 14:46:11 +00:00
|
|
|
EventID: newToken(eventIDSize),
|
2019-07-21 12:08:16 +00:00
|
|
|
Sender: user,
|
|
|
|
OriginServerTS: t})
|
|
|
|
|
2019-07-22 13:46:59 +00:00
|
|
|
// TODO: Add join room event
|
|
|
|
|
2019-07-21 12:08:16 +00:00
|
|
|
// Set join rules event
|
|
|
|
events = append(events, RoomEvent{
|
|
|
|
Content: []byte(request.Visibility), // TODO: check visibility vs join rules
|
|
|
|
Type: rooms.JoinRules,
|
2019-07-22 14:46:11 +00:00
|
|
|
EventID: newToken(eventIDSize),
|
2019-07-21 12:08:16 +00:00
|
|
|
Sender: user,
|
|
|
|
OriginServerTS: t})
|
|
|
|
|
|
|
|
// Set room name event
|
|
|
|
if request.Name != "" {
|
|
|
|
events = append(events, RoomEvent{
|
|
|
|
Content: nil, // TODO: add
|
|
|
|
Type: rooms.Name,
|
2019-07-22 14:46:11 +00:00
|
|
|
EventID: newToken(eventIDSize),
|
2019-07-21 12:08:16 +00:00
|
|
|
Sender: user,
|
|
|
|
OriginServerTS: t})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set room alias event
|
|
|
|
if request.RoomAliasName != "" {
|
|
|
|
events = append(events, RoomEvent{
|
|
|
|
Content: nil, // TODO: add
|
|
|
|
Type: rooms.CanonicalAlias,
|
2019-07-22 14:46:11 +00:00
|
|
|
EventID: newToken(eventIDSize),
|
2019-07-21 12:08:16 +00:00
|
|
|
Sender: user,
|
|
|
|
OriginServerTS: t})
|
|
|
|
}
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
room := &Room{
|
2019-07-31 15:01:20 +00:00
|
|
|
id: newToken(groupIDSize),
|
|
|
|
aliasName: request.RoomAliasName,
|
|
|
|
name: request.Name,
|
|
|
|
topic: request.Topic,
|
|
|
|
events: events,
|
|
|
|
creator: user,
|
|
|
|
joined: []internal.User{user},
|
|
|
|
visibility: request.Visibility,
|
|
|
|
server: user.backend}
|
2019-07-21 12:08:16 +00:00
|
|
|
|
|
|
|
for i, _ := range room.events {
|
|
|
|
room.events[i].Room = room
|
|
|
|
//v.Room = room
|
|
|
|
}
|
2019-07-21 11:52:32 +00:00
|
|
|
|
2019-07-31 15:01:20 +00:00
|
|
|
user.backend.rooms[room.ID()] = room
|
2019-07-21 11:52:32 +00:00
|
|
|
|
|
|
|
return room, nil
|
|
|
|
}
|
2019-07-22 13:38:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-07-22 13:58:18 +00:00
|
|
|
|
|
|
|
func (user *User) LeaveRoom(room internal.Room) *models.ApiError {
|
|
|
|
room.(*Room).mutex.Lock()
|
|
|
|
defer room.(*Room).mutex.Unlock()
|
|
|
|
|
|
|
|
for i, roomMember := range room.(*Room).joined {
|
|
|
|
if roomMember.ID() == user.ID() {
|
2019-07-22 14:27:23 +00:00
|
|
|
room.(*Room).joined = append(room.(*Room).joined[:i], room.(*Room).joined[i+1:]...) // TODO: add event
|
2019-07-22 13:58:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return internal.NewError(models.M_BAD_STATE, "you are not a member of group") // TODO: check error code
|
|
|
|
}
|
2019-07-23 14:24:18 +00:00
|
|
|
|
|
|
|
func (user *User) SendMessage(room internal.Room, text string) *models.ApiError {
|
|
|
|
room.(*Room).mutex.Lock()
|
|
|
|
defer room.(*Room).mutex.Unlock()
|
|
|
|
|
|
|
|
userInRoom := false
|
|
|
|
for _, roomMember := range room.(*Room).joined {
|
|
|
|
if roomMember.ID() == user.ID() {
|
|
|
|
userInRoom = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !userInRoom {
|
|
|
|
return internal.NewError(models.M_FORBIDDEN, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
room.(*Room).events = append(room.(*Room).events, RoomEvent{
|
|
|
|
Content: nil,
|
|
|
|
Type: rooms.Message,
|
|
|
|
EventID: newToken(defaultTokenSize),
|
|
|
|
Sender: user,
|
|
|
|
OriginServerTS: time.Now(),
|
|
|
|
Room: room})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-24 14:15:07 +00:00
|
|
|
|
2019-07-24 15:04:45 +00:00
|
|
|
func (user *User) JoinedRooms() []internal.Room {
|
|
|
|
user.backend.mutex.Lock()
|
|
|
|
defer user.backend.mutex.Unlock()
|
|
|
|
|
|
|
|
var result []internal.Room
|
|
|
|
|
|
|
|
for _, room := range user.backend.rooms {
|
|
|
|
for _, user := range room.(*Room).joined {
|
|
|
|
if user.ID() == user.ID() {
|
|
|
|
result = append(result, room)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:32:38 +00:00
|
|
|
func (user *User) Devices() []devices.Device {
|
|
|
|
user.backend.mutex.Lock()
|
|
|
|
defer user.backend.mutex.Unlock()
|
|
|
|
|
|
|
|
var result []devices.Device
|
|
|
|
|
|
|
|
for _, token := range user.Tokens {
|
|
|
|
device := devices.Device{
|
|
|
|
DeviceID: token.Device}
|
|
|
|
|
|
|
|
result = append(result, device)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:01:20 +00:00
|
|
|
func (user *User) SetRoomVisibility(room internal.Room, visibilityType createroom.VisibilityType) *models.ApiError {
|
|
|
|
if user.ID() != room.Creator().ID() {
|
|
|
|
return internal.NewError(models.M_FORBIDDEN, "only room owner can change visibility") // TODO: room administrators can use this method too
|
|
|
|
}
|
|
|
|
|
|
|
|
room.(*Room).mutex.Lock()
|
|
|
|
defer room.(*Room).mutex.Unlock()
|
|
|
|
|
|
|
|
room.(*Room).visibility = visibilityType
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 16:56:31 +00:00
|
|
|
func (user *User) ChangePassword(newPassword string) {
|
|
|
|
user.mutex.Lock()
|
|
|
|
defer user.mutex.Unlock()
|
|
|
|
|
|
|
|
user.password = newPassword
|
|
|
|
}
|
|
|
|
|
2019-07-24 14:31:07 +00:00
|
|
|
func (user *User) Logout(token string) {
|
|
|
|
delete(user.Tokens, token)
|
|
|
|
}
|
|
|
|
|
2019-07-24 14:15:07 +00:00
|
|
|
func (user *User) LogoutAll() {
|
|
|
|
user.Tokens = make(map[string]Token)
|
|
|
|
}
|