Add channel module wrapping our various JSON requests to the server.

(imported from commit 6489938678551ad8594822363ae6d1c9ae295496)
This commit is contained in:
Tim Abbott
2013-12-18 13:55:18 -05:00
parent 8c6ac1288d
commit 795930c803
17 changed files with 92 additions and 124 deletions

View File

@@ -68,20 +68,16 @@ exports.setup_page = function () {
util.make_loading_indicator($('#admin_page_deactivated_users_loading_indicator'));
// Populate users and bots tables
$.ajax({
type: 'GET',
channel.get({
url: '/json/users',
dataType: 'json',
timeout: 10*1000,
success: populate_users,
error: failed_listing_users
});
// Populate streams table
$.ajax({
type: 'POST',
channel.post({
url: '/json/get_public_streams',
dataType: 'json',
timeout: 10*1000,
success: populate_streams,
error: failed_listing_streams
@@ -126,8 +122,7 @@ exports.setup_page = function () {
$(e.target).closest(".user_row").addClass("active_user_row");
var email = $(".active_user_row").find('.email').text();
$.ajax({
type: 'POST',
channel.post({
url: '/json/users/' + $(".active_user_row").find('.email').text() + "/reactivate",
error: function (xhr, error_type) {
if (xhr.status.toString().charAt(0) === "4") {
@@ -159,8 +154,7 @@ exports.setup_page = function () {
}
$("#deactivation_user_modal").modal("hide");
$(".active_user_row button").prop("disabled", true).text("Working…");
$.ajax({
type: 'DELETE',
channel.del({
url: '/json/users/' + $(".active_user_row").find('.email').text(),
error: function (xhr, error_type) {
if (xhr.status.toString().charAt(0) === "4") {
@@ -193,8 +187,7 @@ exports.setup_page = function () {
}
$("#deactivation_stream_modal").modal("hide");
$(".active_stream_row button").prop("disabled", true).text("Working…");
$.ajax({
type: 'DELETE',
channel.del({
url: '/json/streams/' + encodeURIComponent($(".active_stream_row").find('.stream_name').text()),
error: function (xhr, error_type) {
if (xhr.status.toString().charAt(0) === "4") {

View File

@@ -15,11 +15,9 @@ $(function () {
return word !== "";
});
$.ajax({
type: 'POST',
channel.post({
url: '/json/set_alert_words',
data: {alert_words: JSON.stringify(words)},
dataType: 'json'});
data: {alert_words: JSON.stringify(words)}});
}
function add_alert_word(word, event) {

44
static/js/channel.js Normal file
View File

@@ -0,0 +1,44 @@
var channel = (function () {
var exports = {};
function call(args) {
return $.ajax(args);
}
exports.get = function (options) {
var args = _.extend({type: "GET", dataType: "json"}, options);
return call(args);
};
exports.post = function (options) {
var args = _.extend({type: "POST", dataType: "json"}, options);
return call(args);
};
exports.put = function (options) {
var args = _.extend({type: "PUT", dataType: "json"}, options);
return call(args);
};
// Not called exports.delete because delete is a reserved word in JS
exports.del = function (options) {
var args = _.extend({type: "DELETE", dataType: "json"}, options);
return call(args);
};
exports.patch = function (options) {
// Send a PATCH as a POST in order to work around QtWebkit
// (Linux/Windows desktop app) not supporting PATCH body.
options.method = "PATCH";
if (options.processData === false) {
// If we're submitting a FormData object, we need to add the
// method this way
options.data.append("method", "PATCH");
}
return exports.post(options);
};
return exports;
}());

View File

@@ -349,10 +349,8 @@ function compose_error(error_text, bad_input) {
var send_options;
function send_message_ajax(request, success) {
$.ajax({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
channel.post({
url: '/json/send_message',
type: 'POST',
data: request,
success: success,
error: function (xhr, error_type) {
@@ -368,10 +366,8 @@ function send_message_ajax(request, success) {
}
function report_send_time(send_time, receive_time, display_time) {
$.ajax({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
channel.post({
url: '/json/report_send_time',
type: 'POST',
data: {"time": send_time.toString(),
"received": receive_time.toString(),
"displayed": display_time.toString()}
@@ -569,8 +565,7 @@ exports.check_stream_existence = function (stream_name, autosubscribe) {
if (autosubscribe) {
request.autosubscribe = true;
}
$.ajax({
type: "POST",
channel.post({
url: "/json/subscriptions/exists",
data: request,
async: false,

View File

@@ -338,10 +338,8 @@ exports.initialize = function () {
// press Enter to send.
$("#new_message_content").focus();
return $.ajax({
dataType: 'json',
return channel.post({
url: '/json/change_enter_sends',
type: 'POST',
data: {'enter_sends': page_params.enter_sends}
});
});

View File

@@ -188,12 +188,10 @@ $(document).ready(function () {
$('#submit_invitation').attr('disabled', '');
$('#submit_invitation').text("Inviting …");
$.ajax({
type: 'POST',
channel.post({
url: '/json/bulk_invite_users',
data: { invitee_emails: JSON.stringify(get_invitee_emails()),
csrfmiddlewaretoken: csrf_token },
dataType: 'json',
complete: function () {
// Redirect to home
window.location.href = "/";

View File

@@ -32,11 +32,9 @@ exports.save = function (row) {
// If they didn't change anything, just cancel it.
return true;
}
$.ajax({
type: 'POST',
channel.post({
url: '/json/update_message',
data: request,
dataType: 'json',
success: function (data) {
if (msg_list === current_msg_list) {
return true;
@@ -113,11 +111,9 @@ function edit_message (row, raw_content) {
exports.start = function (row) {
var message = current_msg_list.get(rows.id(row));
var msg_list = current_msg_list;
$.ajax({
type: 'POST',
channel.post({
url: '/json/fetch_raw_message',
data: {message_id: message.id},
dataType: 'json',
success: function (data) {
if (current_msg_list === msg_list) {
message.raw_content = data.raw_content;

View File

@@ -26,11 +26,9 @@ exports.persist_and_rerender = function () {
muted_topics: JSON.stringify(muting.get_muted_topics())
};
last_topic_update = timestamp_ms();
$.ajax({
type: 'POST',
channel.post({
url: '/json/set_muted_topics',
data: data,
dataType: 'json'
data: data
});
};

View File

@@ -97,10 +97,8 @@ exports.stream = function () {
};
function report_narrow_time(initial_core_time, initial_free_time, network_time) {
$.ajax({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
channel.post({
url: '/json/report_narrow_time',
type: 'POST',
data: {"initial_core": initial_core_time.toString(),
"initial_free": initial_free_time.toString(),
"network": network_time.toString()}

View File

@@ -73,10 +73,8 @@ $(function () {
var validator = $("#referral-form").validate({
errorClass: 'text-error',
submitHandler: function () {
$.ajax({
type: "POST",
channel.post({
url: "/json/refer_friend",
dataType: "json",
data: { email: $("#referral-form input").val() },
error: function () {
// We ignore errors from the server because

View File

@@ -40,9 +40,8 @@ $(function () {
var spinner = $("#upload_avatar_spinner").expectOne();
util.make_loading_indicator(spinner, {text: 'Uploading avatar.'});
$.ajax({
channel.post({
url: '/json/set_avatar',
type: 'POST',
data: form_data,
cache: false,
processData: false,
@@ -72,10 +71,8 @@ $(function () {
$("#name_change_container").hide();
}
$.ajax({
type: 'POST',
channel.post({
url: '/json/get_bots',
dataType: 'json',
success: function (data) {
$('#bot_table_error').hide();
@@ -113,9 +110,8 @@ $(function () {
formData.append('file-'+i, file);
});
$('#create_bot_button').val('Adding bot...').prop('disabled', true);
$.ajax({
channel.post({
url: '/json/create_bot',
type: 'POST',
data: formData,
cache: false,
processData: false,
@@ -146,9 +142,8 @@ $(function () {
$("#bots_list").on("click", "button.delete_bot", function (e) {
var email = $(e.currentTarget).data('email');
$.ajax({
channel.del({
url: '/json/users/' + encodeURIComponent(email),
type: 'DELETE',
success: function () {
var row = $(e.currentTarget).closest("li");
row.hide('slow', function () { row.remove(); });
@@ -161,9 +156,8 @@ $(function () {
$("#bots_list").on("click", "button.regenerate_bot_api_key", function (e) {
var email = $(e.currentTarget).data('email');
$.ajax({
channel.post({
url: '/json/bots/' + encodeURIComponent(email) + '/api_key/regenerate',
type: 'POST',
success: function (data) {
var row = $(e.currentTarget).closest("li");
row.find(".api_key").find(".value").text(data.api_key);
@@ -223,17 +217,13 @@ $(function () {
var formData = new FormData();
formData.append('full_name', full_name);
formData.append('csrfmiddlewaretoken', csrf_token);
// Send a PATCH as a POST in order to work around QtWebkit (Linux/Windows desktop app)
// not supporting PATCH body.
formData.append('method', 'PATCH');
jQuery.each(file_input[0].files, function (i, file) {
formData.append('file-'+i, file);
});
util.make_loading_indicator(spinner, {text: 'Editing bot'});
edit_button.hide();
$.ajax({
channel.patch({
url: '/json/bots/' + encodeURIComponent(email),
type: 'POST',
data: formData,
cache: false,
processData: false,
@@ -265,9 +255,8 @@ $(function () {
});
$("#show_api_key_box").on("click", "button.regenerate_api_key", function (e) {
$.ajax({
channel.post({
url: '/json/users/me/api_key/regenerate',
type: 'POST',
success: function (data) {
$('#api_key_value').text(data.api_key);
},

View File

@@ -41,10 +41,8 @@ exports.stream_id = function (stream_name) {
};
function set_stream_property(stream_name, property, value) {
$.ajax({
type: 'POST',
return channel.post({
url: '/json/subscriptions/property',
dataType: 'json',
data: {
"property": property,
"stream_name": stream_name,
@@ -431,14 +429,11 @@ exports.setup_page = function () {
}
if (should_list_all_streams()) {
var req = $.ajax({
type: 'POST',
var req = channel.post({
url: '/json/get_public_streams',
dataType: 'json',
timeout: 10*1000,
success: populate_and_fill,
error: failed_listing
});
} else {
populate_and_fill({streams: []});
@@ -471,10 +466,8 @@ function ajaxSubscribe(stream) {
// Subscribe yourself to a single stream.
var true_stream_name;
return $.ajax({
type: "POST",
return channel.post({
url: "/json/subscriptions/add",
dataType: 'json', // This seems to be ignored. We still get back an xhr.
data: {"subscriptions": JSON.stringify([{"name": stream}]) },
success: function (resp, statusText, xhr, form) {
$("#create_stream_name").val("");
@@ -496,10 +489,8 @@ function ajaxSubscribe(stream) {
}
function ajaxUnsubscribe(stream) {
$.ajax({
type: "POST",
return channel.post({
url: "/json/subscriptions/remove",
dataType: 'json', // This seems to be ignored. We still get back an xhr.
data: {"subscriptions": JSON.stringify([stream]) },
success: function (resp, statusText, xhr, form) {
var name, res = $.parseJSON(xhr.responseText);
@@ -515,10 +506,8 @@ function ajaxUnsubscribe(stream) {
function ajaxSubscribeForCreation(stream, principals, invite_only, announce) {
// Subscribe yourself and possible other people to a new stream.
return $.ajax({
type: "POST",
return channel.post({
url: "/json/subscriptions/add",
dataType: 'json', // This seems to be ignored. We still get back an xhr.
data: {"subscriptions": JSON.stringify([{"name": stream}]),
"principals": JSON.stringify(principals),
"invite_only": JSON.stringify(invite_only),
@@ -590,10 +579,8 @@ function show_new_stream_modal() {
}
exports.invite_user_to_stream = function (user_email, stream_name, success, failure) {
$.ajax({
type: "POST",
return channel.post({
url: "/json/subscriptions/add",
dataType: 'json',
data: {"subscriptions": JSON.stringify([{"name": stream_name}]),
"principals": JSON.stringify([user_email])},
success: success,
@@ -798,10 +785,8 @@ $(function () {
$("#subscriptions-status").hide();
$.ajax({
type: "POST",
channel.post({
url: "/json/rename_stream",
dataType: 'json',
data: {"old_name": old_name, "new_name": new_name},
success: function (data) {
new_name_box.val('');
@@ -842,10 +827,8 @@ $(function () {
util.make_loading_indicator(indicator_elem);
$.ajax({
type: "POST",
channel.post({
url: "/json/get_subscribers",
dataType: 'json', // This seems to be ignored. We still get back an xhr.
data: {stream: stream},
success: function (data) {
util.destroy_loading_indicator(indicator_elem);

View File

@@ -156,8 +156,7 @@ function enable_event_handlers() {
}
function set_tutorial_status(status, callback) {
return $.ajax({
type: 'POST',
return channel.post({
url: '/json/tutorial_status',
data: {status: status},
success: callback

View File

@@ -684,13 +684,11 @@ function hack_for_floating_recipient_bar() {
}
function update_message_flag(messages, flag_name, set_flag) {
$.ajax({
type: 'POST',
channel.post({
url: '/json/update_message_flags',
data: {messages: JSON.stringify(_.pluck(messages, 'id')),
op: set_flag ? 'add' : 'remove',
flag: flag_name},
dataType: 'json'});
flag: flag_name}});
}
function change_message_collapse(message, collapsed) {
@@ -1603,11 +1601,9 @@ $(function () {
return;
}
$.ajax({
type: 'POST',
channel.post({
url: "/accounts/webathena_kerberos_login/",
data: {cred: JSON.stringify(r.session)},
dataType: 'json',
success: function (data, success) {
$("#zephyr-mirror-error").hide();
},

View File

@@ -288,13 +288,11 @@ function batched_flag_updater(flag, op) {
}
function server_request() {
$.ajax({
type: 'POST',
channel.post({
url: '/json/update_message_flags',
data: {messages: JSON.stringify(queue),
op: op,
flag: flag},
dataType: 'json',
success: on_success
});
}
@@ -351,14 +349,12 @@ function mark_all_as_read(cont) {
unread.declare_bankruptcy();
update_unread_counts();
$.ajax({
type: 'POST',
channel.post({
url: '/json/update_message_flags',
data: {messages: JSON.stringify([]),
all: true,
op: 'add',
flag: 'read'},
dataType: 'json',
success: cont});
}
@@ -470,11 +466,9 @@ function respond_to_message(opts) {
function update_pointer() {
if (!pointer_update_in_flight) {
pointer_update_in_flight = true;
return $.ajax({
type: 'POST',
return channel.post({
url: '/json/update_pointer',
data: {pointer: furthest_read},
dataType: 'json',
success: function () {
server_furthest_read = furthest_read;
pointer_update_in_flight = false;
@@ -680,12 +674,10 @@ function maybe_add_narrowed_messages(messages, msg_list, messages_are_new) {
ids.push(elem.id);
});
$.ajax({
type: 'POST',
channel.post({
url: '/json/messages_in_narrow',
data: {msg_ids: JSON.stringify(ids),
narrow: JSON.stringify(narrow.public_operators())},
dataType: 'json',
timeout: 5000,
success: function (data) {
if (msg_list !== current_msg_list) {
@@ -975,11 +967,9 @@ function get_updates(options) {
clearTimeout(get_updates_timeout);
}
get_updates_timeout = undefined;
get_updates_xhr = $.ajax({
type: 'POST',
get_updates_xhr = channel.post({
url: '/json/get_events',
data: get_updates_params,
dataType: 'json',
timeout: page_params.poll_timeout,
success: function (data) {
get_updates_xhr = undefined;
@@ -1041,11 +1031,9 @@ function cleanup_event_queue() {
if (page_params.event_queue_expired === true) {
return;
}
$.ajax({
type: 'DELETE',
channel.del({
url: '/json/events',
data: {queue_id: page_params.event_queue_id},
dataType: 'json'
data: {queue_id: page_params.event_queue_id}
});
}
@@ -1136,11 +1124,9 @@ function load_old_messages(opts) {
data.narrow = JSON.stringify([["stream", page_params.narrow_stream]]);
}
$.ajax({
type: 'POST',
channel.post({
url: '/json/get_old_messages',
data: data,
dataType: 'json',
success: function (data) {
get_old_messages_success(data, opts);
},
@@ -1227,11 +1213,9 @@ $(function () {
});
function fast_forward_pointer() {
$.ajax({
type: 'POST',
channel.post({
url: '/json/get_profile',
data: {email: page_params.email},
dataType: 'json',
success: function (data) {
mark_all_as_read(function () {
furthest_read = data.max_message_id;

View File

@@ -27,7 +27,7 @@ var globals =
+ ' invite ui util activity timerender MessageList MessageListView blueslip unread stream_list'
+ ' message_edit tab_bar emoji popovers navigate settings'
+ ' avatar feature_flags search_suggestion referral stream_color Dict'
+ ' Filter summary admin stream_data muting WinChan muting_ui Socket'
+ ' Filter summary admin stream_data muting WinChan muting_ui Socket channel'
// colorspace.js
+ ' colorspace'

View File

@@ -495,6 +495,7 @@ JS_SPECS = {
'js/summary.js',
'js/util.js',
'js/dict.js',
'js/channel.js',
'js/muting.js',
'js/muting_ui.js',
'js/setup.js',