2019-07-21 09:43:14 +00:00
|
|
|
package memory
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nxshock/signaller/internal"
|
|
|
|
"github.com/nxshock/signaller/internal/models"
|
|
|
|
"github.com/nxshock/signaller/internal/models/createroom"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2019-07-21 11:52:32 +00:00
|
|
|
room := &Room{
|
|
|
|
id: internal.NewToken(groupIDSize),
|
|
|
|
aliasName: request.RoomAliasName,
|
|
|
|
name: request.Name,
|
|
|
|
topic: request.Topic,
|
2019-07-21 12:08:16 +00:00
|
|
|
events: events,
|
|
|
|
creator: user}
|
|
|
|
|
|
|
|
for i, _ := range room.events {
|
|
|
|
room.events[i].Room = room
|
|
|
|
//v.Room = room
|
|
|
|
}
|
2019-07-21 11:52:32 +00:00
|
|
|
|
|
|
|
user.backend.rooms[room.id] = room
|
|
|
|
|
|
|
|
return room, nil
|
|
|
|
}
|