Add user.JoinedRooms() method

This commit is contained in:
nxshock 2019-07-24 20:04:45 +05:00
parent a05a6dfb54
commit 7293b33b6f
7 changed files with 77 additions and 1 deletions

View File

@ -15,3 +15,4 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/
- [x] [5.4.3 POST /_matrix/client/r0/logout](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout)
- [x] [5.4.4 POST /_matrix/client/r0/logout/all](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all)
- [x] [5.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami)
- [x] [10.4.1 GET /_matrix/client/r0/joined_rooms](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms)

View File

@ -32,6 +32,7 @@ type User interface {
LeaveRoom(room Room) *models.ApiError
SetTopic(room Room, topic string) *models.ApiError
SendMessage(room Room, text string) *models.ApiError
JoinedRooms() []Room
Logout(token string)
LogoutAll()
}

View File

@ -85,7 +85,8 @@ func (user *User) CreateRoom(request createroom.Request) (internal.Room, *models
topic: request.Topic,
events: events,
creator: user,
joined: []internal.User{user}}
joined: []internal.User{user},
server: user.backend}
for i, _ := range room.events {
room.events[i].Room = room
@ -155,6 +156,23 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError
return nil
}
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
}
func (user *User) Logout(token string) {
delete(user.Tokens, token)
}

View File

@ -3,6 +3,8 @@ package memory
import (
"testing"
"github.com/nxshock/signaller/internal"
"github.com/stretchr/testify/assert"
"github.com/nxshock/signaller/internal/models/createroom"
@ -98,3 +100,21 @@ func TestLogoutWithWrongToken(t *testing.T) {
user.Logout("worng token")
}
func TestJoinedRooms(t *testing.T) {
backend := NewBackend("localhost")
user, _, err := backend.Register("user1", "", "")
assert.Nil(t, err)
request := createroom.Request{
RoomAliasName: "room1",
Name: "room1",
Topic: "topic"}
room, err := user.CreateRoom(request)
assert.Nil(t, err)
rooms := user.JoinedRooms()
assert.Equal(t, []internal.Room{room}, rooms)
}

View File

@ -11,6 +11,7 @@ import (
"github.com/nxshock/signaller/internal/models/common"
"github.com/nxshock/signaller/internal/models"
"github.com/nxshock/signaller/internal/models/joinedrooms"
login "github.com/nxshock/signaller/internal/models/login"
register "github.com/nxshock/signaller/internal/models/register"
mSync "github.com/nxshock/signaller/internal/models/sync"
@ -168,6 +169,34 @@ func WhoAmIHandler(w http.ResponseWriter, r *http.Request) {
sendJsonResponse(w, http.StatusOK, response)
}
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms
func JoinedRoomsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method)
return
}
token := getTokenFromResponse(r)
if token == "" {
errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "")
}
user := currServer.Backend.GetUserByToken(token)
if user == nil {
errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return
}
rooms := user.JoinedRooms()
var response joinedrooms.Response
for _, room := range rooms {
response.JoinedRooms = append(response.JoinedRooms, room.ID())
}
sendJsonResponse(w, http.StatusOK, response)
}
// https://models.org/docs/spec/client_server/latest#get-models-client-r0-sync
func SyncHandler(w http.ResponseWriter, r *http.Request) {
var request mSync.SyncRequest

View File

@ -0,0 +1,6 @@
package joinedrooms
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms
type Response struct {
JoinedRooms []string `json:"joined_rooms"` // Required. The ID of each room in which the user has joined membership.
}

View File

@ -24,6 +24,7 @@ func New() *Server {
router.HandleFunc("/_matrix/client/r0/logout/all", LogoutAllHandler)
router.HandleFunc("/_matrix/client/r0/register", RegisterHandler)
router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler)
router.HandleFunc("/_matrix/client/r0/joined_rooms", JoinedRoomsHandler)
router.HandleFunc("/_matrix/client/r0/sync", SyncHandler)
router.HandleFunc("/", RootHandler)