-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
339 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{: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 {}))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/status_im/ui/screens/link_previews_settings/styles.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
Oops, something went wrong.