A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {type ChannelID, createChannelID, createMessageID, createUserID, type MessageID} from '@app/api/BrandedTypes';
import {MessageDataRepository} from '@app/api/channel/repositories/message/MessageDataRepository';
import {
deleteOneOrMany,
fetchMany,
fetchOne,
setCassandraQueryExecutorForTesting,
} from '@app/api/database/CassandraQueryExecution';
import type {
ChannelEmptyBucketRow,
ChannelMessageBucketRow,
ChannelStateRow,
MessageRow,
} from '@app/api/database/types/MessageTypes';
import {ChannelEmptyBuckets, ChannelMessageBuckets, ChannelState, Messages} from '@app/api/Tables';
import {InMemoryCassandraQueryExecutor} from '@app/api/test/InMemoryCassandraQueryExecutor';
import {generateSnowflake} from '@fluxer/snowflake/src/Snowflake';
import * as BucketUtils from '@fluxer/snowflake/src/SnowflakeBuckets';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
const FETCH_CHANNEL_STATE = ChannelState.select({where: ChannelState.where.eq('channel_id'), limit: 1});
const FETCH_MESSAGE_BUCKETS = ChannelMessageBuckets.select({
columns: ['bucket'],
where: ChannelMessageBuckets.where.eq('channel_id'),
orderBy: {col: 'bucket', direction: 'DESC'},
});
const FETCH_EMPTY_BUCKETS = ChannelEmptyBuckets.select({
columns: ['bucket'],
where: ChannelEmptyBuckets.where.eq('channel_id'),
orderBy: {col: 'bucket', direction: 'DESC'},
});
function makeMessageRow(channelId: ChannelID, messageId: MessageID): MessageRow {
return {
channel_id: channelId,
bucket: BucketUtils.makeBucket(messageId),
message_id: messageId,
author_id: createUserID(7n),
type: 0,
webhook_id: null,
webhook_name: null,
webhook_avatar_hash: null,
content: 'hello',
edited_timestamp: null,
pinned_timestamp: null,
flags: 0,
mention_everyone: false,
mention_users: null,
mention_roles: null,
mention_channels: null,
attachments: null,
embeds: null,
sticker_items: null,
message_reference: null,
message_snapshots: null,
call: null,
has_reaction: false,
version: 1,
};
}
describe('MessageDataRepository.upsertMessage channel state', () => {
beforeEach(() => {
setCassandraQueryExecutorForTesting(new InMemoryCassandraQueryExecutor());
});
afterEach(() => {
setCassandraQueryExecutorForTesting(new InMemoryCassandraQueryExecutor());
});
it('keeps the newer last_message_id while still recording channel state for an older message', async () => {
const channelId = createChannelID(1710000000000000000n);
const newerMessageId = createMessageID(1730000000000000000n);
const olderMessageId = createMessageID(1720000000000000000n);
const repository = new MessageDataRepository();
await repository.upsertMessage(makeMessageRow(channelId, newerMessageId), null);
await repository.upsertMessage(makeMessageRow(channelId, olderMessageId), null);
expect(await loadChannelState(channelId)).toMatchObject({
created_bucket: BucketUtils.makeBucket(channelId),
has_messages: true,
last_message_id: newerMessageId,
last_message_bucket: BucketUtils.makeBucket(newerMessageId),
});
});
});
describe('MessageDataRepository read scans maintain the bucket index', () => {
beforeEach(() => {
setCassandraQueryExecutorForTesting(new InMemoryCassandraQueryExecutor());
});
afterEach(() => {
setCassandraQueryExecutorForTesting(new InMemoryCassandraQueryExecutor());
});
it('re-indexes a bucket that still holds messages', async () => {
const channelId = createChannelID(generateSnowflake());
const messageId = createMessageID(generateSnowflake());
const bucket = BucketUtils.makeBucket(messageId);
const repository = new MessageDataRepository();
await repository.upsertMessage(makeMessageRow(channelId, messageId), null);
await deleteOneOrMany(ChannelMessageBuckets.deleteByPk({channel_id: channelId, bucket}));
expect(await loadIndexedBuckets(channelId)).toEqual([]);
const messages = await repository.listMessages(channelId);
expect(messages.map((message) => message.id)).toEqual([messageId]);
expect(await loadIndexedBuckets(channelId)).toEqual([bucket]);
expect(await loadEmptyBuckets(channelId)).toEqual([]);
});
it('drops a drained bucket from the index', async () => {
const channelId = createChannelID(generateSnowflake());
const messageId = createMessageID(generateSnowflake());
const bucket = BucketUtils.makeBucket(messageId);
const repository = new MessageDataRepository();
await repository.upsertMessage(makeMessageRow(channelId, messageId), null);
await deleteOneOrMany(Messages.deleteByPk({channel_id: channelId, bucket, message_id: messageId}));
const messages = await repository.listMessages(channelId);
expect(messages).toEqual([]);
expect(await loadIndexedBuckets(channelId)).toEqual([]);
expect(await loadEmptyBuckets(channelId)).toEqual([bucket]);
});
});
async function loadChannelState(channelId: ChannelID): Promise<ChannelStateRow | null> {
return fetchOne<ChannelStateRow>(FETCH_CHANNEL_STATE.bind({channel_id: channelId}));
}
async function loadIndexedBuckets(channelId: ChannelID): Promise<Array<number>> {
const rows = await fetchMany<Pick<ChannelMessageBucketRow, 'bucket'>>(
FETCH_MESSAGE_BUCKETS.bind({channel_id: channelId}),
);
return rows.map((row) => row.bucket);
}
async function loadEmptyBuckets(channelId: ChannelID): Promise<Array<number>> {
const rows = await fetchMany<Pick<ChannelEmptyBucketRow, 'bucket'>>(
FETCH_EMPTY_BUCKETS.bind({channel_id: channelId}),
);
return rows.map((row) => row.bucket);
}