-
Notifications
You must be signed in to change notification settings - Fork 1
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
[recnet-api] Update slack message template #359
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c441cd6
test: add back slack test api
swh00tw 4d771a6
feat: install slack block builder
swh00tw 7807854
feat: add new slack msg template
swh00tw 3860d5e
feat: finish update slack msg template
swh00tw 490300c
revert: remove testing api endpoint
swh00tw c60f341
refactor: extract weekly digest notification text
swh00tw f7b3c43
refactor: extract string const to weekly digest template
swh00tw 7fa7314
chore: remove comment
swh00tw 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 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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { SlackBlockDto } from "slack-block-builder"; | ||
|
||
export type SendSlackResult = { | ||
success: boolean; | ||
skip?: boolean; | ||
userId?: string; | ||
}; | ||
|
||
export type SlackMessageBlocks = Readonly<SlackBlockDto>[]; |
70 changes: 61 additions & 9 deletions
70
apps/recnet-api/src/modules/slack/templates/weekly-digest.template.ts
This file contains 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 |
---|---|---|
@@ -1,19 +1,71 @@ | ||
import groupBy from "lodash.groupby"; | ||
import { BlockCollection, Md, Blocks, BlockBuilder } from "slack-block-builder"; | ||
|
||
import { WeeklyDigestContent } from "@recnet-api/modules/subscription/subscription.type"; | ||
|
||
import { formatDate } from "@recnet/recnet-date-fns"; | ||
|
||
import type { SlackMessageBlocks } from "../slack.type"; | ||
|
||
export const weeklyDigestSlackTemplate = ( | ||
cutoff: Date, | ||
content: WeeklyDigestContent, | ||
nodeEnv: string | ||
): string => { | ||
const subject = `${nodeEnv !== "production" && "[DEV] "}📬 Your Weekly Digest for ${formatDate(cutoff)}`; | ||
const unusedInviteCodes = `You have ${content.numUnusedInviteCodes} unused invite codes! Share the love ❤️`; | ||
const latestAnnouncement = content.latestAnnouncement | ||
? `📢 ${content.latestAnnouncement.title} \n ${content.latestAnnouncement.content}` | ||
: ""; | ||
const recsUrls = content.recs.map( | ||
(rec) => `[${rec.article.title}](https://recnet.io/rec/${rec.id})` | ||
): SlackMessageBlocks => { | ||
const { recs, numUnusedInviteCodes, latestAnnouncement } = content; | ||
|
||
const recsGroupByTitle = groupBy(recs, (rec) => { | ||
const titleLowercase = rec.article.title.toLowerCase(); | ||
const words = titleLowercase.split(" ").filter((w) => w.length > 0); | ||
return words.join(""); | ||
}); | ||
const recSection = Object.values(recsGroupByTitle).map((recs) => { | ||
const article = recs[0].article; | ||
return [ | ||
Blocks.Section({ | ||
text: `${Md.bold(Md.link(article.link, article.title))}\n${Md.italic(article.author)} - ${article.year}`, | ||
}), | ||
...recs.map((rec) => | ||
Blocks.Section({ | ||
text: `${Md.link(`https://recnet.io/${rec.user.handle}`, rec.user.displayName)}${rec.isSelfRec ? Md.italic("(Self-Rec)") : ""}: ${rec.description} (${Md.link(`https://recnet.io/rec/${rec.id}`, "view")})`, | ||
}) | ||
), | ||
Blocks.Divider(), | ||
]; | ||
}); | ||
|
||
const footer: BlockBuilder[] = []; | ||
if (numUnusedInviteCodes > 0) { | ||
footer.push( | ||
Blocks.Section({ | ||
text: `❤️ You have ${Md.bold(`${numUnusedInviteCodes}`)} unused invite codes. Share the love!`, | ||
}) | ||
); | ||
} | ||
if (latestAnnouncement) { | ||
footer.push( | ||
Blocks.Section({ | ||
text: `📢 Announcement - ${latestAnnouncement.title}: ${latestAnnouncement.content}`, | ||
}) | ||
); | ||
} | ||
|
||
return BlockCollection( | ||
// headers: number of rec | ||
Blocks.Header({ | ||
text: `${nodeEnv !== "production" && "[DEV] "}📬 Your Weekly Digest for ${formatDate(cutoff)}`, | ||
}), | ||
Blocks.Section({ | ||
text: `You have ${Md.bold(`${recs.length}`)} recommendations this week!`, | ||
}), | ||
Blocks.Section({ | ||
text: "Check out these rec'd paper for you from your network!", | ||
}), | ||
Blocks.Divider(), | ||
...recSection.flat(), | ||
...footer, | ||
Blocks.Section({ | ||
text: `👀 Any interesting read this week? ${Md.link("https://recnet.io", "Share with your network!")}`, | ||
}) | ||
); | ||
return `${subject}\nYou have ${content.recs.length} recommendations this week!\nCheck out these rec'd paper for you from your network!\n${unusedInviteCodes}\n${latestAnnouncement}\n${recsUrls.join("\n")} \n\nAny interesting read this week? 👀\nShare with your network: https://recnet.io/`; | ||
}; |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
This SlackTransporter class is designed to send messages for general purposes. I suggest putting this text into the template to create a better logic boundary.
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.
You are right. I forget to extract this part.