Skip to content

Commit

Permalink
Merge pull request #3249 from esl/acc_muc_affs
Browse files Browse the repository at this point in the history
Acc muc affs
  • Loading branch information
vkatsuba committed Sep 8, 2021
2 parents 0d49bfe + 0957070 commit cdfd7b9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
21 changes: 20 additions & 1 deletion src/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
%% Hooks handlers
-export([is_muc_room_owner/4,
can_access_room/4,
get_room_affiliations/2,
can_access_identity/4,
disco_local_items/1]).

Expand All @@ -82,7 +83,7 @@
{?MOD_MUC_DB_BACKEND, set_nick, 4},
{?MOD_MUC_DB_BACKEND, store_room, 4},
{?MOD_MUC_DB_BACKEND, unset_nick, 3},
can_access_identity/4, can_access_room/4, create_instant_room/6,
can_access_identity/4, can_access_room/4, get_room_affiliations/2, create_instant_room/6,
disco_local_items/1, hibernated_rooms_number/0, is_muc_room_owner/4,
online_rooms_number/0, register_room/4, restore_room/3, start_link/2
]).
Expand Down Expand Up @@ -1251,6 +1252,23 @@ can_access_room(_, _HostType, Room, User) ->
{ok, CanAccess} -> CanAccess
end.

-spec get_room_affiliations(mongoose_acc:t(), jid:jid()) ->
{mongoose_acc:t(), any()}.
get_room_affiliations(Acc1, Room) ->
case mongoose_acc:get(?MODULE, affiliations, undefined, Acc1) of
undefined ->
case mod_muc_room:get_room_users(Room) of
{error, not_found} ->
Acc2 = mongoose_acc:set(?MODULE, affiliations, {error, not_found}, Acc1),
{Acc2, {error, not_found}};
{ok, _Affs} = Res ->
Acc2 = mongoose_acc:set_permanent(?MODULE, affiliations, Res, Acc1),
{Acc2, Res}
end;
Affs ->
{Acc1, Affs}
end.

-spec can_access_identity(Acc :: boolean(), HostType :: mongooseim:host_type(),
Room :: jid:jid(), User :: jid:jid()) ->
boolean().
Expand Down Expand Up @@ -1322,6 +1340,7 @@ config_metrics(HostType) ->
hooks(HostType) ->
[{is_muc_room_owner, HostType, ?MODULE, is_muc_room_owner, 50},
{can_access_room, HostType, ?MODULE, can_access_room, 50},
{get_room_affiliations, HostType, ?MODULE, get_room_affiliations, 50},
{can_access_identity, HostType, ?MODULE, can_access_identity, 50},
{disco_local_items, HostType, ?MODULE, disco_local_items, 250}].

Expand Down
13 changes: 12 additions & 1 deletion src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
-include("mod_privacy.hrl").
-include("mongoose.hrl").

-ignore_xref([get_room_affiliations/2]).

-export([adhoc_local_commands/4,
adhoc_sm_commands/4,
anonymous_purge_hook/3,
Expand Down Expand Up @@ -84,7 +86,8 @@

-export([is_muc_room_owner/3,
can_access_identity/3,
can_access_room/3]).
can_access_room/3,
get_room_affiliations/2]).

