Skip to content

Commit

Permalink
Merge pull request #1003 from esl/rest-api-list-my-rooms
Browse files Browse the repository at this point in the history
REST API list my rooms
  • Loading branch information
bartekgorny committed Sep 28, 2016
2 parents 0258896 + a51bab5 commit 16a0f44
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 53 deletions.
14 changes: 12 additions & 2 deletions apps/ejabberd/src/mongoose_client_api_rooms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ resource_exists(Req, #{jid := #jid{lserver = Server}} = State) ->
MUCLightDomain = muc_light_domain(Server),
case RoomID of
undefined ->
{false, Req2, State};
{Method, Req3} = cowboy_req:method(Req2),
case Method of
<<"GET">> ->
{true, Req3, State};
_ ->
{false, Req3, State}
end;
_ ->
does_room_exist(RoomID, MUCLightDomain, Req2, State)
end.
Expand Down Expand Up @@ -81,7 +87,11 @@ to_json(Req, #{room := Room} = State) ->
subject => proplists:get_value(subject, Config),
participants => [user_to_json(U) || U <- Users]
},
{jiffy:encode(Resp), Req, State}.
{jiffy:encode(Resp), Req, State};
to_json(Req, #{jid := #jid{luser = User, lserver = Server}} = State) ->
Rooms = mod_muc_light_db_backend:get_user_rooms({User, Server}),
RoomIds = [RoomId || {RoomId, _} <- Rooms],
{jiffy:encode(RoomIds), Req, State}.

from_json(Req, State) ->
{Method, Req2} = cowboy_req:method(Req),
Expand Down
100 changes: 49 additions & 51 deletions doc/http-api/client_swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ info:
basePath: /api
schemes:
- https
produces:
- application/json
consumes:
- application/json
paths:
/messages:
post:
Expand All @@ -31,19 +35,8 @@ paths:
get:
description: Gets all recent messages from the archive.
parameters:
- in: query
name: limit
description: |
Specifies the maximum number of messages to be returned.
Default value is 50.
type: integer
- in: query
name: before
description: |
The timestamp in milliseconds.
If set, only messages before this date will be returned.
type: integer
format: int64
- $ref: '#/parameters/messagesLimit'
- $ref: '#/parameters/beforeGuard'
responses:
200:
description: Messages
Expand All @@ -64,19 +57,8 @@ paths:
Example: **alice@wonderland.com**.
in: path
required: true
- in: query
name: limit
description: |
Specifies the maximum number of messages to be returned.
Default value is 50.
type: integer
- in: query
name: before
description: |
The timestamp in milliseconds.
If set, only messages before this date will be returned.
type: integer
format: int64
- $ref: '#/parameters/messagesLimit'
- $ref: '#/parameters/beforeGuard'
responses:
200:
description: Successful response.
Expand All @@ -99,15 +81,22 @@ paths:
description: The room was successfully created.
schema:
$ref: '#/definitions/ResourceID'
get:
description: "Returns list of room to which user is connected."
responses:
200:
description: "List of rooms."
schema:
title: "Rooms"
type: array
items:
type: string
description: "ID of a room"
/rooms/{id}:
get:
description: "Returns room's details."
parameters:
- in: path
name: id
required: true
type: string
description: The ID of a room
- $ref: '#/parameters/roomID'
responses:
200:
description: Successful response
Expand All @@ -122,11 +111,7 @@ paths:
post:
description: "Adds a user to a room."
parameters:
- in: path
name: id
required: true
type: string
description: The ID of a room.
- $ref: '#/parameters/roomID'
- in: body
name: participant
required: true
Expand All @@ -147,11 +132,7 @@ paths:
Owner can remove any user.
Occupant can also use this method, but can only remove itself.
parameters:
- in: path
name: id
required: true
type: string
description: The ID of a room.
- $ref: '#/parameters/roomID'
- in: path
name: user
required: true
Expand All @@ -167,14 +148,11 @@ paths:
description: |
When authenticated user is not allowed to add users to the room.
/rooms/{id}/messages:
parameters:
- $ref: '#/parameters/roomID'
post:
description: Send a message to a room.
parameters:
- in: path
name: id
required: true
type: string
description: The ID of a room.
- in: body
name: message
required: true
Expand All @@ -193,11 +171,8 @@ paths:
get:
description: Get room's messages from the archive.
parameters:
- in: path
name: id
required: true
type: string
description: The ID of a room.
- $ref: '#/parameters/messagesLimit'
- $ref: '#/parameters/beforeGuard'
responses:
200:
description: List of messages in the room's archive.
Expand All @@ -216,6 +191,29 @@ securityDefinitions:
description: |
The userid part is user's bare JID. F.e. alice@wonderland.com.
The password is the same as used to register the user's account.
parameters:
roomID:
in: path
name: id
required: true
type: string
description: The ID of a room
messagesLimit:
in: query
name: limit
description: |
Specifies the maximum number of messages to be returned.
Default value is 50.
type: integer
beforeGuard:
in: query
name: before
description: |
The timestamp in milliseconds.
If set, only messages before this date will be returned.
type: integer
format: int64

definitions:
ResourceID:
properties:
Expand Down
14 changes: 14 additions & 0 deletions test/ejabberd_tests/tests/rest_client_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_cases() ->
room_is_created,
user_is_invited_to_a_room,
user_is_removed_from_a_room,
rooms_can_be_listed,
owner_can_leave_a_room_and_auto_select_owner,
user_can_leave_a_room,
invitation_to_room_is_forbidden_for_non_memeber,
Expand Down Expand Up @@ -123,6 +124,14 @@ room_is_created(Config) ->
assert_room_info(Alice, RoomInfo)
end).

rooms_can_be_listed(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
[] = get_my_rooms({alice, Alice}),
RoomID = given_new_room_with_users({alice, Alice}, [{bob, Bob}]),
[RoomID] = get_my_rooms({alice, Alice}),
[RoomID] = get_my_rooms({bob, Bob})
end).

user_is_invited_to_a_room(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
RoomID = given_new_room_with_users({alice, Alice}, [{bob, Bob}]),
Expand Down Expand Up @@ -330,6 +339,11 @@ create_room({_AliceJID, _} = Creds, RoomID, Subject) ->
{{<<"200">>, <<"OK">>}, {Result}} = rest_helper:post(<<"/rooms">>, Room, Creds),
proplists:get_value(<<"id">>, Result).

get_my_rooms(User) ->
Creds = credentials(User),
{{<<"200">>, <<"OK">>}, Rooms} = rest_helper:gett(<<"/rooms">>, Creds),
Rooms.

assert_messages([], []) ->
ok;
assert_messages([SentMsg | SentRest], [RecvMsg | RecvRest]) ->
Expand Down

0 comments on commit 16a0f44

Please sign in to comment.