Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/news/edit.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import type { Request, Response } from "express";67import userIsInGroup from "@cocalc/server/accounts/is-in-group";8import editNews from "@cocalc/server/news/edit";9import { clearCache } from "@cocalc/database/postgres/news";10import getAccountId from "lib/account/get-account";11import getParams from "lib/api/get-params";1213export default async function handle(req: Request, res: Response) {14try {15const result = await doIt(req);16// edit has been successful: clear cache17// (there is also a clearCache in server/news/edit but it isn't effective. Module loaded more than once? Can't hurt, though.)18clearCache();19res.json({ ...result, success: true });20} catch (err) {21res.json({ error: `${err.message}` });22return;23}24}2526async function doIt(req: Request) {27// date is unix timestamp in seconds28const { id, title, text, date, channel, url, tags, hide } = getParams(req);2930const account_id = await getAccountId(req);3132if (account_id == null) {33throw Error("must be signed in to create/edit news");34}3536if (!(await userIsInGroup(account_id, "admin"))) {37throw Error("only admins can create/edit news items");38}3940if (title == null) {41throw new Error("must provide title");42}4344if (text == null) {45throw new Error("must provide text");46}4748return await editNews({49id,50title,51text,52url,53tags,54date: date ? new Date(1000 * date) : new Date(),55channel,56hide: !!hide,57});58}596061