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, GuildID, MessageID, UserID} from '@app/api/BrandedTypes';
import {createChannelID} from '@app/api/BrandedTypes';
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
import {dispatchChannelEvent} from '@app/api/channel/services/ChannelGatewayDispatch';
import {
type CrosspostWorkerService,
enqueueCrosspostSourceRemoval,
} from '@app/api/channel/services/message/CrosspostPropagation';
import {decrementThreadMessageCount, purgeMessageAttachments} from '@app/api/channel/services/message/MessageHelpers';
import {
isChannelEligible,
isTimestampInWindow,
type SelfMessageEligibilityContext,
type SelfMessageFilter,
} from '@app/api/channel/services/message/SelfMessageFilter';
import {assertMutableUserId} from '@app/api/constants/Core';
import type {IPurgeQueue} from '@app/api/infrastructure/CachePurgeQueue';
import type {IGatewayService} from '@app/api/infrastructure/IGatewayService';
import type {IStorageService} from '@app/api/infrastructure/IStorageService';
import {Logger} from '@app/api/Logger';
import type {Message} from '@app/api/models/Message';
import {deleteMessageSearchDocuments} from '@app/api/search/MessageSearchIndexCleanup';
import {chunkArray} from '@app/api/utils/ArrayUtils';
import {snowflakeToDate} from '@fluxer/snowflake/src/Snowflake';
interface UserMessageDeletionServiceDeps {
channelRepository: ChannelRepository;
gatewayService: IGatewayService;
storageService: IStorageService;
purgeQueue: IPurgeQueue;
workerService: CrosspostWorkerService;
}
interface DeleteUserMessagesScope {
channelIds?: ReadonlyArray<ChannelID>;
guildId?: GuildID;
}
interface BulkDeleteUserMessagesOptions {
beforeTimestamp?: number;
channelIdAllowlist?: ReadonlySet<string>;
onProgress?: (deleted: number) => void;
}
type DeleteSelfMessagesFilter = SelfMessageFilter;
type DeleteSelfMessagesEligibilityContext = SelfMessageEligibilityContext;
interface DeleteSelfMessagesResult {
totalDeleted: number;
channelCount: number;
}
interface MessageWithChannel {
channelId: ChannelID;
messageId: MessageID;
message: Message;
}
export class UserMessageDeletionService {
private readonly FETCH_BATCH_SIZE = 100;
private readonly DELETE_BATCH_SIZE = 100;
constructor(private readonly deps: UserMessageDeletionServiceDeps) {}
async deleteUserMessagesInScope(userId: UserID, scope: DeleteUserMessagesScope): Promise<number> {
const channelIdAllowlist = await this.resolveChannelAllowlist(scope);
if (channelIdAllowlist.size === 0) {
Logger.debug({userId: userId.toString()}, 'No channels in scope for bulk user message deletion');
return 0;
}
return this.deleteUserMessagesBulk(userId, {channelIdAllowlist});
}
async deleteUserMessagesBulk(userId: UserID, options: BulkDeleteUserMessagesOptions = {}): Promise<number> {
assertMutableUserId(userId);
const {beforeTimestamp = Number.POSITIVE_INFINITY, channelIdAllowlist, onProgress} = options;
Logger.debug({userId, beforeTimestamp}, 'Starting bulk user message deletion');
const {totalDeleted} = await this.deleteMatchingMessages(
userId,
async (channelId, messageId) => {
if (channelIdAllowlist && !channelIdAllowlist.has(channelId.toString())) {
return false;
}
return snowflakeToDate(messageId).getTime() <= beforeTimestamp;
},
onProgress,
);
Logger.debug({userId, totalDeleted}, 'Bulk user message deletion completed');
return totalDeleted;
}
async deleteUserMessagesFiltered(
userId: UserID,
filter: DeleteSelfMessagesFilter,
context: DeleteSelfMessagesEligibilityContext,
onProgress?: (deleted: number) => void,
): Promise<DeleteSelfMessagesResult> {
Logger.debug({userId, filter}, 'Starting filtered user message deletion');
const channelEligibility = new Map<string, boolean>();
const result = await this.deleteMatchingMessages(
userId,
async (channelId, messageId) => {
if (!isTimestampInWindow(snowflakeToDate(messageId).getTime(), filter)) {
return false;
}
const channelIdStr = channelId.toString();
let eligible = channelEligibility.get(channelIdStr);
if (eligible === undefined) {
const channel = await this.deps.channelRepository.channelData.findUnique(channelId);
eligible = channel ? isChannelEligible(channel, userId, filter, context) : false;
channelEligibility.set(channelIdStr, eligible);
}
return eligible;
},
onProgress,
);
Logger.debug(
{userId, totalDeleted: result.totalDeleted, channelCount: result.channelCount},
'Filtered user message deletion completed',
);
return result;
}
private async deleteMatchingMessages(
userId: UserID,
matches: (channelId: ChannelID, messageId: MessageID) => Promise<boolean>,
onProgress?: (deleted: number) => void,
): Promise<DeleteSelfMessagesResult> {
const pendingByChannel = new Map<string, Array<MessageWithChannel>>();
const channelsWithDeletions = new Set<string>();
let totalDeleted = 0;
let lastMessageId: MessageID | undefined;
while (true) {
const messageRefs = await this.deps.channelRepository.messages.listMessagesByAuthor(
userId,
this.FETCH_BATCH_SIZE,
lastMessageId,
);
if (messageRefs.length === 0) {
break;
}
for (const {channelId, messageId} of messageRefs) {
if (!(await matches(channelId, messageId))) {
continue;
}
const message = await this.deps.channelRepository.messages.getMessage(channelId, messageId);
if (!message || message.authorId !== userId) {
continue;
}
const channelIdStr = channelId.toString();
let pending = pendingByChannel.get(channelIdStr);
if (!pending) {
pending = [];
pendingByChannel.set(channelIdStr, pending);
}
pending.push({channelId, messageId, message});
}
lastMessageId = messageRefs[messageRefs.length - 1]!.messageId;
for (const [channelIdStr, messages] of pendingByChannel) {
const deleted = await this.deleteMessagesInChannel(channelIdStr, messages);
if (deleted > 0) {
channelsWithDeletions.add(channelIdStr);
}
totalDeleted += deleted;
onProgress?.(totalDeleted);
}
pendingByChannel.clear();
}
return {totalDeleted, channelCount: channelsWithDeletions.size};
}
private async resolveChannelAllowlist(scope: DeleteUserMessagesScope): Promise<ReadonlySet<string>> {
const allowlist = new Set<string>();
if (scope.channelIds) {
for (const channelId of scope.channelIds) {
allowlist.add(channelId.toString());
}
const channels = await this.deps.channelRepository.channelData.listChannels([...scope.channelIds]);
for (const channel of channels) {
if (!channel.guildId) continue;
const threadIds = await this.deps.channelRepository.threads.listGuildThreadIds(channel.guildId, {
parents: [channel],
});
for (const threadId of threadIds) {
allowlist.add(threadId.toString());
}
}
}
if (scope.guildId) {
const channels = await this.deps.channelRepository.channelData.listGuildChannels(scope.guildId, 'complete');
for (const channel of channels) {
allowlist.add(channel.id.toString());
}
const threadIds = await this.deps.channelRepository.threads.listGuildThreadIds(scope.guildId, {
parents: channels,
});
for (const threadId of threadIds) {
allowlist.add(threadId.toString());
}
}
return allowlist;
}
private async deleteMessagesInChannel(channelIdStr: string, messages: Array<MessageWithChannel>): Promise<number> {
if (messages.length === 0) {
return 0;
}
const channelId = createChannelID(BigInt(channelIdStr));
const channel = await this.deps.channelRepository.channelData.findUnique(channelId);
if (!channel) {
Logger.debug({channelId: channelIdStr}, 'Channel not found, skipping messages');
return 0;
}
let deleted = 0;
const batches = chunkArray(messages, this.DELETE_BATCH_SIZE);
for (const batch of batches) {
const messageIds = batch.map((m: MessageWithChannel) => m.messageId);
const messageObjects = batch.map((m: MessageWithChannel) => m.message);
await Promise.all(
messageObjects.map((message: Message) =>
purgeMessageAttachments(message, this.deps.storageService, this.deps.purgeQueue),
),
);
await this.deps.channelRepository.messages.bulkDeleteMessages(channelId, messageIds);
await decrementThreadMessageCount(this.deps.channelRepository, channel, messageIds);
await dispatchChannelEvent({
gatewayService: this.deps.gatewayService,
channel,
event: 'MESSAGE_DELETE_BULK',
data: {channel_id: channel.id.toString(), ids: messageIds.map((id) => id.toString())},
});
await enqueueCrosspostSourceRemoval(this.deps.workerService, {
messages: messageObjects,
mode: 'source_deleted',
channel,
});
await deleteMessageSearchDocuments(messageIds, {context: {source: 'bulk_user_message_delete'}});
deleted += batch.length;
}
return deleted;
}
}