Skip to content

Commit

Permalink
Hold more than 50 posts in cache, filter out staff/bots better, inclu…
Browse files Browse the repository at this point in the history
…de reaction count (#438)

* Include emoji count in reaction data

* Don't overwrite job data when loading

* Only remove forhire posts from cache after message has been removed

* Filter out bots and staff earlier
  • Loading branch information
vcarl authored Jan 17, 2025
1 parent babcc41 commit 18b54f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
34 changes: 19 additions & 15 deletions src/features/jobs-moderation/job-mod-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface StoredMessage {
tags: string[];
type: PostType;
}
let jobBoardMessageCache: {
const jobBoardMessageCache: {
forHire: StoredMessage[];
hiring: StoredMessage[];
} = { forHire: [], hiring: [] };
Expand All @@ -97,8 +97,16 @@ export const loadJobs = async (bot: Client, channel: TextChannel) => {
!oldestMessage ||
differenceInDays(now, oldestMessage.createdAt) < DAYS_OF_POSTS
) {
const messages = await channel.messages.fetch(
oldestMessage ? { before: oldestMessage.message.id } : {},
const messages = (
await channel.messages.fetch(
oldestMessage ? { before: oldestMessage.message.id } : {},
)
).filter(
(m) =>
differenceInDays(now, m.createdAt) < DAYS_OF_POSTS &&
!m.system &&
!isStaff(m.member) &&
m.author.id !== bot.user?.id,
);
console.log(
"[DEBUG] loadJobs()",
Expand Down Expand Up @@ -134,23 +142,17 @@ export const loadJobs = async (bot: Client, channel: TextChannel) => {
break;
}
oldestMessage = newMessages
.sort((a, b) => compareAsc(a.createdAt, b.createdAt))
.at(0);
.sort((a, b) => compareAsc(b.createdAt, a.createdAt))
.at(-0);
if (!oldestMessage) break;

const humanNonStaffMessages = newMessages.filter(
(m) =>
differenceInDays(now, m.createdAt) < DAYS_OF_POSTS &&
!m.message.system &&
!isStaff(m.message.member) &&
m.authorId !== bot.user?.id,
);
const [hiring, forHire] = partition(
(m) => m.type === PostType.hiring,
humanNonStaffMessages,
newMessages,
);

jobBoardMessageCache = { hiring, forHire };
jobBoardMessageCache.hiring.push(...hiring);
jobBoardMessageCache.forHire.push(...forHire);
}
};

Expand Down Expand Up @@ -178,7 +180,6 @@ export const deleteAgedPosts = async () => {
FORHIRE_AGE_LIMIT
) {
const { message } = jobBoardMessageCache.forHire[0];
jobBoardMessageCache.forHire.shift();
try {
await message.fetch();
if (!message.deletable) {
Expand All @@ -199,7 +200,10 @@ export const deleteAgedPosts = async () => {
message,
extra: `Originally sent ${format(new Date(message.createdAt), "P p")}`,
});

await message.delete();
jobBoardMessageCache.forHire.shift();

console.log(
`[INFO]: deleteAgedPosts() deleted post ${constructDiscordLink(
message,
Expand Down
15 changes: 10 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const openApiConfig = {
},
},
reactions: {
type: "array",
items: { type: "string" },
type: "object",
additionalProperties: { type: "number" },
},
createdAt: {
type: "string",
Expand Down Expand Up @@ -117,7 +117,7 @@ fastify.get(
);

interface RenderedPost extends Omit<StoredMessage, "message" | "authorId"> {
reactions: string[];
reactions: Record<string, number>;
author: {
username: string;
displayName: string;
Expand All @@ -127,7 +127,10 @@ interface RenderedPost extends Omit<StoredMessage, "message" | "authorId"> {

const renderPost = (post: StoredMessage): RenderedPost => {
console.log({
reactions: post.message.reactions.cache.map((r) => r.emoji.name),
reactions: post.message.reactions.cache.map((r) => [
r.emoji.name ?? "☐",
r.count,
]),
});
return {
...post,
Expand All @@ -141,7 +144,9 @@ const renderPost = (post: StoredMessage): RenderedPost => {
forceStatic: true,
}),
},
reactions: post.message.reactions.cache.map((r) => r.emoji.name ?? "☐"),
reactions: Object.fromEntries(
post.message.reactions.cache.map((r) => [r.emoji.name ?? "☐", r.count]),
),
};
};

Expand Down

0 comments on commit 18b54f4

Please sign in to comment.