Skip to content

Commit

Permalink
feat: re-fetch community info in community overview and channel
Browse files Browse the repository at this point in the history
Signed-off-by: yqrashawn <namy.19@gmail.com>
  • Loading branch information
yqrashawn committed Jan 18, 2024
1 parent 8a4302c commit 06f46f7
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 312 deletions.
1 change: 1 addition & 0 deletions src/legacy/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
[react-native.permissions :as permissions]
[react-native.platform :as platform]
[status-im.common.biometric.events :as biometric]
status-im.common.serialization
status-im.common.standard-authentication.events
[status-im.common.theme.core :as theme]
[status-im.common.universal-links :as universal-links]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
[rn/touchable-opacity
{:on-press #(do
(rf/dispatch [:pop-to-root :shell-stack])
(rf/dispatch [:navigate-to :community-overview (:id community)])
(rf/dispatch [:communities/navigate-to-community-overview (:id community)])
(rf/dispatch [:chat/close]))}
[rn/text
{:style {:text-align :center
Expand Down
10 changes: 2 additions & 8 deletions src/status_im/common/router.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@
{:type :wallet-account
:account (when account (string/lower-case account))})

(defn community-route-type
[route-params]
(if (string/starts-with? (:community-id route-params) "z")
:desktop-community
:community))

(defn handle-uri
[chain chats uri cb]
(let [{:keys [handler route-params query-params]} (match-uri uri)]
Expand Down Expand Up @@ -297,14 +291,14 @@
(cb {:type handler :community-id (:community-id route-params)})

(and (= handler :community) (:community-id route-params))
(cb {:type (community-route-type route-params)
(cb {:type :community
:community-id (:community-id route-params)})

(and (= handler :community-chat) (:community-channel-id route-params) (:community-id route-params))
(match-community-channel-async route-params cb)

(and (= handler :community-chat) (:community-id route-params))
(cb {:type (community-route-type route-params)
(cb {:type :community
:community-id (:community-id route-params)})

;; NOTE: removed in `match-uri`, might need this in the future
Expand Down
27 changes: 27 additions & 0 deletions src/status_im/common/serialization.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns status-im.common.serialization
(:require
[native-module.core :as native-module]
[status-im.constants :as constants]
[utils.re-frame :as rf]
[utils.transforms :as transforms]))

(rf/reg-fx :serialization/deserialize-and-compress-key
(fn [{:keys [serialized-key on-success on-error]}]
(native-module/deserialize-and-compress-key
serialized-key
(fn [resp]
(let [{:keys [error]} (transforms/json->clj resp)]
(if error
(on-error error)
(on-success resp)))))))

(rf/reg-fx :serialization/decompress-public-key
(fn [{:keys [compressed-key on-success on-error]}]
(native-module/compressed-key->public-key
compressed-key
constants/deserialization-key
(fn [resp]
(let [{:keys [error]} (transforms/json->clj resp)]
(if error
(on-error error)
(on-success (str "0x" (subs resp 5)))))))))
35 changes: 4 additions & 31 deletions src/status_im/common/universal_links.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
(:require
[clojure.string :as string]
[goog.string :as gstring]
[native-module.core :as native-module]
[re-frame.core :as re-frame]
[react-native.async-storage :as async-storage]
[react-native.core :as rn]
[schema.core :as schema]
[status-im.constants :as constants]
[status-im.contexts.communities.events :as communities.events]
[status-im.navigation.events :as navigation]
[taoensso.timbre :as log]
[utils.ethereum.chain :as chain]
Expand Down Expand Up @@ -68,32 +68,6 @@
(log/info "universal-links: handling community request " community-id)
(navigation/navigate-to cofx :community-requests-to-join {:community-id community-id}))

(rf/defn handle-community
[cofx {:keys [community-id]}]
(log/info "universal-links: handling community" community-id)
(navigation/navigate-to cofx :community {:community-id community-id}))

(rf/defn handle-navigation-to-desktop-community-from-mobile
{:events [:handle-navigation-to-desktop-community-from-mobile]}
[cofx deserialized-key]
(rf/merge
cofx
{:dispatch [:navigate-to :community-overview deserialized-key]}
(navigation/pop-to-root :shell-stack)))

(rf/defn handle-desktop-community
[_ {:keys [community-id]}]
(native-module/deserialize-and-compress-key
community-id
(fn [deserialized-key]
(rf/dispatch [:chat.ui/fetch-community (str deserialized-key)])
(rf/dispatch [:handle-navigation-to-desktop-community-from-mobile (str deserialized-key)]))))

(rf/defn handle-community-chat
[cofx {:keys [chat-id]}]
(log/info "universal-links: handling community chat" chat-id)
{:dispatch [:chat/pop-to-root-and-navigate-to-chat chat-id]})

(rf/defn handle-view-profile
[{:keys [db] :as cofx} {:keys [public-key ens-name]}]
(log/info "universal-links: handling view profile" public-key)
Expand Down Expand Up @@ -143,14 +117,13 @@

(rf/defn on-handle
{:events [::match-value]}
[cofx url {:keys [type] :as data}]
[cofx url {:keys [type chat-id] :as data}]
(case type
:group-chat (handle-group-chat cofx data)
:private-chat (handle-private-chat cofx data)
:community-requests (handle-community-requests cofx data)
:community (handle-community cofx data)
:desktop-community (handle-desktop-community cofx data)
:community-chat (handle-community-chat cofx data)
:community (communities.events/navigate-to-serialized-community cofx data)
:community-chat {:dispatch [:communities/navigate-to-community-chat chat-id :pop-to-root]}
:contact (handle-view-profile cofx data)
:browser (handle-browse cofx data)
:eip681 (handle-eip681 cofx data)
Expand Down
7 changes: 7 additions & 0 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@
(def ^:const local-pairing-action-sync-device 3)
(def ^:const local-pairing-action-pairing-installation 4)

(def ^:const serialization-key
"We pass this serialization key as a parameter to MultiformatSerializePublicKey
function at status-go, This key determines the output base of the serialization.
according to https://specs.status.im/spec/2#public-key-serialization we serialize
keys with base58btc encoding"
"z")

(def ^:const deserialization-key
"We pass this deserialization key as a parameter to MultiformatDeserializePublicKey
function at status-go, This key determines the output base of the deserialization.
Expand Down
16 changes: 1 addition & 15 deletions src/status_im/contexts/chat/home/add_new_contact/effects.cljs
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
(ns status-im.contexts.chat.home.add-new-contact.effects
(:require
[legacy.status-im.ethereum.ens :as ens]
[native-module.core :as native-module]
[status-im.constants :as constants]
[utils.re-frame :as rf]
[utils.transforms :as transforms]))

(rf/reg-fx :effects.contacts/decompress-public-key
(fn [{:keys [compressed-key on-success on-error]}]
(native-module/compressed-key->public-key
compressed-key
constants/deserialization-key
(fn [resp]
(let [{:keys [error]} (transforms/json->clj resp)]
(if error
(on-error error)
(on-success (str "0x" (subs resp 5)))))))))
[utils.re-frame :as rf]))

(rf/reg-fx :effects.contacts/resolve-public-key-from-ens
(fn [{:keys [chain-id ens on-success on-error]}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
:empty {:db (dissoc db :contacts/new-identity)}
(:valid :invalid) {:db (assoc db :contacts/new-identity contact)}
:decompress-key {:db (assoc db :contacts/new-identity contact)
:effects.contacts/decompress-public-key
:serialization/decompress-public-key
{:compressed-key id
:on-success
(dispatcher :contacts/set-new-identity-success input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
:type :compressed-key
:public-key nil ; not yet...
:state :decompress-key}))
:effects.contacts/decompress-public-key
:serialization/decompress-public-key
{:compressed-key user-ckey
:on-success [:contacts/set-new-identity-success user-ckey]
:on-error [:contacts/set-new-identity-error user-ckey]}})))
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
(ns status-im.contexts.chat.messenger.messages.link-preview.events
(:require
[camel-snake-kebab.core :as csk]
[legacy.status-im.mailserver.core :as mailserver]
[schema.core :as schema]
[status-im.contexts.profile.settings.events :as profile.settings.events]
[taoensso.timbre :as log]
[utils.collection]
Expand Down Expand Up @@ -42,96 +40,6 @@
(boolean enabled?)
{}))

(defn community-resolved
[{:keys [db]} [community-id community]]
(when community
{:db (update db :communities/resolve-community-info dissoc community-id)
:fx [[:dispatch [:communities/handle-community community]]
[:dispatch
[:chat.ui/cache-link-preview-data (community-link community-id) community]]]}))

(rf/reg-event-fx :chat.ui/community-resolved community-resolved)

(defn community-failed-to-resolve
[{:keys [db]} [community-id]]
{:db (update db :communities/resolve-community-info dissoc community-id)})

(rf/reg-event-fx :chat.ui/community-failed-to-resolve community-failed-to-resolve)

(defn fetch-community
[{:keys [db]} [community-id]]
(when community-id
{:db (assoc-in db [:communities/resolve-community-info community-id] true)
:json-rpc/call [{:method "wakuext_fetchCommunity"
:params [{:CommunityKey community-id
:TryDatabase true
:WaitForResponse true}]
:on-success (fn [community]
(rf/dispatch [:chat.ui/community-resolved community-id community]))
:on-error (fn [err]
(rf/dispatch [:chat.ui/community-failed-to-resolve community-id])
(log/error {:message
"Failed to request community info from mailserver"
:error err}))}]}))

(schema/=> fetch-community
[:=>
[:catn
[:cofx :schema.re-frame/cofx]
[:args
[:schema [:catn [:community-id [:? :string]]]]]]
[:maybe
[:map
[:db map?]
[:json-rpc/call :schema.common/rpc-call]]]])

(rf/reg-event-fx :chat.ui/fetch-community fetch-community)

(defn spectate-community-success
[{:keys [db]} [{:keys [communities]}]]
(when-let [community (first communities)]
{:db (-> db
(assoc-in [:communities (:id community) :spectated] true)
(assoc-in [:communities (:id community) :spectating] false))
:fx [[:dispatch [:communities/handle-community community]]
[:dispatch [::mailserver/request-messages]]]}))

(rf/reg-event-fx :chat.ui/spectate-community-success spectate-community-success)

(defn spectate-community-failed
[{:keys [db]} [community-id]]
{:db (assoc-in db [:communities community-id :spectating] false)})

(rf/reg-event-fx :chat.ui/spectate-community-failed spectate-community-failed)

(defn spectate-community
[{:keys [db]} [community-id]]
(let [{:keys [spectated spectating joined]} (get-in db [:communities community-id])]
(when (and (not joined) (not spectated) (not spectating))
{:db (assoc-in db [:communities community-id :spectating] true)
:json-rpc/call [{:method "wakuext_spectateCommunity"
:params [community-id]
:on-success [:chat.ui/spectate-community-success]
:on-error (fn [err]
(log/error {:message
"Failed to spectate community"
:error err})
(rf/dispatch [:chat.ui/spectate-community-failed
community-id]))}]})))

(schema/=> spectate-community
[:=>
[:catn
[:cofx :schema.re-frame/cofx]
[:args
[:schema [:catn [:community-id [:? :string]]]]]]
[:maybe
[:map
[:db map?]
[:json-rpc/call :schema.common/rpc-call]]]])

(rf/reg-event-fx :chat.ui/spectate-community spectate-community)

(rf/defn save-link-preview-whitelist
{:events [:chat.ui/link-preview-whitelist-received]}
[{:keys [db]} whitelist]
Expand Down
Loading

0 comments on commit 06f46f7

Please sign in to comment.