blob: 505d1d9436fe2945049255c6cca255273c6dd113 [file] [log] [blame]
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -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 */
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050018import { Router } from 'express';
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040019import asyncHandler from 'express-async-handler';
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050020import { AccountDetails, HttpStatusCode } from 'jami-web-common';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040021import { Container } from 'typedi';
22
Misha Krieger-Raynauldaddd6fe2022-10-22 12:46:04 -040023import { Jamid } from '../jamid/jamid.js';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040024import { authenticateToken } from '../middleware/auth.js';
25
26const jamid = Container.get(Jamid);
27
28export const accountRouter = Router();
29
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040030accountRouter.use(authenticateToken);
31
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040032// TODO: Do we really need this route to return the default moderators?
33// It would be cleaner just to GET /default-moderators for this
34accountRouter.get(
35 '/',
36 asyncHandler(async (_req, res) => {
37 const accountId = res.locals.accountId;
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040038
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040039 // Add usernames for default moderators
40 const defaultModeratorUris = jamid.getDefaultModeratorUris(accountId);
41 const namedDefaultModerators = [];
42 for (const defaultModeratorUri of defaultModeratorUris) {
43 const { username } = await jamid.lookupAddress(defaultModeratorUri, accountId);
44 namedDefaultModerators.push({
45 uri: defaultModeratorUri,
46 registeredName: username,
47 });
48 }
49
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050050 res.send({
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040051 id: accountId,
52 details: jamid.getAccountDetails(accountId),
53 volatileDetails: jamid.getVolatileAccountDetails(accountId),
54 defaultModerators: namedDefaultModerators,
55 devices: jamid.getDevices(accountId),
56 });
57 })
58);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040059
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040060accountRouter.patch('/', (req, res) => {
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040061 const accountId = res.locals.accountId;
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050062
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040063 const currentAccountDetails = jamid.getAccountDetails(accountId);
64 const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body };
simon5da8ca62022-11-09 15:21:25 -050065 jamid.setAccountDetails(accountId, newAccountDetails);
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050066
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040067 res.sendStatus(HttpStatusCode.NoContent);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040068});