1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2017 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
|
17 |
|
-module(mod_global_distrib_disco). |
18 |
|
-author('konrad.zemek@erlang-solutions.com'). |
19 |
|
|
20 |
|
-behaviour(gen_mod). |
21 |
|
-behaviour(mongoose_module_metrics). |
22 |
|
|
23 |
|
-include("mongoose.hrl"). |
24 |
|
-include("jlib.hrl"). |
25 |
|
|
26 |
|
-export([start/2, stop/1, disco_local_items/1]). |
27 |
|
|
28 |
|
-ignore_xref([disco_local_items/1]). |
29 |
|
|
30 |
|
%%-------------------------------------------------------------------- |
31 |
|
%% API |
32 |
|
%%-------------------------------------------------------------------- |
33 |
|
|
34 |
|
-spec start(Host :: jid:server(), Opts :: list()) -> any(). |
35 |
|
start(Host, Opts) -> |
36 |
:-( |
mod_global_distrib_utils:start(?MODULE, Host, Opts, fun start/0). |
37 |
|
|
38 |
|
-spec stop(Host :: jid:server()) -> any(). |
39 |
|
stop(Host) -> |
40 |
:-( |
mod_global_distrib_utils:stop(?MODULE, Host, fun stop/0). |
41 |
|
|
42 |
|
%%-------------------------------------------------------------------- |
43 |
|
%% Hooks implementation |
44 |
|
%%-------------------------------------------------------------------- |
45 |
|
|
46 |
|
-spec disco_local_items(mongoose_disco:item_acc()) -> mongoose_disco:item_acc(). |
47 |
|
disco_local_items(Acc = #{from_jid := From, to_jid := To, node := <<>>}) -> |
48 |
:-( |
Domains = domains_for_disco(To#jid.lserver, From), |
49 |
:-( |
?LOG_DEBUG(#{what => gd_domains_fetched_for_disco, domains => Domains}), |
50 |
:-( |
Items = [#{jid => Domain} || Domain <- Domains], |
51 |
:-( |
mongoose_disco:add_items(Items, Acc); |
52 |
|
disco_local_items(Acc) -> |
53 |
:-( |
Acc. |
54 |
|
|
55 |
|
%%-------------------------------------------------------------------- |
56 |
|
%% Helpers |
57 |
|
%%-------------------------------------------------------------------- |
58 |
|
|
59 |
|
-spec start() -> any(). |
60 |
|
start() -> |
61 |
:-( |
Host = opt(global_host), |
62 |
:-( |
ejabberd_hooks:add(disco_local_items, Host, ?MODULE, disco_local_items, 99). |
63 |
|
|
64 |
|
-spec stop() -> any(). |
65 |
|
stop() -> |
66 |
:-( |
Host = opt(global_host), |
67 |
:-( |
ejabberd_hooks:delete(disco_local_items, Host, ?MODULE, disco_local_items, 99). |
68 |
|
|
69 |
|
-spec opt(Key :: atom()) -> term(). |
70 |
|
opt(Key) -> |
71 |
:-( |
mod_global_distrib_utils:opt(?MODULE, Key). |
72 |
|
|
73 |
|
-spec domains_for_disco(Host :: jid:lserver(), From :: jid:jid()) -> Domains :: [binary()]. |
74 |
|
domains_for_disco(_Host, #jid{ luser = <<>> } = _From) -> |
75 |
|
%% Currently all non-user entities may discover all services |
76 |
:-( |
{ok, Domains} = mod_global_distrib_mapping:all_domains(), |
77 |
:-( |
Domains; |
78 |
|
domains_for_disco(Host, _From) -> |
79 |
:-( |
case gen_mod:get_module_opt(Host, mod_disco, users_can_see_hidden_services, true) of |
80 |
|
true -> |
81 |
:-( |
{ok, Domains} = mod_global_distrib_mapping:all_domains(), |
82 |
:-( |
Domains; |
83 |
|
false -> |
84 |
:-( |
{ok, Domains} = mod_global_distrib_mapping:public_domains(), |
85 |
:-( |
Domains |
86 |
|
end. |
87 |
|
|