./ct_report/coverage/mongoose_client_api_rooms_users.COVER.html

1 -module(mongoose_client_api_rooms_users).
2
3 -behaviour(mongoose_client_api).
4 -export([routes/0]).
5
6 -behaviour(cowboy_rest).
7 -export([trails/0,
8 init/2,
9 is_authorized/2,
10 content_types_accepted/2,
11 allowed_methods/2,
12 from_json/2,
13 delete_resource/2]).
14
15 -ignore_xref([from_json/2, trails/0]).
16
17 -import(mongoose_client_api, [parse_body/1, try_handle_request/3, throw_error/2]).
18 -import(mongoose_client_api_rooms, [get_room_jid/3, get_user_aff/2]).
19
20 -type req() :: cowboy_req:req().
21 -type state() :: map().
22
23 -spec routes() -> mongoose_http_handler:routes().
24 routes() ->
25 4 [{"/rooms/:id/users/[:user]", ?MODULE, #{}}].
26
27 trails() ->
28 4 mongoose_client_api_rooms_users_doc:trails().
29
30 -spec init(req(), state()) -> {cowboy_rest, req(), state()}.
31 init(Req, Opts) ->
32
:-(
mongoose_client_api:init(Req, Opts).
33
34 -spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}.
35 is_authorized(Req, State) ->
36
:-(
mongoose_client_api:is_authorized(Req, State).
37
38 content_types_accepted(Req, State) ->
39
:-(
mongoose_client_api_rooms:content_types_accepted(Req, State).
40
41 allowed_methods(Req, State) ->
42
:-(
{[<<"OPTIONS">>, <<"POST">>, <<"DELETE">>], Req, State}.
43
44 %% @doc Called for a method of type "POST"
45 -spec from_json(req(), state()) -> {true | stop, req(), state()}.
46 from_json(Req, State) ->
47
:-(
try_handle_request(Req, State, fun handle_post/2).
48
49 %% @doc Called for a method of type "DELETE"
50 -spec delete_resource(req(), state()) -> {true | stop, req(), state()}.
51 delete_resource(Req, State) ->
52
:-(
try_handle_request(Req, State, fun handle_delete/2).
53
54 %% Internal functions
55
56 handle_post(Req, State = #{jid := UserJid}) ->
57
:-(
Bindings = cowboy_req:bindings(Req),
58
:-(
RoomJid = get_room_jid(Bindings, State, required),
59
:-(
Args = parse_body(Req),
60
:-(
TargetJid = get_user_jid(Args),
61
:-(
change_affiliation(RoomJid, UserJid, TargetJid, add),
62
:-(
{true, Req, State}.
63
64 handle_delete(Req, State = #{jid := UserJid}) ->
65
:-(
Bindings = cowboy_req:bindings(Req),
66
:-(
RoomJid = get_room_jid(Bindings, State, required),
67
:-(
TargetJid = get_user_jid(Bindings),
68
:-(
change_affiliation(RoomJid, UserJid, TargetJid, remove),
69
:-(
{true, Req, State}.
70
71 change_affiliation(RoomJid, UserJid, TargetJid, Op) ->
72
:-(
case mod_muc_light_api:change_affiliation(RoomJid, UserJid, TargetJid, Op) of
73 {ok, _Msg} ->
74
:-(
ok;
75 {room_not_found, Msg} ->
76
:-(
throw_error(not_found, Msg);
77 {not_allowed, Msg} ->
78
:-(
throw_error(denied, Msg);
79 {not_room_member, Msg} ->
80
:-(
throw_error(denied, Msg)
81 end.
82
83 get_user_jid(#{user := JidBin}) ->
84
:-(
case jid:from_binary(JidBin) of
85
:-(
error -> throw_error(bad_request, <<"Invalid user JID: ", JidBin/binary>>);
86
:-(
Jid -> Jid
87 end;
88 get_user_jid(#{}) ->
89
:-(
throw_error(bad_request, <<"Missing JID">>).
Line Hits Source