Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

2.1 KB
Raw Blame
import assert from "minimalistic-assert";

import type {LocalMessage} from "./message_helper.ts";

const waiting_for_id = new Map<string, LocalMessage>();
let waiting_for_ack = new Map<string, LocalMessage>();

export function set_message_waiting_for_id(local_id: string, message: LocalMessage): void {
    waiting_for_id.set(local_id, message);
}

export function set_message_waiting_for_ack(local_id: string, message: LocalMessage): void {
    waiting_for_ack.set(local_id, message);
}

export function get_message_waiting_for_id(local_id: string): LocalMessage | undefined {
    return waiting_for_id.get(local_id);
}

export function get_message_waiting_for_ack(local_id: string): LocalMessage | undefined {
    return waiting_for_ack.get(local_id);
}

export function remove_message_from_waiting_for_id(local_id: string): void {
    waiting_for_id.delete(local_id);
}

export function remove_message_from_waiting_for_ack(local_id: string): void {
    waiting_for_ack.delete(local_id);
}

export function _patch_waiting_for_ack(data: Map<string, LocalMessage>): void {
    // Only for testing
    waiting_for_ack = data;
}

export function get_waiting_for_ack_private_messages(): LocalMessage[] {
    return waiting_for_ack
        .values()
        .filter((message) => message.type === "private")
        .toArray();
}

export function get_waiting_for_ack_local_ids_by_topic(channel_id: number): Map<string, number> {
    const max_message_id_by_topic = new Map<string, number>();

    const channel_messages_waiting_for_ack = waiting_for_ack
        .values()
        .filter((message) => message.type === "stream" && message.stream_id === channel_id)
        .toArray();

    for (const message of channel_messages_waiting_for_ack) {
        assert(message.type === "stream");
        const topic = message.topic;
        const existing_id = max_message_id_by_topic.get(topic);

        // Here we're accessing message.id === float(message.local_id),
        // since these are all local message IDs.
        if (existing_id === undefined || message.id > existing_id) {
            max_message_id_by_topic.set(topic, message.id);
        }
    }
    return max_message_id_by_topic;
}