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 {createAttachmentID, createChannelID, createMessageID, createUserID} from '@app/api/BrandedTypes';
import {Config} from '@app/api/Config';
import {
EMBED_MEDIA_OWNED_ATTACHMENT_FLAG,
purgeMessageAttachments,
} from '@app/api/channel/services/message/MessageHelpers';
import type {MessageEmbed} from '@app/api/database/types/MessageTypes';
import type {IPurgeQueue} from '@app/api/infrastructure/CachePurgeQueue';
import {Message} from '@app/api/models/Message';
import {MockStorageService} from '@app/api/test/mocks/MockStorageService';
import {MessageTypes} from '@fluxer/constants/src/ChannelConstants';
import {describe, expect, it} from 'vitest';
const CHANNEL_ID = createChannelID(10n);
const ATTACHMENT_KEY = 'attachments/10/200/ação.png';
const OTHER_MESSAGE_ATTACHMENT_KEY = 'attachments/11/300/photo.jpg';
function imageEmbed(key: string, flags = 0): MessageEmbed {
return {
type: 'image',
title: null,
description: null,
url: null,
timestamp: null,
color: null,
author: null,
provider: null,
thumbnail: null,
image: {
url: `${Config.endpoints.media}/${key}`,
width: 16,
height: 16,
description: null,
content_type: 'image/png',
content_hash: null,
placeholder: null,
flags,
duration: null,
},
video: null,
footer: null,
fields: null,
nsfw: null,
};
}
function makeMessageWithMedia(
embeds: Array<MessageEmbed> = [imageEmbed(ATTACHMENT_KEY), imageEmbed(OTHER_MESSAGE_ATTACHMENT_KEY)],
): Message {
return new Message({
channel_id: CHANNEL_ID,
bucket: 0,
message_id: createMessageID(100n),
author_id: createUserID(3n),
type: MessageTypes.DEFAULT,
webhook_id: null,
webhook_name: null,
webhook_avatar_hash: null,
content: '',
edited_timestamp: null,
pinned_timestamp: null,
flags: 0,
mention_everyone: false,
mention_users: null,
mention_roles: null,
mention_channels: null,
attachments: [
{
attachment_id: createAttachmentID(200n),
filename: 'ação.png',
size: 1024n,
title: null,
description: null,
width: 16,
height: 16,
content_type: 'image/png',
content_hash: null,
placeholder: null,
flags: 0,
duration: null,
nsfw: null,
waveform: null,
},
],
embeds,
sticker_items: null,
message_reference: null,
message_snapshots: null,
call: null,
has_reaction: null,
version: 1,
});
}
describe('purgeMessageAttachments', () => {
it('queues each stored media URL of a deleted message once and leaves media of other messages alone', async () => {
const queuedUrls: Array<string> = [];
const storageService = new MockStorageService();
const purgeQueue: IPurgeQueue = {
addUrls: async (urls) => {
queuedUrls.push(...urls);
},
};
await purgeMessageAttachments(makeMessageWithMedia(), storageService, purgeQueue);
expect(storageService.getDeletedObjects()).toEqual([{bucket: Config.s3.buckets.cdn, key: ATTACHMENT_KEY}]);
expect(queuedUrls).toEqual([`${Config.endpoints.media}/${ATTACHMENT_KEY}`]);
});
it('purges embed files the message owns and leaves marked files under other channels alone', async () => {
const storageService = new MockStorageService();
const purgeQueue: IPurgeQueue = {addUrls: async () => {}};
const ownedEmbedKey = 'attachments/10/201/embed.png';
await purgeMessageAttachments(
makeMessageWithMedia([
imageEmbed(ownedEmbedKey, EMBED_MEDIA_OWNED_ATTACHMENT_FLAG),
imageEmbed(OTHER_MESSAGE_ATTACHMENT_KEY, EMBED_MEDIA_OWNED_ATTACHMENT_FLAG),
imageEmbed('attachments/10/202/unmarked.png'),
]),
storageService,
purgeQueue,
);
expect(storageService.getDeletedObjects().map((object) => object.key)).toEqual([ATTACHMENT_KEY, ownedEmbedKey]);
});
});