2024-06-26 06:16:20 +02:00
|
|
|
import assert from "minimalistic-assert";
|
2024-01-11 08:18:20 +01:00
|
|
|
import {z} from "zod";
|
|
|
|
|
2021-04-15 18:51:44 +02:00
|
|
|
import * as channel from "./channel";
|
|
|
|
import * as stream_topic_history from "./stream_topic_history";
|
|
|
|
|
2024-01-11 08:18:20 +01:00
|
|
|
const stream_topic_history_response_schema = z.object({
|
|
|
|
topics: z.array(
|
|
|
|
z.object({
|
|
|
|
name: z.string(),
|
|
|
|
max_id: z.number(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
export function get_server_history(stream_id: number, on_success: () => void): void {
|
2021-04-15 18:51:44 +02:00
|
|
|
if (stream_topic_history.has_history_for(stream_id)) {
|
|
|
|
on_success();
|
|
|
|
return;
|
|
|
|
}
|
2023-10-09 16:19:56 +02:00
|
|
|
if (stream_topic_history.is_request_pending_for(stream_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-15 18:51:44 +02:00
|
|
|
|
2023-10-09 16:19:56 +02:00
|
|
|
stream_topic_history.add_request_pending_for(stream_id);
|
2021-04-15 18:51:44 +02:00
|
|
|
const url = "/json/users/me/" + stream_id + "/topics";
|
|
|
|
|
2024-01-11 08:18:20 +01:00
|
|
|
void channel.get({
|
2021-04-15 18:51:44 +02:00
|
|
|
url,
|
|
|
|
data: {},
|
2024-06-21 20:26:02 +02:00
|
|
|
success(raw_data) {
|
|
|
|
const data = stream_topic_history_response_schema.parse(raw_data);
|
|
|
|
const server_history = data.topics;
|
2021-04-15 18:51:44 +02:00
|
|
|
stream_topic_history.add_history(stream_id, server_history);
|
2023-10-09 16:19:56 +02:00
|
|
|
stream_topic_history.remove_request_pending_for(stream_id);
|
2021-04-15 18:51:44 +02:00
|
|
|
on_success();
|
|
|
|
},
|
2023-10-09 16:19:56 +02:00
|
|
|
error() {
|
|
|
|
stream_topic_history.remove_request_pending_for(stream_id);
|
|
|
|
},
|
2021-04-15 18:51:44 +02:00
|
|
|
});
|
|
|
|
}
|
2024-06-26 06:16:20 +02:00
|
|
|
|
|
|
|
export function update_topic_last_message_id(
|
|
|
|
stream_id: number,
|
|
|
|
topic_name: string,
|
|
|
|
update_dom_on_success: () => void,
|
|
|
|
): void {
|
|
|
|
void channel.get({
|
|
|
|
url: "/json/messages",
|
|
|
|
data: {
|
|
|
|
narrow: JSON.stringify([
|
|
|
|
{operator: "stream", operand: stream_id},
|
|
|
|
{operator: "topic", operand: topic_name},
|
|
|
|
]),
|
|
|
|
anchor: "newest",
|
|
|
|
num_before: 1,
|
|
|
|
num_after: 0,
|
|
|
|
},
|
|
|
|
success(data) {
|
|
|
|
const {messages} = z
|
|
|
|
.object({
|
|
|
|
messages: z.array(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.parse(data);
|
|
|
|
if (messages.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const last_message = messages[0];
|
|
|
|
assert(last_message !== undefined);
|
|
|
|
stream_topic_history.add_history(stream_id, [
|
|
|
|
{
|
|
|
|
name: topic_name,
|
|
|
|
max_id: last_message.id,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
update_dom_on_success();
|
|
|
|
},
|
|
|
|
error() {
|
|
|
|
// Ideally we would retry since we should always be able to get a success response
|
|
|
|
// from the server for this request, but for now we just ignore the error.
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|