./ct_report/coverage/mod_roster_api.COVER.html

1 %% @doc Provide an interface for frontends (like graphql or ctl) to manage roster.
2 -module(mod_roster_api).
3
4 -export([list_contacts/1,
5 get_contact/2,
6 add_contact/4,
7 delete_contact/2,
8 subscription/3,
9 subscribe_both/2,
10 set_mutual_subscription/3]).
11
12 -include("mongoose.hrl").
13 -include("jlib.hrl").
14 -include("mod_roster.hrl").
15
16 -type sub_mutual_action() :: connect | disconnect.
17
18 -define(UNKNOWN_DOMAIN_RESULT, {unknown_domain, "Domain not found"}).
19 -define(INTERNAL_ERROR_RESULT(Error, Operation),
20 {internal, io_lib:format("Cannot ~p a contact because: ~p", [Operation, Error])}).
21
22 -spec add_contact(jid:jid(), jid:jid(), binary(), [binary()]) ->
23 {ok | internal | user_not_exist | unknown_domain, iolist()}.
24 add_contact(#jid{lserver = LServer} = CallerJID, ContactJID, Name, Groups) ->
25
:-(
case mongoose_domain_api:get_domain_host_type(LServer) of
26 {ok, HostType} ->
27
:-(
case does_users_exist(CallerJID, ContactJID) of
28 ok ->
29
:-(
case mod_roster:set_roster_entry(HostType, CallerJID, ContactJID,
30 Name, Groups) of
31 ok ->
32
:-(
{ok, "Contact added successfully"};
33 {error, Error} ->
34
:-(
?INTERNAL_ERROR_RESULT(Error, create)
35 end;
36 Error ->
37
:-(
Error
38 end;
39 {error, not_found} ->
40
:-(
?UNKNOWN_DOMAIN_RESULT
41 end.
42
43 -spec list_contacts(jid:jid()) -> {ok, [mod_roster:roster()]} | {unknown_domain, iolist()}.
44 list_contacts(#jid{lserver = LServer} = CallerJID) ->
45
:-(
case mongoose_domain_api:get_domain_host_type(LServer) of
46 {ok, HostType} ->
47
:-(
case ejabberd_auth:does_user_exist(CallerJID) of
48 true ->
49
:-(
Acc0 = mongoose_acc:new(#{ location => ?LOCATION,
50 host_type => HostType,
51 lserver => LServer,
52 element => undefined }),
53
:-(
Roster = mongoose_hooks:roster_get(Acc0, CallerJID, true),
54
:-(
{ok, Roster};
55 false ->
56
:-(
{user_not_exist, io_lib:format("The user ~s does not exist",
57 [jid:to_binary(CallerJID)])}
58 end;
59 {error, not_found} ->
60
:-(
?UNKNOWN_DOMAIN_RESULT
61 end.
62
63 -spec get_contact(jid:jid(), jid:jid()) ->
64 {ok, mod_roster:roster()} |
65 {contact_not_found | internal | unknown_domain | user_not_exist, iolist()}.
66 get_contact(#jid{lserver = LServer} = UserJID, ContactJID) ->
67
:-(
case mongoose_domain_api:get_domain_host_type(LServer) of
68 {ok, HostType} ->
69
:-(
ContactL = jid:to_lower(ContactJID),
70
:-(
case mod_roster:get_roster_entry(HostType, UserJID, ContactL, full) of
71 #roster{} = R ->
72
:-(
{ok, R};
73 does_not_exist ->
74
:-(
{contact_not_found, "Given contact does not exist"};
75 error ->
76
:-(
?INTERNAL_ERROR_RESULT(error, get)
77 end;
78 {error, not_found} ->
79
:-(
?UNKNOWN_DOMAIN_RESULT
80 end.
81
82 -spec delete_contact(jid:jid(), jid:jid()) ->
83 {ok | contact_not_found | internal | unknown_domain, iolist()}.
84 delete_contact(#jid{lserver = LServer} = CallerJID, ContactJID) ->
85
:-(
case mongoose_domain_api:get_domain_host_type(LServer) of
86 {ok, HostType} ->
87
:-(
case mod_roster:remove_from_roster(HostType, CallerJID, ContactJID) of
88 ok ->
89
:-(
{ok, io_lib:format("Contact ~s deleted successfully",
90 [jid:to_binary(ContactJID)])};
91 {error, does_not_exist} ->
92
:-(
ErrMsg = io_lib:format("Cannot remove ~s contact that does not exist",
93 [jid:to_binary(ContactJID)]),
94
:-(
{contact_not_found, ErrMsg};
95 {error, Error} ->
96
:-(
?INTERNAL_ERROR_RESULT(Error, delete)
97 end;
98 {error, not_found} ->
99
:-(
?UNKNOWN_DOMAIN_RESULT
100 end.
101
102 -spec subscription(jid:jid(), jid:jid(), mod_roster:sub_presence()) ->
103 {ok | unknown_domain, iolist()}.
104 subscription(#jid{lserver = LServer} = CallerJID, ContactJID, Type) ->
105
:-(
StanzaType = atom_to_binary(Type, latin1),
106
:-(
El = #xmlel{name = <<"presence">>, attrs = [{<<"type">>, StanzaType}]},
107
:-(
case mongoose_domain_api:get_domain_host_type(LServer) of
108 {ok, HostType} ->
109
:-(
Acc1 = mongoose_acc:new(#{ location => ?LOCATION,
110 from_jid => CallerJID,
111 to_jid => ContactJID,
112 host_type => HostType,
113 lserver => LServer,
114 element => El }),
115
:-(
Acc2 = mongoose_hooks:roster_out_subscription(Acc1, CallerJID, ContactJID, Type),
116
:-(
ejabberd_router:route(CallerJID, jid:to_bare(ContactJID), Acc2),
117
:-(
{ok, io_lib:format("Subscription stanza with type ~s sent successfully", [StanzaType])};
118 {error, not_found} ->
119
:-(
?UNKNOWN_DOMAIN_RESULT
120 end.
121
122 -spec set_mutual_subscription(jid:jid(), jid:jid(), sub_mutual_action()) ->
123 {ok | contact_not_found | internal | unknown_domain | user_not_exist, iolist()}.
124 set_mutual_subscription(UserA, UserB, connect) ->
125
:-(
subscribe_both({UserA, <<>>, []}, {UserB, <<>>, []});
126 set_mutual_subscription(UserA, UserB, disconnect) ->
127
:-(
Seq = [fun() -> delete_contact(UserA, UserB) end,
128
:-(
fun() -> delete_contact(UserB, UserA) end],
129
:-(
case run_seq(Seq, ok) of
130 ok ->
131
:-(
{ok, "Mutual subscription removed successfully"};
132 Error ->
133
:-(
Error
134 end.
135
136 -spec subscribe_both({jid:jid(), binary(), [binary()]}, {jid:jid(), binary(), [binary()]}) ->
137 {ok | internal | unknown_domain | user_not_exist, iolist()}.
138 subscribe_both({UserA, NameA, GroupsA}, {UserB, NameB, GroupsB}) ->
139
:-(
Seq = [fun() -> add_contact(UserA, UserB, NameB, GroupsB) end,
140
:-(
fun() -> add_contact(UserB, UserA, NameA, GroupsA) end,
141
:-(
fun() -> subscription(UserA, UserB, subscribe) end,
142
:-(
fun() -> subscription(UserB, UserA, subscribe) end,
143
:-(
fun() -> subscription(UserA, UserB, subscribed) end,
144
:-(
fun() -> subscription(UserB, UserA, subscribed) end],
145
:-(
case run_seq(Seq, ok) of
146 ok ->
147
:-(
{ok, io_lib:format("Subscription between users ~s and ~s created successfully",
148 [jid:to_binary(UserA), jid:to_binary(UserB)])};
149 Error ->
150
:-(
Error
151 end.
152
153 %% Internal
154
155 -spec does_users_exist(jid:jid(), jid:jid()) -> ok | {user_not_exist, iolist()}.
156 does_users_exist(User, Contact) ->
157
:-(
case lists:filter(fun(U) -> not ejabberd_auth:does_user_exist(U) end, [User, Contact]) of
158 [] ->
159
:-(
ok;
160 [NotExist | _]->
161
:-(
{user_not_exist, io_lib:format("The user ~s does not exist",
162 [jid:to_binary(NotExist)])}
163 end.
164
165 -spec run_seq([fun(() -> any())], term()) -> ok | {atom(), iolist()}.
166 run_seq([Cmd | Seq], ok) ->
167
:-(
run_seq(Seq, Cmd());
168 run_seq([Cmd | Seq], {ok, _}) ->
169
:-(
run_seq(Seq, Cmd());
170
:-(
run_seq([], _) -> ok;
171 run_seq(_, {_, _} = Error) ->
172
:-(
Error.
Line Hits Source