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,
|
|
|
|
saveMessage,
|
|
|
|
searchMessages,
|
|
|
|
} = dataInterface;
|
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
describe('sql/searchMessages', () => {
|
2021-12-08 19:52:46 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
await removeAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns messages matching query', 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 - generic string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2 - unique string',
|
|
|
|
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 - generic string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
const searchResults = await searchMessages({ query: 'unique' });
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message2.id);
|
|
|
|
|
|
|
|
message3.body = 'message 3 - unique string';
|
2023-08-10 16:43:33 +00:00
|
|
|
await saveMessage(message3, { ourAci });
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
const searchResults2 = await searchMessages({ query: 'unique' });
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(searchResults2, 2);
|
|
|
|
assert.strictEqual(searchResults2[0].id, message3.id);
|
|
|
|
assert.strictEqual(searchResults2[1].id, message2.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('excludes messages with isViewOnce = true', 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 - unique string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2 - unique string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
isViewOnce: 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 - generic string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
isViewOnce: 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
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
const searchResults = await searchMessages({ query: 'unique' });
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message1.id);
|
|
|
|
|
|
|
|
message1.body = 'message 3 - unique string';
|
2023-08-10 16:43:33 +00:00
|
|
|
await saveMessage(message3, { ourAci });
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
const searchResults2 = await searchMessages({ query: 'unique' });
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(searchResults2, 1);
|
|
|
|
assert.strictEqual(searchResults2[0].id, message1.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('excludes messages with storyId !== null', 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 - unique string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 2 - unique string',
|
|
|
|
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
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-12-08 19:52:46 +00:00
|
|
|
body: 'message 3 - generic string',
|
|
|
|
type: 'outgoing',
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
const searchResults = await searchMessages({ query: 'unique' });
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(searchResults, 1);
|
|
|
|
assert.strictEqual(searchResults[0].id, message1.id);
|
|
|
|
|
|
|
|
message1.body = 'message 3 - unique string';
|
2023-08-10 16:43:33 +00:00
|
|
|
await saveMessage(message3, { ourAci });
|
2021-12-08 19:52:46 +00:00
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
const searchResults2 = await searchMessages({ query: 'unique' });
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.lengthOf(searchResults2, 1);
|
|
|
|
assert.strictEqual(searchResults2[0].id, message1.id);
|
|
|
|
});
|
2023-06-26 18:25:48 +00:00
|
|
|
|
|
|
|
it('limits messages returned to a specific conversation if specified', async () => {
|
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
|
|
|
|
|
|
|
const now = Date.now();
|
2023-08-10 16:43:33 +00:00
|
|
|
const conversationId = generateUuid();
|
|
|
|
const otherConversationId = generateUuid();
|
|
|
|
const ourAci = generateAci();
|
2023-06-26 18:25:48 +00:00
|
|
|
|
|
|
|
const message1: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2023-06-26 18:25:48 +00:00
|
|
|
body: 'message 1 - unique string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2023-06-26 18:25:48 +00:00
|
|
|
body: 'message 2 - unique string',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId: otherConversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages([message1, message2], {
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllMessages(), 2);
|
|
|
|
|
|
|
|
const searchResults = await searchMessages({ query: 'unique' });
|
|
|
|
assert.lengthOf(searchResults, 2);
|
|
|
|
|
|
|
|
const searchResultsWithConversationId = await searchMessages({
|
|
|
|
query: 'unique',
|
|
|
|
conversationId: otherConversationId,
|
|
|
|
});
|
|
|
|
assert.lengthOf(searchResultsWithConversationId, 1);
|
|
|
|
assert.strictEqual(searchResultsWithConversationId[0].id, message2.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sql/searchMessages/withMentions', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await removeAll();
|
|
|
|
});
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = generateAci();
|
2023-06-26 18:25:48 +00:00
|
|
|
async function storeMessages(
|
|
|
|
messageOverrides: Array<Partial<MessageAttributesType>>
|
|
|
|
) {
|
|
|
|
const now = Date.now();
|
|
|
|
const messages: Array<MessageAttributesType> = messageOverrides.map(
|
|
|
|
(overrides, idx) => ({
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2023-06-26 18:25:48 +00:00
|
|
|
body: ' ',
|
|
|
|
type: 'incoming',
|
|
|
|
sent_at: now - idx,
|
|
|
|
received_at: now - idx,
|
|
|
|
timestamp: now - idx,
|
2023-08-10 16:43:33 +00:00
|
|
|
conversationId: generateUuid(),
|
2023-06-26 18:25:48 +00:00
|
|
|
...overrides,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
await saveMessages(messages, {
|
|
|
|
forceSave: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci,
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('includes messages with mentions', async () => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const mentionedAcis = [generateAci(), generateAci()];
|
2023-06-26 18:25:48 +00:00
|
|
|
const messages = await storeMessages([
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
bodyRanges: [{ start: 0, length: 1, mentionAci: mentionedAcis[0] }],
|
2023-06-26 18:25:48 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-16 20:54:39 +00:00
|
|
|
bodyRanges: [{ start: 0, length: 1, mentionAci: mentionedAcis[1] }],
|
2023-06-26 18:25:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
bodyRanges: [
|
2023-08-16 20:54:39 +00:00
|
|
|
{ start: 0, length: 1, mentionAci: mentionedAcis[0] },
|
|
|
|
{ start: 1, length: 1, mentionAci: mentionedAcis[1] },
|
2023-06-26 18:25:48 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const searchResults = await searchMessages({
|
|
|
|
query: 'alice',
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery: [mentionedAcis[0], generateAci()],
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.sameOrderedMembers(
|
|
|
|
searchResults.map(res => res.id),
|
|
|
|
[messages[0].id, messages[2].id]
|
|
|
|
);
|
|
|
|
|
|
|
|
const searchResultsForMultipleMatchingUuids = await searchMessages({
|
|
|
|
query: 'alice',
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery: [mentionedAcis[0], mentionedAcis[1]],
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.sameOrderedMembers(
|
|
|
|
searchResultsForMultipleMatchingUuids.map(res => res.id),
|
|
|
|
// TODO: should only return unique messages
|
|
|
|
[messages[0].id, messages[1].id, messages[2].id]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes messages with mentions and those that match the body text', async () => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const mentionedAcis = [generateAci(), generateAci()];
|
2023-06-26 18:25:48 +00:00
|
|
|
const messages = await storeMessages([
|
|
|
|
{
|
|
|
|
body: 'cat',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: 'dog',
|
|
|
|
bodyRanges: [
|
2023-08-16 20:54:39 +00:00
|
|
|
{ start: 0, length: 1, mentionAci: mentionedAcis[0] },
|
|
|
|
{ start: 1, length: 1, mentionAci: mentionedAcis[1] },
|
2023-06-26 18:25:48 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: 'dog',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const searchResults = await searchMessages({
|
|
|
|
query: 'cat',
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery: [mentionedAcis[0], generateAci()],
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.sameOrderedMembers(
|
|
|
|
searchResults.map(res => res.id),
|
|
|
|
[messages[0].id, messages[1].id]
|
|
|
|
);
|
|
|
|
|
|
|
|
// check that results get returned in the right order, independent of whether they
|
|
|
|
// match the mention or the text
|
|
|
|
const searchResultsForDog = await searchMessages({
|
|
|
|
query: 'dog',
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery: [mentionedAcis[1], generateAci()],
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
assert.sameOrderedMembers(
|
|
|
|
searchResultsForDog.map(res => res.id),
|
|
|
|
[messages[1].id, messages[2].id]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('respects conversationId for mention matches', async () => {
|
2023-08-16 20:54:39 +00:00
|
|
|
const mentionedAcis = [generateAci(), generateAci()];
|
2023-08-10 16:43:33 +00:00
|
|
|
const conversationId = generateUuid();
|
2023-06-26 18:25:48 +00:00
|
|
|
const messages = await storeMessages([
|
|
|
|
{
|
|
|
|
body: 'cat',
|
|
|
|
conversationId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: 'dog',
|
2023-08-16 20:54:39 +00:00
|
|
|
bodyRanges: [{ start: 0, length: 1, mentionAci: mentionedAcis[0] }],
|
2023-06-26 18:25:48 +00:00
|
|
|
conversationId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: 'dog',
|
2023-08-16 20:54:39 +00:00
|
|
|
bodyRanges: [{ start: 0, length: 1, mentionAci: mentionedAcis[0] }],
|
2023-06-26 18:25:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
body: 'cat',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const searchResults = await searchMessages({
|
|
|
|
query: 'cat',
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery: [mentionedAcis[0]],
|
2023-06-26 18:25:48 +00:00
|
|
|
conversationId,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.sameOrderedMembers(
|
|
|
|
searchResults.map(res => res.id),
|
|
|
|
[messages[0].id, messages[1].id]
|
|
|
|
);
|
|
|
|
|
|
|
|
const searchResultsWithoutConversationid = await searchMessages({
|
|
|
|
query: 'cat',
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery: [mentionedAcis[0]],
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.sameOrderedMembers(
|
|
|
|
searchResultsWithoutConversationid.map(res => res.id),
|
|
|
|
[messages[0].id, messages[1].id, messages[2].id, messages[3].id]
|
|
|
|
);
|
|
|
|
});
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|