Skip to content

Commit

Permalink
Adding user graphql vcard resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Janusz Jakubiec authored and Janusz Jakubiec committed May 9, 2022
1 parent 574f1bb commit c807c9a
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 175 deletions.
394 changes: 228 additions & 166 deletions big_tests/tests/graphql_vcard_SUITE.erl

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions priv/graphql/schemas/user/user_schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type UserQuery{
stanza: StanzaUserQuery
"Roster/Contacts management"
roster: RosterUserQuery
"Vcard management"
vcard: VcardUserQuery
}

"""
Expand All @@ -39,4 +41,6 @@ type UserMutation @protected{
stanza: StanzaUserMutation
"Roster/Contacts management"
roster: RosterUserMutation
"Vcard management"
vcard: VcardUserMutation
}
13 changes: 13 additions & 0 deletions priv/graphql/schemas/user/vcard.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Allow user to set its vcard
"""
type VcardUserMutation @protected{
setVcard(vcard: VcardInput!): Vcard
}

"""
Allow user to get its vcard
"""
type VcardUserQuery @protected{
getVcard: Vcard
}
2 changes: 0 additions & 2 deletions src/graphql/admin/mongoose_graphql_vcard_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
-include("mongoose.hrl").
-include("jlib.hrl").

-define(UNKNOWN_DOMAIN_RESULT, {unknown_domain, "Domain not found"}).

execute(_Ctx, vcard, <<"setVcard">>, #{<<"user">> := CallerJID, <<"vcard">> := VCARD}) ->
case mod_vcard_api:set_vcard(CallerJID, VCARD) of
{ok, _} = Vcard -> Vcard;
Expand Down
2 changes: 0 additions & 2 deletions src/graphql/admin/mongoose_graphql_vcard_admin_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
-include("mongoose.hrl").
-include("jlib.hrl").

-define(UNKNOWN_DOMAIN_RESULT, {unknown_domain, "Domain not found"}).

execute(_Ctx, vcard, <<"getVcard">>, #{<<"user">> := CallerJID}) ->
case mod_vcard_api:get_vcard(CallerJID) of
{ok, _} = Vcard -> Vcard;
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ user_mapping_rules() ->
'MUCLightUserQuery' => mongoose_graphql_muc_light_user_query,
'RosterUserQuery' => mongoose_graphql_roster_user_query,
'RosterUserMutation' => mongoose_graphql_roster_user_mutation,
'VcardUserMutation' => mongoose_graphql_vcard_user_mutation,
'VcardUserQuery' => mongoose_graphql_vcard_user_query,
'SessionUserQuery' => mongoose_graphql_session_user_query,
'StanzaUserMutation' => mongoose_graphql_stanza_user_mutation,
'StanzaUserQuery' => mongoose_graphql_stanza_user_query,
Expand Down
1 change: 1 addition & 0 deletions src/graphql/mongoose_graphql_enum.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ output(<<"MUCRole">>, Role) ->
{ok, list_to_binary(string:to_upper(atom_to_list(Role)))};
output(<<"MUCAffiliation">>, Aff) ->
{ok, list_to_binary(string:to_upper(atom_to_list(Aff)))};
output(<<"AddressTags">>, Name) -> {ok, Name};
output(<<"EmailTags">>, Name) -> {ok, Name};
output(<<"PrivacyClassification">>, Name) -> {ok, Name};
output(<<"TelephoneTags">>, Name) -> {ok, Name}.
3 changes: 0 additions & 3 deletions src/graphql/mongoose_graphql_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ err(_Ctx, ErrorTerm) ->
%% callback invoked when resolver crashes
-spec crash(map(), term()) -> err_msg().
crash(_Ctx, Err = #{type := Type}) ->
io:format("---------------------------------------\n"),
io:format("~p", [Err]),
io:format("---------------------------------------\n"),
?LOG_ERROR(Err#{what => graphql_crash}),
#{message => <<"Unexpected ", Type/binary, " resolver crash">>,
extensions => #{code => resolver_crash}}.
Expand Down
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 @@ -14,4 +14,6 @@ execute(_Ctx, _Obj, <<"muc_light">>, _Args) ->
execute(_Ctx, _Obj, <<"stanza">>, _Args) ->
{ok, stanza};
execute(_Ctx, _Obj, <<"roster">>, _Args) ->
{ok, roster}.
{ok, roster};
execute(_Ctx, _Obj, <<"vcard">>, _Args) ->
{ok, vcard}.
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 @@ -18,4 +18,6 @@ execute(_Ctx, _Obj, <<"checkAuth">>, _Args) ->
execute(_Ctx, _Obj, <<"stanza">>, _Args) ->
{ok, stanza};
execute(_Ctx, _Obj, <<"roster">>, _Args) ->
{ok, roster}.
{ok, roster};
execute(_Ctx, _Obj, <<"vcard">>, _Args) ->
{ok, vcard}.
23 changes: 23 additions & 0 deletions src/graphql/user/mongoose_graphql_vcard_user_mutation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-module(mongoose_graphql_vcard_user_mutation).
-behaviour(mongoose_graphql).

-include("mod_vcard.hrl").

-export([execute/4]).

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

-ignore_xref([execute/4]).

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

execute(#{user := CallerJID}, vcard, <<"setVcard">>, #{<<"vcard">> := VCARD}) ->
case mod_vcard_api:set_vcard(CallerJID, VCARD) of
{ok, _} = Vcard -> Vcard;
{error, not_found} ->
make_error({error, "User does not exist"}, #{user => CallerJID});
_ ->
make_error({error, "Internal server error"}, #{user => CallerJID})
end.
21 changes: 21 additions & 0 deletions src/graphql/user/mongoose_graphql_vcard_user_query.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(mongoose_graphql_vcard_user_query).
-behaviour(mongoose_graphql).

-export([execute/4]).

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

-ignore_xref([execute/4]).

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

execute(#{user := CallerJID}, vcard, <<"getVcard">>, _) ->
case mod_vcard_api:get_vcard(CallerJID) of
{ok, _} = Vcard -> Vcard;
{error, not_found} ->
make_error({error, "User does not exist"}, #{user => CallerJID});
_ ->
make_error({error, "Internal server error"}, #{user => CallerJID})
end.

0 comments on commit c807c9a

Please sign in to comment.