diff options
author | Raphael Kabo <raphaelkabo@hey.com> | 2023-10-09 19:22:39 +0100 |
---|---|---|
committer | Raphael Kabo <raphaelkabo@hey.com> | 2023-10-09 19:22:39 +0100 |
commit | 7fe1f42d56edb98875399b1da5e9b7e972209a0d (patch) | |
tree | 6f96696b583e4fab1a2d1f54a2f99bcee3267c04 /src/routes | |
parent | dbbb94117c2d6266cfc45a091b4b87012024f788 (diff) |
Add known group linker to event form
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/group.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/routes/group.ts b/src/routes/group.ts index 2801248..40dcccb 100644 --- a/src/routes/group.ts +++ b/src/routes/group.ts @@ -7,6 +7,8 @@ import Jimp from "jimp"; import { addToLog } from "../helpers.js"; import EventGroup from "../models/EventGroup.js"; import { sendEmailFromTemplate } from "../lib/email.js"; +import { marked } from "marked"; +import { renderPlain } from "../util/markdown.js"; const config = getConfig(); @@ -237,4 +239,62 @@ router.put( }, ); +// Accepts a JSON object of event/group IDs mapped to edit tokens. +// Returns an object of basic group data for each of the IDs +// which are valid groups and have an edit token which matches. +router.post("/known/groups", async (req: Request, res: Response) => { + const known = req.body; + if (!known) { + return res.status(400).json({ + errors: [ + { + message: "No known IDs were provided.", + }, + ], + }); + } + + try { + const knownIDs = Object.keys(known); + const groups = await EventGroup.find({ + id: { $in: knownIDs }, + }); + const knownGroups = groups.filter((group) => { + return group.editToken === known[group.id]; + }); + const groupData = knownGroups.map((group) => { + return { + id: group.id, + name: group.name, + description: marked + .parse(group.description, { + renderer: renderPlain(), + }) + .split(" ") + .splice(0, 40) + .join(" ") + .trim(), + image: group.image, + editToken: group.editToken, + url: `/group/${group.id}`, + }; + }); + return res.status(200).json(groupData); + } catch (err) { + console.error(err); + addToLog( + "getKnownGroups", + "error", + "Attempt to get known groups failed with error: " + err, + ); + return res.status(500).json({ + errors: [ + { + message: err, + }, + ], + }); + } +}); + export default router; |