./ct_report/coverage/mongoose_client_api_rooms_config.COVER.html

1 -module(mongoose_client_api_rooms_config).
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
14 -ignore_xref([from_json/2, trails/0]).
15
16 -import(mongoose_client_api, [parse_body/1, try_handle_request/3, throw_error/2]).
17 -import(mongoose_client_api_rooms, [get_room_jid/3, get_room_name/1, get_room_subject/1]).
18
19 -type req() :: cowboy_req:req().
20 -type state() :: map().
21
22 -spec routes() -> mongoose_http_handler:routes().
23 routes() ->
24 107 [{"/rooms/[:id]/config", ?MODULE, #{}}].
25
26 trails() ->
27 107 mongoose_client_api_rooms_config_doc:trails().
28
29 -spec init(req(), state()) -> {cowboy_rest, req(), state()}.
30 init(Req, Opts) ->
31 6 mongoose_client_api:init(Req, Opts).
32
33 -spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}.
34 is_authorized(Req, State) ->
35 6 mongoose_client_api:is_authorized(Req, State).
36
37 content_types_accepted(Req, State) ->
38 6 mongoose_client_api_rooms:content_types_accepted(Req, State).
39
40 allowed_methods(Req, State) ->
41 6 {[<<"OPTIONS">>, <<"PUT">>], Req, State}.
42
43 %% @doc Called for a method of type "PUT"
44 -spec from_json(req(), state()) -> {true | stop, req(), state()}.
45 from_json(Req, State) ->
46 6 try_handle_request(Req, State, fun handle_put/2).
47
48 %% Internal functions
49
50 handle_put(Req, State = #{jid := UserJid}) ->
51 6 Bindings = cowboy_req:bindings(Req),
52 6 RoomJid = get_room_jid(Bindings, State, required),
53 6 Args = parse_body(Req),
54 6 Name = get_room_name(Args),
55 6 Subject = get_room_subject(Args),
56 6 Config = #{<<"roomname">> => Name, <<"subject">> => Subject},
57 6 case mod_muc_light_api:change_room_config(RoomJid, UserJid, Config) of
58 {ok, _} ->
59 2 {true, Req, State};
60 {not_room_member, Msg} ->
61 1 throw_error(denied, Msg);
62 {not_allowed, Msg} ->
63 1 throw_error(denied, Msg);
64 {room_not_found, Msg} ->
65 1 throw_error(not_found, Msg);
66 {validation_error, Msg} ->
67 1 throw_error(bad_request, Msg)
68 end.
Line Hits Source