From 976da94f8964ffffc9583990d4b0ec23ded758b2 Mon Sep 17 00:00:00 2001 From: John White Date: Thu, 7 May 2020 13:21:26 -0500 Subject: [PATCH] [4.3] DUPT-193: Granularize conference blackhole event bindings (#6532) - move the configured published conference events to kapi_conference - update bh_conferences event bindings to take an event argument - refactor bh_conferences to pass * as event binding for backwards compatiblility --- .../blackhole/src/modules/bh_conference.erl | 55 ++++++++++++++----- .../ecallmgr/src/ecallmgr_fs_conference.erl | 21 +------ core/kazoo_amqp/src/api/kapi_conference.erl | 36 ++++++++++++ 3 files changed, 80 insertions(+), 32 deletions(-) diff --git a/applications/blackhole/src/modules/bh_conference.erl b/applications/blackhole/src/modules/bh_conference.erl index e32c04361c7..06fdad5dc6b 100644 --- a/applications/blackhole/src/modules/bh_conference.erl +++ b/applications/blackhole/src/modules/bh_conference.erl @@ -5,6 +5,11 @@ %%% @author Peter Defebvre %%% @author Ben Wann %%% @author Roman Galeev +%%% +%%% This Source Code Form is subject to the terms of the Mozilla Public +%%% License, v. 2.0. If a copy of the MPL was not distributed with this +%%% file, You can obtain one at https://mozilla.org/MPL/2.0/. +%%% %%% @end %%%----------------------------------------------------------------------------- -module(bh_conference). @@ -16,15 +21,30 @@ -include("blackhole.hrl"). +-define(CONFERENCE_EVENTS + ,[<<"*">> | kapi_conference:events() + ]). + -define(ACCOUNT_BINDING(AccountId, ConferenceId, CallId) - ,<<"conference.event.*.", AccountId/binary, ".", ConferenceId/binary, ".", CallId/binary>> + ,?ACCOUNT_BINDING(AccountId, ConferenceId, CallId, <<"*">>) + ). +-define(ACCOUNT_BINDING(AccountId, ConferenceId, CallId, Event) + ,<<"conference.event.",Event/binary,".", AccountId/binary, ".", ConferenceId/binary, ".", CallId/binary>> ). -define(BINDING(ConferenceId, CallId) - ,<<"conference.event.", ConferenceId/binary, ".", CallId/binary>> + ,?BINDING(ConferenceId, CallId, <<"*">>) + ). +-define(BINDING(ConferenceId, CallId, Event) + ,<<"conference.event.", ConferenceId/binary, ".", CallId/binary, ".", Event/binary>> ). -define(COMMAND(ConferenceId) ,<<"conference.command.", ConferenceId/binary>> ). +-define(EVENT_BINDINGS(ConferenceId, CallId) + ,lists:foldl(fun(E, Acc) -> [?BINDING(ConferenceId, CallId, E) | Acc] end, [], ?CONFERENCE_EVENTS) + ). +-define(ALL, <<"*">>). + -spec init() -> any(). init() -> @@ -33,8 +53,8 @@ init() -> blackhole_bindings:bind(<<"blackhole.events.bindings.conference">>, ?MODULE, 'bindings'). init_bindings() -> - Bindings = [?BINDING(<<"{CONFERENCE_ID}">>, <<"{CALL_ID}">>) - ,?COMMAND(<<"{CONFERENCE_ID}">>) + Bindings = [?COMMAND(<<"{CONFERENCE_ID}">>) + | ?EVENT_BINDINGS(<<"{CONFERENCE_ID}">>, <<"{CALL_ID}">>) ], case kapps_config:set_default(?CONFIG_CAT, [<<"bindings">>, <<"conference">>], Bindings) of {'ok', _} -> lager:debug("initialized conference bindings"); @@ -42,14 +62,20 @@ init_bindings() -> end. -spec validate(bh_context:context(), map()) -> bh_context:context(). -validate(Context, #{keys := [<<"command">>, <<"*">>]}) -> +validate(Context, #{keys := [<<"command">>, ?ALL]}) -> bh_context:add_error(Context, <<"ConferenceId required">>); validate(Context, #{keys := [<<"command">>, _]}) -> Context; -validate(Context, #{keys := [<<"event">>, <<"*">>, _]}) -> +validate(Context, #{keys := [<<"event">>, ?ALL | _Rest]}) -> bh_context:add_error(Context, <<"ConferenceId required">>); -validate(Context, #{keys := [<<"event">>, _, _]}) -> +validate(Context, #{keys := [<<"event">>, _, ?ALL]}) -> Context; +validate(Context, #{keys := [<<"event">>, _, ?ALL, Event]}) -> + case lists:member(Event, ?CONFERENCE_EVENTS) of + 'false' -> + bh_context:add_error(Context, <<"Unsupported conference event ",Event/binary>>); + 'true' -> Context + end; validate(Context, #{keys := Keys}) -> bh_context:add_error(Context, <<"invalid format for conference subscription : ", (kz_binary:join(Keys))/binary>>). @@ -64,12 +90,14 @@ bindings(_Context, #{account_id := _AccountId ,subscribed => Subscribed ,listeners => Listeners }; +bindings(Context, #{keys := [<<"event">>, ConferenceId, CallId]}=Map) -> + bindings(Context, Map#{keys => [<<"event">>, ConferenceId, CallId, ?ALL]}); bindings(_Context, #{account_id := AccountId - ,keys := [<<"event">>, ConferenceId, CallId] + ,keys := [<<"event">>, ConferenceId, CallId, Event] }=Map) -> - Requested = ?BINDING(ConferenceId, CallId), - Subscribed = [?ACCOUNT_BINDING(AccountId, ConferenceId, CallId)], - Listeners = [{'amqp', 'conference', event_binding_options(AccountId, ConferenceId, CallId)}], + Requested = ?BINDING(ConferenceId, CallId, Event), + Subscribed = [?ACCOUNT_BINDING(AccountId, ConferenceId, CallId, Event)], + Listeners = [{'amqp', 'conference', event_binding_options(AccountId, ConferenceId, CallId, Event)}], Map#{requested => Requested ,subscribed => Subscribed ,listeners => Listeners @@ -89,11 +117,12 @@ command_binding_options(ConfId) -> ,'federate' ]. --spec event_binding_options(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary()) -> kz_term:proplist(). -event_binding_options(AccountId, ConferenceId, CallId) -> +-spec event_binding_options(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary()) -> kz_term:proplist(). +event_binding_options(AccountId, ConferenceId, CallId, Event) -> [{'restrict_to', [{'event', [{'account_id', AccountId} ,{'conference_id', ConferenceId} ,{'call_id', CallId} + ,{'event', Event} ] }] } diff --git a/applications/ecallmgr/src/ecallmgr_fs_conference.erl b/applications/ecallmgr/src/ecallmgr_fs_conference.erl index baf946463df..4378f4547ec 100644 --- a/applications/ecallmgr/src/ecallmgr_fs_conference.erl +++ b/applications/ecallmgr/src/ecallmgr_fs_conference.erl @@ -24,23 +24,6 @@ -define(SERVER, ?MODULE). --define(MEMBER_UPDATE_EVENTS, [<<"stop-talking">> - ,<<"start-talking">> - ,<<"mute-member">> - ,<<"unmute-member">> - ,<<"deaf-member">> - ,<<"undeaf-member">> - ]). - --define(CONFERENCE_EVENTS, [<<"conference-create">> - ,<<"conference-destroy">> - ,<<"lock">> - ,<<"unlock">> - ,<<"add-member">> - ,<<"del-member">> - | ?MEMBER_UPDATE_EVENTS - ]). - -record(state, {node = 'undefined' :: atom() ,options = [] :: kz_term:proplist() ,events = [] :: kz_term:ne_binaries() @@ -78,7 +61,7 @@ init([Node, Options]) -> lager:info("starting new fs conference event listener for ~s", [Node]), gen_server:cast(self(), 'bind_to_events'), ecallmgr_fs_conferences:sync_node(Node), - Events = kapps_config:get_ne_binaries(?APP_NAME, <<"publish_conference_event">>, ?CONFERENCE_EVENTS), + Events = kapps_config:get_ne_binaries(?APP_NAME, <<"publish_conference_event">>, kapi_conference:events()), {'ok', #state{node=Node ,options=Options ,events=Events @@ -205,7 +188,7 @@ process_event(<<"unlock">>, Props, _) -> UUID = kzd_freeswitch:conference_uuid(Props), ecallmgr_fs_conferences:update(UUID, {#conference.locked, 'false'}); process_event(Action, Props, _Node) -> - case lists:member(Action, ?MEMBER_UPDATE_EVENTS) of + case lists:member(Action, kapi_conference:member_update_events()) of 'true' -> update_participant(Props); 'false' -> 'ok' end. diff --git a/core/kazoo_amqp/src/api/kapi_conference.erl b/core/kazoo_amqp/src/api/kapi_conference.erl index 567e6869fad..57763b09950 100644 --- a/core/kazoo_amqp/src/api/kapi_conference.erl +++ b/core/kazoo_amqp/src/api/kapi_conference.erl @@ -83,6 +83,9 @@ ,publish_dial/2, publish_dial/3 ,publish_dial_resp/2, publish_dial_resp/3 ]). +-export([events/0 + ,member_update_events/0 + ]). -include_lib("kz_amqp_util.hrl"). -include("kapi_dialplan.hrl"). @@ -572,6 +575,39 @@ -define(CONF_PLAY_MACRO_REQ_VALUES, []). -define(CONF_PLAY_MACRO_REQ_TYPES, [{<<"Conference-ID">>, fun is_binary/1}]). +-define(MEMBER_UPDATE_EVENTS, [<<"stop-talking">> + ,<<"start-talking">> + ,<<"mute-member">> + ,<<"unmute-member">> + ,<<"deaf-member">> + ,<<"undeaf-member">> + ]). + +-define(CONFERENCE_EVENTS, [<<"conference-create">> + ,<<"conference-destroy">> + ,<<"lock">> + ,<<"unlock">> + ,<<"add-member">> + ,<<"del-member">> + | ?MEMBER_UPDATE_EVENTS + ]). + +%%------------------------------------------------------------------------------ +%% @doc Get allowed connference events. +%% @end +%%------------------------------------------------------------------------------ +-spec events() -> kz_term:ne_binaries(). +events() -> + kapps_config:get_ne_binaries(<<"ecallmgr">>, <<"publish_conference_event">>, ?CONFERENCE_EVENTS). + +-spec member_update_events() -> kz_term:ne_binaries(). +member_update_events() -> + ?MEMBER_UPDATE_EVENTS. + +%%------------------------------------------------------------------------------ +%% @doc +%% @end +%%------------------------------------------------------------------------------ -spec focus_queue_name(atom()) -> kz_term:ne_binary(). focus_queue_name(Focus) -> <<(kz_term:to_binary(Focus))/binary, "_conference">>.