Skip to content

Commit

Permalink
Implement roster resolvers for user
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Mar 14, 2022
1 parent 3719ade commit 6f32745
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/graphql/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ user_mapping_rules() ->
'AccountUserMutation' => mongoose_graphql_account_user_mutation,
'MUCLightUserMutation' => mongoose_graphql_muc_light_user_mutation,
'MUCLightUserQuery' => mongoose_graphql_muc_light_user_query,
'RosterUserQuery' => mongoose_graphql_roster_user_query,
'RosterUserMutation' => mongoose_graphql_roster_user_mutation,
'SessionUserQuery' => mongoose_graphql_session_user_query,
'StanzaUserMutation' => mongoose_graphql_stanza_user_mutation,
'StanzaUserQuery' => mongoose_graphql_stanza_user_query,
Expand Down
50 changes: 50 additions & 0 deletions src/graphql/user/mongoose_graphql_roster_user_mutation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-module(mongoose_graphql_roster_user_mutation).
-behaviour(mongoose_graphql).

-export([execute/4]).

-ignore_xref([execute/4]).

-import(mongoose_graphql_helper, [make_error/2, format_result/2, null_to_default/2]).

execute(Ctx, _Obj, <<"addContact">>, Args) ->
add_contact(Ctx, Args);
execute(Ctx, _Obj, <<"addContacts">>, Args) ->
add_contacts(Ctx, Args);
execute(Ctx, _Obj, <<"subscription">>, Args) ->
subscription(Ctx, Args);
execute(Ctx, _Obj, <<"deleteContact">>, Args) ->
delete_contact(Ctx, Args);
execute(Ctx, _Obj, <<"deleteContacts">>, Args) ->
delete_contacts(Ctx, Args).

-spec add_contact(mongoose_graphql:context(), mongoose_graphql:args()) ->
mongoose_graphql_roster:binary_result().
add_contact(#{user := UserJID}, #{<<"contact">> := ContactJID,
<<"name">> := Name,
<<"groups">> := Groups}) ->
mongoose_graphql_roster:add_contact(UserJID, ContactJID, Name, Groups).

-spec add_contacts(mongoose_graphql:context(), mongoose_graphql:args()) ->
mongoose_graphql_roster:list_binary_result().
add_contacts(#{user := UserJID}, #{<<"contacts">> := Contacts}) ->
{ok, [mongoose_graphql_roster:add_contact(UserJID, ContactJID, Name, Groups)
|| #{<<"jid">> := ContactJID, <<"name">> := Name, <<"groups">> := Groups} <- Contacts]}.

-spec subscription(mongoose_graphql:context(), mongoose_graphql:args()) ->
mongoose_graphql_roster:binary_result().
subscription(#{user := UserJID}, #{<<"contact">> := ContactJID, <<"action">> := Action}) ->
Type = mongoose_graphql_roster:translate_sub_action(Action),
Res = mod_roster_api:subscription(UserJID, ContactJID, Type),
format_result(Res, #{contact => jid:to_binary(ContactJID)}).

-spec delete_contact(mongoose_graphql:context(), mongoose_graphql:args()) ->
mongoose_graphql_roster:binary_result().
delete_contact(#{user := UserJID}, #{<<"contact">> := ContactJID}) ->
mongoose_graphql_roster:delete_contact(UserJID, ContactJID).

-spec delete_contacts(mongoose_graphql:context(), mongoose_graphql:args()) ->
mongoose_graphql_roster:list_binary_result().
delete_contacts(#{user := UserJID}, #{<<"contacts">> := ContactJIDs}) ->
{ok, [mongoose_graphql_roster:delete_contact(UserJID, ContactJID)
|| ContactJID <- ContactJIDs]}.
31 changes: 31 additions & 0 deletions src/graphql/user/mongoose_graphql_roster_user_query.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-module(mongoose_graphql_roster_user_query).
-behaviour(mongoose_graphql).

-export([execute/4]).

-ignore_xref([execute/4]).

-include("../mongoose_graphql_types.hrl").

-import(mongoose_graphql_helper, [make_error/2, format_result/2]).

execute(Ctx, _Obj, <<"listContacts">>, Args) ->
list_contacts(Ctx, Args);
execute(Ctx, _Obj, <<"getContact">>, Args) ->
get_contact(Ctx, Args).

-spec list_contacts(mongoose_graphql:context(), mongoose_graphql:args()) ->
{ok, mongoose_graphql_roster:contact_list()} | {error, resolver_error()}.
list_contacts(#{user := UserJID}, #{}) ->
{ok, Contacts} = mod_roster_api:list_contacts(UserJID),
{ok, [mongoose_graphql_roster:make_ok_roster(C) || C <- Contacts]}.

-spec get_contact(mongoose_graphql:context(), mongoose_graphql:args()) ->
{ok, mongoose_graphql_roster:contact()} | {error, resolver_error()}.
get_contact(#{user := UserJID}, #{<<"contact">> := ContactJID}) ->
case mod_roster_api:get_contact(UserJID, ContactJID) of
{ok, Contact} ->
mongoose_graphql_roster:make_ok_roster(Contact);
Error ->
make_error(Error, #{contact => jid:to_binary(ContactJID)})
end.
4 changes: 3 additions & 1 deletion src/graphql/user/mongoose_graphql_user_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ execute(_Ctx, _Obj, <<"account">>, _Args) ->
execute(_Ctx, _Obj, <<"muc_light">>, _Args) ->
{ok, muc_light};
execute(_Ctx, _Obj, <<"stanza">>, _Args) ->
{ok, stanza}.
{ok, stanza};
execute(_Ctx, _Obj, <<"roster">>, _Args) ->
{ok, roster}.
4 changes: 3 additions & 1 deletion src/graphql/user/mongoose_graphql_user_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ execute(_Ctx, _Obj, <<"session">>, _Args) ->
execute(_Ctx, _Obj, <<"checkAuth">>, _Args) ->
{ok, user};
execute(_Ctx, _Obj, <<"stanza">>, _Args) ->
{ok, stanza}.
{ok, stanza};
execute(_Ctx, _Obj, <<"roster">>, _Args) ->
{ok, roster}.

0 comments on commit 6f32745

Please sign in to comment.