message flags: Convert module to typescript.

This commit is contained in:
evykassirer 2023-12-28 18:09:36 -08:00 committed by Tim Abbott
parent a87b1d5ad8
commit 08bee0f180
2 changed files with 32 additions and 15 deletions

View File

@ -1,10 +1,13 @@
import type {DebouncedFunc} from "lodash";
import _ from "lodash"; import _ from "lodash";
import {z} from "zod";
import * as channel from "./channel"; import * as channel from "./channel";
import type {Message} from "./message_store";
import * as starred_messages from "./starred_messages"; import * as starred_messages from "./starred_messages";
export function send_flag_update_for_messages(msg_ids, flag, op) { export function send_flag_update_for_messages(msg_ids: number[], flag: string, op: string): void {
channel.post({ void channel.post({
url: "/json/messages/flags", url: "/json/messages/flags",
data: { data: {
messages: JSON.stringify(msg_ids), messages: JSON.stringify(msg_ids),
@ -16,9 +19,9 @@ export function send_flag_update_for_messages(msg_ids, flag, op) {
export const _unread_batch_size = 1000; export const _unread_batch_size = 1000;
export const send_read = (function () { export const send_read = (function () {
let queue = []; let queue: Message[] = [];
let start; let start: DebouncedFunc<() => void>;
function server_request() { function server_request(): void {
// Wait for server IDs before sending flags // Wait for server IDs before sending flags
const real_msgs = queue.filter((msg) => !msg.locally_echoed); const real_msgs = queue.filter((msg) => !msg.locally_echoed);
const real_msg_ids = real_msgs.map((msg) => msg.id); const real_msg_ids = real_msgs.map((msg) => msg.id);
@ -33,7 +36,7 @@ export const send_read = (function () {
// We have some real IDs. If there are any left in the queue when this // We have some real IDs. If there are any left in the queue when this
// call finishes, they will be handled in the success callback. // call finishes, they will be handled in the success callback.
channel.post({ void channel.post({
url: "/json/messages/flags", url: "/json/messages/flags",
data: {messages: JSON.stringify(real_msg_ids_batch), op: "add", flag: "read"}, data: {messages: JSON.stringify(real_msg_ids_batch), op: "add", flag: "read"},
success() { success() {
@ -49,7 +52,7 @@ export const send_read = (function () {
start = _.throttle(server_request, 1000); start = _.throttle(server_request, 1000);
function add(messages) { function add(messages: Message[]): void {
queue = [...queue, ...messages]; queue = [...queue, ...messages];
start(); start();
} }
@ -57,28 +60,39 @@ export const send_read = (function () {
return add; return add;
})(); })();
export function mark_as_read(message_ids) { export function mark_as_read(message_ids: number[]): void {
send_flag_update_for_messages(message_ids, "read", "add"); send_flag_update_for_messages(message_ids, "read", "add");
} }
export function mark_as_unread(message_ids) { export function mark_as_unread(message_ids: number[]): void {
send_flag_update_for_messages(message_ids, "read", "remove"); send_flag_update_for_messages(message_ids, "read", "remove");
} }
export function save_collapsed(message) { export function save_collapsed(message: Message): void {
send_flag_update_for_messages([message.id], "collapsed", "add"); send_flag_update_for_messages([message.id], "collapsed", "add");
} }
export function save_uncollapsed(message) { export function save_uncollapsed(message: Message): void {
send_flag_update_for_messages([message.id], "collapsed", "remove"); send_flag_update_for_messages([message.id], "collapsed", "remove");
} }
export function unstar_all_messages() { export function unstar_all_messages(): void {
const starred_msg_ids = starred_messages.get_starred_msg_ids(); const starred_msg_ids = starred_messages.get_starred_msg_ids();
send_flag_update_for_messages(starred_msg_ids, "starred", "remove"); send_flag_update_for_messages(starred_msg_ids, "starred", "remove");
} }
export function unstar_all_messages_in_topic(stream_id, topic) { // While we're parsing message objects, our code only looks at the
// IDs. TODO: Use a shared zod schema for parsing messages if/when
// message_fetch.ts parses message objects using zod.
const message_response_schema = z.object({
messages: z.array(
z.object({
id: z.number(),
}),
),
});
export function unstar_all_messages_in_topic(stream_id: number, topic: string): void {
const data = { const data = {
anchor: "newest", anchor: "newest",
// In the unlikely event the user has >1000 starred messages // In the unlikely event the user has >1000 starred messages
@ -94,10 +108,11 @@ export function unstar_all_messages_in_topic(stream_id, topic) {
]), ]),
}; };
channel.get({ void channel.get({
url: "/json/messages", url: "/json/messages",
data, data,
success(data) { success(raw_data) {
const data = message_response_schema.parse(raw_data);
const messages = data.messages; const messages = data.messages;
const starred_message_ids = messages.map((message) => message.id); const starred_message_ids = messages.map((message) => message.id);
send_flag_update_for_messages(starred_message_ids, "starred", "remove"); send_flag_update_for_messages(starred_message_ids, "starred", "remove");

View File

@ -109,6 +109,8 @@ export type Message = (
// Added in `reactions.set_clean_reactions`. // Added in `reactions.set_clean_reactions`.
clean_reactions: Map<string, MessageCleanReaction>; clean_reactions: Map<string, MessageCleanReaction>;
locally_echoed?: boolean;
// Added in `message_helper.process_new_message`. // Added in `message_helper.process_new_message`.
sent_by_me: boolean; sent_by_me: boolean;
reply_to: string; reply_to: string;