2020-04-30 14:44:17 +00:00
|
|
|
(ns status-im.chat.models.message-test
|
2018-04-19 08:48:04 +00:00
|
|
|
(:require [cljs.test :refer-macros [deftest is testing]]
|
2020-04-03 11:32:42 +00:00
|
|
|
[status-im.chat.models.loading :as chat-loading]
|
2020-05-05 14:18:23 +00:00
|
|
|
[status-im.chat.models.message :as message]
|
2019-11-12 06:07:17 +00:00
|
|
|
[status-im.chat.models.message-list :as models.message-list]
|
2020-04-03 11:32:42 +00:00
|
|
|
[status-im.ui.screens.chat.state :as view.state]
|
2020-10-20 14:28:52 +00:00
|
|
|
[status-im.utils.datetime :as time]))
|
2018-04-19 08:48:04 +00:00
|
|
|
|
2020-04-03 11:32:42 +00:00
|
|
|
(deftest add-received-message-test
|
|
|
|
(with-redefs [message/add-message (constantly :added)]
|
|
|
|
(let [chat-id "chat-id"
|
|
|
|
clock-value 10
|
|
|
|
cursor-clock-value (dec clock-value)
|
|
|
|
cursor (chat-loading/clock-value->cursor cursor-clock-value)
|
|
|
|
cofx {:now 0
|
|
|
|
:db {:loaded-chat-id chat-id
|
|
|
|
:current-chat-id chat-id
|
|
|
|
:all-loaded? true
|
|
|
|
:chats {chat-id {:cursor cursor
|
|
|
|
:cursor-clock-value cursor-clock-value}}}}
|
2020-08-31 09:52:17 +00:00
|
|
|
message {:chat-id chat-id
|
|
|
|
:clock-value clock-value
|
|
|
|
:alias "alias"
|
|
|
|
:name "name"
|
|
|
|
:identicon "identicon"
|
|
|
|
:from "from"}]
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "not current-chat"
|
|
|
|
(is (nil? (message/add-received-message
|
|
|
|
(update cofx :db dissoc :loaded-chat-id)
|
|
|
|
message))))
|
|
|
|
;; <- cursor
|
|
|
|
;; <- message
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "there's no hidden item"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom nil)]
|
2020-08-31 09:52:17 +00:00
|
|
|
(is (= {:db {:loaded-chat-id "chat-id",
|
|
|
|
:current-chat-id "chat-id",
|
|
|
|
:all-loaded? true,
|
|
|
|
:chats
|
|
|
|
{"chat-id"
|
|
|
|
{:cursor
|
|
|
|
"00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
:cursor-clock-value 9,
|
|
|
|
:users
|
2020-09-30 11:02:44 +00:00
|
|
|
{"from" {:alias "alias",
|
|
|
|
:name "name",
|
|
|
|
:identicon "identicon",
|
|
|
|
:public-key "from"
|
|
|
|
:nickname nil
|
|
|
|
:searchable-phrases ["alias" "name"]}}}}}}
|
2020-08-31 09:52:17 +00:00
|
|
|
(message/add-received-message
|
|
|
|
cofx
|
|
|
|
message)))))
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- cursor
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- message
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the hidden item has a clock value less than the current"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (dec clock-value)})]
|
2020-08-31 09:52:17 +00:00
|
|
|
(is (= {:db {:loaded-chat-id "chat-id",
|
|
|
|
:current-chat-id "chat-id",
|
|
|
|
:all-loaded? true,
|
|
|
|
:chats
|
|
|
|
{"chat-id"
|
|
|
|
{:cursor
|
|
|
|
"00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
:cursor-clock-value 9,
|
|
|
|
:users
|
2020-09-30 11:02:44 +00:00
|
|
|
{"from" {:alias "alias",
|
|
|
|
:name "name",
|
|
|
|
:identicon "identicon",
|
|
|
|
:public-key "from"
|
|
|
|
:nickname nil
|
|
|
|
:searchable-phrases ["alias" "name"]}}}}}}
|
2020-08-31 09:52:17 +00:00
|
|
|
(message/add-received-message
|
|
|
|
cofx
|
|
|
|
message)))))
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- cursor
|
|
|
|
;; <- message
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the message falls between the first-hidden-item and cursor"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (inc clock-value)})]
|
|
|
|
(let [result (message/add-received-message
|
|
|
|
cofx
|
|
|
|
message)]
|
|
|
|
(testing "it sets all-loaded? to false"
|
|
|
|
(is (not (get-in result [:db :chats chat-id :all-loaded?]))))
|
|
|
|
(testing "it updates cursor-clock-value & cursor"
|
|
|
|
(is (= clock-value (get-in result [:db :chats chat-id :cursor-clock-value])))
|
|
|
|
(is (= (chat-loading/clock-value->cursor clock-value) (get-in result [:db :chats chat-id :cursor])))))))
|
|
|
|
;; <- message
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the message falls between the first-hidden-item and cursor is nil"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (inc clock-value)})]
|
|
|
|
(let [result (message/add-received-message
|
|
|
|
(update-in cofx [:db :chats chat-id] dissoc :cursor :cursor-clock-value)
|
|
|
|
message)]
|
|
|
|
(testing "it sets all-loaded? to false"
|
|
|
|
(is (not (get-in result [:db :chats chat-id :all-loaded?]))))
|
|
|
|
(testing "it updates cursor-clock-value & cursor"
|
|
|
|
(is (= clock-value (get-in result [:db :chats chat-id :cursor-clock-value])))
|
|
|
|
(is (= (chat-loading/clock-value->cursor clock-value) (get-in result [:db :chats chat-id :cursor])))))))
|
|
|
|
;; <- message
|
|
|
|
;; <- cursor
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the message falls before both the first-hidden-item and cursor"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (inc clock-value)})]
|
|
|
|
(let [result (message/add-received-message
|
|
|
|
cofx
|
|
|
|
(update message :clock-value #(- % 2)))]
|
|
|
|
(testing "it sets all-loaded? to false"
|
|
|
|
(is (not (get-in result [:db :chats chat-id :all-loaded?]))))
|
|
|
|
(testing "it does not update cursor-clock-value & cursor"
|
|
|
|
(is (= cursor-clock-value (get-in result [:db :chats chat-id :cursor-clock-value])))
|
|
|
|
(is (= cursor (get-in result [:db :chats chat-id :cursor]))))))))))
|
|
|
|
|
2020-04-08 16:01:11 +00:00
|
|
|
(deftest message-loaded?
|
|
|
|
(testing "it returns false when it's not in loaded message"
|
2020-05-05 14:18:23 +00:00
|
|
|
(is (not (#'status-im.chat.models.message/message-loaded? {:db {:chats {"a" {}}}}
|
|
|
|
{:message-id "message-id"
|
|
|
|
:from "a"
|
|
|
|
:clock-value 1
|
|
|
|
:chat-id "a"}))))
|
2020-04-08 16:01:11 +00:00
|
|
|
(testing "it returns true when it's already in the loaded message"
|
Improve chat loading performance
This commit does a few things:
Move collections top level
Move `messages`,`message-lists`,`pagination-info` from nested in
`chats` to top level at the db.
The reason for this change is that if any of the `messages` fields
change, any `sub` that relies on `chat` will be recomputed, which is
unnecessary.
Move chat-name to events
`chat-name` was computed dynamically, while it is now only calculated
when loading chat the first time around.
Remove `enrich-chats`
Enrich chats was doing a lot of work, and many subscriptions were
relying on it.
Not all the computations were necessary, for example it would always
calculate the name of who invited the user to a group chat, regardless
of whether it was actually used in the view.
This commit changes that behavior so that we use smaller subscriptions
to calculate such fields.
In general we should move computations to events, if that's not
desirable (there are some cases where we might not want to do that), we
should have "bottom/leaf heavy" subscriptions as opposed to "top heavy",
especially if they are to be shared, so only when (and if) we load that
particular view, the subscription is triggered, while others can be
re-used.
I have compared performance with current release, and there's a
noticeable difference. Opening a chat is faster (messages are loaded
faster), and clicking on the home view on a chat is more responsing
(the animation on-press is much quicker).
2020-05-27 14:29:33 +00:00
|
|
|
(is (#'status-im.chat.models.message/message-loaded? {:db
|
|
|
|
{:messages {"a" {"message-id" {}}}}}
|
2020-05-05 14:18:23 +00:00
|
|
|
{:message-id "message-id"
|
|
|
|
:from "a"
|
|
|
|
:clock-value 1
|
|
|
|
:chat-id "a"}))))
|
2020-04-08 16:01:11 +00:00
|
|
|
(deftest earlier-than-deleted-at?
|
|
|
|
(testing "it returns true when the clock-value is the same as the deleted-clock-value in chat"
|
2020-05-05 14:18:23 +00:00
|
|
|
(is (#'status-im.chat.models.message/earlier-than-deleted-at? {:db {:chats {"a" {:deleted-at-clock-value 1}}}}
|
|
|
|
{:message-id "message-id"
|
|
|
|
:from "a"
|
|
|
|
:clock-value 1
|
|
|
|
:chat-id "a"})))
|
2020-04-08 16:01:11 +00:00
|
|
|
(testing "it returns false when the clock-value is greater than the deleted-clock-value in chat"
|
2020-05-05 14:18:23 +00:00
|
|
|
(is (not (#'status-im.chat.models.message/earlier-than-deleted-at? {:db {:chats {"a" {:deleted-at-clock-value 1}}}}
|
|
|
|
{:message-id "message-id"
|
|
|
|
:from "a"
|
|
|
|
:clock-value 2
|
|
|
|
:chat-id "a"}))))
|
2020-04-08 16:01:11 +00:00
|
|
|
(testing "it returns true when the clock-value is less than the deleted-clock-value in chat"
|
2020-05-05 14:18:23 +00:00
|
|
|
(is (#'status-im.chat.models.message/earlier-than-deleted-at? {:db {:chats {"a" {:deleted-at-clock-value 1}}}}
|
|
|
|
{:message-id "message-id"
|
|
|
|
:from "a"
|
|
|
|
:clock-value 0
|
|
|
|
:chat-id "a"}))))
|
2018-05-03 18:20:56 +00:00
|
|
|
|
2018-05-10 15:19:51 +00:00
|
|
|
(deftest delete-message
|
2019-11-12 06:07:17 +00:00
|
|
|
(with-redefs [time/day-relative (constantly "day-relative")
|
|
|
|
time/timestamp->time (constantly "timestamp")]
|
Improve chat loading performance
This commit does a few things:
Move collections top level
Move `messages`,`message-lists`,`pagination-info` from nested in
`chats` to top level at the db.
The reason for this change is that if any of the `messages` fields
change, any `sub` that relies on `chat` will be recomputed, which is
unnecessary.
Move chat-name to events
`chat-name` was computed dynamically, while it is now only calculated
when loading chat the first time around.
Remove `enrich-chats`
Enrich chats was doing a lot of work, and many subscriptions were
relying on it.
Not all the computations were necessary, for example it would always
calculate the name of who invited the user to a group chat, regardless
of whether it was actually used in the view.
This commit changes that behavior so that we use smaller subscriptions
to calculate such fields.
In general we should move computations to events, if that's not
desirable (there are some cases where we might not want to do that), we
should have "bottom/leaf heavy" subscriptions as opposed to "top heavy",
especially if they are to be shared, so only when (and if) we load that
particular view, the subscription is triggered, while others can be
re-used.
I have compared performance with current release, and there's a
noticeable difference. Opening a chat is faster (messages are loaded
faster), and clicking on the home view on a chat is more responsing
(the animation on-press is much quicker).
2020-05-27 14:29:33 +00:00
|
|
|
(let [cofx1 {:db {:messages {"chat-id" {0 {:message-id 0
|
|
|
|
:content "a"
|
|
|
|
:clock-value 0
|
|
|
|
:whisper-timestamp 0
|
|
|
|
:timestamp 0}
|
|
|
|
1 {:message-id 1
|
|
|
|
:content "b"
|
|
|
|
:clock-value 1
|
|
|
|
:whisper-timestamp 1
|
|
|
|
:timestamp 1}}}
|
|
|
|
:message-lists {"chat-id" [{:something :something}]}
|
|
|
|
:chats {"chat-id" {}}}}
|
|
|
|
cofx2 {:db {:messages {"chat-id" {0 {:message-id 0
|
|
|
|
:content "a"
|
|
|
|
:clock-value 0
|
|
|
|
:whisper-timestamp 1
|
|
|
|
:timestamp 1}}}
|
|
|
|
:message-list {"chat-id" [{:something :something}]}
|
|
|
|
:chats {"chat-id" {}}}}
|
2019-11-12 06:07:17 +00:00
|
|
|
fx1 (message/delete-message cofx1 "chat-id" 1)
|
|
|
|
fx2 (message/delete-message cofx2 "chat-id" 0)]
|
|
|
|
(testing "Deleting message deletes it along with all references"
|
|
|
|
(is (= '(0)
|
Improve chat loading performance
This commit does a few things:
Move collections top level
Move `messages`,`message-lists`,`pagination-info` from nested in
`chats` to top level at the db.
The reason for this change is that if any of the `messages` fields
change, any `sub` that relies on `chat` will be recomputed, which is
unnecessary.
Move chat-name to events
`chat-name` was computed dynamically, while it is now only calculated
when loading chat the first time around.
Remove `enrich-chats`
Enrich chats was doing a lot of work, and many subscriptions were
relying on it.
Not all the computations were necessary, for example it would always
calculate the name of who invited the user to a group chat, regardless
of whether it was actually used in the view.
This commit changes that behavior so that we use smaller subscriptions
to calculate such fields.
In general we should move computations to events, if that's not
desirable (there are some cases where we might not want to do that), we
should have "bottom/leaf heavy" subscriptions as opposed to "top heavy",
especially if they are to be shared, so only when (and if) we load that
particular view, the subscription is triggered, while others can be
re-used.
I have compared performance with current release, and there's a
noticeable difference. Opening a chat is faster (messages are loaded
faster), and clicking on the home view on a chat is more responsing
(the animation on-press is much quicker).
2020-05-27 14:29:33 +00:00
|
|
|
(keys (get-in fx1 [:db :messages "chat-id"]))))
|
2019-11-12 06:07:17 +00:00
|
|
|
(is (= [{:one-to-one? false
|
|
|
|
:message-id 0
|
|
|
|
:whisper-timestamp 0
|
|
|
|
:type :message
|
|
|
|
:display-photo? true
|
|
|
|
:system-message? false
|
|
|
|
:last-in-group? true
|
|
|
|
:datemark "day-relative"
|
|
|
|
:clock-value 0
|
|
|
|
:first-in-group? true
|
|
|
|
:from nil
|
2019-11-26 13:15:19 +00:00
|
|
|
:first-outgoing? false
|
|
|
|
:outgoing-seen? false
|
2019-11-12 06:07:17 +00:00
|
|
|
:timestamp-str "timestamp"
|
|
|
|
:first? true
|
|
|
|
:display-username? true
|
2019-11-26 13:15:19 +00:00
|
|
|
:outgoing false}]
|
2019-11-12 06:07:17 +00:00
|
|
|
(models.message-list/->seq
|
Improve chat loading performance
This commit does a few things:
Move collections top level
Move `messages`,`message-lists`,`pagination-info` from nested in
`chats` to top level at the db.
The reason for this change is that if any of the `messages` fields
change, any `sub` that relies on `chat` will be recomputed, which is
unnecessary.
Move chat-name to events
`chat-name` was computed dynamically, while it is now only calculated
when loading chat the first time around.
Remove `enrich-chats`
Enrich chats was doing a lot of work, and many subscriptions were
relying on it.
Not all the computations were necessary, for example it would always
calculate the name of who invited the user to a group chat, regardless
of whether it was actually used in the view.
This commit changes that behavior so that we use smaller subscriptions
to calculate such fields.
In general we should move computations to events, if that's not
desirable (there are some cases where we might not want to do that), we
should have "bottom/leaf heavy" subscriptions as opposed to "top heavy",
especially if they are to be shared, so only when (and if) we load that
particular view, the subscription is triggered, while others can be
re-used.
I have compared performance with current release, and there's a
noticeable difference. Opening a chat is faster (messages are loaded
faster), and clicking on the home view on a chat is more responsing
(the animation on-press is much quicker).
2020-05-27 14:29:33 +00:00
|
|
|
(get-in fx1 [:db :message-lists "chat-id"]))))
|
2019-11-12 06:07:17 +00:00
|
|
|
(is (= {}
|
Improve chat loading performance
This commit does a few things:
Move collections top level
Move `messages`,`message-lists`,`pagination-info` from nested in
`chats` to top level at the db.
The reason for this change is that if any of the `messages` fields
change, any `sub` that relies on `chat` will be recomputed, which is
unnecessary.
Move chat-name to events
`chat-name` was computed dynamically, while it is now only calculated
when loading chat the first time around.
Remove `enrich-chats`
Enrich chats was doing a lot of work, and many subscriptions were
relying on it.
Not all the computations were necessary, for example it would always
calculate the name of who invited the user to a group chat, regardless
of whether it was actually used in the view.
This commit changes that behavior so that we use smaller subscriptions
to calculate such fields.
In general we should move computations to events, if that's not
desirable (there are some cases where we might not want to do that), we
should have "bottom/leaf heavy" subscriptions as opposed to "top heavy",
especially if they are to be shared, so only when (and if) we load that
particular view, the subscription is triggered, while others can be
re-used.
I have compared performance with current release, and there's a
noticeable difference. Opening a chat is faster (messages are loaded
faster), and clicking on the home view on a chat is more responsing
(the animation on-press is much quicker).
2020-05-27 14:29:33 +00:00
|
|
|
(get-in fx2 [:db :messages "chat-id"])))
|
2019-11-12 06:07:17 +00:00
|
|
|
(is (= nil
|
Improve chat loading performance
This commit does a few things:
Move collections top level
Move `messages`,`message-lists`,`pagination-info` from nested in
`chats` to top level at the db.
The reason for this change is that if any of the `messages` fields
change, any `sub` that relies on `chat` will be recomputed, which is
unnecessary.
Move chat-name to events
`chat-name` was computed dynamically, while it is now only calculated
when loading chat the first time around.
Remove `enrich-chats`
Enrich chats was doing a lot of work, and many subscriptions were
relying on it.
Not all the computations were necessary, for example it would always
calculate the name of who invited the user to a group chat, regardless
of whether it was actually used in the view.
This commit changes that behavior so that we use smaller subscriptions
to calculate such fields.
In general we should move computations to events, if that's not
desirable (there are some cases where we might not want to do that), we
should have "bottom/leaf heavy" subscriptions as opposed to "top heavy",
especially if they are to be shared, so only when (and if) we load that
particular view, the subscription is triggered, while others can be
re-used.
I have compared performance with current release, and there's a
noticeable difference. Opening a chat is faster (messages are loaded
faster), and clicking on the home view on a chat is more responsing
(the animation on-press is much quicker).
2020-05-27 14:29:33 +00:00
|
|
|
(get-in fx2 [:db :message-lists "chat-id"])))))))
|