2015-12-15 16:11:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2016-04-20 11:27:32 +02:00
|
|
|
%%% File : node_flat_sql.erl
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : Standard PubSub node plugin with ODBC backend
|
|
|
|
%%% Created : 1 Dec 2007 by Christophe Romain <christophe.romain@process-one.net>
|
2015-04-08 17:12:05 +02:00
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 ProcessOne
|
2015-04-08 17:12:05 +02:00
|
|
|
%%%
|
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.
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% 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.
|
2009-08-07 10:26:47 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% 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-08-07 10:26:47 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%%% @doc The module <strong>{@module}</strong> is the default PubSub 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
|
|
|
|
%%% types.</p>
|
|
|
|
%%% <p>PubSub plugin nodes are using the {@link gen_node} behaviour.</p>
|
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-module(node_flat_sql).
|
2015-04-08 17:12:05 +02:00
|
|
|
-behaviour(gen_pubsub_node).
|
2009-08-07 10:26:47 +02:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
2016-05-24 17:44:07 +02:00
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
-include("pubsub.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-05-24 17:44:07 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/3, terminate/2, options/0, features/0,
|
2021-08-22 12:44:50 +02:00
|
|
|
create_node_permission/6, create_node/2, delete_node/1, purge_node/2,
|
|
|
|
subscribe_node/8, unsubscribe_node/4,
|
|
|
|
publish_item/7, delete_item/4,
|
2021-10-30 13:19:30 +02:00
|
|
|
remove_extra_items/2, remove_extra_items/3, remove_expired_items/2,
|
2015-04-08 17:12:05 +02:00
|
|
|
get_entity_affiliations/2, get_node_affiliations/1,
|
|
|
|
get_affiliation/2, set_affiliation/3,
|
|
|
|
get_entity_subscriptions/2, get_node_subscriptions/1,
|
|
|
|
get_subscriptions/2, set_subscriptions/4,
|
|
|
|
get_pending_nodes/2, get_states/1, get_state/2,
|
2015-09-04 12:36:33 +02:00
|
|
|
set_state/1, get_items/7, get_items/3, get_item/7,
|
2015-04-08 17:12:05 +02:00
|
|
|
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
|
|
|
|
path_to_node/1,
|
2019-07-31 12:20:38 +02:00
|
|
|
get_entity_subscriptions_for_send_last/2, get_last_items/3,
|
|
|
|
get_only_item/2]).
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2017-11-15 11:16:15 +01:00
|
|
|
-export([decode_jid/1, encode_jid/1, encode_jid_like/1,
|
|
|
|
decode_affiliation/1, decode_subscriptions/1,
|
|
|
|
encode_affiliation/1, encode_subscriptions/1,
|
|
|
|
encode_host/1, encode_host_like/1]).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
init(_Host, _ServerHost, _Opts) ->
|
2016-11-15 18:35:20 +01:00
|
|
|
%%pubsub_subscription_sql:init(Host, ServerHost, Opts),
|
2015-07-22 10:37:26 +02:00
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
terminate(_Host, _ServerHost) ->
|
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
options() ->
|
2016-04-20 11:27:32 +02:00
|
|
|
[{sql, true}, {rsm, true} | node_flat:options()].
|
2015-04-08 17:12:05 +02:00
|
|
|
|
|
|
|
features() ->
|
|
|
|
[<<"rsm">> | node_flat:features()].
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
create_node_permission(Host, ServerHost, Node, ParentNode, Owner, Access) ->
|
|
|
|
node_flat:create_node_permission(Host, ServerHost, Node, ParentNode, Owner, Access).
|
|
|
|
|
|
|
|
create_node(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{_U, _S, _R} = OwnerKey = jid:tolower(jid:remove_resource(Owner)),
|
2016-05-24 17:44:07 +02:00
|
|
|
J = encode_jid(OwnerKey),
|
|
|
|
A = encode_affiliation(owner),
|
|
|
|
S = encode_subscriptions([]),
|
2016-08-30 08:48:08 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("insert into pubsub_state("
|
|
|
|
"nodeid, jid, affiliation, subscriptions) "
|
|
|
|
"values (%(Nidx)d, %(J)s, %(A)s, %(S)s)")),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, {default, broadcast}}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
delete_node(Nodes) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
Reply = lists:map(
|
|
|
|
fun(#pubsub_node{id = Nidx} = PubsubNode) ->
|
|
|
|
Subscriptions =
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(jid)s, @(subscriptions)s "
|
|
|
|
"from pubsub_state where nodeid=%(Nidx)d")) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
[{decode_jid(SJID), decode_subscriptions(Subs)}
|
|
|
|
|| {SJID, Subs} <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{PubsubNode, Subscriptions}
|
|
|
|
end, Nodes),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, {default, broadcast, Reply}}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
subscribe_node(Nidx, Sender, Subscriber, AccessModel,
|
2015-12-09 16:57:43 +01:00
|
|
|
SendLast, PresenceSubscription, RosterGroup, _Options) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Subscriber),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
|
|
|
Authorized = jid:tolower(jid:remove_resource(Sender)) == GenKey,
|
2015-07-22 10:37:26 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
|
|
|
|
PendingSubscription = lists:any(fun
|
|
|
|
({pending, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions),
|
|
|
|
Owner = Affiliation == owner,
|
|
|
|
if not Authorized ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_bad_request(), mod_pubsub:err_invalid_jid())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
PendingSubscription ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_pending_subscription())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == presence) and (not PresenceSubscription) and (not Owner) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_presence_subscription_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == roster) and (not RosterGroup) and (not Owner) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_not_in_roster_group())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == whitelist) and (not Whitelisted) and (not Owner) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_allowed(), mod_pubsub:err_closed_node())};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
%%ForbiddenAnonymous ->
|
|
|
|
%% % Requesting entity is anonymous
|
|
|
|
%% {error, ?ERR_FORBIDDEN};
|
|
|
|
true ->
|
2016-04-20 11:27:32 +02:00
|
|
|
%%{result, SubId} = pubsub_subscription_sql:subscribe_node(Subscriber, Nidx, Options),
|
2015-12-09 16:57:43 +01:00
|
|
|
{NewSub, SubId} = case Subscriptions of
|
|
|
|
[{subscribed, Id}|_] ->
|
|
|
|
{subscribed, Id};
|
|
|
|
[] ->
|
2016-04-20 11:27:32 +02:00
|
|
|
Id = pubsub_subscription_sql:make_subid(),
|
2015-12-09 16:57:43 +01:00
|
|
|
Sub = case AccessModel of
|
|
|
|
authorize -> pending;
|
|
|
|
_ -> subscribed
|
|
|
|
end,
|
|
|
|
update_subscription(Nidx, SubKey, [{Sub, Id} | Subscriptions]),
|
|
|
|
{Sub, Id}
|
2015-07-22 10:37:26 +02:00
|
|
|
end,
|
|
|
|
case {NewSub, SendLast} of
|
2015-12-09 16:57:43 +01:00
|
|
|
{subscribed, never} ->
|
|
|
|
{result, {default, subscribed, SubId}};
|
|
|
|
{subscribed, _} ->
|
|
|
|
{result, {default, subscribed, SubId, send_last}};
|
|
|
|
{_, _} ->
|
|
|
|
{result, {default, pending, SubId}}
|
2015-07-22 10:37:26 +02:00
|
|
|
end
|
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
unsubscribe_node(Nidx, Sender, Subscriber, SubId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Subscriber),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
|
|
|
Authorized = jid:tolower(jid:remove_resource(Sender)) == GenKey,
|
2015-07-22 10:37:26 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, SubKey),
|
|
|
|
SubIdExists = case SubId of
|
|
|
|
<<>> -> false;
|
|
|
|
Binary when is_binary(Binary) -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
%% Requesting entity is prohibited from unsubscribing entity
|
|
|
|
not Authorized ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
%% Entity did not specify SubId
|
|
|
|
%%SubId == "", ?? ->
|
|
|
|
%% {error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
|
|
|
|
%% Invalid subscription identifier
|
|
|
|
%%InvalidSubId ->
|
|
|
|
%% {error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
|
|
|
%% Requesting entity is not a subscriber
|
|
|
|
Subscriptions == [] ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_unexpected_request(),
|
|
|
|
mod_pubsub:err_not_subscribed())};
|
2015-07-22 10:37:26 +02:00
|
|
|
%% Subid supplied, so use that.
|
|
|
|
SubIdExists ->
|
|
|
|
Sub = first_in_list(fun
|
|
|
|
({_, S}) when S == SubId -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions),
|
|
|
|
case Sub of
|
|
|
|
{value, S} ->
|
|
|
|
delete_subscription(SubKey, Nidx, S, Affiliation, Subscriptions),
|
|
|
|
{result, default};
|
|
|
|
false ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_unexpected_request(),
|
|
|
|
mod_pubsub:err_not_subscribed())}
|
2015-07-22 10:37:26 +02:00
|
|
|
end;
|
|
|
|
%% Asking to remove all subscriptions to the given node
|
|
|
|
SubId == all ->
|
|
|
|
[delete_subscription(SubKey, Nidx, S, Affiliation, Subscriptions)
|
|
|
|
|| S <- Subscriptions],
|
|
|
|
{result, default};
|
|
|
|
%% No subid supplied, but there's only one matching subscription
|
|
|
|
length(Subscriptions) == 1 ->
|
|
|
|
delete_subscription(SubKey, Nidx, hd(Subscriptions), Affiliation, Subscriptions),
|
|
|
|
{result, default};
|
|
|
|
%% No subid and more than one possible subscription match.
|
|
|
|
true ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_bad_request(), mod_pubsub:err_subid_required())}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
delete_subscription(SubKey, Nidx, {Subscription, SubId}, Affiliation, Subscriptions) ->
|
|
|
|
NewSubs = Subscriptions -- [{Subscription, SubId}],
|
2016-04-20 11:27:32 +02:00
|
|
|
%%pubsub_subscription_sql:unsubscribe_node(SubKey, Nidx, SubId),
|
2015-07-22 10:37:26 +02:00
|
|
|
case {Affiliation, NewSubs} of
|
|
|
|
{none, []} -> del_state(Nidx, SubKey);
|
|
|
|
_ -> update_subscription(Nidx, SubKey, NewSubs)
|
|
|
|
end.
|
|
|
|
|
2015-07-01 17:18:32 +02:00
|
|
|
publish_item(Nidx, Publisher, PublishModel, MaxItems, ItemId, Payload,
|
|
|
|
_PubOpts) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Publisher),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Subscribed = case PublishModel of
|
|
|
|
subscribers -> node_flat:is_subscribed(Subscriptions);
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
|
|
|
if not ((PublishModel == open) or
|
|
|
|
(PublishModel == publishers) and
|
|
|
|
((Affiliation == owner)
|
2017-09-27 17:37:38 +02:00
|
|
|
or (Affiliation == publisher)
|
|
|
|
or (Affiliation == publish_only))
|
2015-07-22 10:37:26 +02:00
|
|
|
or (Subscribed == true)) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
true ->
|
2021-08-21 12:29:37 +02:00
|
|
|
if MaxItems > 0;
|
|
|
|
MaxItems == unlimited ->
|
2019-02-27 09:56:20 +01:00
|
|
|
Now = erlang:timestamp(),
|
2017-09-27 17:37:38 +02:00
|
|
|
case get_item(Nidx, ItemId) of
|
|
|
|
{result, #pubsub_item{creation = {_, GenKey}} = OldItem} ->
|
|
|
|
set_item(OldItem#pubsub_item{
|
|
|
|
modification = {Now, SubKey},
|
|
|
|
payload = Payload}),
|
|
|
|
{result, {default, broadcast, []}};
|
|
|
|
{result, _} ->
|
|
|
|
{error, xmpp:err_forbidden()};
|
|
|
|
_ ->
|
2021-08-21 20:02:58 +02:00
|
|
|
OldIds = maybe_remove_extra_items(Nidx, MaxItems,
|
|
|
|
GenKey, ItemId),
|
2017-09-27 17:37:38 +02:00
|
|
|
set_item(#pubsub_item{
|
|
|
|
itemid = {ItemId, Nidx},
|
|
|
|
creation = {Now, GenKey},
|
|
|
|
modification = {Now, SubKey},
|
|
|
|
payload = Payload}),
|
2021-08-21 20:02:58 +02:00
|
|
|
{result, {default, broadcast, OldIds}}
|
2017-09-27 17:37:38 +02:00
|
|
|
end;
|
2015-07-22 10:37:26 +02:00
|
|
|
true ->
|
|
|
|
{result, {default, broadcast, []}}
|
|
|
|
end
|
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2021-08-22 12:44:50 +02:00
|
|
|
remove_extra_items(Nidx, MaxItems) ->
|
|
|
|
remove_extra_items(Nidx, MaxItems, itemids(Nidx)).
|
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
remove_extra_items(_Nidx, unlimited, ItemIds) ->
|
|
|
|
{result, {ItemIds, []}};
|
2015-04-08 17:12:05 +02:00
|
|
|
remove_extra_items(Nidx, MaxItems, ItemIds) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
NewItems = lists:sublist(ItemIds, MaxItems),
|
|
|
|
OldItems = lists:nthtail(length(NewItems), ItemIds),
|
|
|
|
del_items(Nidx, OldItems),
|
|
|
|
{result, {NewItems, OldItems}}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2021-10-30 13:19:30 +02:00
|
|
|
remove_expired_items(_Nidx, infinity) ->
|
|
|
|
{result, []};
|
|
|
|
remove_expired_items(Nidx, Seconds) ->
|
|
|
|
ExpT = encode_now(
|
|
|
|
misc:usec_to_now(
|
|
|
|
erlang:system_time(microsecond) - (Seconds * 1000000))),
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(itemid)s from pubsub_item where nodeid=%(Nidx)d "
|
|
|
|
"and creation < %(ExpT)s")) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
ItemIds = [ItemId || {ItemId} <- RItems],
|
|
|
|
del_items(Nidx, ItemIds),
|
|
|
|
{result, ItemIds};
|
|
|
|
_ ->
|
|
|
|
{result, []}
|
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
delete_item(Nidx, Publisher, PublishModel, ItemId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Publisher),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, Affiliation} = get_affiliation(Nidx, GenKey),
|
|
|
|
Allowed = Affiliation == publisher orelse
|
|
|
|
Affiliation == owner orelse
|
2016-09-29 11:20:56 +02:00
|
|
|
(PublishModel == open andalso
|
|
|
|
case get_item(Nidx, ItemId) of
|
|
|
|
{result, #pubsub_item{creation = {_, GenKey}}} -> true;
|
|
|
|
_ -> false
|
|
|
|
end),
|
2015-07-22 10:37:26 +02:00
|
|
|
if not Allowed ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
true ->
|
2017-09-27 17:37:38 +02:00
|
|
|
Items = itemids(Nidx, GenKey),
|
|
|
|
case lists:member(ItemId, Items) of
|
|
|
|
true ->
|
|
|
|
case del_item(Nidx, ItemId) of
|
|
|
|
{updated, 1} -> {result, {default, broadcast}};
|
|
|
|
_ -> {error, xmpp:err_item_not_found()}
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case Affiliation of
|
|
|
|
owner ->
|
|
|
|
case del_item(Nidx, ItemId) of
|
|
|
|
{updated, 1} -> {result, {default, broadcast}};
|
|
|
|
_ -> {error, xmpp:err_item_not_found()}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, xmpp:err_forbidden()}
|
|
|
|
end
|
2015-07-22 10:37:26 +02:00
|
|
|
end
|
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
purge_node(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
case GenState of
|
|
|
|
#pubsub_state{affiliation = owner} ->
|
|
|
|
{result, States} = get_states(Nidx),
|
|
|
|
lists:foreach(fun
|
|
|
|
(#pubsub_state{items = []}) -> ok;
|
|
|
|
(#pubsub_state{items = Items}) -> del_items(Nidx, Items)
|
|
|
|
end,
|
|
|
|
States),
|
|
|
|
{result, {default, broadcast}};
|
|
|
|
_ ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
get_entity_affiliations(Host, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
H = encode_host(Host),
|
|
|
|
J = encode_jid(GenKey),
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(plugin)s, @(i.nodeid)d, @(affiliation)s "
|
2016-08-30 08:48:08 +02:00
|
|
|
"from pubsub_state i, pubsub_node n where "
|
|
|
|
"i.nodeid = n.nodeid and jid=%(J)s and host=%(H)s")) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
[{nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
|
|
|
decode_affiliation(A)} || {N, T, I, A} <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node_affiliations(Nidx) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(jid)s, @(affiliation)s from pubsub_state "
|
|
|
|
"where nodeid=%(Nidx)d")) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
[{decode_jid(J), decode_affiliation(A)} || {J, A} <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_affiliation(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
J = encode_jid(GenKey),
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(affiliation)s from pubsub_state "
|
|
|
|
"where nodeid=%(Nidx)d and jid=%(J)s")) of
|
|
|
|
{selected, [{A}]} ->
|
|
|
|
decode_affiliation(A);
|
|
|
|
_ ->
|
|
|
|
none
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_affiliation(Nidx, Owner, Affiliation) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
{_, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey),
|
|
|
|
case {Affiliation, Subscriptions} of
|
2019-07-29 16:13:16 +02:00
|
|
|
{none, []} -> {result, del_state(Nidx, GenKey)};
|
|
|
|
_ -> {result, update_affiliation(Nidx, GenKey, Affiliation)}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
get_entity_subscriptions(Host, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
H = encode_host(Host),
|
|
|
|
GJ = encode_jid(GenKey),
|
2017-11-15 11:16:15 +01:00
|
|
|
Query = case SubKey of
|
|
|
|
GenKey ->
|
|
|
|
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(plugin)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
2017-11-15 11:16:15 +01:00
|
|
|
"from pubsub_state i, pubsub_node n "
|
|
|
|
"where i.nodeid = n.nodeid and "
|
2020-01-13 01:36:52 +01:00
|
|
|
"(jid=%(GJ)s or jid like %(GJLike)s %ESCAPE) and host=%(H)s");
|
2017-11-15 11:16:15 +01:00
|
|
|
_ ->
|
|
|
|
SJ = encode_jid(SubKey),
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(plugin)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
2017-11-15 11:16:15 +01:00
|
|
|
"from pubsub_state i, pubsub_node n "
|
|
|
|
"where i.nodeid = n.nodeid and "
|
|
|
|
"jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
|
|
|
|
end,
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(Query) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
lists:foldl(
|
|
|
|
fun({N, T, I, J, S}, Acc) ->
|
|
|
|
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
|
|
|
Jid = decode_jid(J),
|
2017-11-15 11:16:15 +01:00
|
|
|
lists:foldl(
|
|
|
|
fun({Sub, SubId}, Acc2) ->
|
|
|
|
[{Node, Sub, SubId, Jid} | Acc2]
|
|
|
|
end, Acc, decode_subscriptions(S))
|
2016-08-30 08:48:08 +02:00
|
|
|
end, [], RItems);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec get_entity_subscriptions_for_send_last(Host :: mod_pubsub:hostPubsub(),
|
|
|
|
Owner :: jid()) ->
|
|
|
|
{result, [{mod_pubsub:pubsubNode(),
|
|
|
|
mod_pubsub:subscription(),
|
|
|
|
mod_pubsub:subId(),
|
|
|
|
ljid()}]}.
|
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
get_entity_subscriptions_for_send_last(Host, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
H = encode_host(Host),
|
|
|
|
GJ = encode_jid(GenKey),
|
2017-11-15 11:16:15 +01:00
|
|
|
Query = case SubKey of
|
|
|
|
GenKey ->
|
|
|
|
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(plugin)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
2017-11-15 11:16:15 +01:00
|
|
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
|
|
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
|
|
|
|
"name='send_last_published_item' and val='on_sub_and_presence' and "
|
2020-01-13 01:36:52 +01:00
|
|
|
"(jid=%(GJ)s or jid like %(GJLike)s %ESCAPE) and host=%(H)s");
|
2017-11-15 11:16:15 +01:00
|
|
|
_ ->
|
|
|
|
SJ = encode_jid(SubKey),
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(plugin)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
2017-11-15 11:16:15 +01:00
|
|
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
|
|
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
|
|
|
|
"name='send_last_published_item' and val='on_sub_and_presence' and "
|
|
|
|
"jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
|
|
|
|
end,
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(Query) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
lists:foldl(
|
|
|
|
fun ({N, T, I, J, S}, Acc) ->
|
|
|
|
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
|
|
|
Jid = decode_jid(J),
|
2017-11-15 11:16:15 +01:00
|
|
|
lists:foldl(
|
|
|
|
fun ({Sub, SubId}, Acc2) ->
|
|
|
|
[{Node, Sub, SubId, Jid}| Acc2]
|
|
|
|
end, Acc, decode_subscriptions(S))
|
2016-08-30 08:48:08 +02:00
|
|
|
end, [], RItems);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node_subscriptions(Nidx) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(jid)s, @(subscriptions)s from pubsub_state "
|
|
|
|
"where nodeid=%(Nidx)d")) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
lists:foldl(
|
|
|
|
fun ({J, S}, Acc) ->
|
|
|
|
Jid = decode_jid(J),
|
2017-11-15 11:16:15 +01:00
|
|
|
lists:foldl(
|
|
|
|
fun ({Sub, SubId}, Acc2) ->
|
|
|
|
[{Jid, Sub, SubId} | Acc2]
|
|
|
|
end, Acc, decode_subscriptions(S))
|
2016-08-30 08:48:08 +02:00
|
|
|
end, [], RItems);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subscriptions(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
2015-07-22 10:37:26 +02:00
|
|
|
J = encode_jid(SubKey),
|
2016-08-30 08:48:08 +02:00
|
|
|
{result,
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(subscriptions)s from pubsub_state"
|
|
|
|
" where nodeid=%(Nidx)d and jid=%(J)s")) of
|
|
|
|
{selected, [{S}]} ->
|
|
|
|
decode_subscriptions(S);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end}.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_subscriptions(Nidx, Owner, Subscription, SubId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
2015-07-22 10:37:26 +02:00
|
|
|
SubState = get_state_without_itemids(Nidx, SubKey),
|
|
|
|
case {SubId, SubState#pubsub_state.subscriptions} of
|
|
|
|
{_, []} ->
|
|
|
|
case Subscription of
|
|
|
|
none ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_bad_request(),
|
|
|
|
mod_pubsub:err_not_subscribed())};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
|
|
|
new_subscription(Nidx, Owner, Subscription, SubState)
|
|
|
|
end;
|
|
|
|
{<<>>, [{_, SID}]} ->
|
|
|
|
case Subscription of
|
|
|
|
none -> unsub_with_subid(Nidx, SID, SubState);
|
|
|
|
_ -> replace_subscription({Subscription, SID}, SubState)
|
|
|
|
end;
|
|
|
|
{<<>>, [_ | _]} ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_bad_request(),
|
|
|
|
mod_pubsub:err_subid_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
|
|
|
case Subscription of
|
|
|
|
none -> unsub_with_subid(Nidx, SubId, SubState);
|
|
|
|
_ -> replace_subscription({Subscription, SubId}, SubState)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
replace_subscription(NewSub, SubState) ->
|
|
|
|
NewSubs = replace_subscription(NewSub, SubState#pubsub_state.subscriptions, []),
|
2019-07-29 16:13:16 +02:00
|
|
|
{result, set_state(SubState#pubsub_state{subscriptions = NewSubs})}.
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
replace_subscription(_, [], Acc) -> Acc;
|
|
|
|
replace_subscription({Sub, SubId}, [{_, SubId} | T], Acc) ->
|
|
|
|
replace_subscription({Sub, SubId}, T, [{Sub, SubId} | Acc]).
|
|
|
|
|
2015-12-09 16:57:43 +01:00
|
|
|
new_subscription(_Nidx, _Owner, Subscription, SubState) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
%%{result, SubId} = pubsub_subscription_sql:subscribe_node(Owner, Nidx, []),
|
|
|
|
SubId = pubsub_subscription_sql:make_subid(),
|
2015-07-22 10:37:26 +02:00
|
|
|
Subscriptions = [{Subscription, SubId} | SubState#pubsub_state.subscriptions],
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = Subscriptions}),
|
2019-07-29 16:13:16 +02:00
|
|
|
{result, {Subscription, SubId}}.
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
unsub_with_subid(Nidx, SubId, SubState) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
%%pubsub_subscription_sql:unsubscribe_node(SubState#pubsub_state.stateid, Nidx, SubId),
|
2015-07-22 10:37:26 +02:00
|
|
|
NewSubs = [{S, Sid}
|
|
|
|
|| {S, Sid} <- SubState#pubsub_state.subscriptions,
|
|
|
|
SubId =/= Sid],
|
|
|
|
case {NewSubs, SubState#pubsub_state.affiliation} of
|
2019-07-29 16:13:16 +02:00
|
|
|
{[], none} -> {result, del_state(Nidx, element(1, SubState#pubsub_state.stateid))};
|
|
|
|
_ -> {result, set_state(SubState#pubsub_state{subscriptions = NewSubs})}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2009-08-27 10:57:45 +02:00
|
|
|
|
|
|
|
get_pending_nodes(Host, Owner) ->
|
2019-07-17 11:34:09 +02:00
|
|
|
GenKey = encode_jid(jid:remove_resource(jid:tolower(Owner))),
|
|
|
|
PendingIdxs = case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(nodeid)d from pubsub_state "
|
|
|
|
"where subscriptions like '%p%' and affiliation='o'"
|
|
|
|
"and jid=%(GenKey)s")) of
|
|
|
|
{selected, RItems} ->
|
|
|
|
[Nidx || {Nidx} <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2015-07-22 10:37:26 +02:00
|
|
|
NodeTree = mod_pubsub:tree(Host),
|
2019-07-17 11:34:09 +02:00
|
|
|
Reply = lists:foldl(fun(Nidx, Acc) ->
|
|
|
|
case NodeTree:get_node(Nidx) of
|
|
|
|
#pubsub_node{nodeid = {_, Node}} -> [Node | Acc];
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], PendingIdxs),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_states(Nidx) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(jid)s, @(affiliation)s, @(subscriptions)s "
|
|
|
|
"from pubsub_state where nodeid=%(Nidx)d")) of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, RItems} ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{result,
|
2016-08-30 08:48:08 +02:00
|
|
|
lists:map(
|
|
|
|
fun({SJID, Aff, Subs}) ->
|
|
|
|
JID = decode_jid(SJID),
|
|
|
|
#pubsub_state{stateid = {JID, Nidx},
|
2017-12-13 11:35:36 +01:00
|
|
|
nodeidx = Nidx,
|
2016-08-30 08:48:08 +02:00
|
|
|
items = itemids(Nidx, JID),
|
|
|
|
affiliation = decode_affiliation(Aff),
|
|
|
|
subscriptions = decode_subscriptions(Subs)}
|
|
|
|
end, RItems)};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
|
|
|
{result, []}
|
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_state(Nidx, JID) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
State = get_state_without_itemids(Nidx, JID),
|
|
|
|
{SJID, _} = State#pubsub_state.stateid,
|
|
|
|
State#pubsub_state{items = itemids(Nidx, SJID)}.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec get_state_without_itemids(Nidx :: mod_pubsub:nodeIdx(), Key :: ljid()) ->
|
|
|
|
mod_pubsub:pubsubState().
|
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
get_state_without_itemids(Nidx, JID) ->
|
|
|
|
J = encode_jid(JID),
|
2016-08-30 08:48:08 +02:00
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(jid)s, @(affiliation)s, @(subscriptions)s "
|
|
|
|
"from pubsub_state "
|
|
|
|
"where nodeid=%(Nidx)d and jid=%(J)s")) of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [{SJID, Aff, Subs}]} ->
|
2015-07-22 10:37:26 +02:00
|
|
|
#pubsub_state{stateid = {decode_jid(SJID), Nidx},
|
2017-03-30 14:26:30 +02:00
|
|
|
nodeidx = Nidx,
|
2016-08-30 08:48:08 +02:00
|
|
|
affiliation = decode_affiliation(Aff),
|
|
|
|
subscriptions = decode_subscriptions(Subs)};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
2017-03-30 14:26:30 +02:00
|
|
|
#pubsub_state{stateid = {JID, Nidx}, nodeidx = Nidx}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_state(State) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{_, Nidx} = State#pubsub_state.stateid,
|
|
|
|
set_state(Nidx, State).
|
|
|
|
|
|
|
|
set_state(Nidx, State) ->
|
|
|
|
{JID, _} = State#pubsub_state.stateid,
|
|
|
|
J = encode_jid(JID),
|
|
|
|
S = encode_subscriptions(State#pubsub_state.subscriptions),
|
|
|
|
A = encode_affiliation(State#pubsub_state.affiliation),
|
2016-05-24 17:44:07 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"pubsub_state",
|
|
|
|
["!nodeid=%(Nidx)d",
|
|
|
|
"!jid=%(J)s",
|
|
|
|
"affiliation=%(A)s",
|
|
|
|
"subscriptions=%(S)s"
|
|
|
|
]),
|
2015-07-22 10:37:26 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
del_state(Nidx, JID) ->
|
|
|
|
J = encode_jid(JID),
|
2016-05-24 17:44:07 +02:00
|
|
|
catch ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from pubsub_state"
|
|
|
|
" where jid=%(J)s and nodeid=%(Nidx)d")),
|
2015-07-22 10:37:26 +02:00
|
|
|
ok.
|
|
|
|
|
2017-09-27 10:51:37 +02:00
|
|
|
get_items(Nidx, _From, undefined) ->
|
|
|
|
SNidx = misc:i2l(Nidx),
|
|
|
|
case ejabberd_sql:sql_query_t(
|
|
|
|
[<<"select itemid, publisher, creation, modification, payload",
|
2017-09-27 11:12:01 +02:00
|
|
|
" from pubsub_item where nodeid='", SNidx/binary, "'",
|
|
|
|
" order by creation asc">>]) of
|
2017-09-27 10:51:37 +02:00
|
|
|
{selected, _, AllItems} ->
|
2017-12-28 11:48:54 +01:00
|
|
|
{result, {[raw_to_item(Nidx, RItem) || RItem <- AllItems], undefined}};
|
2017-09-27 10:51:37 +02:00
|
|
|
_ ->
|
|
|
|
{result, {[], undefined}}
|
|
|
|
end;
|
2016-08-30 08:48:08 +02:00
|
|
|
get_items(Nidx, _From, #rsm_set{max = Max, index = IncIndex,
|
|
|
|
'after' = After, before = Before}) ->
|
2017-09-27 10:51:37 +02:00
|
|
|
Count = case catch ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(count(itemid))d from pubsub_item"
|
|
|
|
" where nodeid=%(Nidx)d")) of
|
|
|
|
{selected, [{C}]} -> C;
|
|
|
|
_ -> 0
|
|
|
|
end,
|
|
|
|
Offset = case {IncIndex, Before, After} of
|
|
|
|
{I, undefined, undefined} when is_integer(I) -> I;
|
2016-08-30 08:48:08 +02:00
|
|
|
_ -> 0
|
2017-09-27 10:51:37 +02:00
|
|
|
end,
|
|
|
|
Limit = case Max of
|
|
|
|
undefined -> ?MAXITEMS;
|
|
|
|
_ -> Max
|
2016-08-30 08:48:08 +02:00
|
|
|
end,
|
2017-09-27 10:51:37 +02:00
|
|
|
Filters = rsm_filters(misc:i2l(Nidx), Before, After),
|
2016-03-24 13:03:19 +01:00
|
|
|
Query = fun(mssql, _) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-09-27 10:51:37 +02:00
|
|
|
[<<"select top ", (integer_to_binary(Limit))/binary,
|
|
|
|
" itemid, publisher, creation, modification, payload",
|
|
|
|
" from pubsub_item", Filters/binary>>]);
|
|
|
|
%OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
|
2016-03-24 13:03:19 +01:00
|
|
|
(_, _) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-09-27 10:51:37 +02:00
|
|
|
[<<"select itemid, publisher, creation, modification, payload",
|
|
|
|
" from pubsub_item", Filters/binary,
|
|
|
|
" limit ", (integer_to_binary(Limit))/binary,
|
|
|
|
" offset ", (integer_to_binary(Offset))/binary>>])
|
2016-03-24 13:03:19 +01:00
|
|
|
end,
|
2016-08-30 08:48:08 +02:00
|
|
|
case ejabberd_sql:sql_query_t(Query) of
|
2017-09-27 10:51:37 +02:00
|
|
|
{selected, _, []} ->
|
|
|
|
{result, {[], #rsm_set{count = Count}}};
|
2016-08-30 08:48:08 +02:00
|
|
|
{selected, [<<"itemid">>, <<"publisher">>, <<"creation">>,
|
|
|
|
<<"modification">>, <<"payload">>], RItems} ->
|
2017-09-27 10:51:37 +02:00
|
|
|
Rsm = rsm_page(Count, IncIndex, Offset, RItems),
|
|
|
|
{result, {[raw_to_item(Nidx, RItem) || RItem <- RItems], Rsm}};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{result, {[], undefined}}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(JID),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Whitelisted = node_flat:can_fetch_item(Affiliation, Subscriptions),
|
|
|
|
if %%SubId == "", ?? ->
|
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
|
|
|
|
%%InvalidSubId ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == presence) and not PresenceSubscription ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_presence_subscription_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == roster) and not RosterGroup ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_not_in_roster_group())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == whitelist) and not Whitelisted ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_allowed(), mod_pubsub:err_closed_node())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == authorize) and not Whitelisted ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
|
|
|
get_items(Nidx, JID, RSM)
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-09-27 10:51:37 +02:00
|
|
|
get_last_items(Nidx, _From, Limit) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
SNidx = misc:i2l(Nidx),
|
2016-03-24 13:03:19 +01:00
|
|
|
Query = fun(mssql, _) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-09-27 10:51:37 +02:00
|
|
|
[<<"select top ", (integer_to_binary(Limit))/binary,
|
|
|
|
" itemid, publisher, creation, modification, payload",
|
|
|
|
" from pubsub_item where nodeid='", SNidx/binary,
|
|
|
|
"' order by modification desc">>]);
|
2016-03-24 13:03:19 +01:00
|
|
|
(_, _) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-09-27 10:51:37 +02:00
|
|
|
[<<"select itemid, publisher, creation, modification, payload",
|
|
|
|
" from pubsub_item where nodeid='", SNidx/binary,
|
|
|
|
"' order by modification desc ",
|
|
|
|
" limit ", (integer_to_binary(Limit))/binary>>])
|
2016-03-24 13:03:19 +01:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query_t(Query) of
|
2017-09-27 10:51:37 +02:00
|
|
|
{selected, [<<"itemid">>, <<"publisher">>, <<"creation">>,
|
|
|
|
<<"modification">>, <<"payload">>], RItems} ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, [raw_to_item(Nidx, RItem) || RItem <- RItems]};
|
|
|
|
_ ->
|
|
|
|
{result, []}
|
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2019-07-31 12:20:38 +02:00
|
|
|
get_only_item(Nidx, _From) ->
|
|
|
|
SNidx = misc:i2l(Nidx),
|
|
|
|
Query = fun(mssql, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
[<<"select itemid, publisher, creation, modification, payload",
|
|
|
|
" from pubsub_item where nodeid='", SNidx/binary, "'">>]);
|
|
|
|
(_, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
[<<"select itemid, publisher, creation, modification, payload",
|
|
|
|
" from pubsub_item where nodeid='", SNidx/binary, "'">>])
|
|
|
|
end,
|
|
|
|
case catch ejabberd_sql:sql_query_t(Query) of
|
|
|
|
{selected, [<<"itemid">>, <<"publisher">>, <<"creation">>,
|
|
|
|
<<"modification">>, <<"payload">>], RItems} ->
|
|
|
|
{result, [raw_to_item(Nidx, RItem) || RItem <- RItems]};
|
|
|
|
_ ->
|
|
|
|
{result, []}
|
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_item(Nidx, ItemId) ->
|
2016-05-24 17:44:07 +02:00
|
|
|
case catch ejabberd_sql:sql_query_t(
|
2017-09-27 10:51:37 +02:00
|
|
|
?SQL("select @(itemid)s, @(publisher)s, @(creation)s,"
|
|
|
|
" @(modification)s, @(payload)s from pubsub_item"
|
|
|
|
" where nodeid=%(Nidx)d and itemid=%(ItemId)s"))
|
2015-07-22 10:37:26 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [RItem]} ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, raw_to_item(Nidx, RItem)};
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, []} ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_item_not_found()};
|
2016-04-05 12:09:44 +02:00
|
|
|
{'EXIT', _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
{error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
get_item(Nidx, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(JID),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Whitelisted = node_flat:can_fetch_item(Affiliation, Subscriptions),
|
|
|
|
if %%SubId == "", ?? ->
|
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
|
|
|
|
%%InvalidSubId ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == presence) and not PresenceSubscription ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_presence_subscription_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == roster) and not RosterGroup ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_authorized(),
|
|
|
|
mod_pubsub:err_not_in_roster_group())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == whitelist) and not Whitelisted ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, mod_pubsub:extended_error(
|
|
|
|
xmpp:err_not_allowed(), mod_pubsub:err_closed_node())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == authorize) and not Whitelisted ->
|
2016-08-30 08:48:08 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
|
|
|
get_item(Nidx, ItemId)
|
|
|
|
end.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_item(Item) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{ItemId, Nidx} = Item#pubsub_item.itemid,
|
|
|
|
{C, _} = Item#pubsub_item.creation,
|
|
|
|
{M, JID} = Item#pubsub_item.modification,
|
|
|
|
P = encode_jid(JID),
|
|
|
|
Payload = Item#pubsub_item.payload,
|
2016-05-24 17:44:07 +02:00
|
|
|
XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
|
2017-09-27 10:51:37 +02:00
|
|
|
SM = encode_now(M),
|
|
|
|
SC = encode_now(C),
|
2016-05-24 17:44:07 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"pubsub_item",
|
|
|
|
["!nodeid=%(Nidx)d",
|
|
|
|
"!itemid=%(ItemId)s",
|
|
|
|
"publisher=%(P)s",
|
|
|
|
"modification=%(SM)s",
|
|
|
|
"payload=%(XML)s",
|
|
|
|
"-creation=%(SC)s"
|
|
|
|
]),
|
2015-07-22 10:37:26 +02:00
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
del_item(Nidx, ItemId) ->
|
2016-05-24 17:44:07 +02:00
|
|
|
catch ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from pubsub_item where itemid=%(ItemId)s"
|
|
|
|
" and nodeid=%(Nidx)d")).
|
2009-08-27 23:38:23 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
del_items(_, []) ->
|
|
|
|
ok;
|
|
|
|
del_items(Nidx, [ItemId]) ->
|
|
|
|
del_item(Nidx, ItemId);
|
|
|
|
del_items(Nidx, ItemIds) ->
|
2019-12-16 04:52:06 +01:00
|
|
|
I = str:join([ejabberd_sql:to_string_literal_t(X) || X <- ItemIds], <<",">>),
|
2017-04-11 12:13:58 +02:00
|
|
|
SNidx = misc:i2l(Nidx),
|
2015-07-22 10:37:26 +02:00
|
|
|
catch
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t([<<"delete from pubsub_item where itemid in (">>,
|
2016-05-24 17:44:07 +02:00
|
|
|
I, <<") and nodeid='">>, SNidx, <<"';">>]).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
get_item_name(_Host, _Node, Id) ->
|
2019-07-29 16:13:16 +02:00
|
|
|
{result, Id}.
|
2009-08-27 23:38:23 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
node_to_path(Node) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
node_flat:node_to_path(Node).
|
2009-10-20 17:03:07 +02:00
|
|
|
|
|
|
|
path_to_node(Path) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
node_flat:path_to_node(Path).
|
|
|
|
|
|
|
|
|
|
|
|
first_in_list(_Pred, []) ->
|
|
|
|
false;
|
|
|
|
first_in_list(Pred, [H | T]) ->
|
|
|
|
case Pred(H) of
|
|
|
|
true -> {value, H};
|
|
|
|
_ -> first_in_list(Pred, T)
|
|
|
|
end.
|
|
|
|
|
2021-08-22 12:44:50 +02:00
|
|
|
itemids(Nidx) ->
|
|
|
|
case catch
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(itemid)s from pubsub_item where "
|
|
|
|
"nodeid=%(Nidx)d order by modification desc"))
|
|
|
|
of
|
|
|
|
{selected, RItems} ->
|
|
|
|
[ItemId || {ItemId} <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2016-05-12 17:32:13 +02:00
|
|
|
itemids(Nidx, {_U, _S, _R} = JID) ->
|
2016-07-05 16:45:37 +02:00
|
|
|
SJID = encode_jid(JID),
|
2016-10-07 09:31:03 +02:00
|
|
|
SJIDLike = <<(encode_jid_like(JID))/binary, "/%">>,
|
2015-07-22 10:37:26 +02:00
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(itemid)s from pubsub_item where "
|
2016-07-05 16:45:37 +02:00
|
|
|
"nodeid=%(Nidx)d and (publisher=%(SJID)s"
|
2020-01-13 01:36:52 +01:00
|
|
|
" or publisher like %(SJIDLike)s %ESCAPE) "
|
2016-05-24 17:44:07 +02:00
|
|
|
"order by modification desc"))
|
2015-07-22 10:37:26 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, RItems} ->
|
|
|
|
[ItemId || {ItemId} <- RItems];
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
select_affiliation_subscriptions(Nidx, JID) ->
|
|
|
|
J = encode_jid(JID),
|
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(affiliation)s, @(subscriptions)s from "
|
|
|
|
" pubsub_state where nodeid=%(Nidx)d and jid=%(J)s"))
|
2015-07-22 10:37:26 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [{A, S}]} ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{decode_affiliation(A), decode_subscriptions(S)};
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
|
|
|
end.
|
|
|
|
|
|
|
|
select_affiliation_subscriptions(Nidx, JID, JID) ->
|
|
|
|
select_affiliation_subscriptions(Nidx, JID);
|
|
|
|
select_affiliation_subscriptions(Nidx, GenKey, SubKey) ->
|
2020-01-31 11:43:29 +01:00
|
|
|
GJ = encode_jid(GenKey),
|
|
|
|
SJ = encode_jid(SubKey),
|
2020-03-18 14:04:40 +01:00
|
|
|
case catch ejabberd_sql:sql_query_t(
|
2020-05-15 09:19:55 +02:00
|
|
|
?SQL("select @(jid)s, @(affiliation)s, @(subscriptions)s from "
|
2020-03-18 14:04:40 +01:00
|
|
|
" pubsub_state where nodeid=%(Nidx)d and jid in (%(GJ)s, %(SJ)s)"))
|
2020-01-31 11:43:29 +01:00
|
|
|
of
|
|
|
|
{selected, Res} ->
|
|
|
|
lists:foldr(
|
2020-05-15 09:19:55 +02:00
|
|
|
fun({Jid, A, S}, {_, Subs}) when Jid == GJ ->
|
2020-01-31 11:43:29 +01:00
|
|
|
{decode_affiliation(A), Subs ++ decode_subscriptions(S)};
|
|
|
|
({_, _, S}, {Aff, Subs}) ->
|
|
|
|
{Aff, Subs ++ decode_subscriptions(S)}
|
|
|
|
end, {none, []}, Res);
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
|
|
|
end.
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
update_affiliation(Nidx, JID, Affiliation) ->
|
|
|
|
J = encode_jid(JID),
|
|
|
|
A = encode_affiliation(Affiliation),
|
2016-05-24 17:44:07 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"pubsub_state",
|
|
|
|
["!nodeid=%(Nidx)d",
|
|
|
|
"!jid=%(J)s",
|
|
|
|
"affiliation=%(A)s",
|
|
|
|
"-subscriptions=''"
|
|
|
|
]).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
update_subscription(Nidx, JID, Subscription) ->
|
|
|
|
J = encode_jid(JID),
|
|
|
|
S = encode_subscriptions(Subscription),
|
2016-05-24 17:44:07 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"pubsub_state",
|
|
|
|
["!nodeid=%(Nidx)d",
|
|
|
|
"!jid=%(J)s",
|
|
|
|
"subscriptions=%(S)s",
|
|
|
|
"-affiliation='n'"
|
|
|
|
]).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
2021-08-21 20:02:58 +02:00
|
|
|
-spec maybe_remove_extra_items(mod_pubsub:nodeIdx(),
|
|
|
|
non_neg_integer() | unlimited, ljid(),
|
|
|
|
mod_pubsub:itemId()) -> [mod_pubsub:itemId()].
|
|
|
|
maybe_remove_extra_items(_Nidx, unlimited, _GenKey, _ItemId) ->
|
|
|
|
[];
|
|
|
|
maybe_remove_extra_items(Nidx, MaxItems, GenKey, ItemId) ->
|
|
|
|
ItemIds = [ItemId | itemids(Nidx, GenKey)],
|
|
|
|
{result, {_NewIds, OldIds}} = remove_extra_items(Nidx, MaxItems, ItemIds),
|
|
|
|
OldIds.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec decode_jid(SJID :: binary()) -> ljid().
|
2015-07-22 10:37:26 +02:00
|
|
|
decode_jid(SJID) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:tolower(jid:decode(SJID)).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec decode_affiliation(Arg :: binary()) -> atom().
|
2015-07-22 10:37:26 +02:00
|
|
|
decode_affiliation(<<"o">>) -> owner;
|
|
|
|
decode_affiliation(<<"p">>) -> publisher;
|
|
|
|
decode_affiliation(<<"u">>) -> publish_only;
|
|
|
|
decode_affiliation(<<"m">>) -> member;
|
|
|
|
decode_affiliation(<<"c">>) -> outcast;
|
|
|
|
decode_affiliation(_) -> none.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec decode_subscription(Arg :: binary()) -> atom().
|
2015-07-22 10:37:26 +02:00
|
|
|
decode_subscription(<<"s">>) -> subscribed;
|
|
|
|
decode_subscription(<<"p">>) -> pending;
|
|
|
|
decode_subscription(<<"u">>) -> unconfigured;
|
|
|
|
decode_subscription(_) -> none.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec decode_subscriptions(Subscriptions :: binary()) -> [] | [{atom(), binary()},...].
|
2015-07-22 10:37:26 +02:00
|
|
|
decode_subscriptions(Subscriptions) ->
|
|
|
|
lists:foldl(fun (Subscription, Acc) ->
|
|
|
|
case str:tokens(Subscription, <<":">>) of
|
|
|
|
[S, SubId] -> [{decode_subscription(S), SubId} | Acc];
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], str:tokens(Subscriptions, <<",">>)).
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_jid(JID :: ljid()) -> binary().
|
2015-07-22 10:37:26 +02:00
|
|
|
encode_jid(JID) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:encode(JID).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_jid_like(JID :: ljid()) -> binary().
|
2016-05-12 17:32:13 +02:00
|
|
|
encode_jid_like(JID) ->
|
2020-01-13 01:36:52 +01:00
|
|
|
ejabberd_sql:escape_like_arg(jid:encode(JID)).
|
2016-05-12 17:32:13 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_host(Host :: host()) -> binary().
|
2015-07-22 10:37:26 +02:00
|
|
|
encode_host({_U, _S, _R} = LJID) -> encode_jid(LJID);
|
2016-05-24 17:44:07 +02:00
|
|
|
encode_host(Host) -> Host.
|
2015-07-22 10:37:26 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_host_like(Host :: host()) -> binary().
|
2020-01-13 01:36:52 +01:00
|
|
|
encode_host_like({_U, _S, _R} = LJID) -> encode_jid_like(LJID);
|
2016-05-12 17:32:13 +02:00
|
|
|
encode_host_like(Host) ->
|
2020-01-13 01:36:52 +01:00
|
|
|
ejabberd_sql:escape_like_arg(Host).
|
2016-05-12 17:32:13 +02:00
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_affiliation(Arg :: atom()) -> binary().
|
2015-07-22 10:37:26 +02:00
|
|
|
encode_affiliation(owner) -> <<"o">>;
|
|
|
|
encode_affiliation(publisher) -> <<"p">>;
|
|
|
|
encode_affiliation(publish_only) -> <<"u">>;
|
|
|
|
encode_affiliation(member) -> <<"m">>;
|
|
|
|
encode_affiliation(outcast) -> <<"c">>;
|
|
|
|
encode_affiliation(_) -> <<"n">>.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_subscription(Arg :: atom()) -> binary().
|
2015-07-22 10:37:26 +02:00
|
|
|
encode_subscription(subscribed) -> <<"s">>;
|
|
|
|
encode_subscription(pending) -> <<"p">>;
|
|
|
|
encode_subscription(unconfigured) -> <<"u">>;
|
|
|
|
encode_subscription(_) -> <<"n">>.
|
|
|
|
|
2016-07-01 21:18:55 +02:00
|
|
|
-spec encode_subscriptions(Subscriptions :: [] | [{atom(), binary()},...]) -> binary().
|
2015-07-22 10:37:26 +02:00
|
|
|
encode_subscriptions(Subscriptions) ->
|
|
|
|
str:join([<<(encode_subscription(S))/binary, ":", SubId/binary>>
|
|
|
|
|| {S, SubId} <- Subscriptions], <<",">>).
|
|
|
|
|
|
|
|
%%% record getter/setter
|
|
|
|
|
|
|
|
raw_to_item(Nidx, [ItemId, SJID, Creation, Modification, XML]) ->
|
2016-05-24 17:44:07 +02:00
|
|
|
raw_to_item(Nidx, {ItemId, SJID, Creation, Modification, XML});
|
|
|
|
raw_to_item(Nidx, {ItemId, SJID, Creation, Modification, XML}) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
JID = decode_jid(SJID),
|
2016-02-03 19:03:17 +01:00
|
|
|
Payload = case fxml_stream:parse_element(XML) of
|
2015-07-22 10:37:26 +02:00
|
|
|
{error, _Reason} -> [];
|
|
|
|
El -> [El]
|
|
|
|
end,
|
|
|
|
#pubsub_item{itemid = {ItemId, Nidx},
|
2017-12-13 11:35:36 +01:00
|
|
|
nodeidx = Nidx,
|
2017-09-27 10:51:37 +02:00
|
|
|
creation = {decode_now(Creation), jid:remove_resource(JID)},
|
|
|
|
modification = {decode_now(Modification), JID},
|
2015-07-22 10:37:26 +02:00
|
|
|
payload = Payload}.
|
2017-09-27 10:51:37 +02:00
|
|
|
|
|
|
|
rsm_filters(SNidx, undefined, undefined) ->
|
|
|
|
<<" where nodeid='", SNidx/binary, "'",
|
|
|
|
" order by creation asc">>;
|
|
|
|
rsm_filters(SNidx, undefined, After) ->
|
|
|
|
<<" where nodeid='", SNidx/binary, "'",
|
|
|
|
" and creation>'", (encode_stamp(After))/binary, "'",
|
|
|
|
" order by creation asc">>;
|
|
|
|
rsm_filters(SNidx, <<>>, undefined) ->
|
|
|
|
%% 2.5 Requesting the Last Page in a Result Set
|
|
|
|
<<" where nodeid='", SNidx/binary, "'",
|
|
|
|
" order by creation desc">>;
|
|
|
|
rsm_filters(SNidx, Before, undefined) ->
|
|
|
|
<<" where nodeid='", SNidx/binary, "'",
|
|
|
|
" and creation<'", (encode_stamp(Before))/binary, "'",
|
|
|
|
" order by creation desc">>.
|
|
|
|
|
|
|
|
rsm_page(Count, Index, Offset, Items) ->
|
|
|
|
First = decode_stamp(lists:nth(3, hd(Items))),
|
|
|
|
Last = decode_stamp(lists:nth(3, lists:last(Items))),
|
|
|
|
#rsm_set{count = Count, index = Index,
|
|
|
|
first = #rsm_first{index = Offset, data = First},
|
|
|
|
last = Last}.
|
|
|
|
|
|
|
|
encode_stamp(Stamp) ->
|
2021-08-23 21:28:15 +02:00
|
|
|
try xmpp_util:decode_timestamp(Stamp) of
|
|
|
|
Now ->
|
|
|
|
encode_now(Now)
|
|
|
|
catch _:{bad_timestamp, _} ->
|
|
|
|
Stamp % We should return a proper error to the client instead.
|
2017-09-27 10:51:37 +02:00
|
|
|
end.
|
|
|
|
decode_stamp(Stamp) ->
|
2021-08-23 21:28:15 +02:00
|
|
|
xmpp_util:encode_timestamp(decode_now(Stamp)).
|
2017-09-27 10:51:37 +02:00
|
|
|
|
|
|
|
encode_now({T1, T2, T3}) ->
|
|
|
|
<<(misc:i2l(T1, 6))/binary, ":",
|
|
|
|
(misc:i2l(T2, 6))/binary, ":",
|
|
|
|
(misc:i2l(T3, 6))/binary>>.
|
|
|
|
decode_now(NowStr) ->
|
|
|
|
[MS, S, US] = binary:split(NowStr, <<":">>, [global]),
|
|
|
|
{binary_to_integer(MS), binary_to_integer(S), binary_to_integer(US)}.
|