2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2014 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';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
|
|
|
|
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';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci, generatePni } from '../../types/ServiceId';
|
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';
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = generateAci();
|
|
|
|
const ourPni = generatePni();
|
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: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2020-10-20 23:26:58 +00:00
|
|
|
e164: '+15551234567',
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: generateAci(),
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
aci: ourAci,
|
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();
|
2023-04-10 22:19:48 +00:00
|
|
|
await window.ConversationController.getOrCreateAndWait(
|
|
|
|
conversation.attributes.e164 ?? null,
|
|
|
|
conversation.attributes.type,
|
|
|
|
conversation.attributes
|
|
|
|
);
|
2021-07-19 22:44:49 +00:00
|
|
|
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
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: [],
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2022-08-10 17:48:33 +00:00
|
|
|
e164: '+15551234567',
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: generateAci(),
|
2022-08-10 17:48:33 +00:00
|
|
|
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
|
|
|
});
|