Skip to content

Commit

Permalink
Receiving notification of new voicemail causes document conflicts whe…
Browse files Browse the repository at this point in the history
…n we try to fetch that voicemail from the messages list of the vmbox document. It would be helpful to have a notificaiton published once that metadata is saved in the messages list so that we avoid this race condition.

We are going to implement this either way but would prefer to have this in the 2600hz/kazoo code and to get your feedback via a pull request.  I'm sure you have some holes to poke.

Thanks
  • Loading branch information
aberke committed Feb 5, 2015
1 parent 7dc5886 commit d029732
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
23 changes: 21 additions & 2 deletions applications/callflow/src/module/cf_voicemail.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,9 @@ save_meta(Length, #mailbox{mailbox_id=Id}, Call, MediaId) ->
{'ok', JObj} -> wh_json:get_value(<<"external_media_url">>, JObj);
{'error', _} -> 'undefined'
end,
Timestamp = new_timestamp(),
Metadata = wh_json:from_list(props:filter_undefined(
[{<<"timestamp">>, new_timestamp()}
[{<<"timestamp">>, Timestamp}
,{<<"from">>, whapps_call:from(Call)}
,{<<"to">>, whapps_call:to(Call)}
,{<<"caller_id_number">>, get_caller_id_number(Call)}
Expand All @@ -1141,7 +1142,25 @@ save_meta(Length, #mailbox{mailbox_id=Id}, Call, MediaId) ->
,{<<"external_media_url">>, ExternalMediaUrl}
])),
{'ok', _BoxJObj} = save_metadata(Metadata, whapps_call:account_db(Call), Id),
lager:debug("stored voicemail metadata for ~s", [MediaId]).
lager:debug("stored voicemail metadata for ~s", [MediaId]),

Prop = [{<<"From-User">>, whapps_call:from_user(Call)}
,{<<"From-Realm">>, whapps_call:from_realm(Call)}
,{<<"To-User">>, whapps_call:to_user(Call)}
,{<<"To-Realm">>, whapps_call:to_realm(Call)}
,{<<"Account-DB">>, whapps_call:account_db(Call)}
,{<<"Account-ID">>, whapps_call:account_id(Call)}
,{<<"Voicemail-Box">>, Id}
,{<<"Voicemail-Name">>, MediaId}
,{<<"Caller-ID-Number">>, get_caller_id_number(Call)}
,{<<"Caller-ID-Name">>, get_caller_id_name(Call)}
,{<<"Voicemail-Timestamp">>, Timestamp}
,{<<"Voicemail-Length">>, Length}
,{<<"Call-ID">>, whapps_call:call_id(Call)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
wapi_notifications:publish_voicemail_saved(Prop),
lager:debug("published voicemail_saved for ~s", [Id]).

-spec maybe_transcribe(whapps_call:call(), ne_binary(), boolean()) ->
api_object().
Expand Down
33 changes: 32 additions & 1 deletion core/whistle-1.0.0/src/api/wapi_notifications.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
,<<"Preview">>
]).

-define(NOTIFY_VOICEMAIL_SAVED, <<"notifications.voicemail.saved">>).
-define(NOTIFY_VOICEMAIL_NEW, <<"notifications.voicemail.new">>).
-define(NOTIFY_VOICEMAIL_FULL, <<"notifications.voicemail.full">>).
-define(NOTIFY_FAX_INBOUND, <<"notifications.fax.inbound">>).
Expand All @@ -95,7 +96,7 @@
-define(NOTIFY_WEBHOOK_CALLFLOW, <<"notifications.webhook.callflow">>).
-define(NOTIFY_SKEL, <<"notifications.skel">>).

%% Notify New Voicemail
%% Notify New Voicemail or Voicemail Saved
-define(VOICEMAIL_HEADERS, [<<"From-User">>, <<"From-Realm">>
,<<"To-User">>, <<"To-Realm">>
,<<"Account-DB">>
Expand All @@ -112,6 +113,10 @@
]).
-define(VOICEMAIL_TYPES, []).

-define(VOICEMAIL_SAVED_VALUES, [{<<"Event-Category">>, <<"notification">>}
,{<<"Event-Name">>, <<"voicemail_saved">>}
]).

%% Notify Voicemail full
-define(VOICEMAIL_FULL_HEADERS, [<<"Account-DB">>
,<<"Voicemail-Box">> ,<<"Voicemail-Number">>
Expand Down Expand Up @@ -376,6 +381,23 @@ voicemail_v(Prop) when is_list(Prop) ->
wh_api:validate(Prop, ?VOICEMAIL_HEADERS, ?VOICEMAIL_VALUES, ?VOICEMAIL_TYPES);
voicemail_v(JObj) -> voicemail_v(wh_json:to_proplist(JObj)).

%%--------------------------------------------------------------------
%% @doc
%% Takes proplist, creates JSON string or error
%% @end
%%--------------------------------------------------------------------
voicemail_saved(Prop) when is_list(Prop) ->
case voicemail_saved_v(Prop) of
'true' -> wh_api:build_message(Prop, ?VOICEMAIL_HEADERS, ?OPTIONAL_VOICEMAIL_HEADERS);
'false' -> {'error', "Proplist failed validation for voicemail"}
end;
voicemail_saved(JObj) -> voicemail_saved(wh_json:to_proplist(JObj)).

-spec voicemail_saved_v(api_terms()) -> boolean().
voicemail_saved_v(Prop) when is_list(Prop) ->
wh_api:validate(Prop, ?VOICEMAIL_HEADERS, ?VOICEMAIL_SAVED_VALUES, ?VOICEMAIL_TYPES);
voicemail_saved_v(JObj) -> voicemail_saved_v(wh_json:to_proplist(JObj)).

%%--------------------------------------------------------------------
%% @doc
%% Takes proplist, creates JSON string or error
Expand Down Expand Up @@ -726,6 +748,8 @@ bind_to_q(Q, 'undefined') ->
'ok' = amqp_util:bind_q_to_notifications(Q, <<"notifications.*.*">>);
bind_to_q(Q, ['new_voicemail'|T]) ->
'ok' = amqp_util:bind_q_to_notifications(Q, ?NOTIFY_VOICEMAIL_NEW),
bind_to_q(Q, ['voicemail_saved'|T]) ->
'ok' = amqp_util:bind_q_to_notifications(Q, ?NOTIFY_VOICEMAIL_SAVED),
bind_to_q(Q, T);
bind_to_q(Q, ['voicemail_full'|T]) ->
'ok' = amqp_util:bind_q_to_notifications(Q, ?NOTIFY_VOICEMAIL_FULL),
Expand Down Expand Up @@ -882,6 +906,13 @@ unbind_q_from(_Q, []) ->
declare_exchanges() ->
amqp_util:notifications_exchange().

-spec publish_voicemail_saved(api_terms()) -> 'ok'.
-spec publish_voicemail_saved(api_terms(), ne_binary()) -> 'ok'.
publish_voicemail_saved(JObj) -> publish_voicemail_saved(JObj, ?DEFAULT_CONTENT_TYPE).
publish_voicemail_saved(Voicemail, ContentType) ->
{'ok', Payload} = wh_api:prepare_api_payload(Voicemail, ?VOICEMAIL_VALUES, fun ?MODULE:voicemail_saved/1),

This comment has been minimized.

Copy link
@lazedo

lazedo Feb 21, 2015

Member

this should be VOICEMAIL_SAVED_VALUES instead of VOICEMAIL_VALUES

corrected in bf25313

amqp_util:notifications_publish(?NOTIFY_VOICEMAIL_SAVED, Payload, ContentType).

-spec publish_voicemail(api_terms()) -> 'ok'.
-spec publish_voicemail(api_terms(), ne_binary()) -> 'ok'.
publish_voicemail(JObj) -> publish_voicemail(JObj, ?DEFAULT_CONTENT_TYPE).
Expand Down

0 comments on commit d029732

Please sign in to comment.