1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2016 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
|
17 |
|
-module(mod_event_pusher_hook_translator). |
18 |
|
-author('konrad.zemek@erlang-solutions.com'). |
19 |
|
|
20 |
|
-include("jlib.hrl"). |
21 |
|
-include("mod_event_pusher_events.hrl"). |
22 |
|
|
23 |
|
-export([add_hooks/1, delete_hooks/1]). |
24 |
|
|
25 |
|
-export([user_send_message/3, |
26 |
|
filter_local_packet/3, |
27 |
|
user_present/3, |
28 |
|
user_not_present/3, |
29 |
|
unacknowledged_message/3]). |
30 |
|
|
31 |
|
%%-------------------------------------------------------------------- |
32 |
|
%% gen_mod API |
33 |
|
%%-------------------------------------------------------------------- |
34 |
|
|
35 |
|
-spec add_hooks(mongooseim:host_type()) -> ok. |
36 |
|
add_hooks(HostType) -> |
37 |
35 |
gen_hook:add_handlers(hooks(HostType)). |
38 |
|
|
39 |
|
-spec delete_hooks(mongooseim:host_type()) -> ok. |
40 |
|
delete_hooks(HostType) -> |
41 |
35 |
gen_hook:delete_handlers(hooks(HostType)). |
42 |
|
|
43 |
|
%%-------------------------------------------------------------------- |
44 |
|
%% Hook callbacks |
45 |
|
%%-------------------------------------------------------------------- |
46 |
|
-spec filter_local_packet(Acc, Args, Extra) -> {ok, Acc} when |
47 |
|
Acc :: mongoose_hooks:filter_packet_acc(), |
48 |
|
Args :: map(), |
49 |
|
Extra :: gen_hook:extra(). |
50 |
|
filter_local_packet({From, To, Acc0, Packet}, _, _) -> |
51 |
2231 |
Acc = case chat_type(Acc0) of |
52 |
1465 |
false -> Acc0; |
53 |
766 |
Type -> push_chat_event(Acc0, Type, {From, To, Packet}, out) |
54 |
|
end, |
55 |
2231 |
{ok, {From, To, Acc, Packet}}. |
56 |
|
|
57 |
|
-spec user_send_message(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) -> |
58 |
|
mongoose_c2s_hooks:result(). |
59 |
|
user_send_message(Acc, _, _) -> |
60 |
172 |
Packet = mongoose_acc:packet(Acc), |
61 |
172 |
ChatType = chat_type(Acc), |
62 |
172 |
ResultAcc = if |
63 |
:-( |
Packet == undefined -> Acc; |
64 |
:-( |
ChatType == false -> Acc; |
65 |
172 |
true -> push_chat_event(Acc, ChatType, Packet, in) |
66 |
|
end, |
67 |
172 |
{ok, ResultAcc}. |
68 |
|
|
69 |
|
-spec user_present(Acc, Args, Extra) -> {ok, Acc} when |
70 |
|
Acc :: mongoose_acc:t(), |
71 |
|
Args :: #{jid := jid:jid()}, |
72 |
|
Extra :: gen_hook:extra(). |
73 |
|
user_present(Acc, #{jid := UserJID = #jid{}}, _) -> |
74 |
291 |
Event = #user_status_event{jid = UserJID, status = online}, |
75 |
291 |
NewAcc = mod_event_pusher:push_event(Acc, Event), |
76 |
291 |
{ok, merge_acc(Acc, NewAcc)}. |
77 |
|
|
78 |
|
-spec user_not_present(Acc, Args, Extra) -> {ok, Acc} when |
79 |
|
Acc :: mongoose_acc:t(), |
80 |
|
Args :: #{jid := jid:jid()}, |
81 |
|
Extra :: gen_hook:extra(). |
82 |
|
user_not_present(Acc, #{jid := UserJID}, _) -> |
83 |
285 |
Event = #user_status_event{jid = UserJID, status = offline}, |
84 |
285 |
NewAcc = mod_event_pusher:push_event(Acc, Event), |
85 |
285 |
{ok, merge_acc(Acc, NewAcc)}. |
86 |
|
|
87 |
|
-spec unacknowledged_message(Acc, Args, Extra) -> {ok, Acc} when |
88 |
|
Acc :: mongoose_acc:t(), |
89 |
|
Args :: #{jid := jid:jid()}, |
90 |
|
Extra :: gen_hook:extra(). |
91 |
|
unacknowledged_message(Acc, #{jid := Jid}, _) -> |
92 |
10 |
Event = #unack_msg_event{to = Jid}, |
93 |
10 |
NewAcc = mod_event_pusher:push_event(Acc, Event), |
94 |
10 |
{ok, merge_acc(Acc, NewAcc)}. |
95 |
|
|
96 |
|
%%-------------------------------------------------------------------- |
97 |
|
%% Helpers |
98 |
|
%%-------------------------------------------------------------------- |
99 |
|
|
100 |
|
-spec push_chat_event(Acc, Type, {From, To, Packet}, Direction) -> Acc when |
101 |
|
Acc :: mongoose_acc:t(), |
102 |
|
Type :: chat | groupchat | headline | normal | false, |
103 |
|
From :: jid:jid(), |
104 |
|
To :: jid:jid(), |
105 |
|
Packet :: exml:element(), |
106 |
|
Direction :: in | out. |
107 |
|
push_chat_event(Acc, Type, {From, To, Packet}, Direction) -> |
108 |
938 |
Event = #chat_event{type = Type, direction = Direction, |
109 |
|
from = From, to = To, packet = Packet}, |
110 |
938 |
NewAcc = mod_event_pusher:push_event(Acc, Event), |
111 |
938 |
merge_acc(Acc, NewAcc). |
112 |
|
|
113 |
|
-spec chat_type(mongoose_acc:t()) -> chat | groupchat | headline | normal | false. |
114 |
|
chat_type(Acc) -> |
115 |
2403 |
case mongoose_acc:stanza_type(Acc) of |
116 |
249 |
<<"chat">> -> chat; |
117 |
350 |
<<"groupchat">> -> groupchat; |
118 |
:-( |
<<"headline">> -> headline; |
119 |
:-( |
<<"normal">> -> normal; |
120 |
339 |
undefined -> normal; |
121 |
1465 |
_ -> false |
122 |
|
end. |
123 |
|
|
124 |
|
-spec merge_acc(mongoose_acc:t(), mongoose_acc:t()) -> mongoose_acc:t(). |
125 |
|
merge_acc(Acc, EventPusherAcc) -> |
126 |
1524 |
NS = mongoose_acc:get(event_pusher, EventPusherAcc), |
127 |
1524 |
mongoose_acc:set_permanent(event_pusher, NS, Acc). |
128 |
|
|
129 |
|
-spec hooks(mongooseim:host_type()) -> [gen_hook:hook_tuple()]. |
130 |
|
hooks(HostType) -> |
131 |
70 |
[ |
132 |
|
{filter_local_packet, HostType, fun ?MODULE:filter_local_packet/3, #{}, 80}, |
133 |
|
{unset_presence_hook, HostType, fun ?MODULE:user_not_present/3, #{}, 90}, |
134 |
|
{user_available_hook, HostType, fun ?MODULE:user_present/3, #{}, 90}, |
135 |
|
{user_send_message, HostType, fun ?MODULE:user_send_message/3, #{}, 90}, |
136 |
|
{unacknowledged_message, HostType, fun ?MODULE:unacknowledged_message/3, #{}, 90} |
137 |
|
]. |