./ct_report/coverage/mongoose_admin_api_contacts.COVER.html

1 -module(mongoose_admin_api_contacts).
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_provided/2,
10 content_types_accepted/2,
11 allowed_methods/2,
12 to_json/2,
13 from_json/2,
14 delete_resource/2]).
15
16 -ignore_xref([to_json/2, from_json/2]).
17
18 -import(mongoose_admin_api, [parse_body/1, try_handle_request/3, throw_error/2]).
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 47 [{"/contacts/:user/[:contact]", ?MODULE, State},
26 {"/contacts/:user/:contact/manage", ?MODULE, State#{suffix => manage}}].
27
28 -spec init(req(), state()) -> {cowboy_rest, req(), state()}.
29 init(Req, State) ->
30 50 mongoose_admin_api:init(Req, State).
31
32 -spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}.
33 is_authorized(Req, State) ->
34 50 mongoose_admin_api:is_authorized(Req, State).
35
36 -spec content_types_provided(req(), state()) ->
37 {[{{binary(), binary(), '*'}, atom()}], req(), state()}.
38 content_types_provided(Req, State) ->
39 50 {[
40 {{<<"application">>, <<"json">>, '*'}, to_json}
41 ], Req, State}.
42
43 -spec content_types_accepted(req(), state()) ->
44 {[{{binary(), binary(), '*'}, atom()}], req(), state()}.
45 content_types_accepted(Req, State) ->
46 25 {[
47 {{<<"application">>, <<"json">>, '*'}, from_json}
48 ], Req, State}.
49
50 -spec allowed_methods(req(), state()) -> {[binary()], req(), state()}.
51 allowed_methods(Req, State) ->
52 50 {[<<"OPTIONS">>, <<"GET">>, <<"POST">>, <<"PUT">>, <<"DELETE">>], Req, State}.
53
54 %% @doc Called for a method of type "GET"
55 -spec to_json(req(), state()) -> {iodata() | stop, req(), state()}.
56 to_json(Req, State) ->
57 21 try_handle_request(Req, State, fun handle_get/2).
58
59 %% @doc Called for a method of type "POST" or "PUT"
60 -spec from_json(req(), state()) -> {true | stop, req(), state()}.
61 from_json(Req, State) ->
62 25 F = case cowboy_req:method(Req) of
63 8 <<"POST">> -> fun handle_post/2;
64 17 <<"PUT">> -> fun handle_put/2
65 end,
66 25 try_handle_request(Req, State, F).
67
68 %% @doc Called for a method of type "DELETE"
69 -spec delete_resource(req(), state()) -> {true | stop, req(), state()}.
70 delete_resource(Req, State) ->
71 4 try_handle_request(Req, State, fun handle_delete/2).
72
73 %% Internal functions
74
75 handle_get(Req, State) ->
76 21 Bindings = cowboy_req:bindings(Req),
77 21 UserJid = get_user_jid(Bindings),
78 21 case mod_roster_api:list_contacts(UserJid) of
79 {ok, Rosters} ->
80 20 {jiffy:encode(lists:map(fun roster_info/1, Rosters)), Req, State};
81 {unknown_domain, Reason} ->
82 1 throw_error(not_found, Reason)
83 end.
84
85 handle_post(Req, State) ->
86 8 Bindings = cowboy_req:bindings(Req),
87 8 UserJid = get_user_jid(Bindings),
88 7 Args = parse_body(Req),
89 7 ContactJid = get_jid(Args),
90 5 case mod_roster_api:add_contact(UserJid, ContactJid, <<>>, []) of
91 {unknown_domain, Reason} ->
92 1 throw_error(not_found, Reason);
93 {user_not_exist, Reason} ->
94 1 throw_error(not_found, Reason);
95 {ok, _} ->
96 3 {true, Req, State}
97 end.
98
99 handle_put(Req, State) ->
100 17 Bindings = cowboy_req:bindings(Req),
101 17 UserJid = get_user_jid(Bindings),
102 15 ContactJid = get_contact_jid(Bindings),
103 13 Args = parse_body(Req),
104 13 Action = get_action(Args, State),
105 9 case perform_action(UserJid, ContactJid, Action, State) of
106 {unknown_domain, Reason} ->
107 1 throw_error(not_found, Reason);
108 {user_not_exist, Reason} ->
109 1 throw_error(not_found, Reason);
110 {contact_not_found, Reason} ->
111 1 throw_error(not_found, Reason);
112 {ok, _} ->
113 6 {true, Req, State}
114 end.
115
116 handle_delete(Req, State) ->
117 4 Bindings = cowboy_req:bindings(Req),
118 4 UserJid = get_user_jid(Bindings),
119 4 ContactJid = get_contact_jid(Bindings),
120 3 case mod_roster_api:delete_contact(UserJid, ContactJid) of
121 {contact_not_found, Reason} ->
122 1 throw_error(not_found, Reason);
123 {ok, _} ->
124 2 {true, Req, State}
125 end.
126
127 perform_action(UserJid, ContactJid, Action, #{suffix := manage}) ->
128 5 mod_roster_api:set_mutual_subscription(UserJid, ContactJid, Action);
129 perform_action(UserJid, ContactJid, Action, #{}) ->
130 4 mod_roster_api:subscription(UserJid, ContactJid, Action).
131
132 -spec roster_info(mod_roster:roster()) -> jiffy:json_value(). %% returns jiffy:json_object()
133 roster_info(Roster) ->
134 12 #{jid := Jid, subscription := Sub, ask := Ask} = mod_roster:item_to_map(Roster),
135 12 #{jid => jid:to_binary(Jid), subscription => Sub, ask => Ask}.
136
137 get_jid(#{jid := JidBin}) ->
138 6 case jid:from_binary(JidBin) of
139 1 error -> throw_error(bad_request, <<"Invalid JID">>);
140 5 Jid -> Jid
141 end;
142 get_jid(#{}) ->
143 1 throw_error(bad_request, <<"Missing JID">>).
144
145 get_user_jid(#{user := User}) ->
146 50 case jid:from_binary(User) of
147 3 error -> throw_error(bad_request, <<"Invalid user JID">>);
148 47 Jid -> Jid
149 end.
150
151 get_contact_jid(#{contact := Contact}) ->
152 18 case jid:from_binary(Contact) of
153 2 error -> throw_error(bad_request, <<"Invalid contact JID">>);
154 16 Jid -> Jid
155 end;
156 get_contact_jid(#{}) ->
157 1 throw_error(bad_request, <<"Missing contact JID">>).
158
159 get_action(#{action := ActionBin}, State) ->
160 11 decode_action(ActionBin, maps:get(suffix, State, no_suffix));
161 get_action(#{}, _State) ->
162 2 throw_error(bad_request, <<"Missing action">>).
163
164 2 decode_action(<<"subscribe">>, no_suffix) -> subscribe;
165 2 decode_action(<<"subscribed">>, no_suffix) -> subscribed;
166 3 decode_action(<<"connect">>, manage) -> connect;
167 2 decode_action(<<"disconnect">>, manage) -> disconnect;
168 2 decode_action(_, _) -> throw_error(bad_request, <<"Invalid action">>).
Line Hits Source