diff --git a/backend/src/guild/events/message-create/guild-message/index.ts b/backend/src/guild/events/message-create/guild-message/index.ts index d2dd2a3..e8014a1 100644 --- a/backend/src/guild/events/message-create/guild-message/index.ts +++ b/backend/src/guild/events/message-create/guild-message/index.ts @@ -5,6 +5,7 @@ import { MessageFromUserGuard } from 'src/bot/guards/message-from-user.guard'; import { ChannelIdGuard } from 'src/bot/guards/message-in-channel.guard'; import { IsUserUnlockedGuard } from 'src/bot/guards/user-is-unlocked.guard'; import { GuildUserService } from 'src/guild/guild-user/guild-user.service'; +import getAttachmentType from 'src/util/functions/get-attachtment-type'; @Injectable() export default class GuildMessageHandler { @@ -16,10 +17,32 @@ export default class GuildMessageHandler { @UseGuards(MessageFromUserGuard, IsUserUnlockedGuard) async onMessage(message: Message): Promise { await this.guildUserService.insertMessage( - message.author.id, - message.id, - message.channelId, - message.guildId, + { + userId: message.author.id, + messageId: message.id, + channelId: message.channelId, + guildId: message.guildId, + createdAt: new Date(message.createdTimestamp), + }, + { + messageId: message.id, + length: message.content.length, + }, + message.attachments.map((attachment: Attachment) => { + return { + type: getAttachmentType(attachment.contentType), + name: attachment.name, + url: attachment.url, + messageId: message.id, + }; + }), + message.reactions.cache.map((reaction) => { + return { + messageId: message.id, + emoji: reaction.emoji.toString(), + count: reaction.count, + }; + }), ); await this.guildUserService.updateMessageCountBucket( message.author.id, diff --git a/backend/src/guild/guild-user/guild-user.service.ts b/backend/src/guild/guild-user/guild-user.service.ts index 084046e..cbce6df 100644 --- a/backend/src/guild/guild-user/guild-user.service.ts +++ b/backend/src/guild/guild-user/guild-user.service.ts @@ -1,6 +1,13 @@ import { InjectDiscordClient, Once } from '@discord-nestjs/core'; import { Inject, Injectable } from '@nestjs/common'; -import { GuildUser, Rank } from '@prisma/client'; +import { + Attachment, + GuildUser, + MessageMetadata, + Messages, + Rank, + Reaction, +} from '@prisma/client'; import { Client, GuildMember } from 'discord.js'; import { PrismaService } from 'src/prisma.service'; import { GuildSettingsService } from '../guild-settings/guild-settings.service'; @@ -45,18 +52,25 @@ export class GuildUserService { } async insertMessage( - userId: string, - messageId: string, - channelId: string, - guildId: string, - ) { + data: Messages, + metadata: MessageMetadata, + attachments: Omit[], + reactions: Omit[], + ): Promise { + await this.database.messageMetadata.create({ + data: { + ...metadata, + }, + }); + await this.database.attachment.createMany({ + data: attachments, + }); + await this.database.reaction.createMany({ + data: reactions, + }); await this.database.messages.create({ data: { - userId, - guildId, - messageId, - createdAt: new Date(), - channelId, + ...data, }, }); } diff --git a/backend/src/util/functions/get-attachtment-type.ts b/backend/src/util/functions/get-attachtment-type.ts new file mode 100644 index 0000000..4f4d9d3 --- /dev/null +++ b/backend/src/util/functions/get-attachtment-type.ts @@ -0,0 +1,15 @@ +import { AttachmentType } from '@prisma/client'; + +function getAttachmentType(contentType: string): AttachmentType { + if (contentType.startsWith('image/')) { + return AttachmentType.IMAGE; + } else if (contentType.startsWith('video/')) { + return AttachmentType.VIDEO; + } else if (contentType.startsWith('audio/')) { + return AttachmentType.AUDIO; + } else { + return AttachmentType.OTHER; + } +} + +export default getAttachmentType;