From 8e7ab43557e90ac108e61fa52e9e5741e5798607 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Wed, 13 Oct 2021 19:26:39 +0100 Subject: [PATCH] Add link to message --- src/status_im/chat/models.cljs | 21 ++++++++++++ src/status_im/chat/models/loading.cljs | 33 ++++++++++++++++++- src/status_im/data_store/messages.cljs | 11 +++++++ src/status_im/ethereum/json_rpc.cljs | 1 + src/status_im/router/core.cljs | 4 +++ src/status_im/subs.cljs | 1 + src/status_im/ui/screens/chat/views.cljs | 19 +++++++++-- src/status_im/utils/universal_links/core.cljs | 8 +++++ status-go-version.json | 6 ++-- 9 files changed, 97 insertions(+), 7 deletions(-) diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index fb3c37c570..79e0b03e0e 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -228,6 +228,11 @@ [cofx chat-id] (loading/load-messages cofx chat-id)) +(fx/defn preload-chat-message-data + "Takes chat-id and coeffects map, returns effects necessary when navigating to chat" + [cofx chat-id message-id] + (loading/load-message-context cofx chat-id message-id)) + (fx/defn navigate-to-chat "Takes coeffects map and chat-id, returns effects necessary for navigation and preloading data" {:events [:chat.ui/navigate-to-chat]} @@ -245,6 +250,22 @@ (navigation/pop-to-root-tab % :chat-stack)) (navigation/navigate-to-cofx :chat nil))) +(fx/defn navigate-to-chat-message + "Takes coeffects map and chat-id, returns effects necessary for navigation and preloading data" + [{db :db :as cofx} chat-id message-id dont-reset?] + (fx/merge cofx + (close-chat (:current-chat-id db)) + (fn [{:keys [db]}] + {:db (assoc db :current-chat-id chat-id :ignore-close-chat true)}) + (preload-chat-message-data chat-id message-id) + #(when (group-chat? cofx chat-id) + (loading/load-chat % chat-id)) + #(when-not dont-reset? + (navigation/change-tab % :chat)) + #(when-not dont-reset? + (navigation/pop-to-root-tab % :chat-stack)) + (navigation/navigate-to-cofx :chat nil))) + (fx/defn handle-clear-history-response {:events [::history-cleared]} [{:keys [db]} chat-id response] diff --git a/src/status_im/chat/models/loading.cljs b/src/status_im/chat/models/loading.cljs index c56240d868..29a55a4617 100644 --- a/src/status_im/chat/models/loading.cljs +++ b/src/status_im/chat/models/loading.cljs @@ -146,7 +146,9 @@ (let [cursor (get-in db [:pagination-info chat-id :cursor])] (when (or first-request cursor) (merge - {:db (assoc-in db [:pagination-info chat-id :loading-messages?] true)} + {:db (-> db + (assoc-in [:pagination-info chat-id :loading-messages?] true) + (dissoc :chats/initial-message-id))} {:utils/dispatch-later [{:ms 100 :dispatch [:load-more-reactions cursor chat-id]} {:ms 100 :dispatch [::models.pin-message/load-pin-messages chat-id]}]} (data-store.messages/messages-by-chat-id-rpc @@ -168,3 +170,32 @@ {:db (assoc-in db [:pagination-info chat-id :messages-initialized?] now) :utils/dispatch-later [{:ms 500 :dispatch [:chat.ui/mark-all-read-pressed chat-id]}]} (load-more-messages chat-id true)))) + +(fx/defn load-message-context-2 + [{:keys [db]} chat-id message-id first-request] + (when-let [session-id (get-in db [:pagination-info chat-id :messages-initialized?])] + (when (and + (not (get-in db [:pagination-info chat-id :all-loaded?])) + (not (get-in db [:pagination-info chat-id :loading-messages?]))) + (let [cursor (get-in db [:pagination-info chat-id :cursor])] + (when (or first-request cursor) + (merge + {:db + (-> db + (assoc :chats/initial-message-id message-id) + (assoc-in [:pagination-info chat-id :loading-messages?] true))} + (data-store.messages/message-context + chat-id + message-id + constants/default-number-of-messages + #(re-frame/dispatch [::messages-loaded chat-id session-id %]) + #(re-frame/dispatch [::failed-loading-messages chat-id session-id %])))))))) + +(fx/defn load-message-context + [{:keys [db now] :as cofx} chat-id message-id] + (log/info "loading message context" chat-id message-id) + (when-not (get-in db [:pagination-info chat-id :messages-initialized?]) + (fx/merge cofx + {:db (assoc-in db [:pagination-info chat-id :messages-initialized?] now) + :utils/dispatch-later [{:ms 500 :dispatch [:chat.ui/mark-all-read-pressed chat-id]}]} + (load-message-context-2 chat-id message-id true)))) diff --git a/src/status_im/data_store/messages.cljs b/src/status_im/data_store/messages.cljs index 4541e08e9d..9dafa879c5 100644 --- a/src/status_im/data_store/messages.cljs +++ b/src/status_im/data_store/messages.cljs @@ -106,3 +106,14 @@ (fx/defn delete-messages-by-chat-id [cofx chat-id] (delete-messages-by-chat-id-rpc chat-id)) + +(defn message-context [chat-id + message-id + limit + on-success + on-failure] + {::json-rpc/call [{:method (json-rpc/call-ext-method "messageContext") + :params [{:chatId chat-id :limit limit :messageId message-id}] + :on-success (fn [result] + (on-success (update result :messages #(map <-rpc %)))) + :on-failure on-failure}]}) diff --git a/src/status_im/ethereum/json_rpc.cljs b/src/status_im/ethereum/json_rpc.cljs index e61a0e2a3d..a31b7639b1 100644 --- a/src/status_im/ethereum/json_rpc.cljs +++ b/src/status_im/ethereum/json_rpc.cljs @@ -38,6 +38,7 @@ "waku_getSymKey" {} "waku_markTrustedPeer" {} "wakuext_post" {} + "wakuext_messageContext" {} "wakuext_requestAllHistoricMessages" {} "wakuext_editMessage" {} "wakuext_deleteMessageAndSend" {} diff --git a/src/status_im/router/core.cljs b/src/status_im/router/core.cljs index c10d35f86a..63bf3645aa 100644 --- a/src/status_im/router/core.cljs +++ b/src/status_im/router/core.cljs @@ -42,6 +42,7 @@ "chat" {["/public/" :chat-id] :public-chat} "b/" browser-extractor "browser/" browser-extractor + ["m/" :chat-id "/" :message-id] :message ["p/" :chat-id] :private-chat ["cr/" :community-id] :community-requests ["c/" :community-id] :community @@ -217,6 +218,9 @@ (= handler :community) (cb {:type handler :community-id (:community-id route-params)}) + (= handler :message) + (cb {:type handler :chat-id (:chat-id route-params) :message-id (:message-id route-params)}) + (= handler :community-chat) (cb {:type handler :chat-id (:chat-id route-params)}) diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index 37cfd33c3d..3b930e5eeb 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -55,6 +55,7 @@ (reg-root-key-sub :bottom-sheet/show? :bottom-sheet/show?) (reg-root-key-sub :bottom-sheet/view :bottom-sheet/view) (reg-root-key-sub :bottom-sheet/options :bottom-sheet/options) +(reg-root-key-sub :chats/initial-message-id :chats/initial-message-id) ;;general (reg-root-key-sub :sync-state :sync-state) diff --git a/src/status_im/ui/screens/chat/views.cljs b/src/status_im/ui/screens/chat/views.cljs index 7440d9fe99..99c88c3523 100644 --- a/src/status_im/ui/screens/chat/views.cljs +++ b/src/status_im/ui/screens/chat/views.cljs @@ -3,6 +3,7 @@ [reagent.core :as reagent] [status-im.i18n.i18n :as i18n] [status-im.ui.components.chat-icon.screen :as chat-icon.screen] + [taoensso.timbre :as log] [quo.design-system.colors :as colors] [status-im.ui.components.connectivity.view :as connectivity] [status-im.ui.components.icons.icons :as icons] @@ -295,6 +296,8 @@ ;; TODO: memoize + + (defn on-scroll-to-index-failed [^js e] (let [wait (js/Promise. (fn [resolve] (js/setTimeout resolve 50)))] (.then wait (fn [] @@ -302,11 +305,20 @@ (.scrollToIndex @messages-list-ref #js {:index (.-index e)})))))) - +(defn get-index [messages initial-message-id] + (let [i (->> messages + (map-indexed vector) + (filter (fn [[idx {:keys [message-id]}]] + (when (= message-id initial-message-id) + idx))) + first)] + (log/info "Scrolling to I" i) + i)) (defn messages-view [{:keys [chat bottom-space pan-responder space-keeper show-input?]}] (let [{:keys [group-chat chat-id public? community-id admins]} chat - messages @(re-frame/subscribe [:chats/chat-messages-stream chat-id])] + messages @(re-frame/subscribe [:chats/chat-messages-stream chat-id]) + initial-message-id @(re-frame/subscribe [:chats/initial-message-id])] ;;do not use anonymous functions for handlers (when (seq messages) [list/bidi-flat-list @@ -330,7 +342,8 @@ :on-viewable-items-changed on-viewable-items-changed :on-end-reached list-on-end-reached ;; just to simulate initial position on the list - :initial-scroll-index (quot (count messages) 2) + :initial-scroll-index (when initial-message-id + (get-index messages initial-message-id)) :on-scroll-to-index-failed on-scroll-to-index-failed :content-container-style {:padding-top (+ bottom-space 16) :padding-bottom 16} diff --git a/src/status_im/utils/universal_links/core.cljs b/src/status_im/utils/universal_links/core.cljs index 6d3ee9535d..5a0210c829 100644 --- a/src/status_im/utils/universal_links/core.cljs +++ b/src/status_im/utils/universal_links/core.cljs @@ -26,6 +26,7 @@ (def links {:public-chat "%s/%s" :private-chat "%s/p/%s" + :message "%s/m/%s/%s" :community-requests "%s/cr/%s" :community "%s/c/%s" :group-chat "%s/g/%s" @@ -53,6 +54,12 @@ (log/info "universal-links: handling group" params) (group-chats/create-from-link cofx params)) +(fx/defn handle-message [{:keys [db] :as cofx} {:keys [chat-id message-id]}] + (let [chat (get-in db [:chats chat-id])] + (when chat + (log/info "universal-links: handling message" chat-id message-id) + (chat/navigate-to-chat-message cofx chat-id message-id false)))) + (fx/defn handle-private-chat [{:keys [db] :as cofx} {:keys [chat-id]}] (log/info "universal-links: handling private chat" chat-id) (when chat-id @@ -130,6 +137,7 @@ :group-chat (handle-group-chat cofx data) :public-chat (handle-public-chat cofx data) :private-chat (handle-private-chat cofx data) + :message (handle-message cofx data) :community-requests (handle-community-requests cofx data) :community (handle-community cofx data) :community-chat (handle-community-chat cofx data) diff --git a/status-go-version.json b/status-go-version.json index 7ccb2d14ca..4acc50f139 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -3,7 +3,7 @@ "_comment": "Instead use: scripts/update-status-go.sh ", "owner": "status-im", "repo": "status-go", - "version": "v0.89.11", - "commit-sha1": "23308d7e8c4b7f5c85bffe870ba92010a3784dcb", - "src-sha256": "0zlbzys7g1v3sr67l7fq85mhsk58gkplx8ci7lr5imk9cbciarlx" + "version": "feature/pagination-both-sides", + "commit-sha1": "0fa312e5b11b98c46c719fc175f3d7117105b826", + "src-sha256": "1ccl6slayf1gjmlc4x1z32m5i5521vnaa1ngbdkjvxdkpl41n450" }