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: {},
|
|
|
|
success(data) {
|
2024-01-11 08:18:20 +01:00
|
|
|
const clean_data = stream_topic_history_response_schema.parse(data);
|
|
|
|
const server_history = clean_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
|
|
|
});
|
|
|
|
}
|