Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
fix #16 Messageテーブルをリネーム
Browse files Browse the repository at this point in the history
  • Loading branch information
yogarasu committed Aug 22, 2022
1 parent 58ca323 commit 5424b23
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 90 deletions.
4 changes: 2 additions & 2 deletions libs/category2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class CategoryClient {
return await this.client.category.findFirst({
where: {
id: categoryId,
deployId: isDeployed ? { not: { equals: null } } : { equals: null },
deployId: isDeployed ? { not: { equals: null } } : undefined,
},
orderBy: [
{
Expand All @@ -129,7 +129,7 @@ export class CategoryClient {
async getAllCategory(isDeployed: boolean = false) {
return await this.client.category.findMany({
where: {
deployId: isDeployed ? { not: { equals: null } } : { equals: null },
deployId: isDeployed ? { not: { equals: null } } : undefined,
},
orderBy: [
{
Expand Down
2 changes: 1 addition & 1 deletion libs/channel2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class ChannelClient {
return await this.client.channel.findMany({
where: {
type: 1,
deployId: isDeployed ? { not: { equals: null } } : { equals: null },
deployId: isDeployed ? { not: { equals: null } } : undefined,
},
})
}
Expand Down
135 changes: 68 additions & 67 deletions libs/message2.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient, DiscordMessage, User } from "@prisma/client"
import { PrismaClient, Message, User } from "@prisma/client"
import { access, readFile, constants, readdir } from "node:fs/promises"
import { statSync } from "node:fs"
import { join } from "node:path"
Expand Down Expand Up @@ -26,8 +26,12 @@ interface SlackMessageFile {

export class MessageClient {
client: PrismaClient
channelClient: ChannelClient
userClient: UserClient
constructor(client = new PrismaClient()) {
this.client = client
this.channelClient = new ChannelClient(this.client)
this.userClient = new UserClient(this.client)
}

/**
Expand All @@ -36,66 +40,65 @@ export class MessageClient {
* @param srcDirpath
*/
async migrateAllMessage(slackClient: SlackClient, srcDirpath: string) {
const channelClient = new ChannelClient(this.client)
const userClient = new UserClient(this.client)
// Get all channel data
const channels = await this.channelClient.getAllChannel()

// Get all slack channel data
const slackChannels = await channelClient.getAllSlackChannel()
for (const channel of channels) {
if (!channel.deployId)
throw new Error(`Failed to deployed channel id of ${channel.id}`)

for (const slackChannel of slackChannels) {
// Get slack message file paths
const messageDirPath = join(srcDirpath, slackChannel.name)
// Get message file paths
const messageDirPath = join(srcDirpath, channel.name)
const messageFilePaths = await this.getAllSlackMessageFilePath(
messageDirPath
)

for (const messageFilePath of messageFilePaths) {
// Get slack message file
const slackMessages = await this.getSlackMessageFile(messageFilePath)
// Get message file
const messages = await this.getSlackMessageFile(messageFilePath)

const discordMessages: DiscordMessage[] = []
for (const slackMessage of slackMessages) {
const newMessages: Message[] = []
for (const message of messages) {
if (
slackMessage.type === undefined ||
slackMessage.text === undefined ||
slackMessage.ts === undefined
message.type === undefined ||
message.text === undefined ||
message.ts === undefined
) {
throw new Error("Message is missing required parameter")
}

// Convert slack message content
const content = await this.convertSlackMessageContent(
userClient,
slackMessage.text
// Convert message content
const content = await this.convertMessageContent(
this.userClient,
message.text
)

// Get if message is pinned item
const pinIds = slackChannel.pins ? slackChannel.pins.split(",") : []
const isPinned = pinIds.includes(slackMessage.ts)
const pinIds = channel.pins ? channel.pins.split(",") : []
const isPinned = pinIds.includes(message.ts)

// TODO: Message type
let messageType = 1

// Get message author
let author: User | null = null
if (slackMessage.bot_id) {
author = await userClient.getBot(slackClient, slackMessage.bot_id)
} else if (slackMessage.user) {
author = await userClient.getUser(slackClient, slackMessage.user)
if (message.bot_id) {
author = await this.userClient.getBot(slackClient, message.bot_id)
} else if (message.user) {
author = await this.userClient.getUser(slackClient, message.user)
}
if (!author) throw new Error("Failed to get message author")

// TODO:Replace author image url

discordMessages.push({
id: 0,
messageId: null,
channelId: slackChannel.channelId,
newMessages.push({
timestamp: fromUnixTime(Number(message.ts)),
deployId: null,
channelDeployId: channel.deployId,
content: content,
type: messageType,
isPinned: isPinned,
timestamp: fromUnixTime(Number(slackMessage.ts)),
authorId: author.userId,
authorId: author.id,
authorName: author.name,
authorType: author.type,
authorColor: author.color,
Expand All @@ -105,8 +108,8 @@ export class MessageClient {
})
}

// Update many discord message
await this.updateManyDiscordMessage(discordMessages)
// Update many message
await this.updateManyMessage(newMessages)
}
}
}
Expand All @@ -116,11 +119,13 @@ export class MessageClient {
* @param discordClient
*/
async deployAllMessage(discordClient: DiscordClient) {
// Get all slack channel data
const channelClient = new ChannelClient(this.client)
const slackChannels = await channelClient.getAllChannel()
for (const channel of slackChannels) {
const channelManager = discordClient.channels.cache.get(channel.id)
// Get all channel data
const channels = await this.channelClient.getAllChannel()
for (const channel of channels) {
if (!channel.deployId)
throw new Error(`Failed to deployed channel id of ${channel.name}`)

const channelManager = discordClient.channels.cache.get(channel.deployId)
if (
channelManager === undefined ||
channelManager.type !== ChannelType.GuildText
Expand All @@ -130,17 +135,17 @@ export class MessageClient {
// Pagination message
const take = 100
let skip = 0
const total = await this.client.discordMessage.count({
const total = await this.client.message.count({
where: {
channelId: channel.id,
channelDeployId: channel.deployId,
},
})
while (skip < total) {
const messages = await this.client.discordMessage.findMany({
const messages = await this.client.message.findMany({
take: take,
skip: skip,
where: {
channelId: channel.channelId,
channelDeployId: channel.deployId,
},
orderBy: {
timestamp: "asc",
Expand All @@ -160,10 +165,7 @@ export class MessageClient {
* @param channelManager
* @param messages
*/
async deployManyMessage(
channelManager: TextChannel,
messages: DiscordMessage[]
) {
async deployManyMessage(channelManager: TextChannel, messages: Message[]) {
for (const message of messages) {
// Get post datetime of message
const postTime = format(message.timestamp, " HH:mm")
Expand Down Expand Up @@ -202,8 +204,8 @@ export class MessageClient {

// Update message
const newMessage = message
newMessage.messageId = sendMessage.id
await this.updateDiscordMessage(newMessage)
newMessage.deployId = sendMessage.id
await this.updateMessage(newMessage)
}
}

Expand Down Expand Up @@ -238,11 +240,11 @@ export class MessageClient {
}

/**
* Convert slack message content
* Convert message content
* @param userClient
* @param content
*/
async convertSlackMessageContent(userClient: UserClient, content: string) {
async convertMessageContent(userClient: UserClient, content: string) {
let newContent = content

// Replace mention
Expand Down Expand Up @@ -292,31 +294,31 @@ export class MessageClient {
* Update single dicord message
* @param message
*/
async updateDiscordMessage(message: DiscordMessage) {
return await this.client.discordMessage.upsert({
async updateMessage(message: Message) {
return await this.client.message.upsert({
where: {
timestamp: message.timestamp,
},
update: {
messageId: message.messageId,
channelId: message.channelId,
timestamp: message.timestamp,
deployId: message.deployId,
channelDeployId: message.channelDeployId,
content: message.content,
type: message.type,
isPinned: message.isPinned,
timestamp: message.timestamp,
authorId: message.authorId,
authorName: message.authorName,
authorType: message.authorType,
authorColor: message.authorColor,
authorImageUrl: message.authorImageUrl,
},
create: {
messageId: message.messageId,
channelId: message.channelId,
timestamp: message.timestamp,
deployId: message.deployId,
channelDeployId: message.channelDeployId,
content: message.content,
type: message.type,
isPinned: message.isPinned,
timestamp: message.timestamp,
authorId: message.authorId,
authorName: message.authorName,
authorType: message.authorType,
Expand All @@ -329,35 +331,34 @@ export class MessageClient {
}

/**
* Update many dicord message
* Update many message
* @param messages
*/
async updateManyDiscordMessage(messages: DiscordMessage[]) {
async updateManyMessage(messages: Message[]) {
const query = messages.map((message) =>
this.client.discordMessage.upsert({
this.client.message.upsert({
where: {
timestamp: message.timestamp,
},
update: {
messageId: message.messageId,
channelId: message.channelId,
deployId: message.deployId,
channelDeployId: message.channelDeployId,
content: message.content,
type: message.type,
isPinned: message.isPinned,
timestamp: message.timestamp,
authorId: message.authorId,
authorName: message.authorName,
authorType: message.authorType,
authorColor: message.authorColor,
authorImageUrl: message.authorImageUrl,
},
create: {
messageId: message.messageId,
channelId: message.channelId,
timestamp: message.timestamp,
deployId: message.deployId,
channelDeployId: message.channelDeployId,
content: message.content,
type: message.type,
isPinned: message.isPinned,
timestamp: message.timestamp,
authorId: message.authorId,
authorName: message.authorName,
authorType: message.authorType,
Expand Down
10 changes: 5 additions & 5 deletions libs/user2.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ interface SlackUserChannelFile {

export class UserClient {
client: PrismaClient
channelClient: ChannelClient
constructor(client = new PrismaClient()) {
this.client = client
this.channelClient = new ChannelClient(this.client)
}

/**
Expand Down Expand Up @@ -221,8 +223,7 @@ export class UserClient {
*/
async deployUserImageChannel(discordClient: DiscordClient) {
// Deploy channel for hosting user image
const channelClient = new ChannelClient(this.client)
const userChannel = await channelClient.deployChannel(
const userChannel = await this.channelClient.deployChannel(
discordClient,
"mds-user",
"C0000000000",
Expand Down Expand Up @@ -270,14 +271,13 @@ export class UserClient {
*/
async destroyUserImageChannel(discordClient: DiscordClient) {
// Get channel for hosting user image
const channelClient = new ChannelClient(this.client)
const userChannel = await channelClient.getChannel("mds-user", 2)
const userChannel = await this.channelClient.getChannel("mds-user", 2)
if (!userChannel || !userChannel.deployId)
throw new Error("Failed to get deployed channel for hosting user image")

// TODO: Destroy all message for channel for hosting user image

// Destroy channel for hosting user image
await channelClient.destroyChannel(discordClient, userChannel.deployId)
await this.channelClient.destroyChannel(discordClient, userChannel.deployId)
}
}
29 changes: 14 additions & 15 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,18 @@ model User {
updatedAt DateTime @default(now()) @updatedAt
}

model DiscordMessage {
id Int @id @default(autoincrement())
messageId String? @unique
channelId String?
content String
type Int
isPinned Boolean
timestamp DateTime @unique
authorId String
authorType Int
authorColor Int
authorName String
authorImageUrl String
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
model Message {
timestamp DateTime @id
deployId String? @unique
channelDeployId String
content String
type Int
isPinned Boolean
authorId String
authorType Int
authorColor Int
authorName String
authorImageUrl String
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}

0 comments on commit 5424b23

Please sign in to comment.