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, UserID} from '@app/api/BrandedTypes';
import {userIdToChannelId} from '@app/api/BrandedTypes';
import type {ChannelRow} from '@app/api/database/types/ChannelTypes';
import {getInstanceConfigRepository} from '@app/api/middleware/ServiceSingletons';
import type {Channel} from '@app/api/models/Channel';
import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
import {PersonalNotesDisabledError} from '@fluxer/errors/src/domains/channel/PersonalNotesDisabledError';
interface PersonalNotesChannelRepository {
findUnique(channelId: ChannelID): Promise<Channel | null>;
upsert(data: ChannelRow): Promise<Channel>;
}
export function isPersonalNotesChannelId({userId, channelId}: {userId: UserID; channelId: ChannelID}): boolean {
return userIdToChannelId(userId) === channelId;
}
export async function personalNotesEnabled(): Promise<boolean> {
return (await getInstanceConfigRepository().getInstancePolicyConfig()).personal_notes_enabled;
}
export async function assertPersonalNotesEnabled(): Promise<void> {
if (!(await personalNotesEnabled())) throw new PersonalNotesDisabledError();
}
function buildPersonalNotesChannelRow(userId: UserID): ChannelRow {
return {
channel_id: userIdToChannelId(userId),
guild_id: null,
type: ChannelTypes.DM_PERSONAL_NOTES,
name: '',
topic: null,
icon_hash: null,
url: null,
parent_id: null,
position: 0,
owner_id: userId,
recipient_ids: new Set<UserID>(),
nsfw: false,
content_warning_level: null,
content_warning_text: null,
rate_limit_per_user: 0,
bitrate: null,
user_limit: null,
voice_connection_limit: null,
rtc_region: null,
rtc_p2p: null,
last_message_id: null,
last_pin_timestamp: null,
permission_overwrites: null,
nicks: null,
soft_deleted: false,
indexed_at: null,
version: 1,
};
}
export async function ensurePersonalNotesChannelExists({
channelRepository,
userId,
}: {
channelRepository: PersonalNotesChannelRepository;
userId: UserID;
}): Promise<Channel> {
const channelId = userIdToChannelId(userId);
const existingChannel = await channelRepository.findUnique(channelId);
if (existingChannel) {
return existingChannel;
}
return channelRepository.upsert(buildPersonalNotesChannelRow(userId));
}