Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -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 | */ |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 18 | import { Request, Router } from 'express'; |
| 19 | import { ParamsDictionary } from 'express-serve-static-core'; |
| 20 | import { AccountDetails, HttpStatusCode } from 'jami-web-common'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 21 | import { Container } from 'typedi'; |
| 22 | |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 23 | import { Jamid } from '../jamid/jamid.js'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 24 | import { authenticateToken } from '../middleware/auth.js'; |
| 25 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 26 | interface SendAccountTextMessageApi { |
| 27 | from?: string; |
| 28 | to?: string; |
| 29 | type?: string; |
| 30 | data?: string; |
| 31 | } |
| 32 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 33 | const jamid = Container.get(Jamid); |
| 34 | |
| 35 | export const accountRouter = Router(); |
| 36 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 37 | accountRouter.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 | |
| 42 | accountRouter.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-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 52 | }); |
| 53 | |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 54 | accountRouter.patch('/', (req, res) => { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 55 | 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-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 59 | res.sendStatus(HttpStatusCode.NoContent); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 60 | }); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 61 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 62 | accountRouter.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(); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 73 | } |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 74 | ); |