Skip to content

Commit

Permalink
Allow to fetch more history in chats (up to 30 days)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed Apr 24, 2019
1 parent 3ed3c3b commit 412b162
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
36 changes: 25 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,30 @@
(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?]
(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
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 +189,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
21 changes: 19 additions & 2 deletions src/status_im/chat/subs.cljs
Expand Up @@ -178,16 +178,33 @@
(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/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?]
(fn [[messages message-groups message-statuses referenced-messages
messages-gaps range all-loaded?]]
(-> (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?)
chat.db/messages-stream)))

(re-frame/reg-sub
Expand Down
18 changes: 18 additions & 0 deletions src/status_im/events.cljs
Expand Up @@ -758,6 +758,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
21 changes: 14 additions & 7 deletions src/status_im/ui/screens/chat/views.cljs
Expand Up @@ -71,7 +71,7 @@
[message-datemark/chat-datemark-mobile value])

(defview gap
[ids idx list-ref in-progress? connected?]
[ids idx list-ref in-progress? connected? first-gap?]
[react/view {:align-self :stretch
:margin-top 24
:margin-bottom 24
Expand All @@ -89,7 +89,9 @@
(.scrollToIndex @list-ref #js {:index (max 0 (dec idx))
:viewOffset 20
:viewPosition 0.5}))
(re-frame/dispatch [:chat.ui/fill-gaps ids])))}
(if first-gap?
(re-frame/dispatch [:chat.ui/fetch-more])
(re-frame/dispatch [:chat.ui/fill-gaps ids]))))}
[react/view {:flex 1
:align-items :center
:justify-content :center}
Expand All @@ -99,16 +101,21 @@
{:style {:color (if connected?
colors/blue
colors/gray)}}
(i18n/label :t/fetch-messages)])]]])
(if first-gap?
"Load more messages"
(i18n/label :t/fetch-messages))])]]])

(defview gap-wrapper [{:keys [ids]} idx list-ref]
(letsubs [in-progress? [:chats/fetching-gap-in-progress? ids]
(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?]]
[gap ids idx list-ref in-progress? connected?]))
[gap (:ids gaps) idx list-ref in-progress? connected? first-gap?]))

(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

0 comments on commit 412b162

Please sign in to comment.