Skip to content

Commit

Permalink
Allow to fetch more history in public chats (up to 30 days)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed Apr 26, 2019
1 parent f6fce85 commit 9b9eefb
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 58 deletions.
37 changes: 26 additions & 11 deletions src/status_im/chat/db.cljs
Expand Up @@ -6,7 +6,8 @@
[status-im.chat.commands.input :as commands.input]
[status-im.group-chats.db :as group-chats.db]
[status-im.utils.gfycat.core :as gfycat]
[status-im.transport.partitioned-topic :as topic]))
[status-im.transport.partitioned-topic :as topic]
[status-im.mailserver.core :as mailserver]))

(defn group-chat-name
[{:keys [public? name]}]
Expand Down Expand Up @@ -86,10 +87,14 @@
(defn datemark? [{:keys [type]}]
(= type :datemark))

(defn gap? [{:keys [type]}]
(= type :gap))

(defn transform-message
[messages message-statuses referenced-messages]
(fn [{:keys [message-id timestamp-str] :as reference}]
(if (datemark? reference)
(if (or (datemark? reference)
(gap? reference))
reference
(let [{:keys [content] :as message} (get messages message-id)
{:keys [response-to response-to-v2]} content
Expand Down Expand Up @@ -141,18 +146,31 @@
(defn messages-with-datemarks-and-statuses
"Converts message groups into sequence of messages interspersed with datemarks,
with correct user statuses associated into message"
[message-groups messages message-statuses referenced-messages messages-gaps]
[message-groups messages message-statuses referenced-messages messages-gaps
{:keys [highest-request-to lowest-request-from]} all-loaded? public?]
(transduce
(comp
(mapcat add-datemark)
(map (transform-message messages message-statuses referenced-messages)))
(completing
(fn [{:keys [messages datemark-reference previous-message gaps]}
message]
(fn
([]
(let [acc {:messages (list)
:previous-message nil
:gaps messages-gaps}]
(if (and
public?
all-loaded?
(< (- highest-request-to lowest-request-from)
mailserver/max-gaps-range))
(update acc :messages conj {:type :gap
:value (str :first-gap)
:first-gap? true})
acc)))
([{:keys [messages datemark-reference previous-message gaps]} message]
(let [new-datemark? (datemark? message)
{:keys [gaps-number gap]}
(check-gap gaps previous-message message)
add-gap? (pos? gaps-number)]
add-gap? (pos? gaps-number)]
{:messages (cond-> messages

add-gap?
Expand All @@ -172,13 +190,10 @@
:gaps (if add-gap?
(drop gaps-number gaps)
gaps)}))
(fn [{:keys [messages gaps]}]
([{:keys [messages gaps]}]
(cond-> messages
(seq gaps)
(add-gap {:ids (map :id gaps)}))))
{:messages (list)
:previous-message nil
:gaps messages-gaps}
message-groups))

(defn- set-previous-message-info [stream]
Expand Down
29 changes: 27 additions & 2 deletions src/status_im/chat/subs.cljs
Expand Up @@ -178,16 +178,41 @@
(fn [[gaps chat-id]]
(sort-by :from (vals (get gaps chat-id)))))

(re-frame/reg-sub
:chats/range
:<- [:get-in [:mailserver/ranges]]
:<- [:chats/current-chat-id]
(fn [[ranges chat-id]]
(get ranges chat-id)))

(re-frame/reg-sub
:chats/all-loaded?
:<- [:chats/current-chat]
(fn [chat]
(:all-loaded? chat)))

(re-frame/reg-sub
:chats/public?
:<- [:chats/current-chat]
(fn [chat]
(:public? chat)))

(re-frame/reg-sub
:chats/current-chat-messages-stream
:<- [:chats/current-chat-messages]
:<- [:chats/current-chat-message-groups]
:<- [:chats/current-chat-message-statuses]
:<- [:chats/current-chat-referenced-messages]
:<- [:chats/messages-gaps]
(fn [[messages message-groups message-statuses referenced-messages messages-gaps]]
:<- [:chats/range]
:<- [:chats/all-loaded?]
:<- [:chats/public?]
(fn [[messages message-groups message-statuses referenced-messages
messages-gaps range all-loaded? public?]]
(-> (chat.db/sort-message-groups message-groups messages)
(chat.db/messages-with-datemarks-and-statuses messages message-statuses referenced-messages messages-gaps)
(chat.db/messages-with-datemarks-and-statuses
messages message-statuses referenced-messages
messages-gaps range all-loaded? public?)
chat.db/messages-stream)))

(re-frame/reg-sub
Expand Down
18 changes: 18 additions & 0 deletions src/status_im/events.cljs
Expand Up @@ -753,6 +753,24 @@
:topic topic
:chat-id chat-id}))))

(handlers/register-handler-fx
:chat.ui/fetch-more
(fn [{:keys [db] :as cofx}]
(let [chat-id (:current-chat-id db)

{:keys [lowest-request-from]}
(get-in db [:mailserver/ranges chat-id])

topic (chat.db/topic-by-current-chat db)
gaps [{:id :first-gap
:to lowest-request-from
:from (- lowest-request-from mailserver/one-day)}]]
(mailserver/fill-the-gap
cofx
{:gaps gaps
:topic topic
:chat-id chat-id}))))

(handlers/register-handler-fx
:chat.ui/remove-chat-pressed
(fn [_ [_ chat-id]]
Expand Down
12 changes: 6 additions & 6 deletions src/status_im/ui/screens/chat/actions.cljs
Expand Up @@ -45,24 +45,24 @@
[(view-profile chat-id)
(clear-history)
(fetch-history chat-id)
(fetch-history48-60 chat-id)
(fetch-history84-96 chat-id)
#_(fetch-history48-60 chat-id)
#_(fetch-history84-96 chat-id)
(delete-chat chat-id false)])

(defn- group-chat-actions [chat-id]
[(group-info chat-id)
(clear-history)
(fetch-history chat-id)
(fetch-history48-60 chat-id)
(fetch-history84-96 chat-id)
#_(fetch-history48-60 chat-id)
#_(fetch-history84-96 chat-id)
(delete-chat chat-id true)])

(defn- public-chat-actions [chat-id]
[(share-chat chat-id)
(clear-history)
(fetch-history chat-id)
(fetch-history48-60 chat-id)
(fetch-history84-96 chat-id)
#_(fetch-history48-60 chat-id)
#_(fetch-history84-96 chat-id)
(delete-chat chat-id false)])

(defn actions [group-chat? chat-id public?]
Expand Down
104 changes: 67 additions & 37 deletions src/status_im/ui/screens/chat/views.cljs
Expand Up @@ -26,7 +26,8 @@
[status-im.ui.screens.chat.styles.main :as style]
[status-im.ui.screens.chat.toolbar-content :as toolbar-content]
[status-im.utils.platform :as platform]
[status-im.utils.utils :as utils])
[status-im.utils.utils :as utils]
[status-im.utils.datetime :as datetime])
(:require-macros [status-im.utils.views :refer [defview letsubs]]))

(defn add-contact-bar [public-key]
Expand Down Expand Up @@ -71,44 +72,64 @@
[message-datemark/chat-datemark-mobile value])

(defview gap
[ids idx list-ref in-progress? connected?]
[react/view {:align-self :stretch
:margin-top 24
:margin-bottom 24
:height 48
:align-items :center
:justify-content :center
:border-color colors/gray-light
:border-top-width 1
:border-bottom-width 1
:background-color :white}
[react/touchable-highlight
{:on-press (when (and connected? (not in-progress?))
#(do
(when @list-ref
(.scrollToIndex @list-ref #js {:index (max 0 (dec idx))
:viewOffset 20
:viewPosition 0.5}))
(re-frame/dispatch [:chat.ui/fill-gaps ids])))}
[react/view {:flex 1
:align-items :center
:justify-content :center}
(if in-progress?
[react/activity-indicator]
[react/text
{:style {:color (if connected?
colors/blue
colors/gray)}}
(i18n/label :t/fetch-messages)])]]])
[ids idx list-ref in-progress? connected? range first-gap? intro-loading?]
(when-not (and first-gap? intro-loading?)
[react/view {:align-self :stretch
:margin-top 24
:margin-bottom 24
:height 48
:align-items :center
:justify-content :center
:border-color colors/gray-light
:border-top-width 1
:border-bottom-width 1
:background-color :white}
[react/touchable-highlight
{:on-press (when (and connected? (not in-progress?))
#(do
(when @list-ref
(.scrollToIndex @list-ref #js {:index (max 0 (dec idx))
:viewOffset 20
:viewPosition 0.5}))
(if first-gap?
(re-frame/dispatch [:chat.ui/fetch-more])
(re-frame/dispatch [:chat.ui/fill-gaps ids]))))}
[react/view {:style {:flex 1
:align-items :center
:justify-content :center
:text-align :center}}
(if in-progress?
[react/activity-indicator]
[react/nested-text
{:style {:text-align :center
:color (if connected?
colors/blue
colors/gray)}}
(i18n/label (if first-gap?
:t/load-more-messages
:t/fetch-messages))
(when first-gap?
[{:style {:typography :caption
:color colors/gray}}
(str "\n"
(i18n/label :t/load-messages-before
{:date (datetime/timestamp->long-date
(* 1000 (:lowest-request-from range)))}))])])]]]))

(defview gap-wrapper [{:keys [ids]} idx list-ref]
(letsubs [in-progress? [:chats/fetching-gap-in-progress? ids]
connected? [:mailserver/connected?]]
[gap ids idx list-ref in-progress? connected?]))
(defview gap-wrapper [{:keys [gaps first-gap?]} idx list-ref]
(letsubs [in-progress? [:chats/fetching-gap-in-progress?
(if first-gap?
[:first-gap]
(:ids gaps))]
connected? [:mailserver/connected?]
range [:chats/range]
intro-status [:chats/current-chat-intro-status]]
[gap (:ids gaps) idx list-ref in-progress?
connected? range first-gap? (= intro-status :loading)]))

(defmethod message-row :gap
[{:keys [row idx list-ref]}]
[gap-wrapper (:gaps row) idx list-ref])
[gap-wrapper row idx list-ref])

