2007-12-01 06:16:30 +01:00
|
|
|
%%% ====================================================================
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% ``The contents of this file are subject to the Erlang Public License,
|
|
|
|
%%% Version 1.1, (the "License"); you may not use this file except in
|
|
|
|
%%% compliance with the License. You should have received a copy of the
|
|
|
|
%%% Erlang Public License along with this software. If not, it can be
|
|
|
|
%%% retrieved via the world wide web at http://www.erlang.org/.
|
|
|
|
%%%
|
|
|
|
%%% Software distributed under the License is distributed on an "AS IS"
|
|
|
|
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
%%% the License for the specific language governing rights and limitations
|
|
|
|
%%% under the License.
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% The Initial Developer of the Original Code is ProcessOne.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% Portions created by ProcessOne are Copyright 2006-2009, ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% All Rights Reserved.''
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% This software is copyright 2006-2009, ProcessOne.
|
2007-12-01 06:16:30 +01:00
|
|
|
%%%
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% @copyright 2006-2009 ProcessOne
|
2007-12-01 06:16:30 +01:00
|
|
|
%%% @author Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% [http://www.process-one.net/]
|
|
|
|
%%% @version {@vsn}, {@date} {@time}
|
|
|
|
%%% @end
|
|
|
|
%%% ====================================================================
|
|
|
|
|
|
|
|
%%% @doc The module <strong>{@module}</strong> is the default PubSub node tree plugin.
|
|
|
|
%%% <p>It is used as a default for all unknown PubSub node type. It can serve
|
|
|
|
%%% as a developer basis and reference to build its own custom pubsub node tree
|
|
|
|
%%% types.</p>
|
|
|
|
%%% <p>PubSub node tree plugins are using the {@link gen_nodetree} behaviour.</p>
|
|
|
|
%%% <p><strong>The API isn't stabilized yet</strong>. The pubsub plugin
|
|
|
|
%%% development is still a work in progress. However, the system is already
|
|
|
|
%%% useable and useful as is. Please, send us comments, feedback and
|
|
|
|
%%% improvements.</p>
|
|
|
|
|
2009-05-29 01:21:50 +02:00
|
|
|
-module(nodetree_tree).
|
2007-12-01 06:16:30 +01:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
-include_lib("stdlib/include/qlc.hrl").
|
2008-12-08 12:21:32 +01:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-include("pubsub.hrl").
|
|
|
|
|
|
|
|
-behaviour(gen_pubsub_nodetree).
|
|
|
|
|
|
|
|
-export([init/3,
|
|
|
|
terminate/2,
|
|
|
|
options/0,
|
|
|
|
set_node/1,
|
2008-12-09 01:32:36 +01:00
|
|
|
get_node/3,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_node/2,
|
2009-05-26 23:50:13 +02:00
|
|
|
get_node/1,
|
2008-12-09 01:32:36 +01:00
|
|
|
get_nodes/2,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_nodes/1,
|
2009-08-25 19:14:30 +02:00
|
|
|
get_parentnodes/3,
|
|
|
|
get_parentnodes_tree/3,
|
2008-02-02 15:15:37 +01:00
|
|
|
get_subnodes/3,
|
2009-05-01 01:17:38 +02:00
|
|
|
get_subnodes_tree/3,
|
2009-10-21 00:09:43 +02:00
|
|
|
create_node/6,
|
2007-12-01 06:16:30 +01:00
|
|
|
delete_node/2
|
|
|
|
]).
|
|
|
|
|
|
|
|
|
|
|
|
%% ================
|
|
|
|
%% API definition
|
|
|
|
%% ================
|
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (Host, ServerHost, Opts) -> any()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
|
|
|
%% ServerHost = host()
|
|
|
|
%% Opts = list()
|
|
|
|
%% @doc <p>Called during pubsub modules initialisation. Any pubsub plugin must
|
|
|
|
%% implement this function. It can return anything.</p>
|
|
|
|
%% <p>This function is mainly used to trigger the setup task necessary for the
|
|
|
|
%% plugin. It can be used for example by the developer to create the specific
|
|
|
|
%% module database schema if it does not exists yet.</p>
|
|
|
|
init(_Host, _ServerHost, _Opts) ->
|
|
|
|
mnesia:create_table(pubsub_node,
|
|
|
|
[{disc_copies, [node()]},
|
2008-09-16 16:39:57 +02:00
|
|
|
{attributes, record_info(fields, pubsub_node)}]),
|
2009-05-07 02:54:44 +02:00
|
|
|
mnesia:add_table_index(pubsub_node, id),
|
2007-12-01 06:16:30 +01:00
|
|
|
NodesFields = record_info(fields, pubsub_node),
|
|
|
|
case mnesia:table_info(pubsub_node, attributes) of
|
|
|
|
NodesFields -> ok;
|
2009-05-01 01:17:38 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
%% mnesia:transform_table(pubsub_state, ignore, StatesFields)
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
terminate(_Host, _ServerHost) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%% @spec () -> [Option]
|
|
|
|
%% Option = mod_pubsub:nodetreeOption()
|
|
|
|
%% @doc Returns the default pubsub node tree options.
|
|
|
|
options() ->
|
|
|
|
[{virtual_tree, false}].
|
|
|
|
|
|
|
|
%% @spec (NodeRecord) -> ok | {error, Reason}
|
|
|
|
%% Record = mod_pubsub:pubsub_node()
|
|
|
|
set_node(Record) when is_record(Record, pubsub_node) ->
|
|
|
|
mnesia:write(Record);
|
|
|
|
set_node(_) ->
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'internal-server-error'}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-10-13 23:36:32 +02:00
|
|
|
%% @spec (Host, Node, From) -> pubsubNode() | {error, Reason}
|
2009-01-19 12:59:40 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
2009-05-01 01:17:38 +02:00
|
|
|
get_node(Host, Node, _From) ->
|
|
|
|
get_node(Host, Node).
|
2007-12-01 06:16:30 +01:00
|
|
|
get_node(Host, Node) ->
|
|
|
|
case catch mnesia:read({pubsub_node, {Host, Node}}) of
|
|
|
|
[Record] when is_record(Record, pubsub_node) -> Record;
|
2008-12-05 16:13:09 +01:00
|
|
|
[] -> {error, 'item-not-found'};
|
2007-12-01 06:16:30 +01:00
|
|
|
Error -> Error
|
|
|
|
end.
|
2009-05-26 23:50:13 +02:00
|
|
|
get_node(NodeId) ->
|
|
|
|
case catch mnesia:index_read(pubsub_node, NodeId, #pubsub_node.id) of
|
|
|
|
[Record] when is_record(Record, pubsub_node) -> Record;
|
|
|
|
[] -> {error, 'item-not-found'};
|
|
|
|
Error -> Error
|
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-10-13 23:36:32 +02:00
|
|
|
%% @spec (Host, From) -> [pubsubNode()] | {error, Reason}
|
2009-05-01 01:17:38 +02:00
|
|
|
%% Host = mod_pubsub:host() | mod_pubsub:jid()
|
|
|
|
get_nodes(Host, _From) ->
|
|
|
|
get_nodes(Host).
|
|
|
|
get_nodes(Host) ->
|
|
|
|
mnesia:match_object(#pubsub_node{nodeid = {Host, '_'}, _ = '_'}).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
%% @spec (Host, Node, From) -> [{Depth, Record}] | {error, Reason}
|
|
|
|
%% Host = mod_pubsub:host() | mod_pubsub:jid()
|
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
|
|
|
%% From = mod_pubsub:jid()
|
|
|
|
%% Depth = integer()
|
|
|
|
%% Record = pubsubNode()
|
|
|
|
%% @doc <p>Default node tree does not handle parents, return empty list.</p>
|
|
|
|
get_parentnodes(_Host, _Node, _From) ->
|
|
|
|
[].
|
|
|
|
|
|
|
|
%% @spec (Host, Node, From) -> [{Depth, Record}] | {error, Reason}
|
|
|
|
%% Host = mod_pubsub:host() | mod_pubsub:jid()
|
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
|
|
|
%% From = mod_pubsub:jid()
|
|
|
|
%% Depth = integer()
|
|
|
|
%% Record = pubsubNode()
|
|
|
|
%% @doc <p>Default node tree does not handle parents, return a list
|
|
|
|
%% containing just this node.</p>
|
|
|
|
get_parentnodes_tree(Host, Node, From) ->
|
|
|
|
case get_node(Host, Node, From) of
|
|
|
|
N when is_record(N, pubsub_node) -> [{0, [N]}];
|
2009-08-28 01:09:20 +02:00
|
|
|
_Error -> []
|
2009-08-25 19:14:30 +02:00
|
|
|
end.
|
2009-08-28 01:09:20 +02:00
|
|
|
|
2008-02-02 15:15:37 +01:00
|
|
|
%% @spec (Host, Node, From) -> [pubsubNode()] | {error, Reason}
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
2008-02-02 15:15:37 +01:00
|
|
|
%% From = mod_pubsub:jid()
|
|
|
|
get_subnodes(Host, Node, _From) ->
|
2009-05-01 01:17:38 +02:00
|
|
|
get_subnodes(Host, Node).
|
2009-10-21 00:09:43 +02:00
|
|
|
get_subnodes(Host, <<>>) ->
|
|
|
|
Q = qlc:q([N || #pubsub_node{nodeid = {NHost, _},
|
|
|
|
parents = Parents} = N <- mnesia:table(pubsub_node),
|
|
|
|
Host == NHost,
|
|
|
|
Parents == []]),
|
|
|
|
qlc:e(Q);
|
2009-05-01 01:17:38 +02:00
|
|
|
get_subnodes(Host, Node) ->
|
2009-08-25 19:14:30 +02:00
|
|
|
Q = qlc:q([N || #pubsub_node{nodeid = {NHost, _},
|
|
|
|
parents = Parents} = N <- mnesia:table(pubsub_node),
|
|
|
|
Host == NHost,
|
|
|
|
lists:member(Node, Parents)]),
|
|
|
|
qlc:e(Q).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-10-13 23:36:32 +02:00
|
|
|
%% @spec (Host, Index, From) -> [pubsubNodeIdx()] | {error, Reason}
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
2009-05-01 01:17:38 +02:00
|
|
|
%% From = mod_pubsub:jid()
|
|
|
|
get_subnodes_tree(Host, Node, _From) ->
|
|
|
|
get_subnodes_tree(Host, Node).
|
2007-12-01 06:16:30 +01:00
|
|
|
get_subnodes_tree(Host, Node) ->
|
2009-10-21 00:09:43 +02:00
|
|
|
case get_node(Host, Node) of
|
|
|
|
{error, _} ->
|
|
|
|
[];
|
|
|
|
Rec ->
|
|
|
|
BasePlugin = list_to_atom("node_"++Rec#pubsub_node.type),
|
|
|
|
BasePath = BasePlugin:node_to_path(Node),
|
|
|
|
mnesia:foldl(fun(#pubsub_node{nodeid = {H, N}} = R, Acc) ->
|
|
|
|
Plugin = list_to_atom("node_"++R#pubsub_node.type),
|
|
|
|
Path = Plugin:node_to_path(N),
|
|
|
|
case lists:prefix(BasePath, Path) and (H == Host) of
|
|
|
|
true -> [R | Acc];
|
|
|
|
false -> Acc
|
|
|
|
end
|
|
|
|
end, [], pubsub_node)
|
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-10-23 17:14:25 +02:00
|
|
|
%% @spec (Host, Node, Type, Owner, Options, Parents) -> ok | {error, Reason}
|
2009-05-01 01:17:38 +02:00
|
|
|
%% Host = mod_pubsub:host() | mod_pubsub:jid()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
|
|
|
%% NodeType = mod_pubsub:nodeType()
|
|
|
|
%% Owner = mod_pubsub:jid()
|
|
|
|
%% Options = list()
|
2009-10-21 00:09:43 +02:00
|
|
|
create_node(Host, Node, Type, Owner, Options, Parents) ->
|
2009-05-01 01:17:38 +02:00
|
|
|
BJID = jlib:short_prepd_bare_jid(Owner),
|
|
|
|
case mnesia:read({pubsub_node, {Host, Node}}) of
|
2007-12-01 06:16:30 +01:00
|
|
|
[] ->
|
2009-10-21 00:09:43 +02:00
|
|
|
ParentExists =
|
2009-05-01 01:17:38 +02:00
|
|
|
case Host of
|
2007-12-01 06:16:30 +01:00
|
|
|
{_U, _S, _R} ->
|
|
|
|
%% This is special case for PEP handling
|
|
|
|
%% PEP does not uses hierarchy
|
2009-10-21 00:09:43 +02:00
|
|
|
true;
|
2007-12-01 06:16:30 +01:00
|
|
|
_ ->
|
2009-10-21 00:09:43 +02:00
|
|
|
case Parents of
|
|
|
|
[] -> true;
|
|
|
|
[Parent | _] ->
|
2009-05-01 01:17:38 +02:00
|
|
|
case mnesia:read({pubsub_node, {Host, Parent}}) of
|
2009-10-21 00:09:43 +02:00
|
|
|
[#pubsub_node{owners = [{[], Host, []}]}] -> true;
|
|
|
|
[#pubsub_node{owners = Owners}] -> lists:member(BJID, Owners);
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
2008-01-10 18:38:24 +01:00
|
|
|
end
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
|
|
|
case ParentExists of
|
|
|
|
true ->
|
2009-05-01 01:17:38 +02:00
|
|
|
NodeId = pubsub_index:new(node),
|
|
|
|
mnesia:write(#pubsub_node{nodeid = {Host, Node},
|
|
|
|
id = NodeId,
|
2009-10-21 00:09:43 +02:00
|
|
|
parents = Parents,
|
2007-12-01 06:16:30 +01:00
|
|
|
type = Type,
|
2009-05-01 01:17:38 +02:00
|
|
|
owners = [BJID],
|
|
|
|
options = Options}),
|
|
|
|
{ok, NodeId};
|
2007-12-01 06:16:30 +01:00
|
|
|
false ->
|
|
|
|
%% Requesting entity is prohibited from creating nodes
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'}
|
2007-12-01 06:16:30 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
%% NodeID already exists
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'conflict'}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (Host, Node) -> [mod_pubsub:node()]
|
|
|
|
%% Host = mod_pubsub:host() | mod_pubsub:jid()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
2009-05-01 01:17:38 +02:00
|
|
|
delete_node(Host, Node) ->
|
|
|
|
Removed = get_subnodes_tree(Host, Node),
|
|
|
|
lists:foreach(fun(#pubsub_node{nodeid = {_, N}, id = I}) ->
|
|
|
|
pubsub_index:free(node, I),
|
|
|
|
mnesia:delete({pubsub_node, {Host, N}})
|
|
|
|
end, Removed),
|
2007-12-01 06:16:30 +01:00
|
|
|
Removed.
|