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

feat(bot): support forum channel tags on questions #379

Merged
merged 2 commits into from
Mar 2, 2023
Merged
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
45 changes: 45 additions & 0 deletions prisma/migrations/20230302013830_question_tags/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Warnings:

- You are about to drop the column `tagId` on the `Question` table. All the data in the column will be lost.

*/
-- CreateTable
CREATE TABLE "_QuestionToQuestionTag" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL,
CONSTRAINT "_QuestionToQuestionTag_A_fkey" FOREIGN KEY ("A") REFERENCES "Question" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "_QuestionToQuestionTag_B_fkey" FOREIGN KEY ("B") REFERENCES "QuestionTag" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Question" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL,
"updatedAt" DATETIME NOT NULL,
"threadId" TEXT NOT NULL,
"ownerId" TEXT NOT NULL,
"channelName" TEXT NOT NULL,
"threadMetaUpdatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"title" TEXT NOT NULL,
"isSolved" BOOLEAN NOT NULL DEFAULT false,
"url" TEXT,
"guildId" TEXT NOT NULL,
"githubDiscussionId" TEXT,
CONSTRAINT "Question_guildId_fkey" FOREIGN KEY ("guildId") REFERENCES "Guild" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Question_githubDiscussionId_fkey" FOREIGN KEY ("githubDiscussionId") REFERENCES "GitHubDiscussion" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Question" ("channelName", "createdAt", "githubDiscussionId", "guildId", "id", "isSolved", "ownerId", "threadId", "threadMetaUpdatedAt", "title", "updatedAt", "url") SELECT "channelName", "createdAt", "githubDiscussionId", "guildId", "id", "isSolved", "ownerId", "threadId", "threadMetaUpdatedAt", "title", "updatedAt", "url" FROM "Question";
DROP TABLE "Question";
ALTER TABLE "new_Question" RENAME TO "Question";
CREATE UNIQUE INDEX "Question_threadId_key" ON "Question"("threadId");
CREATE UNIQUE INDEX "Question_githubDiscussionId_key" ON "Question"("githubDiscussionId");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;

-- CreateIndex
CREATE UNIQUE INDEX "_QuestionToQuestionTag_AB_unique" ON "_QuestionToQuestionTag"("A", "B");

-- CreateIndex
CREATE INDEX "_QuestionToQuestionTag_B_index" ON "_QuestionToQuestionTag"("B");
6 changes: 2 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ model Question {
threadId String @unique
ownerId String
channelName String
// tags applied to forum channel posts (by ID)
threadMetaUpdatedAt DateTime @default(now())
title String
isSolved Boolean @default(false)
Expand All @@ -36,10 +35,9 @@ model Question {
guildId String
githubDiscussion GitHubDiscussion? @relation(fields: [githubDiscussionId], references: [id])
githubDiscussionId String? @unique
// participants DiscordUser[]
participation Participation[]
tags QuestionTag? @relation(fields: [tagId], references: [id])
tagId String?
// tags applied to forum channel posts (by ID)
tags QuestionTag[]
}

model QuestionTag {
Expand Down
20 changes: 19 additions & 1 deletion src/lib/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ client.on(Events.MessageUpdate, async (oldMessage, newMessage) => {
})

client.on(Events.MessageCreate, async (message: Message) => {
// ignore bot messages
if (message.author.bot) return

/**
* Automatically create a thread when new messages are posted to "help" channels
*/
if (
!message.author.bot &&
message.channel.type === ChannelType.GuildText &&
isHelpChannel(message.channel)
) {
Expand Down Expand Up @@ -187,6 +189,14 @@ client.on(Events.MessageCreate, async (message: Message) => {
isThreadWithinHelpChannel(message.channel)
) {
let record
// const messages = await message.channel.messages.fetch()
let tags = []
if (message.channel.parent?.type === ChannelType.GuildForum) {
const appliedTagIds = message.channel.appliedTags
tags = message.channel.parent.availableTags
.filter((tag) => appliedTagIds.includes(tag.id))
.map(({ id, name }) => ({ id, name }))
}
try {
/**
* @TODO if we need to backfill the question, we'll need to fetch all messages from the thread first
Expand All @@ -211,6 +221,14 @@ client.on(Events.MessageCreate, async (message: Message) => {
id: message.guild?.id,
},
},
tags: tags.length
? {
connectOrCreate: tags.map(({ id, name }) => ({
where: { id },
create: { id, name },
})),
}
: undefined,
},
select: {
id: true,
Expand Down