Skip to content

Commit

Permalink
Replace MessageModel#isUnread with isMessageUnread utility
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal committed Jul 29, 2021
1 parent 0acefaa commit 8cadc40
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
3 changes: 2 additions & 1 deletion ts/messageModifiers/ReadSyncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Collection, Model } from 'backbone';

import { MessageModel } from '../models/messages';
import { isIncoming } from '../state/selectors/message';
import { isMessageUnread } from '../util/isMessageUnread';

type ReadSyncAttributesType = {
senderId: string;
Expand Down Expand Up @@ -109,7 +110,7 @@ export class ReadSyncs extends Collection {
// 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
// the previous read time.
if (message.isUnread()) {
if (isMessageUnread(message.attributes)) {
// TODO DESKTOP-1509: use MessageUpdater.markRead once this is TS
message.markRead(readAt, { skipSave: true });

Expand Down
9 changes: 3 additions & 6 deletions ts/models/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
import { migrateLegacySendAttributes } from '../messages/migrateLegacySendAttributes';
import { getOwn } from '../util/getOwn';
import { markRead } from '../services/MessageUpdater';
import { isMessageUnread } from '../util/isMessageUnread';
import {
isDirectConversation,
isGroupV1,
Expand Down Expand Up @@ -745,10 +746,6 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
}
}

isUnread(): boolean {
return !!this.get('unread');
}

merge(model: MessageModel): void {
const attributes = model.attributes || model;
this.set(attributes);
Expand Down Expand Up @@ -847,7 +844,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
return;
}

if (this.get('unread')) {
if (isMessageUnread(this.attributes)) {
this.set(markRead(this.attributes));
}

Expand Down Expand Up @@ -3175,7 +3172,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
const isFirstRun = false;
await this.modifyTargetMessage(conversation, isFirstRun);

if (this.get('unread')) {
if (isMessageUnread(this.attributes)) {
await conversation.notify(this);
}

Expand Down
5 changes: 3 additions & 2 deletions ts/state/ducks/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
getGroupSizeRecommendedLimit,
getGroupSizeHardLimit,
} from '../../groups/limits';
import { isMessageUnread } from '../../util/isMessageUnread';
import { toggleSelectedContactForGroupAddition } from '../../groups/toggleSelectedContactForGroupAddition';
import { GroupNameCollisionsWithIdsByTitle } from '../../util/groupMemberNameCollisions';
import { ContactSpoofingType } from '../../util/contactSpoofing';
Expand Down Expand Up @@ -2241,7 +2242,7 @@ export function reducer(
const oldestId = newMessageIds.find(messageId => {
const message = lookup[messageId];

return Boolean(message.unread);
return message && isMessageUnread(message);
});

if (oldestId) {
Expand All @@ -2257,7 +2258,7 @@ export function reducer(
const newUnread: number = newMessageIds.reduce((sum, messageId) => {
const message = lookup[messageId];

return sum + (message && message.unread ? 1 : 0);
return sum + (message && isMessageUnread(message) ? 1 : 0);
}, 0);
totalUnread = (totalUnread || 0) + newUnread;
}
Expand Down
21 changes: 21 additions & 0 deletions ts/test-both/util/isMessageUnread_test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions ts/util/isMessageUnread.ts
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 8 additions & 7 deletions ts/views/conversation_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
isOutgoing,
isTapToView,
} from '../state/selectors/message';
import { isMessageUnread } from '../util/isMessageUnread';
import { getMessagesByConversation } from '../state/selectors/conversations';
import { ConversationDetailsMembershipList } from '../components/conversation/conversation-details/ConversationDetailsMembershipList';
import { showSafetyNumberChangeDialog } from '../shims/showSafetyNumberChangeDialog';
Expand Down Expand Up @@ -1313,17 +1314,17 @@ Whisper.ConversationView = Whisper.View.extend({
const newestInMemoryMessage = await getMessageById(newestMessageId, {
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(
`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);
Expand Down

0 comments on commit 8cadc40

Please sign in to comment.