scheduledmessage: Adjust schedule_message to accept callbacks.

This commit is contained in:
Aditya Bansal 2018-01-16 05:15:17 +05:30 committed by showell
parent 76f6f7cb47
commit 732321818d
1 changed files with 21 additions and 11 deletions

View File

@ -379,7 +379,7 @@ function patch_request_for_scheduling(request) {
return new_request; return new_request;
} }
exports.schedule_message = function schedule_message(request) { exports.schedule_message = function schedule_message(request, success, error) {
if (request === undefined) { if (request === undefined) {
request = create_message_object(); request = create_message_object();
} }
@ -389,15 +389,27 @@ exports.schedule_message = function schedule_message(request) {
} else { } else {
request.to = JSON.stringify([request.to]); request.to = JSON.stringify([request.to]);
} }
function success(data) {
/* success and error callbacks are kind of a package deal here. When scheduling
a message either by means of slash command or from message feed, if we need to do
something special on success then we will also need to know if our request errored
and do something appropriate. Therefore we just check if success callback is not
defined and just assume request to be coming from compose box. This is correct
because we won't ever actually have success operate in different context than error. */
if (success === undefined) {
success = function (data) {
notifications.notify_above_composebox('Scheduled your Message to be delivered at: ' + data.deliver_at); notifications.notify_above_composebox('Scheduled your Message to be delivered at: ' + data.deliver_at);
$("#compose-textarea").attr('disabled', false); $("#compose-textarea").attr('disabled', false);
clear_compose_box(); clear_compose_box();
} };
error = function (response) {
function error(response) {
$("#compose-textarea").attr('disabled', false); $("#compose-textarea").attr('disabled', false);
compose_error(response, $('#compose-textarea')); compose_error(response, $('#compose-textarea'));
};
/* We are adding a disable on compose under this block since it actually
has its place with the branch of code which does stuff when slash command
is incoming from compose_box */
$("#compose-textarea").attr('disabled', true);
} }
request = patch_request_for_scheduling(request); request = patch_request_for_scheduling(request);
@ -406,9 +418,7 @@ exports.schedule_message = function schedule_message(request) {
return; return;
} }
$("#compose-textarea").attr('disabled', true);
exports.transmit_message(request, success, error); exports.transmit_message(request, success, error);
}; };
exports.enter_with_preview_open = function () { exports.enter_with_preview_open = function () {