Replace MessageModel#isUnread with isMessageUnread utility
This commit is contained in:
parent
0acefaa656
commit
8cadc40975
6 changed files with 45 additions and 16 deletions
|
@ -7,6 +7,7 @@ import { Collection, Model } from 'backbone';
|
||||||
|
|
||||||
import { MessageModel } from '../models/messages';
|
import { MessageModel } from '../models/messages';
|
||||||
import { isIncoming } from '../state/selectors/message';
|
import { isIncoming } from '../state/selectors/message';
|
||||||
|
import { isMessageUnread } from '../util/isMessageUnread';
|
||||||
|
|
||||||
type ReadSyncAttributesType = {
|
type ReadSyncAttributesType = {
|
||||||
senderId: string;
|
senderId: string;
|
||||||
|
@ -109,7 +110,7 @@ export class ReadSyncs extends Collection {
|
||||||
// If message is unread, we mark it read. Otherwise, we update the expiration
|
// If message is unread, we mark it read. Otherwise, we update the expiration
|
||||||
// timer to the time specified by the read sync if it's earlier than
|
// timer to the time specified by the read sync if it's earlier than
|
||||||
// the previous read time.
|
// the previous read time.
|
||||||
if (message.isUnread()) {
|
if (isMessageUnread(message.attributes)) {
|
||||||
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
|
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
|
||||||
message.markRead(readAt, { skipSave: true });
|
message.markRead(readAt, { skipSave: true });
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,7 @@ import {
|
||||||
import { migrateLegacySendAttributes } from '../messages/migrateLegacySendAttributes';
|
import { migrateLegacySendAttributes } from '../messages/migrateLegacySendAttributes';
|
||||||
import { getOwn } from '../util/getOwn';
|
import { getOwn } from '../util/getOwn';
|
||||||
import { markRead } from '../services/MessageUpdater';
|
import { markRead } from '../services/MessageUpdater';
|
||||||
|
import { isMessageUnread } from '../util/isMessageUnread';
|
||||||
import {
|
import {
|
||||||
isDirectConversation,
|
isDirectConversation,
|
||||||
isGroupV1,
|
isGroupV1,
|
||||||
|
@ -745,10 +746,6 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isUnread(): boolean {
|
|
||||||
return !!this.get('unread');
|
|
||||||
}
|
|
||||||
|
|
||||||
merge(model: MessageModel): void {
|
merge(model: MessageModel): void {
|
||||||
const attributes = model.attributes || model;
|
const attributes = model.attributes || model;
|
||||||
this.set(attributes);
|
this.set(attributes);
|
||||||
|
@ -847,7 +844,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.get('unread')) {
|
if (isMessageUnread(this.attributes)) {
|
||||||
this.set(markRead(this.attributes));
|
this.set(markRead(this.attributes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3175,7 +3172,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
const isFirstRun = false;
|
const isFirstRun = false;
|
||||||
await this.modifyTargetMessage(conversation, isFirstRun);
|
await this.modifyTargetMessage(conversation, isFirstRun);
|
||||||
|
|
||||||
if (this.get('unread')) {
|
if (isMessageUnread(this.attributes)) {
|
||||||
await conversation.notify(this);
|
await conversation.notify(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ import {
|
||||||
getGroupSizeRecommendedLimit,
|
getGroupSizeRecommendedLimit,
|
||||||
getGroupSizeHardLimit,
|
getGroupSizeHardLimit,
|
||||||
} from '../../groups/limits';
|
} from '../../groups/limits';
|
||||||
|
import { isMessageUnread } from '../../util/isMessageUnread';
|
||||||
import { toggleSelectedContactForGroupAddition } from '../../groups/toggleSelectedContactForGroupAddition';
|
import { toggleSelectedContactForGroupAddition } from '../../groups/toggleSelectedContactForGroupAddition';
|
||||||
import { GroupNameCollisionsWithIdsByTitle } from '../../util/groupMemberNameCollisions';
|
import { GroupNameCollisionsWithIdsByTitle } from '../../util/groupMemberNameCollisions';
|
||||||
import { ContactSpoofingType } from '../../util/contactSpoofing';
|
import { ContactSpoofingType } from '../../util/contactSpoofing';
|
||||||
|
@ -2241,7 +2242,7 @@ export function reducer(
|
||||||
const oldestId = newMessageIds.find(messageId => {
|
const oldestId = newMessageIds.find(messageId => {
|
||||||
const message = lookup[messageId];
|
const message = lookup[messageId];
|
||||||
|
|
||||||
return Boolean(message.unread);
|
return message && isMessageUnread(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (oldestId) {
|
if (oldestId) {
|
||||||
|
@ -2257,7 +2258,7 @@ export function reducer(
|
||||||
const newUnread: number = newMessageIds.reduce((sum, messageId) => {
|
const newUnread: number = newMessageIds.reduce((sum, messageId) => {
|
||||||
const message = lookup[messageId];
|
const message = lookup[messageId];
|
||||||
|
|
||||||
return sum + (message && message.unread ? 1 : 0);
|
return sum + (message && isMessageUnread(message) ? 1 : 0);
|
||||||
}, 0);
|
}, 0);
|
||||||
totalUnread = (totalUnread || 0) + newUnread;
|
totalUnread = (totalUnread || 0) + newUnread;
|
||||||
}
|
}
|
||||||
|
|
21
ts/test-both/util/isMessageUnread_test.ts
Normal file
21
ts/test-both/util/isMessageUnread_test.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2021 Signal Messenger, LLC
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import { assert } from 'chai';
|
||||||
|
|
||||||
|
import { isMessageUnread } from '../../util/isMessageUnread';
|
||||||
|
|
||||||
|
describe('isMessageUnread', () => {
|
||||||
|
it("returns false if the message's `unread` field is undefined", () => {
|
||||||
|
assert.isFalse(isMessageUnread({}));
|
||||||
|
assert.isFalse(isMessageUnread({ unread: undefined }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false if the message is read', () => {
|
||||||
|
assert.isFalse(isMessageUnread({ unread: false }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns true if the message is unread', () => {
|
||||||
|
assert.isTrue(isMessageUnread({ unread: true }));
|
||||||
|
});
|
||||||
|
});
|
8
ts/util/isMessageUnread.ts
Normal file
8
ts/util/isMessageUnread.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
// Copyright 2021 Signal Messenger, LLC
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import type { MessageAttributesType } from '../model-types.d';
|
||||||
|
|
||||||
|
export const isMessageUnread = (
|
||||||
|
message: Readonly<Pick<MessageAttributesType, 'unread'>>
|
||||||
|
): boolean => Boolean(message.unread);
|
|
@ -40,6 +40,7 @@ import {
|
||||||
isOutgoing,
|
isOutgoing,
|
||||||
isTapToView,
|
isTapToView,
|
||||||
} from '../state/selectors/message';
|
} from '../state/selectors/message';
|
||||||
|
import { isMessageUnread } from '../util/isMessageUnread';
|
||||||
import { getMessagesByConversation } from '../state/selectors/conversations';
|
import { getMessagesByConversation } from '../state/selectors/conversations';
|
||||||
import { ConversationDetailsMembershipList } from '../components/conversation/conversation-details/ConversationDetailsMembershipList';
|
import { ConversationDetailsMembershipList } from '../components/conversation/conversation-details/ConversationDetailsMembershipList';
|
||||||
import { showSafetyNumberChangeDialog } from '../shims/showSafetyNumberChangeDialog';
|
import { showSafetyNumberChangeDialog } from '../shims/showSafetyNumberChangeDialog';
|
||||||
|
@ -1313,17 +1314,17 @@ Whisper.ConversationView = Whisper.View.extend({
|
||||||
const newestInMemoryMessage = await getMessageById(newestMessageId, {
|
const newestInMemoryMessage = await getMessageById(newestMessageId, {
|
||||||
Message: Whisper.Message,
|
Message: Whisper.Message,
|
||||||
});
|
});
|
||||||
if (!newestInMemoryMessage) {
|
if (newestInMemoryMessage) {
|
||||||
|
// If newest in-memory message is unread, scrolling down would mean going to
|
||||||
|
// the very bottom, not the oldest unread.
|
||||||
|
if (isMessageUnread(newestInMemoryMessage.attributes)) {
|
||||||
|
scrollToLatestUnread = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
window.log.warn(
|
window.log.warn(
|
||||||
`loadNewestMessages: did not find message ${newestMessageId}`
|
`loadNewestMessages: did not find message ${newestMessageId}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If newest in-memory message is unread, scrolling down would mean going to
|
|
||||||
// the very bottom, not the oldest unread.
|
|
||||||
if (newestInMemoryMessage && newestInMemoryMessage.isUnread()) {
|
|
||||||
scrollToLatestUnread = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const metrics = await getMessageMetricsForConversation(conversationId);
|
const metrics = await getMessageMetricsForConversation(conversationId);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue