blob: 797aac312509b30b7e37b02b609bbcfe48198557 [file] [log] [blame]
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { Router } from 'express';
19import asyncHandler from 'express-async-handler';
20import { HttpStatusCode } from 'jami-web-common';
21import { Container } from 'typedi';
22
23import { Jamid } from '../jamid/jamid.js';
24import { authenticateToken } from '../middleware/auth.js';
25
26const jamid = Container.get(Jamid);
27
28export const defaultModeratorsRouter = Router();
29
30defaultModeratorsRouter.use(authenticateToken);
31
32defaultModeratorsRouter.get(
33 '/',
34 asyncHandler(async (_req, res) => {
35 const accountId = res.locals.accountId;
36
37 // Add usernames for default moderators
38 const defaultModeratorUris = jamid.getDefaultModeratorUris(accountId);
39 const namedDefaultModerators = [];
40 for (const defaultModeratorUri of defaultModeratorUris) {
41 const { username } = await jamid.lookupAddress(defaultModeratorUri, accountId);
42 namedDefaultModerators.push({
43 uri: defaultModeratorUri,
44 registeredName: username,
45 });
46 }
47
48 res.send(namedDefaultModerators);
49 })
50);
51
52defaultModeratorsRouter.put('/:contactId', (req, res) => {
53 jamid.addDefaultModerator(res.locals.accountId, req.params.contactId);
54 res.end();
55});
56
57defaultModeratorsRouter.delete('/:contactId', (req, res) => {
58 jamid.removeDefaultModerator(res.locals.accountId, req.params.contactId);
59 res.sendStatus(HttpStatusCode.NoContent);
60});