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 |
:-( |
add_contact(Args); |
14 |
|
execute(_Ctx, _Obj, <<"addContacts">>, Args) -> |
15 |
:-( |
add_contacts(Args); |
16 |
|
execute(_Ctx, _Obj, <<"subscription">>, Args) -> |
17 |
:-( |
subscription(Args); |
18 |
|
execute(_Ctx, _Obj, <<"setMutualSubscription">>, Args) -> |
19 |
:-( |
set_mutual_subscription(Args); |
20 |
|
execute(_Ctx, _Obj, <<"deleteContact">>, Args) -> |
21 |
:-( |
delete_contact(Args); |
22 |
|
execute(_Ctx, _Obj, <<"deleteContacts">>, Args) -> |
23 |
:-( |
delete_contacts(Args); |
24 |
|
execute(_Ctx, _Obj, <<"subscribeToAll">>, Args) -> |
25 |
:-( |
subscribe_to_all(Args); |
26 |
|
execute(_Ctx, _Obj, <<"subscribeAllToAll">>, Args) -> |
27 |
:-( |
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 |
:-( |
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 |
:-( |
{ok, [mongoose_graphql_roster:add_contact(UserJID, ContactJID, Name, Groups) |
37 |
:-( |
|| #{<<"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 |
:-( |
Type = mongoose_graphql_roster:translate_sub_action(Action), |
42 |
:-( |
Res = mod_roster_api:subscription(UserJID, ContactJID, Type), |
43 |
:-( |
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 |
:-( |
Res = mod_roster_api:set_mutual_subscription(UserAJID, UserBJID, Action), |
50 |
:-( |
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 |
:-( |
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 |
:-( |
{ok, [mongoose_graphql_roster:delete_contact(UserJID, ContactJID) |
60 |
:-( |
|| 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 |
:-( |
{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 |
:-( |
{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 |
:-( |
UserTuple = contact_input_map_to_tuple(User), |
75 |
:-( |
lists:map(fun(Contact) -> |
76 |
:-( |
ContactTuple = contact_input_map_to_tuple(Contact), |
77 |
:-( |
Res = mod_roster_api:subscribe_both(UserTuple, ContactTuple), |
78 |
:-( |
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 |
:-( |
[]; |
86 |
|
do_subscribe_all_to_all([_]) -> |
87 |
:-( |
[]; |
88 |
|
do_subscribe_all_to_all([User | Contacts]) -> |
89 |
:-( |
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 |
:-( |
Groups1 = null_to_default(Groups, []), |
93 |
:-( |
{JID, Name, Groups1}. |