1 |
|
-module(mongoose_admin_api_muc). |
2 |
|
|
3 |
|
-behaviour(mongoose_admin_api). |
4 |
|
-export([routes/1]). |
5 |
|
|
6 |
|
-behaviour(cowboy_rest). |
7 |
|
-export([init/2, |
8 |
|
is_authorized/2, |
9 |
|
content_types_accepted/2, |
10 |
|
allowed_methods/2, |
11 |
|
from_json/2, |
12 |
|
delete_resource/2]). |
13 |
|
|
14 |
|
-ignore_xref([to_json/2, from_json/2]). |
15 |
|
|
16 |
|
-import(mongoose_admin_api, [try_handle_request/3, throw_error/2, parse_body/1, resource_created/4]). |
17 |
|
|
18 |
|
-include("jlib.hrl"). |
19 |
|
|
20 |
|
-type req() :: cowboy_req:req(). |
21 |
|
-type state() :: mongoose_admin_api:state(). |
22 |
|
|
23 |
|
-spec routes(state()) -> mongoose_http_handler:routes(). |
24 |
|
routes(State) -> |
25 |
100 |
[{"/mucs/:domain", ?MODULE, State}, |
26 |
|
{"/mucs/:domain/:name/:arg", ?MODULE, State}]. |
27 |
|
|
28 |
|
-spec init(req(), state()) -> {cowboy_rest, req(), state()}. |
29 |
|
init(Req, State) -> |
30 |
33 |
mongoose_admin_api:init(Req, State). |
31 |
|
|
32 |
|
-spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}. |
33 |
|
is_authorized(Req, State) -> |
34 |
33 |
mongoose_admin_api:is_authorized(Req, State). |
35 |
|
|
36 |
|
-spec content_types_accepted(req(), state()) -> |
37 |
|
{[{{binary(), binary(), '*'}, atom()}], req(), state()}. |
38 |
|
content_types_accepted(Req, State) -> |
39 |
29 |
{[ |
40 |
|
{{<<"application">>, <<"json">>, '*'}, from_json} |
41 |
|
], Req, State}. |
42 |
|
|
43 |
|
-spec allowed_methods(req(), state()) -> {[binary()], req(), state()}. |
44 |
|
allowed_methods(Req, State) -> |
45 |
33 |
{[<<"OPTIONS">>, <<"POST">>, <<"DELETE">>], Req, State}. |
46 |
|
|
47 |
|
%% @doc Called for a method of type "POST" |
48 |
|
-spec from_json(req(), state()) -> {true | stop, req(), state()}. |
49 |
|
from_json(Req, State) -> |
50 |
29 |
try_handle_request(Req, State, fun handle_post/2). |
51 |
|
|
52 |
|
%% @doc Called for a method of type "DELETE" |
53 |
|
-spec delete_resource(req(), state()) -> {true | stop, req(), state()}. |
54 |
|
delete_resource(Req, State) -> |
55 |
4 |
try_handle_request(Req, State, fun handle_delete/2). |
56 |
|
|
57 |
|
%% Internal functions |
58 |
|
|
59 |
|
handle_post(Req, State) -> |
60 |
29 |
Bindings = cowboy_req:bindings(Req), |
61 |
29 |
handle_post(Req, State, Bindings). |
62 |
|
|
63 |
|
handle_post(Req, State, #{arg := <<"participants">>} = Bindings) -> |
64 |
11 |
RoomJid = get_room_jid(Bindings), |
65 |
10 |
Args = parse_body(Req), |
66 |
10 |
SenderJid = get_sender_jid(Args), |
67 |
8 |
RecipientJid = get_recipient_jid(Args), |
68 |
6 |
Reason = get_invite_reason(Args), |
69 |
5 |
case mod_muc_api:invite_to_room(RoomJid, SenderJid, RecipientJid, Reason) of |
70 |
|
{ok, _} -> |
71 |
3 |
{true, Req, State}; |
72 |
|
{room_not_found, Msg} -> |
73 |
2 |
throw_error(not_found, Msg) |
74 |
|
end; |
75 |
|
handle_post(Req, State, #{arg := <<"messages">>} = Bindings) -> |
76 |
6 |
RoomJid = get_room_jid(Bindings), |
77 |
6 |
Args = parse_body(Req), |
78 |
6 |
SenderJid = get_from_jid(Args), |
79 |
4 |
Body = get_message_body(Args), |
80 |
3 |
{ok, _} = mod_muc_api:send_message_to_room(RoomJid, SenderJid, Body), |
81 |
3 |
{true, Req, State}; |
82 |
|
handle_post(Req, State, #{domain := Domain}) -> |
83 |
12 |
Args = parse_body(Req), |
84 |
12 |
RoomName = get_room_name(Args), |
85 |
11 |
OwnerJid = get_owner_jid(Args), |
86 |
9 |
Nick = get_nick(Args), |
87 |
8 |
RoomJid = make_room_jid(RoomName, get_muc_domain(Domain)), |
88 |
7 |
case mod_muc_api:create_instant_room(RoomJid, OwnerJid, Nick) of |
89 |
|
{ok, #{title := R}} -> |
90 |
6 |
Path = [cowboy_req:uri(Req), "/", R], |
91 |
6 |
resource_created(Req, State, Path, R); |
92 |
|
{user_not_found, Msg} -> |
93 |
1 |
throw_error(not_found, Msg) |
94 |
|
end. |
95 |
|
|
96 |
|
handle_delete(Req, State) -> |
97 |
4 |
Bindings = cowboy_req:bindings(Req), |
98 |
4 |
RoomJid = get_room_jid(Bindings), |
99 |
4 |
#{arg := Nick} = Bindings, % 'name' was present, so 'arg' is present as well (see Cowboy paths) |
100 |
4 |
Reason = <<"User kicked from the admin REST API">>, |
101 |
4 |
case mod_muc_api:kick_user_from_room(RoomJid, Nick, Reason) of |
102 |
|
{ok, _} -> |
103 |
2 |
{true, Req, State}; |
104 |
|
{moderator_not_found, Msg} -> |
105 |
1 |
throw_error(not_found, Msg); |
106 |
|
{room_not_found, Msg} -> |
107 |
1 |
throw_error(not_found, Msg) |
108 |
|
end. |
109 |
|
|
110 |
|
get_owner_jid(#{owner := Owner}) -> |
111 |
10 |
case jid:binary_to_bare(Owner) of |
112 |
1 |
error -> throw_error(bad_request, <<"Invalid owner JID">>); |
113 |
9 |
OwnerJid -> OwnerJid |
114 |
|
end; |
115 |
1 |
get_owner_jid(#{}) -> throw_error(bad_request, <<"Missing owner JID">>). |
116 |
|
|
117 |
|
get_room_jid(#{domain := Domain} = Bindings) -> |
118 |
21 |
MUCDomain = get_muc_domain(Domain), |
119 |
20 |
RoomName = get_room_name(Bindings), |
120 |
20 |
make_room_jid(RoomName, MUCDomain). |
121 |
|
|
122 |
|
make_room_jid(RoomName, MUCDomain) -> |
123 |
28 |
try #jid{} = jid:make_bare(RoomName, MUCDomain) |
124 |
1 |
catch _:_ -> throw_error(bad_request, <<"Invalid room name">>) |
125 |
|
end. |
126 |
|
|
127 |
8 |
get_nick(#{nick := Nick}) -> Nick; |
128 |
1 |
get_nick(#{}) -> throw_error(bad_request, <<"Missing nickname">>). |
129 |
|
|
130 |
31 |
get_room_name(#{name := Name}) -> Name; |
131 |
1 |
get_room_name(#{}) -> throw_error(bad_request, <<"Missing room name">>). |
132 |
|
|
133 |
3 |
get_message_body(#{body := Body}) -> Body; |
134 |
1 |
get_message_body(#{}) -> throw_error(bad_request, <<"Missing message body">>). |
135 |
|
|
136 |
5 |
get_invite_reason(#{reason := Reason}) -> Reason; |
137 |
1 |
get_invite_reason(#{}) -> throw_error(bad_request, <<"Missing invite reason">>). |
138 |
|
|
139 |
|
get_from_jid(#{from := Sender}) -> |
140 |
5 |
case jid:from_binary(Sender) of |
141 |
1 |
error -> throw_error(bad_request, <<"Invalid sender JID">>); |
142 |
4 |
SenderJid -> SenderJid |
143 |
|
end; |
144 |
1 |
get_from_jid(#{}) -> throw_error(bad_request, <<"Missing sender JID">>). |
145 |
|
|
146 |
|
get_sender_jid(#{sender := Sender}) -> |
147 |
9 |
case jid:from_binary(Sender) of |
148 |
1 |
error -> throw_error(bad_request, <<"Invalid sender JID">>); |
149 |
8 |
SenderJid -> SenderJid |
150 |
|
end; |
151 |
1 |
get_sender_jid(#{}) -> throw_error(bad_request, <<"Missing sender JID">>). |
152 |
|
|
153 |
|
get_recipient_jid(#{recipient := Recipient}) -> |
154 |
7 |
case jid:from_binary(Recipient) of |
155 |
1 |
error -> throw_error(bad_request, <<"Invalid recipient JID">>); |
156 |
6 |
RecipientJid -> RecipientJid |
157 |
|
end; |
158 |
1 |
get_recipient_jid(#{}) -> throw_error(bad_request, <<"Missing recipient JID">>). |
159 |
|
|
160 |
|
get_muc_domain(Domain) -> |
161 |
29 |
try |
162 |
29 |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(Domain), |
163 |
28 |
mod_muc:server_host_to_muc_host(HostType, Domain) |
164 |
|
catch _:_ -> |
165 |
1 |
throw_error(not_found, <<"MUC domain not found">>) |
166 |
|
end. |