Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | import { Router } from 'express'; |
| 19 | import asyncHandler from 'express-async-handler'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 20 | import { HttpStatusCode, IContact } from 'jami-web-common'; |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 21 | import { Container } from 'typedi'; |
| 22 | |
| 23 | import { Jamid } from '../jamid/jamid.js'; |
| 24 | import { authenticateToken } from '../middleware/auth.js'; |
| 25 | |
| 26 | const jamid = Container.get(Jamid); |
| 27 | |
| 28 | export const defaultModeratorsRouter = Router(); |
| 29 | |
| 30 | defaultModeratorsRouter.use(authenticateToken); |
| 31 | |
| 32 | defaultModeratorsRouter.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); |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 39 | const namedDefaultModerators: IContact[] = []; |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 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 | |
| 52 | defaultModeratorsRouter.put('/:contactId', (req, res) => { |
| 53 | jamid.addDefaultModerator(res.locals.accountId, req.params.contactId); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 54 | res.sendStatus(HttpStatusCode.NoContent); |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 55 | }); |
| 56 | |
| 57 | defaultModeratorsRouter.delete('/:contactId', (req, res) => { |
| 58 | jamid.removeDefaultModerator(res.locals.accountId, req.params.contactId); |
| 59 | res.sendStatus(HttpStatusCode.NoContent); |
| 60 | }); |