1 |
|
%% @doc Provide an interface for frontends (like graphql or ctl) to manage private. |
2 |
|
-module(mod_private_api). |
3 |
|
|
4 |
|
-include("mongoose.hrl"). |
5 |
|
-include("jlib.hrl"). |
6 |
|
-include_lib("exml/include/exml.hrl"). |
7 |
|
|
8 |
|
-export([private_get/3, private_set/2]). |
9 |
|
|
10 |
|
-spec private_get(jid:jid(), binary(), binary()) -> {ok, exml:element()} | {Error, string()} when |
11 |
|
Error :: not_found. |
12 |
|
private_get(JID, Element, Ns) -> |
13 |
8 |
case ejabberd_auth:does_user_exist(JID) of |
14 |
|
true -> |
15 |
6 |
do_private_get(JID, Element, Ns); |
16 |
|
false -> |
17 |
2 |
{not_found, io_lib:format("User ~ts does not exist", [jid:to_binary(JID)])} |
18 |
|
end. |
19 |
|
|
20 |
|
-spec private_set(jid:jid(), exml:element()) -> |
21 |
|
{ok, exml:element()} | {not_found, iolist()}. |
22 |
|
private_set(#jid{lserver = Domain} = JID, Xml) -> |
23 |
5 |
case ejabberd_auth:does_user_exist(JID) of |
24 |
|
true -> |
25 |
3 |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(Domain), |
26 |
3 |
send_iq(set, Xml, JID, HostType), |
27 |
3 |
{ok, Xml}; |
28 |
|
false -> |
29 |
2 |
{not_found, io_lib:format("User ~ts does not exist", [jid:to_binary(JID)])} |
30 |
|
end. |
31 |
|
|
32 |
|
do_private_get(JID, Element, Ns) -> |
33 |
6 |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(JID#jid.lserver), |
34 |
6 |
Xml = #xmlel{ name = Element, attrs = [{<<"xmlns">>, Ns}]}, |
35 |
6 |
{_, ResIq} = send_iq(get, Xml, JID, HostType), |
36 |
6 |
[#xmlel{ name = <<"query">>, |
37 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
38 |
|
children = [SubEl] }] = ResIq#iq.sub_el, |
39 |
6 |
{ok, SubEl}. |
40 |
|
|
41 |
|
send_iq(Method, Xml, From = To = _JID, HostType) -> |
42 |
9 |
IQ = {iq, <<>>, Method, ?NS_PRIVATE, <<>>, |
43 |
|
#xmlel{ name = <<"query">>, |
44 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
45 |
|
children = [Xml] } }, |
46 |
9 |
Acc = mongoose_acc:new(#{ location => ?LOCATION, |
47 |
|
from_jid => From, |
48 |
|
to_jid => To, |
49 |
|
lserver => From#jid.lserver, |
50 |
|
host_type => HostType, |
51 |
|
element => jlib:iq_to_xml(IQ) }), |
52 |
9 |
mod_private:process_iq(Acc, From, To, IQ, #{}). |