2021-12-08 19:52:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
import dataInterface from '../../sql/Client';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../../types/ServiceId';
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
import type { MessageAttributesType } from '../../model-types.d';
|
|
|
|
|
|
|
|
const {
|
|
|
|
removeAll,
|
|
|
|
_getAllMessages,
|
|
|
|
saveMessages,
|
|
|
|
getMessagesWithVisualMediaAttachments,
|
|
|
|
getMessagesWithFileAttachments,
|
|
|
|
} = dataInterface;
|
|
|
|
|
|
|
|
describe('sql/allMedia', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await removeAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getMessagesWithVisualMediaAttachments', () => {
|
|
|
|
it('returns messages matching with visual attachments', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const conversationId = generateUuid();
|
|
|
|
const ourAci = generateAci();
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
hasVisualMediaAttachments: true,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
hasVisualMediaAttachments: true,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 3);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const searchResults = await getMessagesWithVisualMediaAttachments(
|
|
|
|
conversationId,
|
|
|
|
{ limit: 5 }
|
|
|
|
);
|
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message1.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('excludes stories and story replies', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const conversationId = generateUuid();
|
|
|
|
const ourAci = generateAci();
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
hasVisualMediaAttachments: true,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
2023-08-10 16:43:33 +00:00
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
hasVisualMediaAttachments: true,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 3',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
2023-08-10 16:43:33 +00:00
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
hasVisualMediaAttachments: true,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 3);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const searchResults = await getMessagesWithVisualMediaAttachments(
|
|
|
|
conversationId,
|
|
|
|
{ limit: 5 }
|
|
|
|
);
|
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message1.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getMessagesWithFileAttachments', () => {
|
|
|
|
it('returns messages matching with visual attachments', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const conversationId = generateUuid();
|
|
|
|
const ourAci = generateAci();
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
hasFileAttachments: true,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
hasFileAttachments: true,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 3);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const searchResults = await getMessagesWithFileAttachments(
|
|
|
|
conversationId,
|
|
|
|
{ limit: 5 }
|
|
|
|
);
|
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message1.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('excludes stories and story replies', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const conversationId = generateUuid();
|
|
|
|
const ourAci = generateAci();
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
hasFileAttachments: true,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
2023-08-10 16:43:33 +00:00
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
hasFileAttachments: true,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 3',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
2023-08-10 16:43:33 +00:00
|
|
|
storyId: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
hasFileAttachments: true,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2021-12-20 21:04:02 +00:00
|
|
|
});
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 3);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const searchResults = await getMessagesWithFileAttachments(
|
|
|
|
conversationId,
|
|
|
|
{ limit: 5 }
|
|
|
|
);
|
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message1.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|