Repository navigation
feat(#126): display showcase posts in fortnightly showcase-digest #134
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
Open
michal-skraburski
wants to merge
29
commits into
r-webdev:main
Choose a base branch
from
michal-skraburski:showcase-digest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e51e151
feat: add showcase-digest
michal-skraburski 1407a2b
fix: split the showcase-digest in multiple files
michal-skraburski 1ac90c0
fix: split out the functions for showcase digest
michal-skraburski 9ea1842
fix: hoist Date.now() out of filter per post
michal-skraburski d0697b4
fix: rewrite conversion to be safe, apply KISS&DRY
michal-skraburski 55f3d06
fix: move functions out of handlers into util + unit tests
michal-skraburski 4dd7a2d
feat: add showcase-digest cronjob trigger
michal-skraburski abd0f69
fix: add missing functions for showcase-digest
michal-skraburski 5549661
feat: add pingable role
michal-skraburski 33c2c9f
fix: update the default emoji to thumbsup
michal-skraburski d988d95
fix: prevent non-moderators from running manual digest
michal-skraburski ec44c5f
feat: allow the digest scheduler to only execute on within correct ti…
michal-skraburski c99c7cc
fix: append example vars into .env.example + typo fix
michal-skraburski 9200e44
fix: correct import on commands/index.ts
michal-skraburski 1c3e8c8
fix: add missing .env.test vars
michal-skraburski a181b32
fix: rm comments & lint
michal-skraburski fc63a36
feat: drop variables showcase-digest config from .env
michal-skraburski c4c0b89
fix: refactor code to prefer throwing errors and handling them
michal-skraburski 2f91566
fix: update all to use codeblocks, allSettled use, updated user msgs
michal-skraburski 714ed49
fix: spelling, error handling fixes, digest cron simplified
michal-skraburski 6320ad2
fix: update sortByDescPost to byLikesDescending
michal-skraburski 5a1a243
fix: formatting on byLikesDescending
michal-skraburski 5b64411
fix: stale test from rejection handling
michal-skraburski 2fec8f3
fix: readd diff line from showcase accident
michal-skraburski 5190b38
fix: remove promise.then, simplify error class
michal-skraburski 8ccd500
fix: add digest .env vars to production
michal-skraburski d4c41b7
fix: remove unused import
michal-skraburski 951d020
fix: rename showcase error vars to useful names
michal-skraburski e74dd04
fix: update showcase error to be more verbose
michal-skraburski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const LOOKBACK_N: number = 14; | ||
| export const REACT_EMOJI = '%F0%9F%91%8D'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { SERVER_CHANNELS } from '@/constants/channels.js'; | ||
| import { config } from '@/env.js'; | ||
| import { getCommandUser } from '@/util/member.js'; | ||
| import { isModerator } from '@/util/permissions.js'; | ||
| import { LOOKBACK_N, REACT_EMOJI } from './config.js'; | ||
| import { | ||
| ButtonInteraction, | ||
| ChatInputCommandInteraction, | ||
| Collection, | ||
| MessageFlags, | ||
| ContainerBuilder, | ||
| TextDisplayBuilder, | ||
| ForumThreadChannel, | ||
| channelMention, | ||
| roleMention, | ||
| } from 'discord.js'; | ||
| import { | ||
| convertPosts, | ||
| filterPostsByCreationTime, | ||
| type Post, | ||
| ShowcaseDigestValidationError, | ||
| } from './util.js'; | ||
|
|
||
| const postProcessor = async (posts: Collection<string, ForumThreadChannel>) => { | ||
| const postsUnordered: Post[] = await convertPosts(posts, REACT_EMOJI); | ||
| const byLikesDescending = (postA: Post, postB: Post) => { | ||
| return postB.likes - postA.likes; | ||
| }; | ||
| return postsUnordered.sort(byLikesDescending); | ||
| }; | ||
|
|
||
| function createContainer(posts: Post[]): ContainerBuilder { | ||
| if (posts.length == 0) { | ||
| throw new ShowcaseDigestValidationError( | ||
| 'Something went wrong. Please try again later.', | ||
| 'createContainer passed posts[] with length 0. Impossible scenario.' | ||
| ); | ||
| } | ||
|
|
||
| const container = new ContainerBuilder(); | ||
| container.addTextDisplayComponents( | ||
| new TextDisplayBuilder().setContent('## Showcase Digest') | ||
| ); | ||
|
|
||
| posts.forEach((post, index) => { | ||
| container.addTextDisplayComponents( | ||
| new TextDisplayBuilder().setContent( | ||
| `${index + 1}. ${channelMention(post.id)} (Comments: ${post.messageCount})` | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| container.addTextDisplayComponents( | ||
| new TextDisplayBuilder().setContent( | ||
| `-# ${roleMention(config.roleIds.digestRole)}` | ||
| ) | ||
| ); | ||
|
|
||
| return container; | ||
| } | ||
|
|
||
| export const createShowcaseDigest = async (): Promise<void> => { | ||
| const digestChannel = SERVER_CHANNELS.showcaseDigest; | ||
| const forumChannel = SERVER_CHANNELS.showcase; | ||
| const posts = filterPostsByCreationTime( | ||
| forumChannel.threads.cache, | ||
| LOOKBACK_N | ||
| ); | ||
|
|
||
| if (posts.size === 0) { | ||
| throw new ShowcaseDigestValidationError( | ||
| 'There was no posts to be displayed in Showcase Digest.', | ||
| 'There was no posts to display in this digest.' | ||
| ); | ||
| } | ||
|
|
||
| const postsOrdered: Post[] = await postProcessor(posts); | ||
| const container = createContainer(postsOrdered); | ||
|
|
||
| await digestChannel.send({ | ||
| flags: MessageFlags.IsComponentsV2, | ||
| components: [container], | ||
| allowedMentions: { roles: [config.roleIds.digestRole] }, | ||
| }); | ||
| }; | ||
|
|
||
| export const digestHandler = async ( | ||
| interaction: ButtonInteraction | ChatInputCommandInteraction | ||
| ) => { | ||
| if (!interaction.inGuild() || !interaction.guild) { | ||
| await interaction.reply({ | ||
| content: 'This command can only be run in the web development discord.', | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (!isModerator(getCommandUser(interaction))) { | ||
| await interaction.reply({ | ||
| content: 'Only moderators can run the showcase digest.', | ||
| flags: MessageFlags.Ephemeral, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await createShowcaseDigest(); | ||
| } catch (error: unknown) { | ||
| if (error instanceof ShowcaseDigestValidationError) { | ||
| console.error(error.message); | ||
| await interaction.reply({ | ||
| flags: MessageFlags.Ephemeral, | ||
| content: error.getExternalMessage(), | ||
| }); | ||
| } else { | ||
| console.error('An unexpected error caught:', error); | ||
| await interaction.reply({ | ||
| flags: MessageFlags.Ephemeral, | ||
| content: 'Something went wrong, please try again.', | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| await interaction.reply({ | ||
| flags: MessageFlags.Ephemeral, | ||
| content: 'Successfully created a showcase digest!', | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as cron from 'node-cron'; | ||
| import { createSlashCommand } from '@/common/commands/create-commands.js'; | ||
| import { LOOKBACK_N } from './config.js'; | ||
| import { digestHandler, createShowcaseDigest } from './handlers.js'; | ||
|
|
||
| export const createDigestCommand = createSlashCommand({ | ||
| data: { | ||
| name: 'digest-force', | ||
| description: `Forcefully consumes posts of the last ${LOOKBACK_N} days`, | ||
| }, | ||
| execute: digestHandler, | ||
| }); | ||
|
|
||
| // Fire on the 1st and 3rd Friday of every month at 16:00 UTC using node-cron's | ||
| // nth-weekday syntax (5#1 = first Friday, 5#3 = third Friday). | ||
| // https://www.nodecron.com/cron-syntax#nth-weekday | ||
| const SHOWCASE_DIGEST_CRONS = ['0 16 * * 5#1', '0 16 * * 5#3']; | ||
|
|
||
| const runScheduledShowcaseDigest = async (): Promise<void> => { | ||
| console.log('Running scheduled showcase digest...'); | ||
| try { | ||
| await createShowcaseDigest(); | ||
| console.log(`Successfully displayed showcase digest!`); | ||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
| }; | ||
|
|
||
| export function initializeShowcaseDigestScheduler(): void { | ||
| console.log('Initializing showcase digest scheduler...'); | ||
|
|
||
| SHOWCASE_DIGEST_CRONS.forEach((expression) => { | ||
| cron.schedule(expression, runScheduledShowcaseDigest, { timezone: 'UTC' }); | ||
| }); | ||
|
|
||
| console.log( | ||
| 'Showcase digest scheduler initialized (runs 1st and 3rd Fridays at 16:00 UTC)' | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful