Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.3] - DUPT-193: Granularize conference blackhole event bindings #6532

Merged
merged 1 commit into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions applications/blackhole/src/modules/bh_conference.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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() ->
Expand All @@ -33,23 +53,29 @@ 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");
{'error', _E} -> lager:info("failed to initialize conference bindings: ~p", [_E])
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>>).

Expand All @@ -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
Expand All @@ -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}
]
}]
}
Expand Down
21 changes: 2 additions & 19 deletions applications/ecallmgr/src/ecallmgr_fs_conference.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions core/kazoo_amqp/src/api/kapi_conference.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -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">>.

Expand Down