From 6f3274502927c17068908f9a750a6dba64afca0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Sun, 13 Mar 2022 22:54:38 +0100 Subject: [PATCH] Implement roster resolvers for user --- src/graphql/mongoose_graphql.erl | 2 + .../mongoose_graphql_roster_user_mutation.erl | 50 +++++++++++++++++++ .../mongoose_graphql_roster_user_query.erl | 31 ++++++++++++ .../user/mongoose_graphql_user_mutation.erl | 4 +- .../user/mongoose_graphql_user_query.erl | 4 +- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/graphql/user/mongoose_graphql_roster_user_mutation.erl create mode 100644 src/graphql/user/mongoose_graphql_roster_user_query.erl diff --git a/src/graphql/mongoose_graphql.erl b/src/graphql/mongoose_graphql.erl index 828ee34606..9dce7bbc85 100644 --- a/src/graphql/mongoose_graphql.erl +++ b/src/graphql/mongoose_graphql.erl @@ -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, diff --git a/src/graphql/user/mongoose_graphql_roster_user_mutation.erl b/src/graphql/user/mongoose_graphql_roster_user_mutation.erl new file mode 100644 index 0000000000..01387c71dd --- /dev/null +++ b/src/graphql/user/mongoose_graphql_roster_user_mutation.erl @@ -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]}. diff --git a/src/graphql/user/mongoose_graphql_roster_user_query.erl b/src/graphql/user/mongoose_graphql_roster_user_query.erl new file mode 100644 index 0000000000..086dbf8213 --- /dev/null +++ b/src/graphql/user/mongoose_graphql_roster_user_query.erl @@ -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. diff --git a/src/graphql/user/mongoose_graphql_user_mutation.erl b/src/graphql/user/mongoose_graphql_user_mutation.erl index 91ad10c287..4af65ecc34 100644 --- a/src/graphql/user/mongoose_graphql_user_mutation.erl +++ b/src/graphql/user/mongoose_graphql_user_mutation.erl @@ -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}. diff --git a/src/graphql/user/mongoose_graphql_user_query.erl b/src/graphql/user/mongoose_graphql_user_query.erl index 1f57f9d43f..78cba221b0 100644 --- a/src/graphql/user/mongoose_graphql_user_query.erl +++ b/src/graphql/user/mongoose_graphql_user_query.erl @@ -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}.