1 |
|
-module(mongoose_graphql_muc_helper). |
2 |
|
|
3 |
|
-export([make_rooms_payload/2, make_room_desc/1, muc_room_config_to_map/1, make_muc_room_config/2, |
4 |
|
format_user/1, format_users/1, add_user_resource/2, format_affs/1]). |
5 |
|
|
6 |
|
-export([default_kick_reason/1, default_invite_reason/1, default_room_removal_reason/1]). |
7 |
|
|
8 |
|
-ignore_xref(format_user/1). |
9 |
|
|
10 |
|
-import(mongoose_graphql_helper, [undefined_to_null/1]). |
11 |
|
|
12 |
|
-include("mongoose_rsm.hrl"). |
13 |
|
-include("mod_muc_room.hrl"). |
14 |
|
|
15 |
|
-spec add_user_resource(jid:jid(), binary() | null) -> |
16 |
|
{ok, jid:jid()} | {atom(), binary()}. |
17 |
|
add_user_resource(JID, null) -> |
18 |
3 |
case mongoose_session_api:get_user_resource(JID, 1) of |
19 |
|
{ok, Resource} -> |
20 |
2 |
{ok, jid:replace_resource(JID, Resource)}; |
21 |
|
{wrong_res_number, _} -> |
22 |
1 |
{no_session, <<"Given user does not have any session">>}; |
23 |
|
Error -> |
24 |
:-( |
Error |
25 |
|
end; |
26 |
|
add_user_resource(JID, Resource) -> |
27 |
1 |
{ok, jid:replace_resource(JID, Resource)}. |
28 |
|
|
29 |
|
make_rooms_payload(Rooms, #rsm_out{count = Count, index = Index, first = First, last = Last}) -> |
30 |
17 |
Rooms2 = [{ok, make_room_desc(R)} || R <- Rooms], |
31 |
17 |
#{<<"rooms">> => Rooms2, |
32 |
|
<<"count">> => undefined_to_null(Count), <<"index">> => undefined_to_null(Index), |
33 |
|
<<"first">> => undefined_to_null(First), <<"last">> => undefined_to_null(Last)}. |
34 |
|
|
35 |
|
make_room_desc(#{jid := JID, title := Title} = Room) -> |
36 |
48 |
Private = maps:get(private, Room, null), |
37 |
48 |
UsersNumber = maps:get(users_number, Room, null), |
38 |
48 |
#{<<"jid">> => JID, <<"title">> => Title, |
39 |
|
<<"private">> => Private, <<"usersNumber">> => UsersNumber}. |
40 |
|
|
41 |
|
default_invite_reason(EndpointName) -> |
42 |
7 |
iolist_to_binary(io_lib:format("User invited through the ~s GraphQL API", [EndpointName])). |
43 |
|
|
44 |
|
default_kick_reason(EndpointName) -> |
45 |
9 |
iolist_to_binary(io_lib:format("User kicked through the ~s GraphQL API", [EndpointName])). |
46 |
|
|
47 |
|
default_room_removal_reason(EndpointName) -> |
48 |
9 |
iolist_to_binary(io_lib:format("Room deleted through the ~s GraphQL API", [EndpointName])). |
49 |
|
|
50 |
|
format_users(Users) -> |
51 |
4 |
[{ok, format_user(U)} || U <- Users]. |
52 |
|
|
53 |
|
format_user(#{jid := JID, role := Role, nick := Nick}) -> |
54 |
8 |
#{<<"jid">> => JID, <<"role">> => Role, <<"nick">> => Nick}. |
55 |
|
|
56 |
|
format_affs(Affs) -> |
57 |
9 |
[{ok, format_aff(A)} || A <- Affs]. |
58 |
|
|
59 |
|
format_aff({JID, Aff}) -> |
60 |
12 |
#{<<"jid">> => JID, <<"affiliation">> => Aff}. |
61 |
|
|
62 |
|
-spec muc_room_config_to_map(mod_muc_room:config()) -> map(). |
63 |
|
muc_room_config_to_map(Conf) -> |
64 |
6 |
#{<<"title">> => Conf#config.title, |
65 |
|
<<"description">> => Conf#config.description, |
66 |
|
<<"allowChangeSubject">> => Conf#config.allow_change_subj, |
67 |
|
<<"allowQueryUsers">> => Conf#config.allow_query_users, |
68 |
|
<<"allowPrivateMessages">> => Conf#config.allow_private_messages, |
69 |
|
<<"allowVisitorStatus">> => Conf#config.allow_visitor_status, |
70 |
|
<<"allowVisitorNickchange">> => Conf#config.allow_visitor_nickchange, |
71 |
|
<<"public">> => Conf#config.public, |
72 |
|
<<"publicList">> => Conf#config.public_list, |
73 |
|
<<"persistent">> => Conf#config.persistent, |
74 |
|
<<"moderated">> => Conf#config.moderated, |
75 |
|
<<"membersByDefault">> => Conf#config.members_by_default, |
76 |
|
<<"membersOnly">> => Conf#config.members_only, |
77 |
|
<<"allowUserInvites">> => Conf#config.allow_user_invites, |
78 |
|
<<"allowMultipleSession">> => Conf#config.allow_multiple_sessions, |
79 |
|
<<"passwordProtected">> => Conf#config.password_protected, |
80 |
|
<<"password">> => Conf#config.password, |
81 |
|
<<"anonymous">> => Conf#config.anonymous, |
82 |
|
<<"mayGetMemberList">> => format_maygetmemberlist(Conf#config.maygetmemberlist), |
83 |
|
<<"maxUsers">> => Conf#config.max_users, |
84 |
|
<<"logging">> => Conf#config.logging}. |
85 |
|
|
86 |
|
-spec make_muc_room_config(map(), mod_muc_room:config()) -> mod_muc_room:config(). |
87 |
|
make_muc_room_config(Map, Conf) -> |
88 |
3 |
#config{ |
89 |
|
title = maybe_value(<<"title">>, Conf#config.title, Map), |
90 |
|
description = maybe_value(<<"description">>, Conf#config.description, Map), |
91 |
|
allow_change_subj = maybe_value(<<"allowChangeSubject">>, Conf#config.allow_change_subj, Map), |
92 |
|
allow_query_users = maybe_value(<<"allowQueryUsers">>, Conf#config.allow_query_users, Map), |
93 |
|
allow_private_messages = maybe_value(<<"allowPrivateMessages">>, |
94 |
|
Conf#config.allow_private_messages, Map), |
95 |
|
allow_visitor_status = maybe_value(<<"allowVisitorStatus">>, |
96 |
|
Conf#config.allow_visitor_status, Map), |
97 |
|
allow_visitor_nickchange = maybe_value(<<"allowVisitorNickchange">>, |
98 |
|
Conf#config.allow_visitor_nickchange, Map), |
99 |
|
public = maybe_value(<<"public">>, Conf#config.public, Map), |
100 |
|
public_list = maybe_value(<<"publicList">>, Conf#config.public_list, Map), |
101 |
|
persistent = maybe_value(<<"persistent">>, Conf#config.persistent, Map), |
102 |
|
moderated = maybe_value(<<"moderated">>, Conf#config.moderated, Map), |
103 |
|
members_by_default = maybe_value(<<"membersByDefault">>, Conf#config.members_by_default, Map), |
104 |
|
members_only = maybe_value(<<"membersOnly">>, Conf#config.members_only, Map), |
105 |
|
allow_user_invites = maybe_value(<<"allowUserInvites">>, Conf#config.allow_user_invites, Map), |
106 |
|
allow_multiple_sessions = maybe_value(<<"allowMultipleSession">>, |
107 |
|
Conf#config.allow_multiple_sessions, Map), |
108 |
|
password_protected = maybe_value(<<"passwordProtected">>, |
109 |
|
Conf#config.password_protected, Map), |
110 |
|
password = maybe_value(<<"password">>, Conf#config.password, Map), |
111 |
|
anonymous = maybe_value(<<"anonymous">>, Conf#config.anonymous, Map), |
112 |
|
maygetmemberlist = maybe_value(<<"mayGetMemberList">>, Conf#config.maygetmemberlist, Map), |
113 |
|
max_users = maybe_value(<<"maxUsers">>, Conf#config.max_users, Map), |
114 |
|
logging = maybe_value(<<"logging">>, Conf#config.logging, Map) |
115 |
|
}. |
116 |
|
|
117 |
|
maybe_value(Key, Default, Map) -> |
118 |
63 |
case maps:get(Key, Map) of |
119 |
54 |
null -> Default; |
120 |
9 |
Val -> Val |
121 |
|
end. |
122 |
|
|
123 |
6 |
format_maygetmemberlist(Elements) -> [{ok, E} || E <- Elements]. |