2021-12-08 19:52:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
|
|
|
|
import dataInterface from '../../sql/Client';
|
|
|
|
import { UUID } from '../../types/UUID';
|
|
|
|
import type { UUIDStringType } from '../../types/UUID';
|
|
|
|
|
|
|
|
import type { MessageAttributesType } from '../../model-types.d';
|
|
|
|
import { ReadStatus } from '../../messages/MessageReadStatus';
|
|
|
|
|
|
|
|
const {
|
|
|
|
removeAll,
|
|
|
|
_getAllMessages,
|
|
|
|
saveMessages,
|
|
|
|
getMessageMetricsForConversation,
|
|
|
|
getNewerMessagesByConversation,
|
|
|
|
getOlderMessagesByConversation,
|
|
|
|
} = dataInterface;
|
|
|
|
|
|
|
|
function getUuid(): UUIDStringType {
|
|
|
|
return UUID.generate().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('sql/timelineFetches', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await removeAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getOlderMessagesByConversation', () => {
|
|
|
|
it('returns N most recent messages', 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();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const storyId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId: getUuid(),
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
};
|
|
|
|
const message4: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 4',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message5: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 5',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages([message1, message2, message3, message4, message5], {
|
|
|
|
forceSave: true,
|
2021-12-20 21:04:02 +00:00
|
|
|
ourUuid,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 5);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const messages = await getOlderMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: false,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
2022-03-16 17:30:14 +00:00
|
|
|
assert.lengthOf(messages, 3);
|
2021-12-10 22:51:54 +00:00
|
|
|
|
|
|
|
// Fetched with DESC query, but with reverse() call afterwards
|
|
|
|
assert.strictEqual(messages[0].id, message1.id);
|
|
|
|
assert.strictEqual(messages[1].id, message2.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns N most recent messages for a given story', 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();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const storyId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story reply 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'normal message',
|
|
|
|
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,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getOlderMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
|
|
|
storyId,
|
|
|
|
});
|
|
|
|
assert.lengthOf(messages, 1);
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.strictEqual(messages[0].id, message2.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
2022-04-20 23:33:38 +00:00
|
|
|
it('returns N most recent messages excluding group story replies', async () => {
|
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
|
|
|
|
|
|
|
const now = Date.now();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const storyId = getUuid();
|
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story',
|
|
|
|
type: 'incoming',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 20,
|
|
|
|
received_at: now - 20,
|
|
|
|
timestamp: now - 20,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story reply 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now - 10,
|
|
|
|
received_at: now - 10,
|
|
|
|
timestamp: now - 10,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'normal message',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllMessages(), 3);
|
|
|
|
|
|
|
|
const messages = await getOlderMessagesByConversation(conversationId, {
|
|
|
|
isGroup: true,
|
|
|
|
limit: 5,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2022-04-20 23:33:38 +00:00
|
|
|
});
|
|
|
|
assert.lengthOf(messages, 1);
|
|
|
|
assert.strictEqual(messages[0].id, message3.id);
|
|
|
|
});
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
it('returns N messages older than provided received_at', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target - 10,
|
|
|
|
timestamp: target - 10,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target + 10,
|
|
|
|
received_at: target + 10,
|
|
|
|
timestamp: target + 10,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getOlderMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
|
|
|
receivedAt: target,
|
|
|
|
sentAt: target,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
assert.lengthOf(messages, 1);
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.strictEqual(messages[0].id, message1.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns N older messages with received_at, lesser sent_at', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 20,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getOlderMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
|
|
|
receivedAt: target,
|
|
|
|
sentAt: target,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(messages, 2);
|
2021-12-10 22:51:54 +00:00
|
|
|
|
|
|
|
// Fetched with DESC query, but with reverse() call afterwards
|
|
|
|
assert.strictEqual(messages[0].id, message1.id, 'checking message 1');
|
|
|
|
assert.strictEqual(messages[1].id, message2.id, 'checking message 2');
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns N older messages, same received_at/sent_at but excludes messageId', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getOlderMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
2022-05-11 22:20:47 +00:00
|
|
|
messageId: message2.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
receivedAt: target,
|
|
|
|
sentAt: target,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(messages, 1);
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.strictEqual(messages[0].id, message1.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getNewerMessagesByConversation', () => {
|
|
|
|
it('returns N oldest messages with no parameters', 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();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const storyId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId: getUuid(),
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message4: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 4',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now + 10,
|
|
|
|
received_at: now + 10,
|
|
|
|
timestamp: now + 10,
|
|
|
|
};
|
|
|
|
const message5: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 5',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now + 20,
|
|
|
|
received_at: now + 20,
|
|
|
|
timestamp: now + 20,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages([message1, message2, message3, message4, message5], {
|
|
|
|
forceSave: true,
|
2021-12-20 21:04:02 +00:00
|
|
|
ourUuid,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 5);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const messages = await getNewerMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: false,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
2022-03-16 17:30:14 +00:00
|
|
|
assert.lengthOf(messages, 3);
|
|
|
|
assert.strictEqual(messages[0].id, message3.id, 'checking message 3');
|
|
|
|
assert.strictEqual(messages[1].id, message4.id, 'checking message 4');
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns N oldest messages for a given story with no parameters', 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();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const storyId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now,
|
|
|
|
received_at: now,
|
|
|
|
timestamp: now,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now + 10,
|
|
|
|
received_at: now + 10,
|
|
|
|
timestamp: now + 10,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: now + 20,
|
|
|
|
received_at: now + 20,
|
|
|
|
timestamp: now + 20,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getNewerMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
|
|
|
storyId,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(messages, 1);
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.strictEqual(messages[0].id, message2.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns N messages newer than provided received_at', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target - 10,
|
|
|
|
timestamp: target - 10,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target + 10,
|
|
|
|
received_at: target + 10,
|
|
|
|
timestamp: target + 10,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getNewerMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
|
|
|
receivedAt: target,
|
|
|
|
sentAt: target,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
assert.lengthOf(messages, 1);
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.strictEqual(messages[0].id, message3.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
2022-04-20 23:33:38 +00:00
|
|
|
it('returns N messages excluding group story replies', async () => {
|
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target - 10,
|
|
|
|
timestamp: target - 10,
|
|
|
|
storyId: getUuid(),
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target + 20,
|
|
|
|
received_at: target + 20,
|
|
|
|
timestamp: target + 20,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target + 10,
|
|
|
|
received_at: target + 10,
|
|
|
|
timestamp: target + 10,
|
|
|
|
storyId: getUuid(),
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(await _getAllMessages(), 3);
|
|
|
|
|
|
|
|
const messages = await getNewerMessagesByConversation(conversationId, {
|
|
|
|
isGroup: true,
|
|
|
|
limit: 5,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2022-04-20 23:33:38 +00:00
|
|
|
receivedAt: target,
|
|
|
|
sentAt: target,
|
|
|
|
});
|
|
|
|
assert.lengthOf(messages, 1);
|
|
|
|
assert.strictEqual(messages[0].id, message2.id);
|
|
|
|
});
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
it('returns N newer messages with same received_at, greater sent_at', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
|
|
|
|
2021-12-08 19:52:46 +00:00
|
|
|
const message1: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 1',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message2: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 2',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target + 10,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
const message3: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'message 3',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target + 20,
|
|
|
|
received_at: target,
|
|
|
|
timestamp: target,
|
|
|
|
};
|
|
|
|
|
2021-12-20 21:04:02 +00:00
|
|
|
await saveMessages([message1, message2, message3], {
|
|
|
|
forceSave: true,
|
|
|
|
ourUuid,
|
|
|
|
});
|
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 messages = await getNewerMessagesByConversation(conversationId, {
|
2022-05-11 22:20:47 +00:00
|
|
|
isGroup: true,
|
2021-12-08 19:52:46 +00:00
|
|
|
limit: 5,
|
|
|
|
receivedAt: target,
|
|
|
|
sentAt: target,
|
2022-05-11 22:20:47 +00:00
|
|
|
storyId: undefined,
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.lengthOf(messages, 2);
|
|
|
|
// They are not in DESC order because MessageCollection is sorting them
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.strictEqual(messages[0].id, message2.id);
|
|
|
|
assert.strictEqual(messages[1].id, message3.id);
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getMessageMetricsForConversation', () => {
|
|
|
|
it('returns metrics properly for story and non-story timelines', async () => {
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 0);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const target = Date.now();
|
|
|
|
const conversationId = getUuid();
|
|
|
|
const storyId = getUuid();
|
2021-12-20 21:04:02 +00:00
|
|
|
const ourUuid = getUuid();
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const story: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'story',
|
|
|
|
type: 'story',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 10,
|
|
|
|
received_at: target - 10,
|
|
|
|
timestamp: target - 10,
|
|
|
|
};
|
|
|
|
const oldestInStory: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'oldestInStory',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 9,
|
|
|
|
received_at: target - 9,
|
|
|
|
timestamp: target - 9,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const oldest: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'oldest',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 8,
|
|
|
|
received_at: target - 8,
|
|
|
|
timestamp: target - 8,
|
|
|
|
};
|
2022-04-22 18:35:14 +00:00
|
|
|
const oldestUnseen: MessageAttributesType = {
|
2021-12-08 19:52:46 +00:00
|
|
|
id: getUuid(),
|
2022-04-22 18:35:14 +00:00
|
|
|
body: 'oldestUnseen',
|
2021-12-08 19:52:46 +00:00
|
|
|
type: 'incoming',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 7,
|
|
|
|
received_at: target - 7,
|
|
|
|
timestamp: target - 7,
|
|
|
|
readStatus: ReadStatus.Unread,
|
|
|
|
};
|
|
|
|
const oldestStoryUnread: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'oldestStoryUnread',
|
|
|
|
type: 'incoming',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 6,
|
|
|
|
received_at: target - 6,
|
|
|
|
timestamp: target - 6,
|
|
|
|
readStatus: ReadStatus.Unread,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const anotherUnread: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'anotherUnread',
|
|
|
|
type: 'incoming',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 5,
|
|
|
|
received_at: target - 5,
|
|
|
|
timestamp: target - 5,
|
|
|
|
readStatus: ReadStatus.Unread,
|
|
|
|
};
|
|
|
|
const newestInStory: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'newestStory',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 4,
|
|
|
|
received_at: target - 4,
|
|
|
|
timestamp: target - 4,
|
|
|
|
storyId,
|
|
|
|
};
|
|
|
|
const newest: MessageAttributesType = {
|
|
|
|
id: getUuid(),
|
|
|
|
body: 'newest',
|
|
|
|
type: 'outgoing',
|
|
|
|
conversationId,
|
|
|
|
sent_at: target - 3,
|
|
|
|
received_at: target - 3,
|
|
|
|
timestamp: target - 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
await saveMessages(
|
|
|
|
[
|
|
|
|
story,
|
|
|
|
oldestInStory,
|
|
|
|
oldest,
|
2022-04-22 18:35:14 +00:00
|
|
|
oldestUnseen,
|
2021-12-08 19:52:46 +00:00
|
|
|
oldestStoryUnread,
|
|
|
|
anotherUnread,
|
|
|
|
newestInStory,
|
|
|
|
newest,
|
|
|
|
],
|
2021-12-20 21:04:02 +00:00
|
|
|
{ forceSave: true, ourUuid }
|
2021-12-08 19:52:46 +00:00
|
|
|
);
|
|
|
|
|
2021-12-10 22:51:54 +00:00
|
|
|
assert.lengthOf(await _getAllMessages(), 8);
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const metricsInTimeline = await getMessageMetricsForConversation(
|
|
|
|
conversationId
|
|
|
|
);
|
2022-03-16 17:30:14 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
metricsInTimeline?.oldest?.id,
|
|
|
|
oldestInStory.id,
|
|
|
|
'oldest'
|
|
|
|
);
|
2021-12-08 19:52:46 +00:00
|
|
|
assert.strictEqual(metricsInTimeline?.newest?.id, newest.id, 'newest');
|
|
|
|
assert.strictEqual(
|
2022-04-22 18:35:14 +00:00
|
|
|
metricsInTimeline?.oldestUnseen?.id,
|
|
|
|
oldestUnseen.id,
|
|
|
|
'oldestUnseen'
|
2021-12-08 19:52:46 +00:00
|
|
|
);
|
2022-04-22 18:35:14 +00:00
|
|
|
assert.strictEqual(metricsInTimeline?.totalUnseen, 3, 'totalUnseen');
|
2021-12-08 19:52:46 +00:00
|
|
|
|
|
|
|
const metricsInStory = await getMessageMetricsForConversation(
|
|
|
|
conversationId,
|
|
|
|
storyId
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
metricsInStory?.oldest?.id,
|
|
|
|
oldestInStory.id,
|
|
|
|
'oldestInStory'
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
metricsInStory?.newest?.id,
|
|
|
|
newestInStory.id,
|
|
|
|
'newestInStory'
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
2022-04-22 18:35:14 +00:00
|
|
|
metricsInStory?.oldestUnseen?.id,
|
2021-12-08 19:52:46 +00:00
|
|
|
oldestStoryUnread.id,
|
|
|
|
'oldestStoryUnread'
|
|
|
|
);
|
2022-04-22 18:35:14 +00:00
|
|
|
assert.strictEqual(metricsInStory?.totalUnseen, 1, 'totalUnseen');
|
2021-12-08 19:52:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|