1 |
|
-module(mongoose_graphql_roster_admin_mutation). |
2 |
|
-behaviour(mongoose_graphql). |
3 |
|
|
4 |
|
-export([execute/4]). |
5 |
|
|
6 |
|
-ignore_xref([execute/4]). |
7 |
|
|
8 |
|
-include("../mongoose_graphql_types.hrl"). |
9 |
|
|
10 |
|
-import(mongoose_graphql_helper, [make_error/2, format_result/2, null_to_default/2]). |
11 |
|
|
12 |
|
execute(_Ctx, _Obj, <<"addContact">>, Args) -> |
13 |
25 |
add_contact(Args); |
14 |
|
execute(_Ctx, _Obj, <<"addContacts">>, Args) -> |
15 |
5 |
add_contacts(Args); |
16 |
|
execute(_Ctx, _Obj, <<"subscription">>, Args) -> |
17 |
12 |
subscription(Args); |
18 |
|
execute(_Ctx, _Obj, <<"setMutualSubscription">>, Args) -> |
19 |
10 |
set_mutual_subscription(Args); |
20 |
|
execute(_Ctx, _Obj, <<"deleteContact">>, Args) -> |
21 |
8 |
delete_contact(Args); |
22 |
|
execute(_Ctx, _Obj, <<"deleteContacts">>, Args) -> |
23 |
2 |
delete_contacts(Args); |
24 |
|
execute(_Ctx, _Obj, <<"subscribeToAll">>, Args) -> |
25 |
9 |
subscribe_to_all(Args); |
26 |
|
execute(_Ctx, _Obj, <<"subscribeAllToAll">>, Args) -> |
27 |
10 |
subscribe_all_to_all(Args). |
28 |
|
|
29 |
|
-spec add_contact(mongoose_graphql:args()) -> mongoose_graphql_roster:binary_result(). |
30 |
|
add_contact(#{<<"user">> := UserJID, <<"contact">> := ContactJID, |
31 |
|
<<"name">> := Name, <<"groups">> := Groups}) -> |
32 |
25 |
mongoose_graphql_roster:add_contact(UserJID, ContactJID, Name, Groups). |
33 |
|
|
34 |
|
-spec add_contacts(mongoose_graphql:args()) -> mongoose_graphql_roster:list_binary_result(). |
35 |
|
add_contacts(#{<<"user">> := UserJID, <<"contacts">> := Contacts}) -> |
36 |
5 |
{ok, [mongoose_graphql_roster:add_contact(UserJID, ContactJID, Name, Groups) |
37 |
5 |
|| #{<<"jid">> := ContactJID, <<"name">> := Name, <<"groups">> := Groups} <- Contacts]}. |
38 |
|
|
39 |
|
-spec subscription(mongoose_graphql:args()) -> mongoose_graphql_roster:binary_result(). |
40 |
|
subscription(#{<<"user">> := UserJID, <<"contact">> := ContactJID, <<"action">> := Action}) -> |
41 |
12 |
Type = mongoose_graphql_roster:translate_sub_action(Action), |
42 |
12 |
Res = mod_roster_api:subscription(UserJID, ContactJID, Type), |
43 |
12 |
format_result(Res, #{user => jid:to_binary(UserJID), |
44 |
|
contact => jid:to_binary(ContactJID)}). |
45 |
|
|
46 |
|
-spec set_mutual_subscription(mongoose_graphql:args()) -> mongoose_graphql_roster:binary_result(). |
47 |
|
set_mutual_subscription(#{<<"userA">> := UserAJID, <<"userB">> := UserBJID, |
48 |
|
<<"action">> := Action}) -> |
49 |
10 |
Res = mod_roster_api:set_mutual_subscription(UserAJID, UserBJID, Action), |
50 |
10 |
format_result(Res, #{userA => jid:to_binary(UserAJID), |
51 |
|
userB => jid:to_binary(UserBJID)}). |
52 |
|
|
53 |
|
-spec delete_contact(mongoose_graphql:args()) -> mongoose_graphql_roster:binary_result(). |
54 |
|
delete_contact(#{<<"user">> := UserJID, <<"contact">> := ContactJID}) -> |
55 |
8 |
mongoose_graphql_roster:delete_contact(UserJID, ContactJID). |
56 |
|
|
57 |
|
-spec delete_contacts(mongoose_graphql:args()) -> mongoose_graphql_roster:list_binary_result(). |
58 |
|
delete_contacts(#{<<"user">> := UserJID, <<"contacts">> := ContactJIDs}) -> |
59 |
2 |
{ok, [mongoose_graphql_roster:delete_contact(UserJID, ContactJID) |
60 |
2 |
|| ContactJID <- ContactJIDs]}. |
61 |
|
|
62 |
|
-spec subscribe_to_all(mongoose_graphql:args()) -> mongoose_graphql_roster:list_binary_result(). |
63 |
|
subscribe_to_all(#{<<"user">> := User, <<"contacts">> := Contacts}) -> |
64 |
9 |
{ok, do_subscribe_to_all(User, Contacts)}. |
65 |
|
|
66 |
|
-spec subscribe_all_to_all(mongoose_graphql:args()) -> mongoose_graphql_roster:list_binary_result(). |
67 |
|
subscribe_all_to_all(#{<<"contacts">> := Contacts}) -> |
68 |
10 |
{ok, do_subscribe_all_to_all(Contacts)}. |
69 |
|
|
70 |
|
-spec do_subscribe_to_all(mongoose_graphql_roster:contact_input(), |
71 |
|
[mongoose_graphql_roster:contact_input()]) -> |
72 |
|
[mongoose_graphql_roster:binary_result()]. |
73 |
|
do_subscribe_to_all(User, Contacts) -> |
74 |
25 |
UserTuple = contact_input_map_to_tuple(User), |
75 |
25 |
lists:map(fun(Contact) -> |
76 |
42 |
ContactTuple = contact_input_map_to_tuple(Contact), |
77 |
42 |
Res = mod_roster_api:subscribe_both(UserTuple, ContactTuple), |
78 |
42 |
format_result(Res, #{user => jid:to_binary(element(1, UserTuple)), |
79 |
|
contact => jid:to_binary(element(1, ContactTuple))}) |
80 |
|
end, Contacts). |
81 |
|
|
82 |
|
-spec do_subscribe_all_to_all([mongoose_graphql_roster:contact_input()]) -> |
83 |
|
[mongoose_graphql_roster:binary_result()]. |
84 |
|
do_subscribe_all_to_all([]) -> |
85 |
2 |
[]; |
86 |
|
do_subscribe_all_to_all([_]) -> |
87 |
8 |
[]; |
88 |
|
do_subscribe_all_to_all([User | Contacts]) -> |
89 |
16 |
do_subscribe_to_all(User, Contacts) ++ do_subscribe_all_to_all(Contacts). |
90 |
|
|
91 |
|
contact_input_map_to_tuple(#{<<"jid">> := JID, <<"name">> := Name, <<"groups">> := Groups}) -> |
92 |
67 |
Groups1 = null_to_default(Groups, []), |
93 |
67 |
{JID, Name, Groups1}. |