Skip to content

Commit

Permalink
feat: fetch author data from hashnode
Browse files Browse the repository at this point in the history
  • Loading branch information
naomi-lgbt committed Nov 13, 2024
1 parent ce44448 commit 9e300f1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 59 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"discord.js": "14.16.3",
"dotenv": "16.4.5",
"fastify": "5.1.0",
"graphql-query": "0.1.2",
"highlight.js": "11.10.0",
"mongodb": "6.10.0",
"node-html-to-image": "5.0.0",
Expand Down
37 changes: 27 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ model supporters {
model authors {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String @unique
email String @unique
hashnodeUsername String @unique
}
25 changes: 0 additions & 25 deletions src/adminApi.d.ts

This file was deleted.

66 changes: 43 additions & 23 deletions src/commands/author.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line @typescript-eslint/naming-convention
import GhostAdminApi from "@tryghost/admin-api";
import { InteractionContextType, SlashCommandBuilder } from "discord.js";
import { gql, request } from "graphql-request";
import { authorRoleId } from "../config/roles.js";
import { errorHandler } from "../utils/errorHandler.js";
import type { Command } from "../interfaces/command.js";
import type { HashnodeUser } from "../interfaces/hashnode.js";

export const author: Command = {
data: new SlashCommandBuilder().
Expand All @@ -12,14 +12,14 @@ export const author: Command = {
setContexts(InteractionContextType.Guild).
addStringOption((option) => {
return option.
setName("email").
setDescription("The email tied to your freeCodeCamp NEWS account.").
setName("username").
setDescription("Your Hashnode username.").
setRequired(true);
}),
run: async(camperChan, interaction) => {
try {
await interaction.deferReply({ ephemeral: true });
const email = interaction.options.getString("email", true);
const username = interaction.options.getString("username", true);
const { member } = interaction;
if (member.roles.cache.has(authorRoleId)) {
await interaction.editReply({
Expand All @@ -39,39 +39,59 @@ export const author: Command = {
});
return;
}
const existsByEmail = await camperChan.db.authors.findUnique({
const existsByUsername = await camperChan.db.authors.findUnique({
where: {
email,
hashnodeUsername: username,
},
});
if (existsByEmail) {
if (existsByUsername) {
await interaction.editReply({
content:
`An author record already exists on your email. If you believe this is an error, please contact Naomi.`,
`An author record already exists on your Hashnode account. If you believe this is an error, please contact Naomi.`,
});
return;
}
const api = new GhostAdminApi({
key: camperChan.config.ghostKey,
url: "https://freecodecamp.org/news",
version: "v3",
});
const user = await api.users.
browse({ filter: `email:'${email}'` }).
catch(() => {
return null;
});

if (!user || user.length !== 1) {
const ourId = "65dc2b7cbb4eb0cd565b4463";
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const query = gql`
query getMember {
user(username: "Koded001") {
publications(first: 50) {
edges {
node {
id
}
}
}
}
}
`;
const data
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/consistent-type-assertions
= await request("https://api.hashnode.com", query) as HashnodeUser;

if (!data.user) {
await interaction.editReply({
content: `There does not appear to be a Hashnode account associated with ${username}.`,
});
return;
}
const publications = data.user.publications.edges;
const isFreeCodeCamp = publications.some((pub) => {
return pub.node.id === ourId;
});
if (!isFreeCodeCamp) {
await interaction.editReply({
content: `There does not appear to be a news account associated with ${email}. This has been flagged internally for Naomi to investigate.`,
content:
`It appears that you are not a member of the freeCodeCamp publication.`,
});
return;
}
await camperChan.db.authors.create({
data: {
email: email,
userId: member.id,
hashnodeUsername: username,
userId: member.id,
},
});
await member.roles.add(authorRoleId).catch(async() => {
Expand Down
1 change: 1 addition & 0 deletions src/gql.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "graphql-request";
11 changes: 11 additions & 0 deletions src/interfaces/hashnode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface HashnodeUser {
user: {
publications: {
edges: Array<{
node: {
id: string;
};
}>;
};
} | null;
}

0 comments on commit 9e300f1

Please sign in to comment.