Skip to content

Commit

Permalink
Fix message Insert
Browse files Browse the repository at this point in the history
  • Loading branch information
89Q12 committed Jan 13, 2024
1 parent cb007e3 commit a1fee9f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
31 changes: 27 additions & 4 deletions backend/src/guild/events/message-create/guild-message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,10 +17,32 @@ export default class GuildMessageHandler {
@UseGuards(MessageFromUserGuard, IsUserUnlockedGuard)
async onMessage(message: Message): Promise<void> {
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,
Expand Down
36 changes: 25 additions & 11 deletions backend/src/guild/guild-user/guild-user.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -45,18 +52,25 @@ export class GuildUserService {
}

async insertMessage(
userId: string,
messageId: string,
channelId: string,
guildId: string,
) {
data: Messages,
metadata: MessageMetadata,
attachments: Omit<Attachment, 'id'>[],
reactions: Omit<Reaction, 'id'>[],
): Promise<void> {
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,
},
});
}
Expand Down
15 changes: 15 additions & 0 deletions backend/src/util/functions/get-attachtment-type.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit a1fee9f

Please sign in to comment.