blob: 59bad059e4fad82a7607784f6cb18a1444aa57a7 [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';
19import { ParamsDictionary } from 'express-serve-static-core';
20import { 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
Charlieb62c6782022-10-30 15:14:56 -040026interface SendAccountTextMessageApi {
27 from?: string;
28 to?: string;
29 type?: string;
30 data?: string;
31}
32
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040033const jamid = Container.get(Jamid);
34
35export const accountRouter = Router();
36
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040037accountRouter.use(authenticateToken);
38
39// TODO: If tokens can be generated on one daemon and used on another (transferrable between daemons),
40// then add middleware to check that the currently logged-in accountId is stored in this daemon instance
41
42accountRouter.get('/', (_req, res) => {
43 const accountId = res.locals.accountId;
44
45 res.json({
46 id: accountId,
47 details: jamid.getAccountDetails(accountId),
48 volatileDetails: jamid.getVolatileAccountDetails(accountId),
49 defaultModerators: jamid.getDefaultModerators(accountId),
50 devices: jamid.getDevices(accountId),
51 });
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040052});
53
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040054accountRouter.patch('/', (req, res) => {
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040055 const accountId = res.locals.accountId;
56 const currentAccountDetails = jamid.getAccountDetails(accountId);
57 const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body };
58 jamid.setAccountDetails(res.locals.accountId, newAccountDetails);
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040059 res.sendStatus(HttpStatusCode.NoContent);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040060});
Charlieb62c6782022-10-30 15:14:56 -040061
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040062accountRouter.post(
63 '/send-account-message',
64 (req: Request<ParamsDictionary, string, SendAccountTextMessageApi>, res) => {
65 const { from, to, type, data } = req.body;
66 if (!from || !to || !type || !data) {
67 res.status(HttpStatusCode.BadRequest).send('Missing arguments in request');
68 return;
69 }
70
71 jamid.sendAccountTextMessage(from, to, type, data);
72 res.end();
Charlieb62c6782022-10-30 15:14:56 -040073 }
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040074);