zulip/static/js/typing_events.js

85 lines
2.9 KiB
JavaScript
Raw Normal View History

const render_typing_notifications = require("../templates/typing_notifications.hbs");
// See docs/subsystems/typing-indicators.md for details on typing indicators.
2017-09-25 20:33:29 +02:00
2017-03-22 15:11:41 +01:00
// This code handles the inbound side of typing notifications.
// When another user is typing, we process the events here.
//
// We also handle the local event of re-narrowing.
// (For the outbound code, see typing.js.)
// How long before we assume a client has gone away
// and expire its typing status
const TYPING_STARTED_EXPIRY_PERIOD = 15000; // 15s
2017-03-22 15:11:41 +01:00
// Note!: There are also timing constants in typing_status.js
// that make typing indicators work.
function get_users_typing_for_narrow() {
if (!narrow_state.narrowed_to_pms()) {
2017-03-22 15:11:41 +01:00
// Narrow is neither pm-with nor is: private
return [];
}
const first_term = narrow_state.operators()[0];
if (first_term.operator === "pm-with") {
2017-03-22 15:11:41 +01:00
// Get list of users typing in this conversation
const narrow_emails_string = first_term.operand;
2017-03-22 15:11:41 +01:00
// TODO: Create people.emails_strings_to_user_ids.
const narrow_user_ids_string = people.reply_to_to_user_ids_string(narrow_emails_string);
if (!narrow_user_ids_string) {
return [];
}
const narrow_user_ids = narrow_user_ids_string.split(",").map((user_id_string) => parseInt(user_id_string, 10));
const group = narrow_user_ids.concat([page_params.user_id]);
2017-03-22 15:11:41 +01:00
return typing_data.get_group_typists(group);
}
// Get all users typing (in all private conversations with current user)
return typing_data.get_all_typists();
}
exports.render_notifications_for_narrow = function () {
const user_ids = get_users_typing_for_narrow();
const users_typing = user_ids.map(people.get_by_user_id);
2017-03-22 15:11:41 +01:00
if (users_typing.length === 0) {
$("#typing_notifications").hide();
2017-03-22 15:11:41 +01:00
} else {
$("#typing_notifications").html(render_typing_notifications({users: users_typing}));
$("#typing_notifications").show();
2017-03-22 15:11:41 +01:00
}
};
2017-03-22 15:11:41 +01:00
exports.hide_notification = function (event) {
const recipients = event.recipients.map((user) => user.user_id);
2017-03-22 15:11:41 +01:00
recipients.sort();
typing_data.clear_inbound_timer(recipients);
2017-03-22 15:11:41 +01:00
const removed = typing_data.remove_typist(recipients, event.sender.user_id);
2017-03-22 15:11:41 +01:00
if (removed) {
exports.render_notifications_for_narrow();
2017-03-22 15:11:41 +01:00
}
};
exports.display_notification = function (event) {
const recipients = event.recipients.map((user) => user.user_id);
2017-03-22 15:11:41 +01:00
recipients.sort();
const sender_id = event.sender.user_id;
event.sender.name = people.get_by_user_id(sender_id).full_name;
2017-03-22 15:11:41 +01:00
typing_data.add_typist(recipients, sender_id);
exports.render_notifications_for_narrow();
typing_data.kickstart_inbound_timer(
recipients,
TYPING_STARTED_EXPIRY_PERIOD,
() => {
exports.hide_notification(event);
},
);
2017-03-22 15:11:41 +01:00
};
window.typing_events = exports;