-export([mam_archive_id/2,
mam_archive_size/3,
Expand Down Expand Up @@ -899,6 +902,14 @@ can_access_identity(HostType, Room, User) ->
can_access_room(HostType, Room, User) ->
run_hook_for_host_type(can_access_room, HostType, false, [HostType, Room, User]).

-spec get_room_affiliations(Acc, Room) -> Result when
Acc :: mongoose_acc:t(),
Room :: jid:jid(),
Result :: {mongoose_acc:t(), term()}.
get_room_affiliations(Acc, Room) ->
HostType = mongoose_acc:host_type(Acc),
run_hook_for_host_type(get_room_affiliations, HostType, Acc, [Room]).

%% MAM related hooks

%%% @doc The `mam_archive_id' hook is called to determine
Expand Down
26 changes: 22 additions & 4 deletions src/muc_light/mod_muc_light.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
process_iq_set/4,
is_muc_room_owner/4,
can_access_room/4,
get_room_affiliations/2,
can_access_identity/4]).

%% For propEr
Expand Down Expand Up @@ -170,13 +171,12 @@ try_to_create_room(CreatorUS, RoomJID, #create{raw_config = RawConfig} = Creatio
Acc :: mongoose_acc:t()) ->
{ok, jid:jid(), config_req_props()}
| {error, validation_error() | bad_request | not_allowed}.
change_room_config(UserJid, RoomID, MUCLightDomain, ConfigReq, Acc) ->
change_room_config(UserJid, RoomID, MUCLightDomain, ConfigReq, Acc1) ->
R = {RoomID, MUCLightDomain},
RoomJID = jid:make(RoomID, MUCLightDomain, <<>>),
RoomUS = jid:to_lus(RoomJID),
AffUsersRes = mod_muc_light_db_backend:get_aff_users(RoomUS),

case mod_muc_light_room:process_request(UserJid, R, {set, ConfigReq}, AffUsersRes, Acc) of
{Acc2, AffUsersRes} = mod_muc_light:get_room_affiliations(Acc1, RoomJID),
case mod_muc_light_room:process_request(UserJid, RoomUS, {set, ConfigReq}, AffUsersRes, Acc2) of
{set, ConfigResp, _} ->
{ok, RoomJID, ConfigResp};
{error, _Reason} = E ->
Expand Down Expand Up @@ -299,6 +299,7 @@ hooks(HostType) ->
Roster = gen_mod:get_module_opt(HostType, ?MODULE, rooms_in_rosters, ?DEFAULT_ROOMS_IN_ROSTERS),
[{is_muc_room_owner, HostType, ?MODULE, is_muc_room_owner, 50},
{can_access_room, HostType, ?MODULE, can_access_room, 50},
{get_room_affiliations, HostType, ?MODULE, get_room_affiliations, 50},
{can_access_identity, HostType, ?MODULE, can_access_identity, 50},
%% Prevent sending service-unavailable on groupchat messages
{offline_groupchat_message_hook, HostType, ?MODULE, prevent_service_unavailable, 90},
Expand Down Expand Up @@ -531,6 +532,23 @@ can_access_room(true, _HostType, _Room, _User) ->
can_access_room(_, _HostType, Room, User) ->
none =/= get_affiliation(Room, User).

-spec get_room_affiliations(mongoose_acc:t(), jid:jid()) ->
{mongoose_acc:t(), {ok, aff_users(), binary()} | {error, not_exists}}.
get_room_affiliations(Acc1, Room) ->
case mongoose_acc:get(?MODULE, affiliations, undefined, Acc1) of
undefined ->
case mod_muc_light_db_backend:get_aff_users(jid:to_lus(Room)) of
{error, not_exists} ->
Acc2 = mongoose_acc:set(?MODULE, affiliations, {error, not_exists}, Acc1),
{Acc2, {error, not_exists}};
{ok, _Affs, _Version} = Res ->
Acc2 = mongoose_acc:set_permanent(?MODULE, affiliations, Res, Acc1),
{Acc2, Res}
end;
Affs ->
{Acc1, Affs}
end.

-spec can_access_identity(Acc :: boolean(), HostType :: mongooseim:host_type(),
Room :: jid:jid(), User :: jid:jid()) ->
boolean().
Expand Down
9 changes: 4 additions & 5 deletions src/muc_light/mod_muc_light_room.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
{?MOD_MUC_LIGHT_DB_BACKEND_BACKEN, modify_aff_users, 4},
{?MOD_MUC_LIGHT_DB_BACKEND_BACKEN, set_config, 3},
{?MOD_MUC_LIGHT_DB_BACKEND_BACKEN, destroy_room, 1},
{?MOD_MUC_LIGHT_DB_BACKEND_BACKEN, get_aff_users, 1},
{?MOD_MUC_LIGHT_CODEC_BACKEND_BACKEN, encode, 5},
{?MOD_MUC_LIGHT_CODEC_BACKEND_BACKEN, encode_error, 5}
]).
Expand All @@ -54,11 +53,11 @@

-spec handle_request(From :: jid:jid(), RoomJID :: jid:jid(), OrigPacket :: exml:element(),
Request :: muc_light_packet(), Acc :: mongoose_acc:t()) -> mongoose_acc:t().
handle_request(From, To, OrigPacket, Request, Acc) ->
handle_request(From, To, OrigPacket, Request, Acc1) ->
RoomUS = jid:to_lus(To),
AffUsersRes = mod_muc_light_db_backend:get_aff_users(RoomUS),
Response = process_request(From, RoomUS, Request, AffUsersRes, Acc),
send_response(From, To, RoomUS, OrigPacket, Response, Acc).
{Acc2, AffUsersRes} = mod_muc_light:get_room_affiliations(Acc1, To),
Response = process_request(From, RoomUS, Request, AffUsersRes, Acc2),
send_response(From, To, RoomUS, OrigPacket, Response, Acc2).

-spec maybe_forget(Acc :: mongoose_acc:t(),
RoomUS :: jid:simple_bare_jid(), NewAffUsers :: aff_users()) -> any().
Expand Down

0 comments on commit cdfd7b9

Please sign in to comment.