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 |
4 |
[{"/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 |
:-( |
mongoose_admin_api:init(Req, State). |
31 |
|
|
32 |
|
-spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}. |
33 |
|
is_authorized(Req, State) -> |
34 |
:-( |
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 |
:-( |
{[ |
40 |
|
{{<<"application">>, <<"json">>, '*'}, from_json} |
41 |
|
], Req, State}. |
42 |
|
|
43 |
|
-spec allowed_methods(req(), state()) -> {[binary()], req(), state()}. |
44 |
|
allowed_methods(Req, State) -> |
45 |
:-( |
{[<<"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 |
:-( |
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 |
:-( |
try_handle_request(Req, State, fun handle_delete/2). |
56 |
|
|
57 |
|
%% Internal functions |
58 |
|
|
59 |
|
handle_post(Req, State) -> |
60 |
:-( |
Bindings = cowboy_req:bindings(Req), |
61 |
:-( |
handle_post(Req, State, Bindings). |
62 |
|
|
63 |
|
handle_post(Req, State, #{arg := <<"participants">>} = Bindings) -> |
64 |
:-( |
RoomJid = get_room_jid(Bindings), |
65 |
:-( |
Args = parse_body(Req), |
66 |
:-( |
SenderJid = get_sender_jid(Args), |
67 |
:-( |
RecipientJid = get_recipient_jid(Args), |
68 |
:-( |
Reason = get_invite_reason(Args), |
69 |
:-( |
case mod_muc_api:invite_to_room(RoomJid, SenderJid, RecipientJid, Reason) of |
70 |
|
{ok, _} -> |
71 |
:-( |
{true, Req, State}; |
72 |
|
{room_not_found, Msg} -> |
73 |
:-( |
throw_error(not_found, Msg) |
74 |
|
end; |
75 |
|
handle_post(Req, State, #{arg := <<"messages">>} = Bindings) -> |
76 |
:-( |
RoomJid = get_room_jid(Bindings), |
77 |
:-( |
Args = parse_body(Req), |
78 |
:-( |
SenderJid = get_from_jid(Args), |
79 |
:-( |
Body = get_message_body(Args), |
80 |
:-( |
{ok, _} = mod_muc_api:send_message_to_room(RoomJid, SenderJid, Body), |
81 |
:-( |
{true, Req, State}; |
82 |
|
handle_post(Req, State, #{domain := Domain}) -> |
83 |
:-( |
Args = parse_body(Req), |
84 |
:-( |
RoomName = get_room_name(Args), |
85 |
:-( |
OwnerJid = get_owner_jid(Args), |
86 |
:-( |
Nick = get_nick(Args), |
87 |
:-( |
RoomJid = make_room_jid(RoomName, get_muc_domain(Domain)), |
88 |
:-( |
case mod_muc_api:create_instant_room(RoomJid, OwnerJid, Nick) of |
89 |
|
{ok, #{title := R}} -> |
90 |
:-( |
Path = [cowboy_req:uri(Req), "/", R], |
91 |
:-( |
resource_created(Req, State, Path, R); |
92 |
|
{user_not_found, Msg} -> |
93 |
:-( |
throw_error(not_found, Msg) |
94 |
|
end. |
95 |
|
|
96 |
|
handle_delete(Req, State) -> |
97 |
:-( |
Bindings = cowboy_req:bindings(Req), |
98 |
:-( |
RoomJid = get_room_jid(Bindings), |
99 |
:-( |
#{arg := Nick} = Bindings, % 'name' was present, so 'arg' is present as well (see Cowboy paths) |
100 |
:-( |
Reason = <<"User kicked from the admin REST API">>, |
101 |
:-( |
case mod_muc_api:kick_user_from_room(RoomJid, Nick, Reason) of |
102 |
|
{ok, _} -> |
103 |
:-( |
{true, Req, State}; |
104 |
|
{moderator_not_found, Msg} -> |
105 |
:-( |
throw_error(not_found, Msg); |
106 |
|
{room_not_found, Msg} -> |
107 |
:-( |
throw_error(not_found, Msg) |
108 |
|
end. |
109 |
|
|
110 |
|
get_owner_jid(#{owner := Owner}) -> |
111 |
:-( |
case jid:binary_to_bare(Owner) of |
112 |
:-( |
error -> throw_error(bad_request, <<"Invalid owner JID">>); |
113 |
:-( |
OwnerJid -> OwnerJid |
114 |
|
end; |
115 |
:-( |
get_owner_jid(#{}) -> throw_error(bad_request, <<"Missing owner JID">>). |
116 |
|
|
117 |
|
get_room_jid(#{domain := Domain} = Bindings) -> |
118 |
:-( |
MUCDomain = get_muc_domain(Domain), |
119 |
:-( |
RoomName = get_room_name(Bindings), |
120 |
:-( |
make_room_jid(RoomName, MUCDomain). |
121 |
|
|
122 |
|
make_room_jid(RoomName, MUCDomain) -> |
123 |
:-( |
try #jid{} = jid:make_bare(RoomName, MUCDomain) |
124 |
:-( |
catch _:_ -> throw_error(bad_request, <<"Invalid room name">>) |
125 |
|
end. |
126 |
|
|
127 |
:-( |
get_nick(#{nick := Nick}) -> Nick; |
128 |
:-( |
get_nick(#{}) -> throw_error(bad_request, <<"Missing nickname">>). |
129 |
|
|
130 |
:-( |
get_room_name(#{name := Name}) -> Name; |
131 |
:-( |
get_room_name(#{}) -> throw_error(bad_request, <<"Missing room name">>). |
132 |
|
|
133 |
:-( |
get_message_body(#{body := Body}) -> Body; |
134 |
:-( |
get_message_body(#{}) -> throw_error(bad_request, <<"Missing message body">>). |
135 |
|
|
136 |
:-( |
get_invite_reason(#{reason := Reason}) -> Reason; |
137 |
:-( |
get_invite_reason(#{}) -> throw_error(bad_request, <<"Missing invite reason">>). |
138 |
|
|
139 |
|
get_from_jid(#{from := Sender}) -> |
140 |
:-( |
case jid:from_binary(Sender) of |
141 |
:-( |
error -> throw_error(bad_request, <<"Invalid sender JID">>); |
142 |
:-( |
SenderJid -> SenderJid |
143 |
|
end; |
144 |
:-( |
get_from_jid(#{}) -> throw_error(bad_request, <<"Missing sender JID">>). |
145 |
|
|
146 |
|
get_sender_jid(#{sender := Sender}) -> |
147 |
:-( |
case jid:from_binary(Sender) of |
148 |
:-( |
error -> throw_error(bad_request, <<"Invalid sender JID">>); |
149 |
:-( |
SenderJid -> SenderJid |
150 |
|
end; |
151 |
:-( |
get_sender_jid(#{}) -> throw_error(bad_request, <<"Missing sender JID">>). |
152 |
|
|
153 |
|
get_recipient_jid(#{recipient := Recipient}) -> |
154 |
:-( |
case jid:from_binary(Recipient) of |
155 |
:-( |
error -> throw_error(bad_request, <<"Invalid recipient JID">>); |
156 |
:-( |
RecipientJid -> RecipientJid |
157 |
|
end; |
158 |
:-( |
get_recipient_jid(#{}) -> throw_error(bad_request, <<"Missing recipient JID">>). |
159 |
|
|
160 |
|
get_muc_domain(Domain) -> |
161 |
:-( |
try |
162 |
:-( |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(Domain), |
163 |
:-( |
mod_muc:server_host_to_muc_host(HostType, Domain) |
164 |
|
catch _:_ -> |
165 |
:-( |
throw_error(not_found, <<"MUC domain not found">>) |
166 |
|
end. |