(defmethod message-row :default
[{:keys [group-chat current-public-key modal? row]}]
Expand Down Expand Up @@ -182,7 +203,8 @@
universal-link]} no-messages]
(letsubs [intro-status [:chats/current-chat-intro-status]
height [:chats/content-layout-height]
input-height [:chats/current-chat-ui-prop :input-height]]
input-height [:chats/current-chat-ui-prop :input-height]
{:keys [:lowest-request-from :highest-request-to]} [:chats/range]]
(let [icon-text (if public? chat-id name)
intro-name (if public? chat-name name)]
;; TODO This when check ought to be unnecessary but for now it prevents
Expand Down Expand Up @@ -226,7 +248,15 @@
(when public?
[react/nested-text {:style (merge style/intro-header-description
{:margin-bottom 36})}
(i18n/label :t/empty-chat-description-public)
(let [quiet-hours (quot (- highest-request-to lowest-request-from)
(* 60 60))
quiet-time (if (<= quiet-hours 24)
(i18n/label :t/quiet-hours
{:quiet-hours quiet-hours})
(i18n/label :t/quiet-days
{:quiet-days (quot quiet-hours 24)}))]
(i18n/label :t/empty-chat-description-public
{:quiet-hours quiet-time}))
[{:style {:color colors/blue}
:on-press #(list-selection/open-share
{:message
Expand Down
20 changes: 19 additions & 1 deletion test/cljs/status_im/test/chat/db.cljs
Expand Up @@ -132,7 +132,25 @@
nil
nil
nil
nil))))
nil
nil
false
false))))
(testing "empty state pub-chat"
(is (=
[{:type :gap
:value ":first-gap"
:first-gap? true}]
(db/messages-with-datemarks-and-statuses
nil
nil
nil
nil
nil
{:lowest-request-from 10
:highest-request-to 30}
true
true))))
(testing "simple case"
(is (=
'({:whisper-timestamp 40
Expand Down
6 changes: 5 additions & 1 deletion translations/en.json
Expand Up @@ -283,7 +283,9 @@
"wallet-set-up-title": "Set up your wallet",
"loading": "Loading... ",
"empty-chat-description": "There are no messages \nin this chat yet",
"empty-chat-description-public": "It's been quiet here for the last 24h. Start the conversation or ",
"empty-chat-description-public": "It's been quiet here for the last {{quiet-hours}}. Start the conversation or ",
"quiet-hours": "{{quiet-hours}} hours",
"quiet-days": "{{quiet-days}} days",
"empty-chat-description-public-share-this": "share this chat.",
"camera-access-error": "To grant the required camera permission, please go to your system settings and make sure that Status > Camera is selected.",
"wallet-invalid-address": "Invalid address: \n {{data}}",
Expand Down Expand Up @@ -1000,6 +1002,8 @@
"mailserver-format": "enode://{enode-id}:{password}@{ip-address}:{port}",
"bootnode-format": "enode://{enode-id}@{ip-address}:{port}",
"fetch-messages": "↓ Fetch messages",
"load-more-messages": "↓ Fetch more messages",
"load-messages-before": "before {{date}}",
"open-dapp-store": "Discover ÐApps",
"browsed-websites": "Browsed websites will appear here."
}

0 comments on commit 9b9eefb

Please sign in to comment.