1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% @author Dominik Stanaszek dominik.stanaszek@erlang-solutions.com |
3 |
|
%%% @copyright (C) 2018, Erlang-Solutions |
4 |
|
%%% @doc |
5 |
|
%%% |
6 |
|
%%% @end |
7 |
|
%%% Created : 6.07.2018 |
8 |
|
%%%------------------------------------------------------------------- |
9 |
|
-module(mod_inbox_muc). |
10 |
|
-author("dominik.stanaszek@erlang-solutions.com"). |
11 |
|
-include("jlib.hrl"). |
12 |
|
-include("mongoose.hrl"). |
13 |
|
|
14 |
|
-export([update_inbox_for_muc/1, start/1, stop/1]). |
15 |
|
|
16 |
|
-ignore_xref([update_inbox_for_muc/1]). |
17 |
|
|
18 |
|
%% User jid example is "alice@localhost" |
19 |
|
-type user_jid() :: jid:jid(). |
20 |
|
-type receiver_bare_user_jid() :: user_jid(). |
21 |
|
-type room_bare_jid() :: jid:jid(). |
22 |
|
-type packet() :: exml:element(). |
23 |
|
|
24 |
|
start(HostType) -> |
25 |
3 |
ejabberd_hooks:add(update_inbox_for_muc, HostType, ?MODULE, update_inbox_for_muc, 90), |
26 |
|
% TODO check ooptions: if system messages stored -> |
27 |
|
% add hook handler for system messages on hook ie. invitation_sent |
28 |
3 |
ok. |
29 |
|
|
30 |
|
stop(HostType) -> |
31 |
25 |
ejabberd_hooks:delete(update_inbox_for_muc, HostType, ?MODULE, update_inbox_for_muc, 90), |
32 |
25 |
ok. |
33 |
|
|
34 |
|
|
35 |
|
-spec update_inbox_for_muc(Acc) -> Acc when |
36 |
|
Acc :: mod_muc_room:update_inbox_for_muc_payload(). |
37 |
|
update_inbox_for_muc( |
38 |
|
#{host_type := HostType, |
39 |
|
room_jid := Room, |
40 |
|
from_jid := From, |
41 |
|
from_room_jid := FromRoomJid, |
42 |
|
packet := Packet, |
43 |
|
affiliations_map := AffsMap} = Acc) -> |
44 |
10 |
F = fun(AffLJID, Affiliation) -> |
45 |
28 |
case is_allowed_affiliation(Affiliation) of |
46 |
|
true -> |
47 |
28 |
To = jid:to_bare(jid:make(AffLJID)), |
48 |
|
%% Guess direction based on user JIDs |
49 |
28 |
Direction = direction(From, To), |
50 |
28 |
Packet2 = jlib:replace_from_to(FromRoomJid, To, Packet), |
51 |
28 |
update_inbox_for_user(HostType, Direction, Room, To, Packet2); |
52 |
|
false -> |
53 |
:-( |
ok |
54 |
|
end |
55 |
|
end, |
56 |
10 |
mongoose_lib:maps_foreach(F, AffsMap), |
57 |
10 |
Acc. |
58 |
|
|
59 |
|
-spec is_allowed_affiliation(mod_muc:affiliation()) -> boolean(). |
60 |
:-( |
is_allowed_affiliation(outcast) -> false; |
61 |
28 |
is_allowed_affiliation(_) -> true. |
62 |
|
|
63 |
|
-spec update_inbox_for_user(HostType, Direction, Room, To, Packet) -> term() when |
64 |
|
HostType :: mongooseim:host_type(), |
65 |
|
Direction :: incoming | outgoing, |
66 |
|
Room :: room_bare_jid(), |
67 |
|
To :: receiver_bare_user_jid(), |
68 |
|
Packet :: packet(). |
69 |
|
update_inbox_for_user(HostType, Direction, Room, To, Packet) -> |
70 |
28 |
ReceiverDomain = To#jid.lserver, |
71 |
28 |
MucDomain = mod_muc:server_host_to_muc_host(HostType, ReceiverDomain), |
72 |
28 |
case Room#jid.lserver of |
73 |
|
MucDomain -> |
74 |
28 |
handle_message(HostType, Room, To, Packet, Direction); |
75 |
|
_ -> |
76 |
|
%% We ignore inbox for users on the remote (s2s) hosts |
77 |
|
%% We ignore inbox for components (also known as services or bots) |
78 |
:-( |
ok |
79 |
|
end. |
80 |
|
|
81 |
|
handle_message(HostType, Room, To, Packet, outgoing) -> |
82 |
10 |
handle_outgoing_message(HostType, Room, To, Packet); |
83 |
|
handle_message(HostType, Room, To, Packet, incoming) -> |
84 |
18 |
handle_incoming_message(HostType, Room, To, Packet). |
85 |
|
|
86 |
|
-spec direction(From :: user_jid(), To :: user_jid()) -> incoming | outgoing. |
87 |
|
direction(From, To) -> |
88 |
28 |
case jid:are_bare_equal(From, To) of |
89 |
10 |
true -> outgoing; |
90 |
18 |
false -> incoming |
91 |
|
end. |
92 |
|
|
93 |
|
%% Sender and receiver is the same user |
94 |
|
-spec handle_outgoing_message(HostType, Room, To, Packet) -> term() when |
95 |
|
HostType :: mongooseim:host_type(), |
96 |
|
Room :: room_bare_jid(), |
97 |
|
To :: receiver_bare_user_jid(), |
98 |
|
Packet :: packet(). |
99 |
|
handle_outgoing_message(HostType, Room, To, Packet) -> |
100 |
10 |
maybe_reset_unread_count(HostType, To, Room, Packet), |
101 |
10 |
Acc = mongoose_acc:new(#{location => ?LOCATION, lserver => To#jid.lserver, host_type => HostType}), |
102 |
10 |
maybe_write_to_inbox(HostType, To, Room, Packet, Acc, fun write_to_sender_inbox/5). |
103 |
|
|
104 |
|
-spec handle_incoming_message(HostType, Room, To, Packet) -> term() when |
105 |
|
HostType :: mongooseim:host_type(), |
106 |
|
Room :: room_bare_jid(), |
107 |
|
To :: receiver_bare_user_jid(), |
108 |
|
Packet :: packet(). |
109 |
|
handle_incoming_message(HostType, Room, To, Packet) -> |
110 |
18 |
Acc = mongoose_acc:new(#{location => ?LOCATION, lserver => To#jid.lserver, host_type => HostType}), |
111 |
18 |
maybe_write_to_inbox(HostType, Room, To, Packet, Acc, fun write_to_receiver_inbox/5). |
112 |
|
|
113 |
|
maybe_reset_unread_count(HostType, User, Room, Packet) -> |
114 |
10 |
mod_inbox_utils:maybe_reset_unread_count(HostType, User, Room, Packet). |
115 |
|
|
116 |
|
maybe_write_to_inbox(HostType, User, Remote, Packet, Acc, WriteF) -> |
117 |
28 |
mod_inbox_utils:maybe_write_to_inbox(HostType, User, Remote, Packet, Acc, WriteF). |
118 |
|
|
119 |
|
write_to_sender_inbox(Server, User, Remote, Packet, Acc) -> |
120 |
8 |
mod_inbox_utils:write_to_sender_inbox(Server, User, Remote, Packet, Acc). |
121 |
|
|
122 |
|
write_to_receiver_inbox(Server, User, Remote, Packet, Acc) -> |
123 |
14 |
mod_inbox_utils:write_to_receiver_inbox(Server, User, Remote, Packet, Acc). |