CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/news/edit.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import type { Request, Response } from "express";
7
8
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
9
import editNews from "@cocalc/server/news/edit";
10
import { clearCache } from "@cocalc/database/postgres/news";
11
import getAccountId from "lib/account/get-account";
12
import getParams from "lib/api/get-params";
13
14
export default async function handle(req: Request, res: Response) {
15
try {
16
const result = await doIt(req);
17
// edit has been successful: clear cache
18
// (there is also a clearCache in server/news/edit but it isn't effective. Module loaded more than once? Can't hurt, though.)
19
clearCache();
20
res.json({ ...result, success: true });
21
} catch (err) {
22
res.json({ error: `${err.message}` });
23
return;
24
}
25
}
26
27
async function doIt(req: Request) {
28
// date is unix timestamp in seconds
29
const { id, title, text, date, channel, url, tags, hide } = getParams(req);
30
31
const account_id = await getAccountId(req);
32
33
if (account_id == null) {
34
throw Error("must be signed in to create/edit news");
35
}
36
37
if (!(await userIsInGroup(account_id, "admin"))) {
38
throw Error("only admins can create/edit news items");
39
}
40
41
if (title == null) {
42
throw new Error("must provide title");
43
}
44
45
if (text == null) {
46
throw new Error("must provide text");
47
}
48
49
return await editNews({
50
id,
51
title,
52
text,
53
url,
54
tags,
55
date: date ? new Date(1000 * date) : new Date(),
56
channel,
57
hide: !!hide,
58
});
59
}
60
61