Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove deleted threads/questions #480

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Participation" (
"id" TEXT NOT NULL PRIMARY KEY,
"questionId" TEXT NOT NULL,
"participantId" TEXT NOT NULL,
CONSTRAINT "Participation_questionId_fkey" FOREIGN KEY ("questionId") REFERENCES "Question" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Participation_participantId_fkey" FOREIGN KEY ("participantId") REFERENCES "DiscordUser" ("id") ON DELETE NO ACTION ON UPDATE CASCADE
);
INSERT INTO "new_Participation" ("id", "participantId", "questionId") SELECT "id", "participantId", "questionId" FROM "Participation";
DROP TABLE "Participation";
ALTER TABLE "new_Participation" RENAME TO "Participation";
CREATE UNIQUE INDEX "Participation_questionId_participantId_key" ON "Participation"("questionId", "participantId");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ model QuestionTag {

model Participation {
id String @id @default(cuid())
question Question @relation(fields: [questionId], references: [id])
question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
questionId String
participant DiscordUser @relation(fields: [participantId], references: [id])
participant DiscordUser @relation(fields: [participantId], references: [id], onDelete: NoAction)
// @TODO - track role at the time of participation, accounts for users that _leave_ "staff"
participantId String
participantRoles DiscordRole[]
Expand Down
24 changes: 23 additions & 1 deletion src/lib/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ client.on(Events.MessageCreate, async (message: Message) => {
// capture thread updates in public "help" channels
if (
message.channel.type === ChannelType.PublicThread &&
!message.author.bot &&
isThreadWithinHelpChannel(message.channel)
) {
let record
Expand Down Expand Up @@ -432,6 +431,29 @@ client.on(Events.ThreadUpdate, async (oldThread, newThread) => {
}
})

client.on(Events.ThreadDelete, async (thread) => {
const log = (message: string) =>
console.log(`[client:events.ThreadDelete] ${message}`)
log('started')
const timeMessage = '[client:events:ThreadDelete] finished'
console.time(timeMessage)
log('deleting question...')
try {
await prisma.question.delete({
where: {
threadId: thread.id,
},
include: {
participation: true,
answer: true,
},
})
} catch (error) {
console.error('Unable to delete question', error)
}
console.timeEnd(timeMessage)
})

export function createBot(token = process.env.DISCORD_BOT_TOKEN) {
return client.login(token)
}
Expand Down
Loading