zulip/web/src/typing.ts

160 lines
4.5 KiB
TypeScript
Raw Normal View History

import $ from "jquery";
import * as typing_status from "../shared/src/typing_status";
2024-01-23 22:15:42 +01:00
import type {Recipient} from "../shared/src/typing_status";
import * as blueslip from "./blueslip";
import * as channel from "./channel";
import * as compose_pm_pill from "./compose_pm_pill";
import * as compose_state from "./compose_state";
import {page_params} from "./page_params";
import * as people from "./people";
import * as stream_data from "./stream_data";
import {user_settings} from "./user_settings";
2020-08-20 21:24:06 +02:00
2024-01-23 22:15:42 +01:00
type TypingAPIRequest = {op: "start" | "stop"} & (
| {
to: string;
}
| {
type: string;
stream_id: string;
topic: string;
}
);
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.
// See docs/subsystems/typing-indicators.md for more details.
2024-01-23 22:15:42 +01:00
function send_typing_notification_ajax(data: TypingAPIRequest): void {
void channel.post({
url: "/json/typing",
data,
error(xhr) {
if (xhr.readyState !== 0) {
blueslip.warn("Failed to send typing event: " + xhr.responseText);
}
},
});
}
2024-01-23 22:15:42 +01:00
function send_direct_message_typing_notification(
user_ids_array: number[],
operation: "start" | "stop",
): void {
const data = {
to: JSON.stringify(user_ids_array),
op: operation,
};
send_typing_notification_ajax(data);
}
2024-01-23 22:15:42 +01:00
function send_stream_typing_notification(
stream_id: number,
topic: string,
operation: "start" | "stop",
): void {
const data = {
type: "stream",
stream_id: JSON.stringify(stream_id),
topic,
op: operation,
};
send_typing_notification_ajax(data);
}
2024-01-23 22:15:42 +01:00
function send_typing_notification_based_on_message_type(
to: Recipient,
operation: "start" | "stop",
): void {
if (to.message_type === "direct" && user_settings.send_private_typing_notifications) {
send_direct_message_typing_notification(to.ids, operation);
} else if (to.message_type === "stream" && user_settings.send_stream_typing_notifications) {
send_stream_typing_notification(to.stream_id, to.topic, operation);
}
}
2024-01-23 22:15:42 +01:00
function get_user_ids_array(): number[] | null {
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);
}
2024-01-23 22:15:42 +01:00
function is_valid_conversation(): boolean {
const compose_empty = !compose_state.has_message_content();
if (compose_empty) {
return false;
}
return true;
}
2024-01-23 22:15:42 +01:00
function get_current_time(): number {
return Date.now();
}
2024-01-23 22:15:42 +01:00
function notify_server_start(to: Recipient): void {
send_typing_notification_based_on_message_type(to, "start");
}
2024-01-23 22:15:42 +01:00
function notify_server_stop(to: Recipient): void {
send_typing_notification_based_on_message_type(to, "stop");
}
2024-01-23 22:15:42 +01:00
export function get_recipient(): Recipient | null {
const message_type = compose_state.get_message_type();
if (message_type === "private") {
return {
message_type: "direct",
2024-01-23 22:15:42 +01:00
ids: get_user_ids_array()!,
};
}
if (message_type === "stream") {
const stream_name = compose_state.stream_name();
2024-01-23 22:15:42 +01:00
const stream_id = stream_data.get_stream_id(stream_name)!;
const topic = compose_state.topic();
return {
message_type: "stream",
stream_id,
topic,
};
}
return null;
}
2024-01-23 22:15:42 +01:00
export function initialize(): void {
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,
page_params.server_typing_started_wait_period_milliseconds,
page_params.server_typing_stopped_wait_period_milliseconds,
);
});
// 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,
page_params.server_typing_started_wait_period_milliseconds,
page_params.server_typing_stopped_wait_period_milliseconds,
);
});
}