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.

compose: Poll for thumbnail availability in preview.

santhoshkumarCodes966193d869bf
diff --git a/web/src/compose.ts b/web/src/compose.ts
index bcdc8be7904..dab09870587 100644
--- a/web/src/compose.ts
+++ b/web/src/compose.ts
@@ -84,6 +84,8 @@ export function clear_preview_area(): void {
// so here we are re-enabling those compose_control_buttons
$("#compose").removeClass("preview_mode");
$("#compose .preview_mode_disabled .compose_control_button").attr("tabindex", 0);
+
+ compose_ui.clear_thumbnail_polling();
}
export function show_preview_area(): void {
diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.ts
index bdd8f06c8ad..102a29779f5 100644
--- a/web/src/compose_ui.ts
+++ b/web/src/compose_ui.ts
@@ -1473,6 +1473,112 @@ export function show_compose_spinner(): void {
$(".compose-submit-button").addClass("compose-button-disabled");
}
+let thumbnail_poll_timeout: ReturnType<typeof setTimeout> | null = null;
+let pending_thumbnail_paths = new Set<string>();
+const MAX_THUMBNAIL_RETRIES = 5;
+
+function extract_thumbnail_paths($preview_content: JQuery): Set<string> {
+ const paths = new Set<string>();
+
+ $preview_content.find(".image-loading-placeholder").each(function () {
+ const $img = $(this);
+ const $link = $img.closest("a");
+ const href = $link.attr("href");
+
+ if (href?.startsWith("/user_uploads/")) {
+ paths.add(href.slice("/user_uploads/".length));
+ }
+ });
+
+ return paths;
+}
+
+async function check_thumbnail_status(path_id: string): Promise<boolean> {
+ const thumbnail_status_schema = z.object({
+ has_thumbnail: z.boolean(),
+ });
+
+ try {
+ const response: unknown = await channel.get({
+ url: `/json/thumbnail/status/${path_id}`,
+ });
+ const data = thumbnail_status_schema.parse(response);
+ return data.has_thumbnail;
+ } catch {
+ return false;
+ }
+}
+
+async function poll_thumbnail_status(
+ $preview_container: JQuery,
+ $preview_spinner: JQuery,
+ $preview_content_box: JQuery,
+ content: string,
+ attempt = 1,
+): Promise<void> {
+ if (attempt > MAX_THUMBNAIL_RETRIES) {
+ pending_thumbnail_paths.clear();
+ return;
+ }
+
+ if (pending_thumbnail_paths.size === 0 || !$preview_container.hasClass("preview_mode")) {
+ return;
+ }
+
+ // Check all pending thumbnails in parallel
+ const thumbnails_to_check = [...pending_thumbnail_paths];
+ const thumbnail_status_results = await Promise.all(
+ thumbnails_to_check.map(async (path_id) => ({
+ path_id,
+ ready: await check_thumbnail_status(path_id),
+ })),
+ );
+
+ // Remove thumbnails that are now ready
+ let any_thumbnail_ready = false;
+ for (const {path_id, ready} of thumbnail_status_results) {
+ if (ready) {
+ pending_thumbnail_paths.delete(path_id);
+ any_thumbnail_ready = true;
+ }
+ }
+
+ // We check preview mode again since the user could have exited preview
+ // while we were waiting for the thumbnail status
+ if ($preview_container.hasClass("preview_mode")) {
+ if (any_thumbnail_ready) {
+ render_and_show_preview(
+ $preview_container,
+ $preview_spinner,
+ $preview_content_box,
+ content,
+ );
+ return;
+ }
+
+ if (pending_thumbnail_paths.size > 0) {
+ const retry_delay_secs = util.get_retry_backoff_seconds(undefined, attempt, true);
+ thumbnail_poll_timeout = setTimeout(() => {
+ void poll_thumbnail_status(
+ $preview_container,
+ $preview_spinner,
+ $preview_content_box,
+ content,
+ attempt + 1,
+ );
+ }, retry_delay_secs * 1000);
+ }
+ }
+}
+
+export function clear_thumbnail_polling(): void {
+ if (thumbnail_poll_timeout !== null) {
+ clearTimeout(thumbnail_poll_timeout);
+ thumbnail_poll_timeout = null;
+ }
+ pending_thumbnail_paths.clear();
+}
+
export function render_and_show_preview(
$preview_container: JQuery,
$preview_spinner: JQuery,
@@ -1499,6 +1605,19 @@ export function render_and_show_preview(
$preview_content_box.html(postprocess_content(rendered_preview_html));
rendered_markdown.update_elements($preview_content_box);
+
+ // Check for thumbnail loading placeholders and start polling
+ clear_thumbnail_polling();
+ pending_thumbnail_paths = extract_thumbnail_paths($preview_content_box);
+
+ if (pending_thumbnail_paths.size > 0) {
+ void poll_thumbnail_status(
+ $preview_container,
+ $preview_spinner,
+ $preview_content_box,
+ content,
+ );
+ }
}
if (content.length === 0) {
diff --git a/web/tests/compose.test.cjs b/web/tests/compose.test.cjs
index d216807a61c..c5ffaf6830d 100644
--- a/web/tests/compose.test.cjs
+++ b/web/tests/compose.test.cjs
@@ -685,6 +685,11 @@ test_ui("on_events", ({override, override_rewire}) => {
})();
(function test_markdown_preview_compose_clicked() {
+ $("#compose .preview_content").set_find_results(
+ ".image-loading-placeholder",
+ $.create("no-images", {elements: []}),
+ );
+
function setup_mock_markdown_contains_backend_only_syntax(msg_content, return_val) {
override(markdown, "contains_backend_only_syntax", (msg) => {
assert.equal(msg, msg_content);