compose_preview: Render link previews in message preview.

Earlier, link previews weren't rendered for message preview even
for realms having preview option turned on.

This commit adds the link preview feature for message previews.
We render message two times, once without previews and then adding
previews to the render.

Fixes: zulip#19465.
This commit is contained in:
Pratik Chanda 2024-09-22 20:16:45 +05:30
parent 3aed247e1c
commit ee5267db8c
2 changed files with 27 additions and 4 deletions

View File

@ -67,6 +67,7 @@ const message_render_response_schema = z.object({
msg: z.string(),
result: z.string(),
rendered: z.string(),
rerender: z.boolean(),
});
export let compose_spinner_visible = false;
@ -1210,16 +1211,19 @@ export function render_and_show_preview(
rendered_markdown.update_elements($preview_content_box);
}
function render_message(content: string): void {
function render_message(content: string, render_link: boolean): void {
void channel.post({
url: "/json/messages/render",
data: {content},
data: {content, render_link},
success(response_data) {
const data = message_render_response_schema.parse(response_data);
if (markdown.contains_backend_only_syntax(content)) {
loading.destroy_indicator($preview_spinner);
}
show_preview(data.rendered, content);
if (data.rerender) {
render_message(content, true);
}
},
error() {
if (markdown.contains_backend_only_syntax(content)) {
@ -1247,6 +1251,6 @@ export function render_and_show_preview(
// authoritative backend rendering from the server).
markdown.render(content);
}
render_message(content);
render_message(content, false);
}
}

View File

@ -30,6 +30,7 @@ from zerver.lib.zcommand import process_zcommands
from zerver.lib.zephyr import compute_mit_user_fullname
from zerver.models import Client, Message, RealmDomain, UserProfile
from zerver.models.users import get_user_including_cross_realm
from zerver.worker.embed_links import get_url_embed_data
class InvalidMirrorInputError(Exception):
@ -284,6 +285,7 @@ def render_message_backend(
user_profile: UserProfile,
*,
content: str,
render_link: Json[bool],
) -> HttpResponse:
message = Message()
message.sender = user_profile
@ -294,4 +296,21 @@ def render_message_backend(
message.sending_client = client
rendering_result = render_message_markdown(message, content, realm=user_profile.realm)
return json_success(request, data={"rendered": rendering_result.rendered_content})
links_for_embed: set[str] = set()
links_for_embed |= rendering_result.links_for_preview
if render_link is True:
rendering_result = render_message_markdown(
message,
content,
realm=user_profile.realm,
url_embed_data=get_url_embed_data(list(links_for_embed)),
)
links_for_embed.clear()
return json_success(
request,
data={
"rendered": rendering_result.rendered_content,
"rerender": bool(links_for_embed),
},
)