mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-25 20:02:20 +00:00
refactor: Some backend-interface universalization
This commit is contained in:
parent
cb76704a5e
commit
9a1400709b
@ -21,12 +21,17 @@ type Backend interface {
|
|||||||
PutEvent(events.Event) error
|
PutEvent(events.Event) error
|
||||||
GetRoomByAlias(string) Room
|
GetRoomByAlias(string) Room
|
||||||
GetEventsSince(user User, sinceToken string, limit int) []events.Event
|
GetEventsSince(user User, sinceToken string, limit int) []events.Event
|
||||||
|
GetRecentEventsOfRoom(roomID string, user User, limit int) []events.Event
|
||||||
|
Hostname() string
|
||||||
|
Rooms() map[string]Room // RoomID : Room
|
||||||
|
Aliases() map[string]Room // Alias : Room
|
||||||
|
PutRoom(room Room) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Room interface {
|
type Room interface {
|
||||||
ID() string
|
ID() string
|
||||||
Creator() User
|
Creator() string
|
||||||
Users() []User
|
Users() []string
|
||||||
AliasName() string
|
AliasName() string
|
||||||
Aliases() []string
|
Aliases() []string
|
||||||
Name() string
|
Name() string
|
||||||
@ -36,6 +41,8 @@ type Room interface {
|
|||||||
GuestCanJoin() bool
|
GuestCanJoin() bool
|
||||||
AvatarURL() string
|
AvatarURL() string
|
||||||
State() createroom.Preset
|
State() createroom.Preset
|
||||||
|
PutInvited(invitedID string)
|
||||||
|
PutJoined(joinedID string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type User interface {
|
type User interface {
|
||||||
|
@ -278,3 +278,7 @@ func extractRoomIDsFromModel(rooms []internal.Room) []string {
|
|||||||
|
|
||||||
return roomIDs
|
return roomIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Backend) PutRoom(room *Room) {
|
||||||
|
this.rooms[room.ID()] = room
|
||||||
|
}
|
||||||
|
@ -3,7 +3,6 @@ package memory
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/signaller-matrix/signaller/internal"
|
|
||||||
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
"github.com/signaller-matrix/signaller/internal/models/createroom"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,9 +17,9 @@ type Room struct {
|
|||||||
guestCanJoin bool
|
guestCanJoin bool
|
||||||
avatarURL string
|
avatarURL string
|
||||||
|
|
||||||
creator internal.User
|
creator string
|
||||||
joined []internal.User
|
joined []string
|
||||||
invites []internal.User
|
invites []string
|
||||||
|
|
||||||
server *Backend
|
server *Backend
|
||||||
|
|
||||||
@ -69,11 +68,11 @@ func (room *Room) Topic() string {
|
|||||||
return room.topic
|
return room.topic
|
||||||
}
|
}
|
||||||
|
|
||||||
func (room *Room) Users() []internal.User {
|
func (this *Room) Users() []string {
|
||||||
room.mutex.RLock()
|
this.mutex.RLock()
|
||||||
defer room.mutex.RUnlock()
|
defer this.mutex.RUnlock()
|
||||||
|
|
||||||
return room.joined
|
return this.joined
|
||||||
}
|
}
|
||||||
|
|
||||||
func (room *Room) Visibility() createroom.VisibilityType {
|
func (room *Room) Visibility() createroom.VisibilityType {
|
||||||
@ -83,11 +82,11 @@ func (room *Room) Visibility() createroom.VisibilityType {
|
|||||||
return room.visibility
|
return room.visibility
|
||||||
}
|
}
|
||||||
|
|
||||||
func (room *Room) Creator() internal.User {
|
func (this *Room) Creator() string {
|
||||||
room.mutex.RLock()
|
this.mutex.RLock()
|
||||||
defer room.mutex.RUnlock()
|
defer this.mutex.RUnlock()
|
||||||
|
|
||||||
return room.creator
|
return this.creator
|
||||||
}
|
}
|
||||||
|
|
||||||
func (room *Room) State() createroom.Preset {
|
func (room *Room) State() createroom.Preset {
|
||||||
@ -117,3 +116,17 @@ func (room *Room) AvatarURL() string {
|
|||||||
|
|
||||||
return room.avatarURL
|
return room.avatarURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Room) PutInvited(invitedID string) {
|
||||||
|
this.mutex.RLock()
|
||||||
|
defer this.mutex.RUnlock()
|
||||||
|
|
||||||
|
this.invites = append(this.invites, invitedID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Room) PutJoined(joinedID string) {
|
||||||
|
this.mutex.RLock()
|
||||||
|
defer this.mutex.RUnlock()
|
||||||
|
|
||||||
|
this.joined = append(this.joined, joinedID)
|
||||||
|
}
|
||||||
|
@ -91,8 +91,8 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
|
|||||||
aliasName: request.RoomAliasName,
|
aliasName: request.RoomAliasName,
|
||||||
name: request.Name,
|
name: request.Name,
|
||||||
topic: request.Topic,
|
topic: request.Topic,
|
||||||
creator: user,
|
creator: user.ID(),
|
||||||
joined: []internal.User{user},
|
joined: []string{user.ID()},
|
||||||
visibility: request.Visibility,
|
visibility: request.Visibility,
|
||||||
server: user.backend,
|
server: user.backend,
|
||||||
state: request.Preset}
|
state: request.Preset}
|
||||||
@ -105,7 +105,7 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, models.
|
|||||||
user.backend.PutEvent(&eventsSlice[i])
|
user.backend.PutEvent(&eventsSlice[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
user.backend.rooms[room.ID()] = room
|
user.backend.PutRoom(room)
|
||||||
|
|
||||||
return room, nil
|
return room, nil
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ func (user *User) SetTopic(room internal.Room, topic string) models.ApiError {
|
|||||||
|
|
||||||
memRoom.mutex.Lock()
|
memRoom.mutex.Lock()
|
||||||
|
|
||||||
if memRoom.creator.ID() != user.ID() { // TODO: currently only creator can change topic
|
if memRoom.creator != user.ID() { // TODO: currently only creator can change topic
|
||||||
memRoom.mutex.Unlock()
|
memRoom.mutex.Unlock()
|
||||||
return models.NewError(models.M_FORBIDDEN, "")
|
return models.NewError(models.M_FORBIDDEN, "")
|
||||||
}
|
}
|
||||||
@ -144,7 +144,7 @@ func (user *User) Invite(room internal.Room, invitee internal.User) models.ApiEr
|
|||||||
userInRoom := false
|
userInRoom := false
|
||||||
|
|
||||||
for _, roomUser := range memRoom.joined {
|
for _, roomUser := range memRoom.joined {
|
||||||
if user.ID() == roomUser.ID() {
|
if user.ID() == roomUser {
|
||||||
userInRoom = true
|
userInRoom = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,19 +155,18 @@ func (user *User) Invite(room internal.Room, invitee internal.User) models.ApiEr
|
|||||||
|
|
||||||
// TODO: remove repeated cycle
|
// TODO: remove repeated cycle
|
||||||
for _, roomUser := range memRoom.joined {
|
for _, roomUser := range memRoom.joined {
|
||||||
if roomUser.ID() == invitee.ID() {
|
if roomUser == invitee.ID() {
|
||||||
return models.NewError(models.M_FORBIDDEN, "the invitee is already a member of the room.") // TODO: check code
|
return models.NewError(models.M_FORBIDDEN, "the invitee is already a member of the room.") // TODO: check code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, inviteeUser := range memRoom.invites {
|
for _, inviteeUser := range memRoom.invites {
|
||||||
if inviteeUser.ID() == invitee.ID() {
|
if inviteeUser == invitee.ID() {
|
||||||
return models.NewError(models.M_FORBIDDEN, "user already has been invited") // TODO: check code
|
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
|
memRoom.PutInvited(invitee.ID()) // TODO: add invite event + info about inviter
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +177,7 @@ func (user *User) LeaveRoom(room internal.Room) models.ApiError {
|
|||||||
defer memRoom.mutex.Unlock()
|
defer memRoom.mutex.Unlock()
|
||||||
|
|
||||||
for i, roomMember := range room.(*Room).joined {
|
for i, roomMember := range room.(*Room).joined {
|
||||||
if roomMember.ID() == user.ID() {
|
if roomMember == user.ID() {
|
||||||
room.(*Room).joined = append(room.(*Room).joined[:i], room.(*Room).joined[i+1:]...) // TODO: add event
|
room.(*Room).joined = append(room.(*Room).joined[:i], room.(*Room).joined[i+1:]...) // TODO: add event
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -195,7 +194,7 @@ func (user *User) SendMessage(room internal.Room, text string) models.ApiError {
|
|||||||
|
|
||||||
userInRoom := false
|
userInRoom := false
|
||||||
for _, roomMember := range memRoom.joined {
|
for _, roomMember := range memRoom.joined {
|
||||||
if roomMember.ID() == user.ID() {
|
if roomMember == user.ID() {
|
||||||
userInRoom = true
|
userInRoom = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,7 +224,7 @@ func (user *User) JoinedRooms() []internal.Room {
|
|||||||
|
|
||||||
for _, room := range user.backend.rooms {
|
for _, room := range user.backend.rooms {
|
||||||
for _, user := range room.(*Room).joined {
|
for _, user := range room.(*Room).joined {
|
||||||
if user.ID() == user.ID() {
|
if user == user {
|
||||||
result = append(result, room)
|
result = append(result, room)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -251,7 +250,7 @@ func (user *User) Devices() []devices.Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) SetRoomVisibility(room internal.Room, visibilityType createroom.VisibilityType) models.ApiError {
|
func (user *User) SetRoomVisibility(room internal.Room, visibilityType createroom.VisibilityType) models.ApiError {
|
||||||
if user.ID() != room.Creator().ID() {
|
if user.ID() != room.Creator() {
|
||||||
return models.NewError(models.M_FORBIDDEN, "only room owner can change visibility") // TODO: room administrators can use this method too
|
return models.NewError(models.M_FORBIDDEN, "only room owner can change visibility") // TODO: room administrators can use this method too
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,12 +289,12 @@ func (user *User) JoinRoom(room internal.Room) models.ApiError {
|
|||||||
defer memRoom.mutex.Unlock()
|
defer memRoom.mutex.Unlock()
|
||||||
|
|
||||||
for _, roomUser := range memRoom.joined {
|
for _, roomUser := range memRoom.joined {
|
||||||
if roomUser.ID() == user.ID() {
|
if roomUser == user.ID() {
|
||||||
return models.NewError(models.M_BAD_STATE, "user already in room") // TODO: check code
|
return models.NewError(models.M_BAD_STATE, "user already in room") // TODO: check code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memRoom.joined = append(memRoom.joined, user)
|
memRoom.PutJoined(user.ID())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -304,7 +303,7 @@ func (user *User) AddRoomAlias(room internal.Room, alias string) models.ApiError
|
|||||||
user.backend.mutex.Lock()
|
user.backend.mutex.Lock()
|
||||||
defer user.backend.mutex.Unlock()
|
defer user.backend.mutex.Unlock()
|
||||||
|
|
||||||
if room.Creator().ID() != user.ID() {
|
if room.Creator() != user.ID() {
|
||||||
return models.NewError(models.M_FORBIDDEN, "only room creator can add room alias") // TODO: make room admins can use this method
|
return models.NewError(models.M_FORBIDDEN, "only room creator can add room alias") // TODO: make room admins can use this method
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,7 +325,7 @@ func (user *User) DeleteRoomAlias(alias string) models.ApiError {
|
|||||||
return models.NewError(models.M_NOT_FOUND, "room not found")
|
return models.NewError(models.M_NOT_FOUND, "room not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if room.Creator().ID() != user.ID() {
|
if room.Creator() != user.ID() {
|
||||||
return models.NewError(models.M_FORBIDDEN, "only room creator can delete room alias") // TODO: make room admins can use this method
|
return models.NewError(models.M_FORBIDDEN, "only room creator can delete room alias") // TODO: make room admins can use this method
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user