Skip to content

Commit

Permalink
Split mod_push_plugin into two modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rslota committed Feb 17, 2017
1 parent dfda063 commit 76f5a03
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
43 changes: 2 additions & 41 deletions apps/ejabberd/src/mod_push_plugin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
%%% @end
%%%-------------------------------------------------------------------
%%% @doc
%%% Default plugin module for mod_push. This module allows for some dynamic customizations.
%%% Plugin behaviour module for mod_push. This module defines API for some dynamic customizations.
%%% @end
%%%-------------------------------------------------------------------
-module(mod_push_plugin).
-behavior(mod_push_plugin).
-author('rafal.slota@erlang-solutions.com').

-include_lib("ejabberd/include/jlib.hrl").
Expand All @@ -19,8 +18,6 @@
%% API
-export([should_publish/4, sender_id/3]).

%% Callback API
-export([should_publish/3, sender_id/2]).

-callback should_publish(From :: ejabberd:jid(), To :: ejabberd:jid(), Packet :: jlib:xmlel()) ->
boolean().
Expand All @@ -42,46 +39,10 @@ sender_id(Host, From, Packet) ->
PluginModule = plugin_module(Host),
PluginModule:sender_id(From, Packet).

%%--------------------------------------------------------------------
%% Callbacks
%%--------------------------------------------------------------------

%% Callback 'should_publish'
-spec should_publish(From :: ejabberd:jid(), To :: ejabberd:jid(), Packet :: jlib:xmlel()) ->
boolean().
should_publish(_From, To = #jid{luser = LUser, lserver = LServer}, _Packet) ->
try ejabberd_users:does_user_exist(LUser, LServer) of
false ->
false;
true ->
is_offline(To)
catch
_:_ ->
is_offline(To)
end.

is_offline(#jid{luser = LUser, lserver = LServer}) ->
case catch lists:max(ejabberd_sm:get_user_present_pids(LUser, LServer)) of
{Priority, _} when is_integer(Priority), Priority >= 0 ->
false;
_ ->
true
end.

%% Callback 'sender_id'
-spec sender_id(From :: ejabberd:jid(), Packet :: jlib:xmlel()) -> SenderId :: binary().
sender_id(From, Packet) ->
case exml_query:attr(Packet, <<"type">>) of
<<"chat">> ->
jid:to_binary(jid:to_bare(jid:to_lower(From)));
<<"groupchat">> ->
jid:to_binary(jid:to_lower(From))
end.

%%--------------------------------------------------------------------
%% Helper functions
%%--------------------------------------------------------------------

-spec plugin_module(Host :: ejabberd:server()) -> Module :: atom().
plugin_module(Host) ->
gen_mod:get_module_opt(Host, mod_push, plugin_module, ?MODULE).
gen_mod:get_module_opt(Host, mod_push, plugin_module, mod_push_plugin_default).
56 changes: 56 additions & 0 deletions apps/ejabberd/src/mod_push_plugin_default.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
%%%-------------------------------------------------------------------
%%% @author Rafal Slota
%%% @copyright (C) 2017 Erlang Solutions Ltd.
%%% This software is released under the Apache License, Version 2.0
%%% cited in 'LICENSE.txt'.
%%% @end
%%%-------------------------------------------------------------------
%%% @doc
%%% Default plugin module for mod_push. This module allows for some dynamic customizations.
%%% @end
%%%-------------------------------------------------------------------
-module(mod_push_plugin_default).
-behavior(mod_push_plugin).
-author('rafal.slota@erlang-solutions.com').

-include_lib("ejabberd/include/jlib.hrl").
-include_lib("ejabberd/include/ejabberd.hrl").

%% Callback API
-export([should_publish/3, sender_id/2]).

%%--------------------------------------------------------------------
%% Callbacks
%%--------------------------------------------------------------------

%% Callback 'should_publish'
-spec should_publish(From :: ejabberd:jid(), To :: ejabberd:jid(), Packet :: jlib:xmlel()) ->
boolean().
should_publish(_From, To = #jid{luser = LUser, lserver = LServer}, _Packet) ->
try ejabberd_users:does_user_exist(LUser, LServer) of
false ->
false;
true ->
is_offline(To)
catch
_:_ ->
is_offline(To)
end.

is_offline(#jid{luser = LUser, lserver = LServer}) ->
case catch lists:max(ejabberd_sm:get_user_present_pids(LUser, LServer)) of
{Priority, _} when is_integer(Priority), Priority >= 0 ->
false;
_ ->
true
end.

%% Callback 'sender_id'
-spec sender_id(From :: ejabberd:jid(), Packet :: jlib:xmlel()) -> SenderId :: binary().
sender_id(From, Packet) ->
case exml_query:attr(Packet, <<"type">>) of
<<"chat">> ->
jid:to_binary(jid:to_bare(jid:to_lower(From)));
<<"groupchat">> ->
jid:to_binary(jid:to_lower(From))
end.
6 changes: 3 additions & 3 deletions doc/modules/mod_push.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ This module implements [XEP-0357: Push Notifications](https://xmpp.org/extension

* **backend** (atom, default: `mnesia`) - Backend to use for storing registrations. Currently
only `mnesia` may be used.
* **wpool** (list, default: []) - List of options that will be passed to `worker_pool` library
* **wpool** (list, default: `[]`) - List of options that will be passed to `worker_pool` library
that handles all the requests. Please refer to [Project Site](https://github.com/inaka/worker_pool) for more details.
* **plugin_module** (atom, default: mod_push_plugin) - Plugin module that implements some dynamic
* **plugin_module** (atom, default: `mod_push_plugin_default`) - Plugin module that implements some dynamic
configurations. Currently this module allow configuration of parsing message `sender id` and can
filter messages that shall not be published to `PubSub` node.

Expand All @@ -25,6 +25,6 @@ that handles all the requests. Please refer to [Project Site](https://github.com
{mod_push, [
{backend, mnesia},
{wpool, [{workers, 200}]},
{plugin_module, mod_push_plugin}
{plugin_module, mod_push_plugin_default}
]}.
```

0 comments on commit 76f5a03

Please sign in to comment.