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

Render jobs in API #437

Merged
merged 5 commits into from
Jan 17, 2025
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
22 changes: 0 additions & 22 deletions .github/workflows/codesee-arch-diagram.yml

This file was deleted.

44 changes: 43 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fastify": "^5.2.0",
"gists": "2.0.0",
"lru-cache": "10.2.2",
"marked": "^15.0.6",
"node-cron": "3.0.3",
"node-fetch": "2.6.12",
"open-graph-scraper": "6.5.2",
Expand All @@ -40,7 +41,8 @@
"pino": "^9.6.0",
"pino-pretty": "^13.0.0",
"query-string": "7.1.3",
"uuid": "9.0.1"
"uuid": "9.0.1",
"xss": "^1.0.15"
},
"devDependencies": {
"@types/node": "20.14.2",
Expand Down
2 changes: 1 addition & 1 deletion src/features/jobs-moderation/job-mod-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const failedTooManyEmojis = (
): e is PostFailureTooManyEmojis =>
e.type === POST_FAILURE_REASONS.tooManyEmojis;

interface StoredMessage {
export interface StoredMessage {
message: Message;
authorId: Snowflake;
createdAt: Date;
Expand Down
21 changes: 18 additions & 3 deletions src/features/jobs-moderation/parse-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("parseContent", () => {
);
expectTypeOf(parsed).toBeArray();
expect(parsed[0]).toMatchObject({
tags: ["company", "jobtitle", "location", "compensation", "jobtype"],
tags: ["company", "job title", "location", "compensation", "job type"],
});

const emptyTags = ["|", "|||", "[]", " [ ] "];
Expand All @@ -27,7 +27,6 @@ describe("parseContent", () => {
"[forhire]",
"for hire|",
"|for hire|",
"[f o r h i r e]",
"|FoRhIrE|",
];
validForHireTags.forEach((tag) => {
Expand All @@ -42,14 +41,30 @@ describe("parseContent", () => {
"[hire]",
"[HIRE]",
"hiring|",
"|h i r i n g|",
"|HiRiNg|",
];
validHiringTags.forEach((tag) => {
const [parsed] = parseContent(tag);
expect(parsed).toMatchObject({ tags: [PostType.hiring] });
});
});
it("fancy", () => {
const parsed = parseContent(
"Company | Job Title | Location | Compensation | Job Type | Part time / full time",
);
expectTypeOf(parsed).toBeArray();
expect(parsed[0]).toMatchObject({
tags: [
"company",
"job title",
"location",
"compensation",
"job type",
"part time",
"full time",
],
});
});
});
it("parses description", () => {
let parsed = parseContent(`[hiring]
Expand Down
11 changes: 8 additions & 3 deletions src/features/jobs-moderation/parse-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ type StandardTag = string;
// interpreting compensation, all sorts of fun follow ons.
const tagMap = new Map<string, (s: SimplifiedTag) => StandardTag>([
["forhire", () => PostType.forHire],
["for hire", () => PostType.forHire],
["hiring", () => PostType.hiring],
["hire", () => PostType.hiring],
]);

const standardizeTag = (tag: string) => {
const simpleTag = simplifyString(tag).replace(/\W/g, "");
const standardizeTag = (tag: string): string | string[] => {
if (tag.includes("/")) {
return tag.split("/").flatMap(standardizeTag);
}

const simpleTag = simplifyString(tag).replace(/\W+/g, " ").trim();
const standardTagBuilder = tagMap.get(simpleTag);
return standardTagBuilder?.(simpleTag) ?? simpleTag;
};

export const parseTags = (tags: string) => {
return tags
.split(/[|[\]]/g)
.map((tag) => standardizeTag(tag.trim()))
.flatMap((tag) => standardizeTag(tag.trim()))
.filter((tag) => tag !== "");
};

Expand Down
8 changes: 8 additions & 0 deletions src/helpers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export const extractEmoji = (s: string) => s.match(EMOJI_RANGE) || [];

const NEWLINE = /\n/g;
export const countLines = (s: string) => s.match(NEWLINE)?.length || 0;

const DOUBLE_NEWLINE = /\n\n/g;
export const compressLineBreaks = (s: string) => {
while (DOUBLE_NEWLINE.test(s)) {
s = s.replaceAll(DOUBLE_NEWLINE, "\n");
}
return s;
};
61 changes: 53 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import Fastify from "fastify";
import cors from "@fastify/cors";
import helmet from "@fastify/helmet";
import swagger from "@fastify/swagger";
import { getJobPosts } from "./features/jobs-moderation/job-mod-helpers.js";
import { marked } from "marked";
import xss from "xss";
import {
StoredMessage,
getJobPosts,
} from "./features/jobs-moderation/job-mod-helpers.js";
import { compressLineBreaks } from "./helpers/string.js";

const fastify = Fastify({ logger: true });

Expand All @@ -23,13 +29,18 @@ const openApiConfig = {
items: { type: "string" },
},
description: { type: "string" },
authorId: {
type: "string",
format: "snowflake",
},
message: {
author: {
type: "object",
description: "Discord Message object",
requried: ["username", "displayName", "avatar"],
properties: {
username: { type: "string" },
displayName: { type: "string" },
avatar: { type: "string" },
},
},
reactions: {
type: "array",
items: { type: "string" },
},
createdAt: {
type: "string",
Expand Down Expand Up @@ -99,8 +110,42 @@ fastify.get(
},
},
async () => {
return getJobPosts();
const { hiring, forHire } = getJobPosts();

return { hiring: hiring.map(renderPost), forHire: forHire.map(renderPost) };
},
);

interface RenderedPost extends Omit<StoredMessage, "message" | "authorId"> {
reactions: string[];
author: {
username: string;
displayName: string;
avatar: string;
};
}

const renderPost = (post: StoredMessage): RenderedPost => {
console.log({
reactions: post.message.reactions.cache.map((r) => r.emoji.name),
});
return {
...post,
description: renderMdToHtml(compressLineBreaks(post.description)),
author: {
username: post.message.author.username,
displayName: post.message.author.displayName,
avatar: post.message.author.displayAvatarURL({
size: 128,
extension: "jpg",
forceStatic: true,
}),
},
reactions: post.message.reactions.cache.map((r) => r.emoji.name ?? "☐"),
};
};

await fastify.listen({ port: 3000, host: "0.0.0.0" });

const renderMdToHtml = (md: string) =>
xss(marked(md, { async: false, gfm: true }));
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"module": "NodeNext",
"sourceMap": true,
"outDir": "dist",
"skipLibCheck": true,
"strict": true,
"allowJs": false,
"moduleResolution": "node16",
"moduleResolution": "nodenext",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
},
Expand Down
Loading