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 |
:-( |
case ejabberd_auth:does_user_exist(JID) of |
14 |
|
true -> |
15 |
:-( |
do_private_get(JID, Element, Ns); |
16 |
|
false -> |
17 |
:-( |
{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 |
:-( |
case ejabberd_auth:does_user_exist(JID) of |
24 |
|
true -> |
25 |
:-( |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(Domain), |
26 |
:-( |
send_iq(set, Xml, JID, HostType), |
27 |
:-( |
{ok, Xml}; |
28 |
|
false -> |
29 |
:-( |
{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 |
:-( |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(JID#jid.lserver), |
34 |
:-( |
Xml = #xmlel{ name = Element, attrs = [{<<"xmlns">>, Ns}]}, |
35 |
:-( |
{_, ResIq} = send_iq(get, Xml, JID, HostType), |
36 |
:-( |
[#xmlel{ name = <<"query">>, |
37 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
38 |
|
children = [SubEl] }] = ResIq#iq.sub_el, |
39 |
:-( |
{ok, SubEl}. |
40 |
|
|
41 |
|
send_iq(Method, Xml, From = To = _JID, HostType) -> |
42 |
:-( |
IQ = {iq, <<>>, Method, ?NS_PRIVATE, <<>>, |
43 |
|
#xmlel{ name = <<"query">>, |
44 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
45 |
|
children = [Xml] } }, |
46 |
:-( |
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 |
:-( |
mod_private:process_iq(Acc, From, To, IQ, #{}). |