-
Notifications
You must be signed in to change notification settings - Fork 985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Link preview generation #11311
Link preview generation #11311
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns status-im.chat.models.link-preview | ||
(:require [re-frame.core :as re-frame] | ||
[status-im.utils.fx :as fx] | ||
[status-im.multiaccounts.update.core :as multiaccounts.update] | ||
[status-im.ethereum.json-rpc :as json-rpc] | ||
[taoensso.timbre :as log])) | ||
|
||
(fx/defn enable | ||
{:events [::enable]} | ||
[{{:keys [multiaccount]} :db :as cofx} site enabled?] | ||
(fx/merge cofx | ||
(multiaccounts.update/multiaccount-update | ||
:link-previews-enabled-sites | ||
(if enabled? | ||
(conj (get multiaccount :link-previews-enabled-sites #{}) site) | ||
(disj (get multiaccount :link-previews-enabled-sites #{}) site)) | ||
{}))) | ||
|
||
(fx/defn load-link-preview-data | ||
{:events [::load-link-preview-data]} | ||
[cofx link] | ||
(fx/merge cofx | ||
{::json-rpc/call [{:method (json-rpc/call-ext-method "getLinkPreviewData") | ||
:params [link] | ||
:on-success #(re-frame/dispatch [::cache-link-preview-data link %]) | ||
:on-error #(log/error "Can't get preview data for " link)}]})) | ||
|
||
(fx/defn cache-link-preview-data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you pls elaborate on this ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. During session we save locally data about link preview to not fetch it every time when chat re-creates |
||
{:events [::cache-link-preview-data]} | ||
[{{:keys [multiaccount]} :db :as cofx} site {:keys [error] :as data}] | ||
(when-not error | ||
(multiaccounts.update/optimistic | ||
cofx | ||
:link-previews-cache | ||
(assoc (get multiaccount :link-previews-cache {}) site data)))) | ||
|
||
(fx/defn should-suggest-link-preview | ||
{:events [::should-suggest-link-preview]} | ||
[{:keys [db] :as cofx} enabled?] | ||
(multiaccounts.update/multiaccount-update | ||
cofx | ||
:link-preview-request-enabled (boolean enabled?) | ||
{})) | ||
|
||
(fx/defn request-link-preview-whitelist | ||
[_] | ||
{::json-rpc/call [{:method (json-rpc/call-ext-method "getLinkPreviewWhitelist") | ||
:params [] | ||
:on-success #(re-frame/dispatch [::link-preview-whitelist-received %]) | ||
:on-error #(log/error "Failed to get link preview whitelist")}]}) | ||
|
||
(fx/defn save-link-preview-whitelist | ||
{:events [::link-preview-whitelist-received]} | ||
[cofx whitelist] | ||
(fx/merge cofx | ||
(multiaccounts.update/multiaccount-update | ||
:link-previews-whitelist whitelist {}))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,8 @@ | |
"wakuext_sendEmojiReaction" {} | ||
"wakuext_sendEmojiReactionRetraction" {} | ||
"wakuext_emojiReactionsByChatID" {} | ||
"wakuext_getLinkPreviewWhitelist" {} | ||
"wakuext_getLinkPreviewData" {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably better to move this to accounts or settings service |
||
;;TODO not used anywhere? | ||
"wakuext_deleteChat" {} | ||
"wakuext_saveContact" {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
(ns status-im.ui.screens.chat.message.link-preview | ||
(:require [re-frame.core :as re-frame] | ||
[clojure.string :as string] | ||
[status-im.ui.components.react :as react] | ||
[quo.core :as quo] | ||
[status-im.utils.security :as security] | ||
[status-im.i18n :as i18n] | ||
[status-im.ui.screens.chat.message.styles :as styles] | ||
[status-im.react-native.resources :as resources] | ||
[status-im.chat.models.link-preview :as link-preview]) | ||
(:require-macros [status-im.utils.views :refer [defview letsubs]])) | ||
|
||
(defn link-belongs-to-domain [link domain] | ||
(cond | ||
(string/starts-with? link (str "https://" domain)) true | ||
(string/starts-with? link (str "https://www." domain)) true | ||
:else false)) | ||
|
||
(defn domain-info-if-whitelisted [link whitelist] | ||
(first (filter | ||
#(link-belongs-to-domain link (:address %)) | ||
whitelist))) | ||
|
||
(defn link-extended-info [link whitelist enabled-list] | ||
(let [domain-info (domain-info-if-whitelisted link whitelist)] | ||
{:whitelisted (not (nil? domain-info)) | ||
:enabled (contains? enabled-list (:title domain-info)) | ||
:link link})) | ||
|
||
(defn previewable-link [links whitelist enabled-list] | ||
(->> links | ||
(map #(link-extended-info % whitelist enabled-list)) | ||
(filter #(:whitelisted %)) | ||
(first))) | ||
|
||
(defview link-preview-enable-request [] | ||
[react/view (styles/link-preview-request-wrapper) | ||
[react/view {:margin 12} | ||
[react/image {:source (resources/get-theme-image :unfurl) | ||
:style styles/link-preview-request-image}] | ||
[quo/text {:size :small | ||
:align :center | ||
:style {:margin-top 6}} | ||
(i18n/label :t/enable-link-previews)] | ||
[quo/text {:size :small | ||
:color :secondary | ||
:align :center | ||
:style {:margin-top 2}} | ||
(i18n/label :t/once-enabled-share-metadata)]] | ||
[quo/separator] | ||
[quo/button {:on-press #(re-frame/dispatch [:navigate-to :link-preview-settings]) | ||
:type :secondary} | ||
(i18n/label :enable)] | ||
[quo/separator] | ||
[quo/button {:on-press #(re-frame/dispatch | ||
[::link-preview/should-suggest-link-preview false]) | ||
:type :secondary} | ||
(i18n/label :t/dont-ask)]]) | ||
|
||
(defview link-preview-loader [link outgoing] | ||
(letsubs [cache [:link-preview/cache]] | ||
(let [{:keys [site title thumbnailUrl] :as preview-data} (get cache link)] | ||
(if (not preview-data) | ||
(do | ||
(re-frame/dispatch | ||
[::link-preview/load-link-preview-data link]) | ||
nil) | ||
|
||
[react/touchable-highlight | ||
{:on-press #(when (and (security/safe-link? link)) | ||
(re-frame/dispatch | ||
[:browser.ui/message-link-pressed link]))} | ||
|
||
[react/view (styles/link-preview-wrapper outgoing) | ||
[react/image {:source {:uri thumbnailUrl} | ||
:style (styles/link-preview-image outgoing) | ||
:accessibility-label :member-photo}] | ||
[quo/text {:size :small | ||
:style styles/link-preview-title} | ||
title] | ||
[quo/text {:size :small | ||
:color :secondary | ||
:style styles/link-preview-site} | ||
site]]])))) | ||
|
||
(defview link-preview-wrapper [links outgoing] | ||
(letsubs | ||
[ask-user? [:link-preview/link-preview-request-enabled] | ||
whitelist [:link-preview/whitelist] | ||
enabled-sites [:link-preview/enabled-sites]] | ||
(let [link-info (previewable-link links whitelist enabled-sites) | ||
{:keys [link whitelisted enabled]} link-info] | ||
(when (and link whitelisted) | ||
(if enabled | ||
[link-preview-loader link outgoing] | ||
(when ask-user? | ||
[link-preview-enable-request])))))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(ns status-im.ui.screens.link-previews-settings.styles) | ||
|
||
(def link-preview-settings-image | ||
{:height 100 | ||
:align-self :center | ||
:margin-top 16}) | ||
|
||
(def whitelist-container | ||
{:flex-direction :row | ||
:justify-content :space-between}) | ||
|
||
(def enable-all | ||
{:align-self :flex-end}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{:events [:link-preview/enable]}