blob: 9b72667ba2673697d4dca368d6177eb3fb0a5a67 [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 */
Charlieb62c6782022-10-30 15:14:56 -040018import { Request, Router } from 'express';
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040019import asyncHandler from 'express-async-handler';
Charlieb62c6782022-10-30 15:14:56 -040020import { ParamsDictionary } from 'express-serve-static-core';
Charlie2bc0d672022-11-04 11:53:44 -040021import { AccountDetails, AccountTextMessage, HttpStatusCode } from 'jami-web-common';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040022import { Container } from 'typedi';
23
Misha Krieger-Raynauldaddd6fe2022-10-22 12:46:04 -040024import { Jamid } from '../jamid/jamid.js';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040025import { authenticateToken } from '../middleware/auth.js';
26
27const jamid = Container.get(Jamid);
28
29export const accountRouter = Router();
30
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040031accountRouter.use(authenticateToken);
32
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040033// TODO: Do we really need this route to return the default moderators?
34// It would be cleaner just to GET /default-moderators for this
35accountRouter.get(
36 '/',
37 asyncHandler(async (_req, res) => {
38 const accountId = res.locals.accountId;
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040039
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040040 // Add usernames for default moderators
41 const defaultModeratorUris = jamid.getDefaultModeratorUris(accountId);
42 const namedDefaultModerators = [];
43 for (const defaultModeratorUri of defaultModeratorUris) {
44 const { username } = await jamid.lookupAddress(defaultModeratorUri, accountId);
45 namedDefaultModerators.push({
46 uri: defaultModeratorUri,
47 registeredName: username,
48 });
49 }
50
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050051 res.send({
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040052 id: accountId,
53 details: jamid.getAccountDetails(accountId),
54 volatileDetails: jamid.getVolatileAccountDetails(accountId),
55 defaultModerators: namedDefaultModerators,
56 devices: jamid.getDevices(accountId),
57 });
58 })
59);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040060
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040061accountRouter.patch('/', (req, res) => {
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040062 const accountId = res.locals.accountId;
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050063
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040064 const currentAccountDetails = jamid.getAccountDetails(accountId);
65 const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body };
simon5da8ca62022-11-09 15:21:25 -050066 jamid.setAccountDetails(accountId, newAccountDetails);
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050067
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040068 res.sendStatus(HttpStatusCode.NoContent);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040069});
Charlieb62c6782022-10-30 15:14:56 -040070
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050071// TODO: Should this endpoint be removed?
Issam E. Maghni0432cb72022-11-12 06:09:26 +000072accountRouter.post('/send-account-message', (req: Request<ParamsDictionary, any, AccountTextMessage<unknown>>, res) => {
Charlie2bc0d672022-11-04 11:53:44 -040073 const { from, to, message } = req.body;
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050074 if (from === undefined || to === undefined || message === undefined) {
75 res.status(HttpStatusCode.BadRequest).send('Missing from, to, or message in body');
Charlie2bc0d672022-11-04 11:53:44 -040076 return;
Charlieb62c6782022-10-30 15:14:56 -040077 }
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050078
Charlie2bc0d672022-11-04 11:53:44 -040079 jamid.sendAccountTextMessage(from, to, JSON.stringify(message));
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050080 res.sendStatus(HttpStatusCode.NoContent);
Charlie2bc0d672022-11-04 11:53:44 -040081});