Skip to content

Commit 8e3acc6

Browse files
authored
Merge pull request #4522 from esl/clear-inbox-after-destroying-muc-light-room
Clear inbox after destroying muc light room
2 parents f5e3ed2 + 70c81d2 commit 8e3acc6

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

big_tests/tests/inbox_SUITE.erl

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-include_lib("exml/include/exml.hrl").
88
-include_lib("jid/include/jid.hrl").
99
-include_lib("inbox.hrl").
10+
-include("muc_light.hrl").
1011

1112
%% tests
1213
-import(muc_light_helper, [room_bin_jid/1]).
@@ -97,7 +98,9 @@ groups() ->
9798
leave_and_remove_conversation,
9899
groupchat_markers_one_reset_room_created,
99100
groupchat_markers_all_reset_room_created,
100-
inbox_does_not_trigger_does_user_exist
101+
inbox_does_not_trigger_does_user_exist,
102+
remove_muclight_messages_after_the_room_is_deleted_xmpp,
103+
remove_muclight_messages_after_the_room_is_deleted_graphql
101104
]},
102105
{muclight_config, [sequence],
103106
[
@@ -678,6 +681,54 @@ simple_groupchat_stored_in_all_inbox(Config) ->
678681
check_inbox(Kate, [#conv{unread = 1, from = AliceRoomJid, to = KateJid, content = Msg}])
679682
end).
680683

684+
remove_muclight_messages_after_the_room_is_deleted_xmpp(Config) ->
685+
remove_muclight_messages_after_the_room_is_deleted(Config, xmpp).
686+
687+
remove_muclight_messages_after_the_room_is_deleted_graphql(Config) ->
688+
remove_muclight_messages_after_the_room_is_deleted(Config, graphql).
689+
690+
remove_muclight_messages_after_the_room_is_deleted(Config, ReqType) ->
691+
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}, {kate, 1}], fun(Alice, Bob, Kate) ->
692+
Msg = <<"Hi Room!">>,
693+
Id = <<"MyID">>,
694+
Users = [Alice, Bob, Kate],
695+
Room = inbox_helper:create_room(Alice, [Bob, Kate]),
696+
AliceJid = inbox_helper:to_bare_lower(Alice),
697+
KateJid = inbox_helper:to_bare_lower(Kate),
698+
BobJid = inbox_helper:to_bare_lower(Bob),
699+
RoomJid = room_bin_jid(Room),
700+
AliceRoomJid = <<RoomJid/binary, "/", AliceJid/binary>>,
701+
Stanza = escalus_stanza:set_id(
702+
escalus_stanza:groupchat_to(RoomJid, Msg), Id),
703+
%% Alice sends message to a room
704+
escalus:send(Alice, Stanza),
705+
inbox_helper:wait_for_groupchat_msg(Users),
706+
%% Alice has 0 unread messages
707+
check_inbox(Alice, [#conv{unread = 0, from = AliceRoomJid, to = AliceJid, content = Msg}]),
708+
%% Bob and Kate have one conv with 1 unread message
709+
check_inbox(Bob, [#conv{unread = 1, from = AliceRoomJid, to = BobJid, content = Msg}]),
710+
check_inbox(Kate, [#conv{unread = 1, from = AliceRoomJid, to = KateJid, content = Msg}]),
711+
712+
%% Remove the room
713+
destroy_room(Config, Alice, Bob, Kate, RoomJid, ReqType),
714+
715+
%% Inbox entries are removed
716+
check_inbox(Alice, []),
717+
check_inbox(Bob, []),
718+
check_inbox(Kate, [])
719+
end).
720+
721+
destroy_room(_, Alice, Bob, Kate, RoomJid, xmpp) ->
722+
Stanza = escalus_stanza:to(
723+
escalus_stanza:iq_set(?NS_MUC_LIGHT_DESTROY, []), RoomJid),
724+
escalus:send(Alice, Stanza),
725+
AffUsersChanges = [{Alice, none}, {Bob, none}, {Kate, none}],
726+
muc_light_helper:verify_aff_bcast([], AffUsersChanges, [?NS_MUC_LIGHT_DESTROY]);
727+
destroy_room(Config, _, _, _, RoomJid, graphql) ->
728+
Vars = #{<<"room">> => RoomJid},
729+
graphql_helper:execute_command(<<"muc_light">>, <<"deleteRoom">>, Vars,
730+
graphql_helper:init_admin_handler(Config)).
731+
681732
advanced_groupchat_stored_in_all_inbox(Config) ->
682733
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}, {kate, 1}], fun(Alice, Bob, Kate) ->
683734
Msg1 = <<"Hi Room!">>,

doc/modules/mod_inbox.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Only changes that affect the user directly will be stored in their inbox.
9292

9393
Use this option when `muclight` is enabled.
9494
If true, the inbox conversation is removed for a user when they are removed from the groupchat.
95+
Enabling this option will also clear all inbox entries associated with a destroyed room.
9596

9697
### `modules.mod_inbox.iqdisc.type`
9798
* **Syntax:** string, one of `"one_queue"`, `"no_queue"`, `"queues"`, `"parallel"`
@@ -121,7 +122,7 @@ Inbox currently supports the following DBs:
121122
## Legacy MUC support
122123
Inbox comes with support for the legacy MUC as well. It stores all groupchat messages sent to
123124
room in each sender's and recipient's inboxes and private messages. Currently it is not possible to
124-
configure it to store system messages like [subject](https://xmpp.org/extensions/xep-0045.html#enter-subject)
125+
configure it to store system messages like [subject](https://xmpp.org/extensions/xep-0045.html#enter-subject)
125126
or [affiliation](https://xmpp.org/extensions/xep-0045.html#affil) change.
126127

127128

src/inbox/mod_inbox_muclight.erl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-include("jlib.hrl").
1212
-include("mongoose.hrl").
1313

14-
-export([handle_outgoing_message/5, handle_incoming_message/5]).
14+
-export([handle_outgoing_message/5, handle_incoming_message/5, handle_room_destruction/2]).
1515

1616
-type role() :: r_member() | r_owner() | r_none().
1717
-type r_member() :: binary().
@@ -106,9 +106,17 @@ maybe_store_system_message(HostType, Room, Remote, Packet, Acc) ->
106106
ok
107107
end.
108108

109+
-spec handle_room_destruction(HostType :: mongooseim:host_type(),
110+
Room :: jid:jid()) -> ok.
111+
handle_room_destruction(HostType, RoomJID) ->
112+
CheckRemove = mod_inbox_utils:get_option_remove_on_kicked(HostType),
113+
{ok, AffList, _} = mod_muc_light_db_backend:get_aff_users(HostType, jid:to_lus(RoomJID)),
114+
[maybe_remove_inbox_row(HostType, RoomJID, UserJID, CheckRemove) || {UserJID, _Aff} <- AffList],
115+
ok.
116+
109117
-spec maybe_remove_inbox_row(HostType :: mongooseim:host_type(),
110118
Room :: jid:jid(),
111-
Remote :: jid:jid(),
119+
Remote :: jid:jid() | jid:simple_bare_jid(),
112120
WriteAffChanges :: boolean()) -> ok.
113121
maybe_remove_inbox_row(_, _, _, false) ->
114122
ok;

src/muc_light/mod_muc_light_db_rdbms.erl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ create_room_with_specified_name(HostType, RoomUS, Config, AffUsers, Version) ->
417417
RoomUS :: jid:simple_bare_jid()) ->
418418
ok | {error, not_exists}.
419419
destroy_room(HostType, RoomUS) ->
420+
case is_inbox_enabled(HostType) andalso room_exists(HostType, RoomUS) of
421+
true ->
422+
mod_inbox_muclight:handle_room_destruction(HostType, RoomUS);
423+
false ->
424+
ok
425+
end,
420426
F = fun() -> destroy_room_transaction(HostType, RoomUS) end,
421427
{atomic, Res} = mongoose_rdbms:sql_transaction(HostType, F),
422428
Res.
@@ -793,3 +799,9 @@ room_us_to_host_type({_, RoomS}) ->
793799
-spec muc_server_to_host_type(jid:lserver()) -> mongooseim:host_type().
794800
muc_server_to_host_type(MUCServer) ->
795801
mod_muc_light_utils:muc_host_to_host_type(MUCServer).
802+
803+
%% ------------------------ Inbox ------------------------
804+
805+
-spec is_inbox_enabled(mongooseim:host_type()) -> boolean().
806+
is_inbox_enabled(HostType) ->
807+
gen_mod:is_loaded(HostType, mod_inbox).

0 commit comments

Comments
 (0)