-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
typing.js
122 lines (104 loc) · 3.58 KB
/
typing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import $ from "jquery";
import * as typing_status from "../shared/js/typing_status";
import * as channel from "./channel";
import * as compose_pm_pill from "./compose_pm_pill";
import * as compose_state from "./compose_state";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as stream_topic_history from "./stream_topic_history";
// 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.
//
// See docs/subsystems/typing-indicators.md for details on typing indicators.
function send_typing_notification_ajax(data) {
channel.post({
url: "/json/typing",
data,
success() {},
error(xhr) {
blueslip.warn("Failed to send typing event: " + xhr.responseText);
},
});
}
function send_pm_typing_notification(user_ids_array, operation) {
const data = {
to: JSON.stringify(user_ids_array),
op: operation,
};
send_typing_notification_ajax(data);
}
function send_stream_typing_notification(stream_id, topic, operation) {
const data = {
stream_id,
topic,
op: operation,
};
send_typing_notification_ajax(data);
}
function send_typing_notification_based_on_msg_type(to, operation) {
const message_type = compose_state.get_message_type();
if (Array.isArray(to) && message_type === "private") {
const user_ids_array = to;
send_pm_typing_notification(user_ids_array, operation);
} else if (message_type === "stream") {
send_stream_typing_notification(to.stream_id, to.topic, operation);
}
}
function get_user_ids_array() {
const user_ids_string = compose_pm_pill.get_user_ids_string();
if (user_ids_string === "") {
return null;
}
return people.user_ids_string_to_ids_array(user_ids_string);
}
function is_valid_conversation() {
const compose_empty = !compose_state.has_message_content();
if (compose_empty) {
return false;
}
return true;
}
function get_current_time() {
return Date.now();
}
function notify_server_start(to) {
send_typing_notification_based_on_msg_type(to, "start");
}
function notify_server_stop(to) {
send_typing_notification_based_on_msg_type(to, "stop");
}
export function get_recipient() {
const message_type = compose_state.get_message_type();
if (message_type === "private") {
return get_user_ids_array();
}
if (message_type === "stream") {
const stream_name = compose_state.stream_name();
const stream_id = stream_data.get_stream_id(stream_name);
const topic = compose_state.topic();
if (!stream_topic_history.get_recent_topic_names(stream_id).includes(topic)) {
return null; // Send notification only if topic already exists.
}
return {stream_id, topic};
}
return null;
}
export function initialize() {
const worker = {
get_current_time,
notify_server_start,
notify_server_stop,
};
$(document).on("input", "#compose-textarea", () => {
// If our previous state was no typing notification, send a
// start-typing notice immediately.
const new_recipient = is_valid_conversation() ? get_recipient() : null;
typing_status.update(worker, new_recipient);
});
// We send a stop-typing notification immediately when compose is
// closed/cancelled
$(document).on("compose_canceled.zulip compose_finished.zulip", () => {
typing_status.update(worker, null);
});
}