From 4edeba2775f7d6b17c8da4f5974139d86e00df47 Mon Sep 17 00:00:00 2001 From: Nikhil Kothari Date: Fri, 10 May 2024 19:52:27 +0530 Subject: [PATCH] fix: show twitter link previews --- .../feature/chat/ChatMessage/MessageItem.tsx | 4 ++-- .../ChatMessage/Renderers/TiptapRenderer/Link.tsx | 8 ++++---- raven/api/preview_links.py | 12 ++++++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/raven-app/src/components/feature/chat/ChatMessage/MessageItem.tsx b/raven-app/src/components/feature/chat/ChatMessage/MessageItem.tsx index 890f3aace..2f6869f5b 100644 --- a/raven-app/src/components/feature/chat/ChatMessage/MessageItem.tsx +++ b/raven-app/src/components/feature/chat/ChatMessage/MessageItem.tsx @@ -87,9 +87,9 @@ export const MessageItem = ({ message, setDeleteMessage, isHighlighted, onReplyM transition-colors p-2 rounded-md`, isHighlighted ? 'bg-yellow-50 hover:bg-yellow-50 dark:bg-yellow-300/20 dark:hover:bg-yellow-300/20' : '')}> - + - + {!is_continuation ? { const linkPreview = data?.message?.[0] return - {linkPreview ? linkPreview.site_name && linkPreview.description ? + {linkPreview ? linkPreview.site_name && linkPreview.description ? {(linkPreview.absolute_image || linkPreview.image) && @@ -116,10 +116,10 @@ export const LinkPreview = memo(({ messageID }: { messageID: string }) => { } - - + + - + {linkPreview.title} {linkPreview.site_name} diff --git a/raven/api/preview_links.py b/raven/api/preview_links.py index 30997f040..f018e7214 100644 --- a/raven/api/preview_links.py +++ b/raven/api/preview_links.py @@ -2,7 +2,7 @@ import re import frappe -from linkpreview import link_preview +from linkpreview import Link, LinkGrabber, LinkPreview, link_preview @frappe.whitelist(methods=["GET"]) @@ -39,7 +39,15 @@ def get_preview_link(urls): else: preview = None try: - preview = link_preview(url) + # If this is a Twitter/X URL, then we need to fetch the preview from the Twitter API + # This is because the linkpreview library doesn't support Twitter previews with the default bot + if "twitter.com" in url or "x.com" in url: + grabber = LinkGrabber() + content, url_fetched = grabber.get_content(url, headers="imessagebot") + link = Link(url_fetched, content) + preview = LinkPreview(link) + else: + preview = link_preview(url) except Exception: preview = None pass