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';
|
2022-08-10 17:48:33 +00:00
|
|
|
import { IMAGE_PNG } from '../../types/MIME';
|
2021-10-26 22:59:08 +00:00
|
|
|
import { UUID } from '../../types/UUID';
|
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';
|
2021-10-26 22:59:08 +00:00
|
|
|
const ourUuid = UUID.generate().toString();
|
2021-11-30 19:33:51 +00:00
|
|
|
const ourPni = UUID.generate().toString();
|
2021-01-20 17:31:44 +00:00
|
|
|
|
2020-10-20 23:26:58 +00:00
|
|
|
// Creating a fake conversation
|
|
|
|
const conversation = new window.Whisper.Conversation({
|
2021-08-06 00:17:05 +00:00
|
|
|
avatars: [],
|
2021-10-26 22:59:08 +00:00
|
|
|
id: UUID.generate().toString(),
|
2020-10-20 23:26:58 +00:00
|
|
|
e164: '+15551234567',
|
2021-10-26 22:59:08 +00:00
|
|
|
uuid: UUID.generate().toString(),
|
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,
|
2021-11-30 19:33:51 +00:00
|
|
|
pni: ourPni,
|
2021-07-23 17:23:50 +00:00
|
|
|
deviceId: 2,
|
|
|
|
deviceName: 'my device',
|
|
|
|
password: 'password',
|
|
|
|
});
|
2021-11-04 21:11:47 +00:00
|
|
|
await window.ConversationController.load();
|
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-10-26 22:59:08 +00:00
|
|
|
id: UUID.generate().toString(),
|
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,
|
2021-12-20 21:04:02 +00:00
|
|
|
ourUuid,
|
2020-10-20 23:26:58 +00:00
|
|
|
});
|
|
|
|
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'), '');
|
|
|
|
});
|
2022-08-10 17:48:33 +00:00
|
|
|
|
|
|
|
it('only produces attachments on a quote with an image', async () => {
|
|
|
|
// Creating a fake conversation
|
|
|
|
const conversation = new window.Whisper.Conversation({
|
|
|
|
avatars: [],
|
|
|
|
id: UUID.generate().toString(),
|
|
|
|
e164: '+15551234567',
|
|
|
|
uuid: UUID.generate().toString(),
|
|
|
|
type: 'private',
|
|
|
|
inbox_position: 0,
|
|
|
|
isPinned: false,
|
|
|
|
markedUnread: false,
|
|
|
|
lastMessageDeletedForEveryone: false,
|
|
|
|
messageCount: 0,
|
|
|
|
sentMessageCount: 0,
|
|
|
|
profileSharing: true,
|
|
|
|
version: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
const resultNoImage = await conversation.getQuoteAttachment(
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
url: 'https://sometest.signal.org/',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(resultNoImage, []);
|
|
|
|
|
|
|
|
const [resultWithImage] = await conversation.getQuoteAttachment(
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
url: 'https://sometest.signal.org/',
|
|
|
|
image: {
|
|
|
|
contentType: IMAGE_PNG,
|
|
|
|
size: 100,
|
|
|
|
data: new Uint8Array(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(resultWithImage.contentType, 'image/png');
|
|
|
|
assert.equal(resultWithImage.fileName, null);
|
|
|
|
});
|
2020-10-20 23:26:58 +00:00
|
|
|
});
|