2021-07-02 20:40:36 +00:00
|
|
|
// Copyright 2014-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-07-02 20:40:36 +00:00
|
|
|
import { assert } from 'chai';
|
2021-07-19 22:44:49 +00:00
|
|
|
import { SendStatus } from '../../messages/MessageSendState';
|
2021-07-02 20:40:36 +00:00
|
|
|
|
2020-10-20 23:26:58 +00:00
|
|
|
describe('Conversations', () => {
|
2021-07-02 20:40:36 +00:00
|
|
|
async function resetConversationController(): Promise<void> {
|
|
|
|
window.ConversationController.reset();
|
|
|
|
await window.ConversationController.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(resetConversationController);
|
|
|
|
|
|
|
|
afterEach(resetConversationController);
|
|
|
|
|
2020-10-20 23:26:58 +00:00
|
|
|
it('updates lastMessage even in race conditions with db', async () => {
|
2021-01-20 17:31:44 +00:00
|
|
|
const ourNumber = '+15550000000';
|
|
|
|
const ourUuid = window.getGuid();
|
|
|
|
|
2020-10-20 23:26:58 +00:00
|
|
|
// Creating a fake conversation
|
|
|
|
const conversation = new window.Whisper.Conversation({
|
2021-07-19 22:44:49 +00:00
|
|
|
id: window.getGuid(),
|
2020-10-20 23:26:58 +00:00
|
|
|
e164: '+15551234567',
|
2021-07-19 22:44:49 +00:00
|
|
|
uuid: window.getGuid(),
|
2020-10-20 23:26:58 +00:00
|
|
|
type: 'private',
|
2021-07-02 20:40:36 +00:00
|
|
|
inbox_position: 0,
|
|
|
|
isPinned: false,
|
|
|
|
markedUnread: false,
|
|
|
|
lastMessageDeletedForEveryone: false,
|
|
|
|
messageCount: 0,
|
|
|
|
sentMessageCount: 0,
|
|
|
|
profileSharing: true,
|
|
|
|
version: 0,
|
2020-10-20 23:26:58 +00:00
|
|
|
});
|
|
|
|
|
2021-07-23 17:23:50 +00:00
|
|
|
await window.textsecure.storage.user.setCredentials({
|
|
|
|
number: ourNumber,
|
|
|
|
uuid: ourUuid,
|
|
|
|
deviceId: 2,
|
|
|
|
deviceName: 'my device',
|
|
|
|
password: 'password',
|
|
|
|
});
|
2021-07-02 20:40:36 +00:00
|
|
|
await window.ConversationController.loadPromise();
|
2020-10-20 23:26:58 +00:00
|
|
|
|
2021-07-19 22:44:49 +00:00
|
|
|
await window.Signal.Data.saveConversation(conversation.attributes);
|
|
|
|
|
2020-10-20 23:26:58 +00:00
|
|
|
// Creating a fake message
|
|
|
|
const now = Date.now();
|
|
|
|
let message = new window.Whisper.Message({
|
|
|
|
attachments: [],
|
|
|
|
body: 'bananas',
|
|
|
|
conversationId: conversation.id,
|
|
|
|
expirationStartTimestamp: now,
|
2021-07-02 20:40:36 +00:00
|
|
|
hasAttachments: false,
|
|
|
|
hasFileAttachments: false,
|
|
|
|
hasVisualMediaAttachments: false,
|
2021-07-19 22:44:49 +00:00
|
|
|
id: window.getGuid(),
|
2020-10-20 23:26:58 +00:00
|
|
|
received_at: now,
|
|
|
|
sent_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
type: 'outgoing',
|
2021-07-19 22:44:49 +00:00
|
|
|
sendStateByConversationId: {
|
|
|
|
[conversation.id]: {
|
|
|
|
status: SendStatus.Sent,
|
|
|
|
updatedAt: now,
|
|
|
|
},
|
|
|
|
},
|
2020-10-20 23:26:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Saving to db and updating the convo's last message
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
|
|
|
forceSave: true,
|
|
|
|
});
|
|
|
|
message = window.MessageController.register(message.id, message);
|
2021-07-19 22:44:49 +00:00
|
|
|
await window.Signal.Data.updateConversation(conversation.attributes);
|
2020-10-20 23:26:58 +00:00
|
|
|
await conversation.updateLastMessage();
|
|
|
|
|
|
|
|
// Should be set to bananas because that's the last message sent.
|
|
|
|
assert.strictEqual(conversation.get('lastMessage'), 'bananas');
|
|
|
|
|
|
|
|
// Erasing message contents (DOE)
|
|
|
|
message.set({
|
|
|
|
isErased: true,
|
|
|
|
body: '',
|
|
|
|
bodyRanges: undefined,
|
|
|
|
attachments: [],
|
2021-07-02 20:40:36 +00:00
|
|
|
quote: undefined,
|
2020-10-20 23:26:58 +00:00
|
|
|
contact: [],
|
2021-07-02 20:40:36 +00:00
|
|
|
sticker: undefined,
|
2020-10-20 23:26:58 +00:00
|
|
|
preview: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Not saving the message to db on purpose
|
|
|
|
// to simulate that a save hasn't taken place yet.
|
|
|
|
|
|
|
|
// Updating convo's last message, should pick it up from memory
|
|
|
|
await conversation.updateLastMessage();
|
|
|
|
|
|
|
|
assert.strictEqual(conversation.get('lastMessage'), '');
|
|
|
|
});
|
|
|
|
});
|