1 |
|
-module(mongoose_graphql_roster). |
2 |
|
|
3 |
|
-export([make_ok_roster/1, translate_sub_action/1]). |
4 |
|
|
5 |
|
-export([add_contact/4, delete_contact/2]). |
6 |
|
|
7 |
|
-include("mod_roster.hrl"). |
8 |
|
-include("mongoose_graphql_types.hrl"). |
9 |
|
|
10 |
|
-type binary_result() :: {ok, binary()} | {error, resolver_error()}. |
11 |
|
-type list_binary_result() :: {ok, [{ok, binary()} | {error, resolver_error()}]}. |
12 |
|
-type contact_input() :: map(). |
13 |
|
-type contact() :: map(). |
14 |
|
-type contact_list() :: [{ok, contact()}]. |
15 |
|
|
16 |
|
-export_type([binary_result/0, list_binary_result/0, contact_input/0, |
17 |
|
contact/0, contact_list/0]). |
18 |
|
|
19 |
|
-spec add_contact(jid:jid(), jid:jid(), binary(), [binary()]) -> binary_result(). |
20 |
|
add_contact(UserJID, ContactJID, Name, Groups) -> |
21 |
44 |
DefaultName = mongoose_graphql_helper:null_to_default(Name, <<>>), |
22 |
44 |
DefaultGroups = mongoose_graphql_helper:null_to_default(Groups, []), |
23 |
44 |
Res = mod_roster_api:add_contact(UserJID, ContactJID, DefaultName, DefaultGroups), |
24 |
44 |
mongoose_graphql_helper:format_result(Res, #{user => jid:to_binary(UserJID), |
25 |
|
contact => jid:to_binary(ContactJID)}). |
26 |
|
|
27 |
|
-spec delete_contact(jid:jid(), jid:jid()) -> binary_result(). |
28 |
|
delete_contact(UserJID, ContactJID) -> |
29 |
16 |
Res = mod_roster_api:delete_contact(UserJID, ContactJID), |
30 |
16 |
mongoose_graphql_helper:format_result(Res, #{user => jid:to_binary(UserJID), |
31 |
|
contact => jid:to_binary(ContactJID)}). |
32 |
|
|
33 |
|
-spec make_ok_roster(mod_roster:roster()) -> {ok, map()}. |
34 |
|
make_ok_roster(#roster{subscription = Sub, ask = Ask, jid = JID, name = Name, groups = Groups}) -> |
35 |
8 |
{ok, #{<<"jid">> => JID, <<"subscription">> => Sub, <<"ask">> => Ask, |
36 |
8 |
<<"name">> => Name, <<"groups">> => [{ok, Group} || Group <- Groups]}}. |
37 |
|
|
38 |
8 |
translate_sub_action(invite) -> subscribe; |
39 |
3 |
translate_sub_action(accept) -> subscribed; |
40 |
3 |
translate_sub_action(cancel) -> unsubscribe; |
41 |
3 |
translate_sub_action(decline) -> unsubscribed. |