2015-12-15 16:11:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : nodetree_dag.erl
|
|
|
|
%%% Author : Brian Cully <bjc@kublai.com>
|
|
|
|
%%% Purpose : experimental support of XEP-248
|
|
|
|
%%% Created : 15 Jun 2009 by Brian Cully <bjc@kublai.com>
|
2009-06-15 15:45:40 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2015-12-15 16:11:29 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-06-15 15:45:40 +02:00
|
|
|
|
|
|
|
-module(nodetree_dag).
|
2015-04-08 17:12:05 +02:00
|
|
|
-behaviour(gen_pubsub_nodetree).
|
2009-06-15 15:45:40 +02:00
|
|
|
-author('bjc@kublai.com').
|
|
|
|
|
|
|
|
-include_lib("stdlib/include/qlc.hrl").
|
|
|
|
|
|
|
|
-include("pubsub.hrl").
|
2016-09-08 14:49:27 +02:00
|
|
|
-include("xmpp.hrl").
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-export([init/3, terminate/2, options/0, set_node/1,
|
|
|
|
get_node/3, get_node/2, get_node/1, get_nodes/2,
|
|
|
|
get_nodes/1, get_parentnodes/3, get_parentnodes_tree/3,
|
|
|
|
get_subnodes/3, get_subnodes_tree/3, create_node/6,
|
|
|
|
delete_node/2]).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-define(DEFAULT_NODETYPE, leaf).
|
2009-06-15 15:45:40 +02:00
|
|
|
-define(DEFAULT_PARENTS, []).
|
|
|
|
-define(DEFAULT_CHILDREN, []).
|
|
|
|
|
|
|
|
init(Host, ServerHost, Opts) ->
|
2009-10-20 17:03:07 +02:00
|
|
|
nodetree_tree:init(Host, ServerHost, Opts).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
|
|
|
terminate(Host, ServerHost) ->
|
|
|
|
nodetree_tree:terminate(Host, ServerHost).
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_node(#pubsub_node{nodeid = {Key, _}, owners = Owners, options = Options} = Node) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Parents = find_opt(collection, ?DEFAULT_PARENTS, Options),
|
2009-06-15 15:45:40 +02:00
|
|
|
case validate_parentage(Key, Owners, Parents) of
|
2015-04-08 17:12:05 +02:00
|
|
|
true -> mnesia:write(Node#pubsub_node{parents = Parents});
|
|
|
|
Other -> Other
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
create_node(Key, Node, Type, Owner, Options, Parents) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
OwnerJID = jid:tolower(jid:remove_resource(Owner)),
|
2015-04-08 17:12:05 +02:00
|
|
|
case find_node(Key, Node) of
|
|
|
|
false ->
|
|
|
|
Nidx = pubsub_index:new(node),
|
|
|
|
N = #pubsub_node{nodeid = oid(Key, Node), id = Nidx,
|
|
|
|
type = Type, parents = Parents, owners = [OwnerJID],
|
|
|
|
options = Options},
|
|
|
|
case set_node(N) of
|
|
|
|
ok -> {ok, Nidx};
|
|
|
|
Other -> Other
|
|
|
|
end;
|
|
|
|
_ ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_conflict(<<"Node already exists">>, ?MYLANG)}
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
delete_node(Key, Node) ->
|
|
|
|
case find_node(Key, Node) of
|
|
|
|
false ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)};
|
2015-04-08 17:12:05 +02:00
|
|
|
Record ->
|
|
|
|
lists:foreach(fun (#pubsub_node{options = Opts} = Child) ->
|
|
|
|
NewOpts = remove_config_parent(Node, Opts),
|
|
|
|
Parents = find_opt(collection, ?DEFAULT_PARENTS, NewOpts),
|
|
|
|
ok = mnesia:write(pubsub_node,
|
|
|
|
Child#pubsub_node{parents = Parents,
|
|
|
|
options = NewOpts},
|
|
|
|
write)
|
|
|
|
end,
|
|
|
|
get_subnodes(Key, Node)),
|
|
|
|
pubsub_index:free(node, Record#pubsub_node.id),
|
|
|
|
mnesia:delete_object(pubsub_node, Record, write),
|
|
|
|
[Record]
|
|
|
|
end.
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
options() ->
|
|
|
|
nodetree_tree:options().
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Host, Node, _From) ->
|
|
|
|
get_node(Host, Node).
|
|
|
|
|
|
|
|
get_node(Host, Node) ->
|
|
|
|
case find_node(Host, Node) of
|
2016-09-08 14:49:27 +02:00
|
|
|
false -> {error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)};
|
2015-04-08 17:12:05 +02:00
|
|
|
Record -> Record
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Node) ->
|
|
|
|
nodetree_tree:get_node(Node).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
|
|
|
get_nodes(Key, From) ->
|
|
|
|
nodetree_tree:get_nodes(Key, From).
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_nodes(Key) ->
|
|
|
|
nodetree_tree:get_nodes(Key).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_parentnodes(Host, Node, _From) ->
|
|
|
|
case find_node(Host, Node) of
|
|
|
|
false ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)};
|
2015-04-08 17:12:05 +02:00
|
|
|
#pubsub_node{parents = Parents} ->
|
|
|
|
Q = qlc:q([N
|
|
|
|
|| #pubsub_node{nodeid = {NHost, NNode}} = N
|
2013-03-14 10:33:02 +01:00
|
|
|
<- mnesia:table(pubsub_node),
|
2015-04-08 17:12:05 +02:00
|
|
|
Parent <- Parents, Host == NHost, Parent == NNode]),
|
|
|
|
qlc:e(Q)
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_parentnodes_tree(Host, Node, _From) ->
|
|
|
|
Pred = fun (NID, #pubsub_node{nodeid = {_, NNode}}) ->
|
|
|
|
NID == NNode
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Tr = fun (#pubsub_node{parents = Parents}) -> Parents
|
2015-04-08 17:12:05 +02:00
|
|
|
end,
|
|
|
|
traversal_helper(Pred, Tr, Host, [Node]).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subnodes(Host, Node, _From) ->
|
|
|
|
get_subnodes(Host, Node).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2009-10-27 15:26:22 +01:00
|
|
|
get_subnodes(Host, <<>>) ->
|
|
|
|
get_subnodes_helper(Host, <<>>);
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subnodes(Host, Node) ->
|
|
|
|
case find_node(Host, Node) of
|
2016-09-08 14:49:27 +02:00
|
|
|
false -> {error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)};
|
2015-04-08 17:12:05 +02:00
|
|
|
_ -> get_subnodes_helper(Host, Node)
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subnodes_helper(Host, Node) ->
|
|
|
|
Q = qlc:q([N
|
|
|
|
|| #pubsub_node{nodeid = {NHost, _},
|
|
|
|
parents = Parents} =
|
|
|
|
N
|
|
|
|
<- mnesia:table(pubsub_node),
|
|
|
|
Host == NHost, lists:member(Node, Parents)]),
|
2009-10-27 15:26:22 +01:00
|
|
|
qlc:e(Q).
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subnodes_tree(Host, Node, From) ->
|
2009-06-15 15:45:40 +02:00
|
|
|
Pred = fun (NID, #pubsub_node{parents = Parents}) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
lists:member(NID, Parents)
|
|
|
|
end,
|
2009-07-06 10:05:33 +02:00
|
|
|
Tr = fun (#pubsub_node{nodeid = {_, N}}) -> [N] end,
|
2015-04-08 17:12:05 +02:00
|
|
|
traversal_helper(Pred, Tr, 1, Host, [Node],
|
|
|
|
[{0, [get_node(Host, Node, From)]}]).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%%====================================================================
|
|
|
|
oid(Key, Name) -> {Key, Name}.
|
|
|
|
|
|
|
|
%% Key = jlib:jid() | host()
|
2015-04-08 17:12:05 +02:00
|
|
|
%% Node = string()
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec find_node(Key :: mod_pubsub:hostPubsub(), Node :: mod_pubsub:nodeId()) ->
|
|
|
|
mod_pubsub:pubsubNode() | false.
|
2015-04-08 17:12:05 +02:00
|
|
|
find_node(Key, Node) ->
|
|
|
|
case mnesia:read(pubsub_node, oid(Key, Node), read) of
|
|
|
|
[] -> false;
|
|
|
|
[Node] -> Node
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Key = jlib:jid() | host()
|
|
|
|
%% Default = term()
|
|
|
|
%% Options = [{Key = atom(), Value = term()}]
|
|
|
|
find_opt(Key, Default, Options) ->
|
|
|
|
case lists:keysearch(Key, 1, Options) of
|
2015-04-08 17:12:05 +02:00
|
|
|
{value, {Key, Val}} -> Val;
|
|
|
|
_ -> Default
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec traversal_helper(Pred :: fun(), Tr :: fun(), Host :: mod_pubsub:hostPubsub(),
|
|
|
|
Nodes :: [mod_pubsub:nodeId(),...]) ->
|
|
|
|
[{Depth::non_neg_integer(),
|
|
|
|
Nodes::[mod_pubsub:pubsubNode(),...]}].
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
traversal_helper(Pred, Tr, Host, Nodes) ->
|
|
|
|
traversal_helper(Pred, Tr, 0, Host, Nodes, []).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
|
|
|
traversal_helper(_Pred, _Tr, _Depth, _Host, [], Acc) ->
|
|
|
|
Acc;
|
2015-04-08 17:12:05 +02:00
|
|
|
traversal_helper(Pred, Tr, Depth, Host, Nodes, Acc) ->
|
|
|
|
Q = qlc:q([N
|
|
|
|
|| #pubsub_node{nodeid = {NHost, _}} = N
|
|
|
|
<- mnesia:table(pubsub_node),
|
|
|
|
Node <- Nodes, Host == NHost, Pred(Node, N)]),
|
2009-06-15 15:45:40 +02:00
|
|
|
Nodes = qlc:e(Q),
|
|
|
|
IDs = lists:flatmap(Tr, Nodes),
|
2015-04-08 17:12:05 +02:00
|
|
|
traversal_helper(Pred, Tr, Depth + 1, Host, IDs, [{Depth, Nodes} | Acc]).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
remove_config_parent(Node, Options) ->
|
|
|
|
remove_config_parent(Node, Options, []).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
remove_config_parent(_Node, [], Acc) ->
|
2009-06-15 15:45:40 +02:00
|
|
|
lists:reverse(Acc);
|
2015-04-08 17:12:05 +02:00
|
|
|
remove_config_parent(Node, [{collection, Parents} | T], Acc) ->
|
|
|
|
remove_config_parent(Node, T, [{collection, lists:delete(Node, Parents)} | Acc]);
|
|
|
|
remove_config_parent(Node, [H | T], Acc) ->
|
|
|
|
remove_config_parent(Node, T, [H | Acc]).
|
2009-06-15 15:45:40 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec validate_parentage(Key :: mod_pubsub:hostPubsub(), Owners :: [ljid(),...],
|
|
|
|
Parent_Nodes :: [mod_pubsub:nodeId()]) ->
|
|
|
|
true | {error, xmlel()}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
validate_parentage(_Key, _Owners, []) ->
|
|
|
|
true;
|
2009-06-15 15:45:40 +02:00
|
|
|
validate_parentage(Key, Owners, [[] | T]) ->
|
|
|
|
validate_parentage(Key, Owners, T);
|
2009-10-27 15:26:22 +01:00
|
|
|
validate_parentage(Key, Owners, [<<>> | T]) ->
|
|
|
|
validate_parentage(Key, Owners, T);
|
2009-06-15 15:45:40 +02:00
|
|
|
validate_parentage(Key, Owners, [ParentID | T]) ->
|
|
|
|
case find_node(Key, ParentID) of
|
2015-04-08 17:12:05 +02:00
|
|
|
false ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)};
|
2015-04-08 17:12:05 +02:00
|
|
|
#pubsub_node{owners = POwners, options = POptions} ->
|
|
|
|
NodeType = find_opt(node_type, ?DEFAULT_NODETYPE, POptions),
|
|
|
|
MutualOwners = [O || O <- Owners, PO <- POwners, O == PO],
|
|
|
|
case {MutualOwners, NodeType} of
|
2016-09-08 14:49:27 +02:00
|
|
|
{[], _} -> {error, xmpp:err_forbidden()};
|
2015-04-08 17:12:05 +02:00
|
|
|
{_, collection} -> validate_parentage(Key, Owners, T);
|
2016-09-08 14:49:27 +02:00
|
|
|
{_, _} -> {error, xmpp:err_not_allowed()}
|
2015-04-08 17:12:05 +02:00
|
|
|
end
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|