2019-10-12 04:48:15 +02:00
|
|
|
const typing_status = require("./typing_status");
|
|
|
|
|
2016-10-12 20:57:59 +02:00
|
|
|
var typing = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
2017-03-22 15:11:41 +01:00
|
|
|
// This module handles the outbound side of typing indicators.
|
|
|
|
// We detect changes in the compose box and notify the server
|
|
|
|
// when we are typing. For the inbound side see typing_events.js.
|
2017-09-25 20:33:29 +02:00
|
|
|
//
|
2017-11-08 17:55:36 +01:00
|
|
|
// See docs/subsystems/typing-indicators.md for details on typing indicators.
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2019-06-06 21:54:55 +02:00
|
|
|
function send_typing_notification_ajax(user_ids_array, operation) {
|
2016-10-12 20:57:59 +02:00
|
|
|
channel.post({
|
|
|
|
url: '/json/typing',
|
|
|
|
data: {
|
2019-06-06 21:54:55 +02:00
|
|
|
to: JSON.stringify(user_ids_array),
|
2016-10-12 20:57:59 +02:00
|
|
|
op: operation,
|
|
|
|
},
|
|
|
|
success: function () {},
|
|
|
|
error: function (xhr) {
|
|
|
|
blueslip.warn("Failed to send typing event: " + xhr.responseText);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-06 21:54:55 +02:00
|
|
|
function get_user_ids_array() {
|
2018-08-25 17:22:48 +02:00
|
|
|
var user_ids_string = compose_pm_pill.get_user_ids_string();
|
|
|
|
if (user_ids_string === "") {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
2019-06-06 21:54:55 +02:00
|
|
|
|
|
|
|
return people.user_ids_string_to_ids_array(user_ids_string);
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
2016-10-12 20:57:59 +02:00
|
|
|
|
2019-10-04 23:57:34 +02:00
|
|
|
function is_valid_conversation(user_ids_array) {
|
2017-03-22 00:41:09 +01:00
|
|
|
// TODO: Check to make sure we're in a PM conversation
|
|
|
|
// with valid emails.
|
2019-10-04 23:57:34 +02:00
|
|
|
if (!user_ids_array) {
|
2017-03-22 00:41:09 +01:00
|
|
|
return false;
|
2016-10-12 20:57:59 +02:00
|
|
|
}
|
2017-03-22 00:41:09 +01:00
|
|
|
|
2018-08-25 17:22:48 +02:00
|
|
|
if (compose_pm_pill.has_unconverted_data()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-22 00:41:09 +01:00
|
|
|
var compose_empty = !compose_state.has_message_content();
|
|
|
|
if (compose_empty) {
|
|
|
|
return false;
|
2016-10-12 20:57:59 +02:00
|
|
|
}
|
2017-03-22 00:41:09 +01:00
|
|
|
|
2017-04-24 20:35:26 +02:00
|
|
|
if (compose_state.get_message_type() !== 'private') {
|
2017-03-22 14:06:40 +01:00
|
|
|
// We only use typing indicators in PMs for now.
|
|
|
|
// There was originally some support for having
|
|
|
|
// typing indicators related to stream conversations,
|
|
|
|
// but the initial rollout led to users being
|
|
|
|
// confused by them. We may revisit this.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 00:41:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_current_time() {
|
2019-10-05 01:44:22 +02:00
|
|
|
return new Date().getTime();
|
2016-10-12 20:57:59 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 23:57:34 +02:00
|
|
|
function notify_server_start(user_ids_array) {
|
|
|
|
send_typing_notification_ajax(user_ids_array, "start");
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 23:57:34 +02:00
|
|
|
function notify_server_stop(user_ids_array) {
|
|
|
|
send_typing_notification_ajax(user_ids_array, "stop");
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.get_recipient = get_user_ids_array;
|
2018-03-16 12:39:46 +01:00
|
|
|
exports.initialize = function () {
|
|
|
|
var worker = {
|
2019-06-07 03:17:35 +02:00
|
|
|
get_recipient: exports.get_recipient,
|
2018-03-16 12:39:46 +01:00
|
|
|
is_valid_conversation: is_valid_conversation,
|
|
|
|
get_current_time: get_current_time,
|
|
|
|
notify_server_start: notify_server_start,
|
|
|
|
notify_server_stop: notify_server_stop,
|
|
|
|
};
|
|
|
|
|
|
|
|
$(document).on('input', '#compose-textarea', function () {
|
|
|
|
// If our previous state was no typing notification, send a
|
|
|
|
// start-typing notice immediately.
|
|
|
|
typing_status.handle_text_input(worker);
|
|
|
|
});
|
2017-03-22 00:41:09 +01:00
|
|
|
|
2018-03-16 12:39:46 +01:00
|
|
|
// We send a stop-typing notification immediately when compose is
|
|
|
|
// closed/cancelled
|
|
|
|
$(document).on('compose_canceled.zulip compose_finished.zulip', function () {
|
|
|
|
typing_status.stop(worker);
|
|
|
|
});
|
|
|
|
};
|
2016-10-12 20:57:59 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = typing;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.typing = typing;
|