This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105// SPDX-License-Identifier: AGPL-3.0-or-later
import type {MessageTypeValue} from '@fluxer/constants/src/ChannelConstants';import type {ChannelID, EmojiID, MessageID, RoleID, UserID, WebhookID} from '../BrandedTypes';import type {MessageRow} from '../database/types/MessageTypes';import {Attachment} from './Attachment';import {CallInfo} from './CallInfo';import {Embed} from './Embed';import {MessageRef} from './MessageRef';import {MessageSnapshot} from './MessageSnapshot';import {StickerItem} from './StickerItem';
export class Message { readonly channelId: ChannelID; readonly bucket: number; readonly id: MessageID; readonly authorId: UserID | null; readonly type: MessageTypeValue; readonly webhookId: WebhookID | null; readonly webhookName: string | null; readonly webhookAvatarHash: string | null; readonly content: string | null; readonly editedTimestamp: Date | null; readonly pinnedTimestamp: Date | null; readonly flags: number; readonly mentionEveryone: boolean; readonly mentionedUserIds: Set<UserID>; readonly mentionedRoleIds: Set<RoleID>; readonly mentionedChannelIds: Set<ChannelID>; readonly attachments: Array<Attachment>; readonly embeds: Array<Embed>; readonly stickers: Array<StickerItem>; readonly reference: MessageRef | null; readonly messageSnapshots: Array<MessageSnapshot>; readonly call: CallInfo | null; readonly nsfwEmojis: Set<EmojiID>; readonly hasReaction: boolean | null; readonly version: number;
constructor(row: MessageRow) { this.channelId = row.channel_id; this.bucket = row.bucket; this.id = row.message_id; this.authorId = row.author_id ?? null; this.type = row.type as MessageTypeValue; this.webhookId = row.webhook_id ?? null; this.webhookName = row.webhook_name ?? null; this.webhookAvatarHash = row.webhook_avatar_hash ?? null; this.content = row.content ?? null; this.editedTimestamp = row.edited_timestamp ?? null; this.pinnedTimestamp = row.pinned_timestamp ?? null; this.flags = row.flags ?? 0; this.mentionEveryone = row.mention_everyone ?? false; this.mentionedUserIds = row.mention_users ?? new Set(); this.mentionedRoleIds = row.mention_roles ?? new Set(); this.mentionedChannelIds = row.mention_channels ?? new Set(); this.attachments = (row.attachments ?? []).map((att) => new Attachment(att)); this.embeds = (row.embeds ?? []).flatMap((embed) => { try { return [new Embed(embed)]; } catch { return []; } }); this.stickers = (row.sticker_items ?? []).map((sticker) => new StickerItem(sticker)); this.reference = row.message_reference ? new MessageRef(row.message_reference) : null; this.messageSnapshots = (row.message_snapshots ?? []).map((snapshot) => new MessageSnapshot(snapshot)); this.call = row.call ? new CallInfo(row.call) : null; this.nsfwEmojis = row.nsfw_emojis ?? new Set(); this.hasReaction = row.has_reaction ?? null; this.version = row.version; }
toRow(): MessageRow { return { channel_id: this.channelId, bucket: this.bucket, message_id: this.id, author_id: this.authorId, type: this.type, webhook_id: this.webhookId, webhook_name: this.webhookName, webhook_avatar_hash: this.webhookAvatarHash, content: this.content, edited_timestamp: this.editedTimestamp, pinned_timestamp: this.pinnedTimestamp, flags: this.flags, mention_everyone: this.mentionEveryone, mention_users: this.mentionedUserIds.size > 0 ? this.mentionedUserIds : null, mention_roles: this.mentionedRoleIds.size > 0 ? this.mentionedRoleIds : null, mention_channels: this.mentionedChannelIds.size > 0 ? this.mentionedChannelIds : null, attachments: this.attachments.length > 0 ? this.attachments.map((att) => att.toMessageAttachment()) : null, embeds: this.embeds.length > 0 ? this.embeds.map((embed) => embed.toMessageEmbed()) : null, sticker_items: this.stickers.length > 0 ? this.stickers.map((sticker) => sticker.toMessageStickerItem()) : null, message_reference: this.reference?.toMessageReference() ?? null, message_snapshots: this.messageSnapshots.length > 0 ? this.messageSnapshots.map((snapshot) => snapshot.toMessageSnapshot()) : null, call: this.call?.toMessageCall() ?? null, nsfw_emojis: this.nsfwEmojis.size > 0 ? this.nsfwEmojis : null, has_reaction: this.hasReaction ?? null, version: this.version, }; }}