2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const render_typing_notifications = require("../templates/typing_notifications.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
|
|
|
|
2017-11-08 17:55:36 +01:00
|
|
|
// 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
|
2019-11-02 00:06:25 +01:00
|
|
|
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() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (!narrow_state.narrowed_to_pms()) {
|
2017-03-22 15:11:41 +01:00
|
|
|
// Narrow is neither pm-with nor is: private
|
|
|
|
return [];
|
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const first_term = narrow_state.operators()[0];
|
2020-07-15 01:29:15 +02:00
|
|
|
if (first_term.operator === "pm-with") {
|
2017-03-22 15:11:41 +01:00
|
|
|
// Get list of users typing in this conversation
|
2019-11-02 00:06:25 +01:00
|
|
|
const narrow_emails_string = first_term.operand;
|
2017-03-22 15:11:41 +01:00
|
|
|
// TODO: Create people.emails_strings_to_user_ids.
|
2019-11-02 00:06:25 +01:00
|
|
|
const narrow_user_ids_string = people.reply_to_to_user_ids_string(narrow_emails_string);
|
2017-06-15 17:32:13 +02:00
|
|
|
if (!narrow_user_ids_string) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
const narrow_user_ids = narrow_user_ids_string
|
|
|
|
.split(",")
|
2020-10-07 09:17:30 +02:00
|
|
|
.map((user_id_string) => Number.parseInt(user_id_string, 10));
|
2019-11-02 00:06:25 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:09:51 +02:00
|
|
|
exports.render_notifications_for_narrow = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = get_users_typing_for_narrow();
|
2021-01-23 02:36:54 +01:00
|
|
|
const users_typing = user_ids.map((user_id) => people.get_by_user_id(user_id));
|
2017-03-22 15:11:41 +01:00
|
|
|
if (users_typing.length === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#typing_notifications").hide();
|
2017-03-22 15:11:41 +01:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#typing_notifications").html(render_typing_notifications({users: users_typing}));
|
|
|
|
$("#typing_notifications").show();
|
2017-03-22 15:11:41 +01:00
|
|
|
}
|
2018-08-06 18:09:51 +02:00
|
|
|
};
|
2017-03-22 15:11:41 +01:00
|
|
|
|
|
|
|
exports.hide_notification = function (event) {
|
2020-07-02 01:45:54 +02:00
|
|
|
const recipients = event.recipients.map((user) => user.user_id);
|
2017-03-22 15:11:41 +01:00
|
|
|
recipients.sort();
|
|
|
|
|
2017-03-22 16:20:16 +01:00
|
|
|
typing_data.clear_inbound_timer(recipients);
|
2017-03-22 15:11:41 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const removed = typing_data.remove_typist(recipients, event.sender.user_id);
|
2017-03-22 15:11:41 +01:00
|
|
|
|
|
|
|
if (removed) {
|
2018-08-06 18:09:51 +02:00
|
|
|
exports.render_notifications_for_narrow();
|
2017-03-22 15:11:41 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.display_notification = function (event) {
|
2020-07-02 01:45:54 +02:00
|
|
|
const recipients = event.recipients.map((user) => user.user_id);
|
2017-03-22 15:11:41 +01:00
|
|
|
recipients.sort();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sender_id = event.sender.user_id;
|
2020-02-05 14:30:59 +01:00
|
|
|
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);
|
|
|
|
|
2018-08-06 18:09:51 +02:00
|
|
|
exports.render_notifications_for_narrow();
|
2017-03-22 16:20:16 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
typing_data.kickstart_inbound_timer(recipients, TYPING_STARTED_EXPIRY_PERIOD, () => {
|
|
|
|
exports.hide_notification(event);
|
|
|
|
});
|
2017-03-22 15:11:41 +01:00
|
|
|
};
|
2019-10-25 09:45:13 +02:00
|
|
|
window.typing_events = exports;
|