diff --git a/frappe/public/js/frappe/social/components/Post.vue b/frappe/public/js/frappe/social/components/Post.vue index 5b9d4fcbbe..2e25675150 100644 --- a/frappe/public/js/frappe/social/components/Post.vue +++ b/frappe/public/js/frappe/social/components/Post.vue @@ -128,20 +128,21 @@ export default { frappe.db.insert(comment); }, generate_preview(link_element) { - // TODO: create meta parse and move the code to separate component - fetch(`https://opengraph.io/api/1.1/site/${link_element.href}?app_id=5bc7fee2a2677843003b8bba`).then(res => { - res.json().then(data => { - const og = data['openGraph'] + // TODO: move the code to separate component + frappe.xcall('frappe.social.doctype.post.post.get_link_info', { + 'url': link_element.href + }).then(info => { + if (info['og:title'] || info['title']) { link_element.insertAdjacentHTML('afterend', ` -
+ ` ); - }); + } }) .catch(console.error) } diff --git a/frappe/public/less/social.less b/frappe/public/less/social.less index df8719b857..a8535a241f 100644 --- a/frappe/public/less/social.less +++ b/frappe/public/less/social.less @@ -74,6 +74,7 @@ body[data-route*="social"] { .post-card { .generic-card(); + max-width: 600px; .post-body { padding: 15px; .user-name { @@ -114,16 +115,21 @@ body[data-route*="social"] { border-top: 1px solid @border-color; } .preview-card { + text-decoration: none; height: 150px; border: 1px solid @light-border-color; border-radius: 5px; margin-top: 15px; padding-right: 10px; background: white; + display: flex; img { - height: 100%; border: none; - margin-left: 10px; + margin: 0 10px; + max-width: 150px; + height: auto; + max-height: 90%; + align-self: center; } } } diff --git a/frappe/social/doctype/post/post.py b/frappe/social/doctype/post/post.py index abf5bd3ab3..dcf7154e1b 100644 --- a/frappe/social/doctype/post/post.py +++ b/frappe/social/doctype/post/post.py @@ -34,3 +34,30 @@ def frequently_visited_links(): return frappe.get_all('Route History', fields=['route', 'count(name) as count'], filters={ 'user': frappe.session.user }, group_by="route", order_by="count desc", limit=10) + +@frappe.whitelist() +def get_link_info(url): + cached_link_info = frappe.cache().hget("link_info", url) + if cached_link_info: + return cached_link_info + + from bs4 import BeautifulSoup + import requests + try: + page = requests.get(url) + except: + frappe.cache().hset("link_info", url, {}) + return {} + + soup = BeautifulSoup(page.text) + + meta_obj = {} + for meta in soup.findAll('meta'): + meta_name = meta.get('property') or meta.get('name', '').lower() + if meta_name: + meta_obj[meta_name] = meta.get('content') + + frappe.cache().hset("link_info", url, meta_obj) + + return meta_obj +