read_sub(Subscriber, Node, NodeID, SubID, Lang) ->
case pubsub_subscription:get_subscription(Subscriber, NodeID, SubID) of
- {error, notfound} ->
- {error, extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
{result, #pubsub_subscription{options = Options}} ->
{result, XdataEl} = pubsub_subscription:get_options_xform(Lang, Options),
OptionsEl = {xmlelement, "options", [{"jid", jlib:jid_to_string(Subscriber)},
- {"subid", SubID}|nodeAttr(Node)],
- [XdataEl]},
+ {"subid", SubID}|nodeAttr(Node)],
+ [XdataEl]},
+ PubsubEl = {xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}], [OptionsEl]},
+ {result, PubsubEl};
+ _ -> OptionsEl = {xmlelement, "options", [{"jid", jlib:jid_to_string(Subscriber)},
+ {"subid", SubID}|nodeAttr(Node)]},
PubsubEl = {xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}], [OptionsEl]},
{result, PubsubEl}
end.
write_sub(_Subscriber, _NodeID, _SubID, invalid) ->
{error, extended_error(?ERR_BAD_REQUEST, "invalid-options")};
+write_sub(_Subscriber, _NodeID, _SubID, []) ->
+ {result, []};
write_sub(Subscriber, NodeID, SubID, Options) ->
case pubsub_subscription:set_subscription(Subscriber, NodeID, SubID, Options) of
- {error, notfound} ->
- {error, extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
{result, _} ->
- {result, []}
+ {result, []};
+ {error, _} ->
+ {error, extended_error('not-acceptable', "invalid-subid")}
end.
%% @spec (Host, Node, JID, Plugins) -> {error, Reason} | {result, Response}
pubsub_subscription:init(),
mnesia:create_table(pubsub_state,
[{disc_copies, [node()]},
+ {index, [nodeidx]},
{attributes, record_info(fields, pubsub_state)}]),
mnesia:create_table(pubsub_item,
[{disc_only_copies, [node()]},
+ {index, [nodeidx]},
{attributes, record_info(fields, pubsub_item)}]),
ItemsFields = record_info(fields, pubsub_item),
case mnesia:table_info(pubsub_item, attributes) of
%% @doc <p></p>
create_node(NodeIdx, Owner) ->
OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- set_state(#pubsub_state{stateid = {OwnerKey, NodeIdx}, affiliation = owner}),
+ set_state(#pubsub_state{stateid = {OwnerKey, NodeIdx}, nodeidx = NodeIdx, affiliation = owner}),
{result, {default, broadcast}}.
%% @spec (Nodes) -> {result, {default, broadcast, Reply}}
payload = Payload};
_ ->
#pubsub_item{itemid = {ItemId, NodeIdx},
+ nodeidx = NodeIdx,
creation = {Now, GenKey},
modification = PubId,
payload = Payload}
%% ```get_states(NodeIdx) ->
%% node_default:get_states(NodeIdx).'''</p>
get_states(NodeIdx) ->
- States = case catch mnesia:match_object(
- #pubsub_state{stateid = {'_', NodeIdx}, _ = '_'}) of
+ States = case catch mnesia:index_read(
+ pubsub_state, NodeIdx, #pubsub_state.nodeidx) of
List when is_list(List) -> List;
_ -> []
end,
get_state(NodeIdx, JID) ->
StateId = {JID, NodeIdx},
case catch mnesia:read({pubsub_state, StateId}) of
- [State] when is_record(State, pubsub_state) -> State;
- _ -> #pubsub_state{stateid=StateId}
+ [#pubsub_state{} = State] -> State;
+ _ -> #pubsub_state{stateid=StateId, nodeidx=NodeIdx}
end.
%% @spec (State) -> ok | {error, Reason}
%% ```get_items(NodeIdx, From) ->
%% node_default:get_items(NodeIdx, From).'''</p>
get_items(NodeIdx, _From) ->
- Items = mnesia:match_object(#pubsub_item{itemid = {'_', NodeIdx}, _ = '_'}),
+ Items = mnesia:index_read(pubsub_item, NodeIdx, #pubsub_item.nodeidx),
{result, lists:reverse(lists:keysort(#pubsub_item.modification, Items))}.
get_items(NodeIdx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
options = []
}).
-%% @type pubsubState() = {pubsub_state, StateId, Items, Affiliation, Subscriptions}
+%% @type pubsubState() = {pubsub_state, StateId, NodeIdx, Items, Affiliation, Subscriptions}
%% StateId = {ljid(), nodeIdx()}
+%% NodeIdx = nodeIdx(),
%% Items = [itemId()]
%% Affiliation = affiliation()
%% Subscriptions = [{subscription(), subId()}].
-record(pubsub_state,
{
stateid,
+ nodeidx,
items = [],
affiliation = 'none',
subscriptions = []
-record(pubsub_item,
{
itemid,
+ nodeidx,
creation = {'unknown','unknown'},
modification = {'unknown','unknown'},
payload = []
Other -> Other
end.
+add_subscription(_JID, _NodeID, []) ->
+ make_subid();
add_subscription(_JID, _NodeID, Options) ->
SubID = make_subid(),
mnesia:write(#pubsub_subscription{subid = SubID, options = Options}),
_ -> {error, notfound}
end.
-write_subscription(JID, NodeID, SubID, Options) ->
- case read_subscription(JID, NodeID, SubID) of
- {error, notfound} -> {error, notfound};
- Sub -> mnesia:write(Sub#pubsub_subscription{options = Options})
- end.
+write_subscription(_JID, _NodeID, SubID, Options) ->
+ mnesia:write(#pubsub_subscription{subid = SubID, options = Options}).
make_subid() ->
{T1, T2, T3} = now(),