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'; |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame^] | 19 | import asyncHandler from 'express-async-handler'; |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 20 | import { ParamsDictionary } from 'express-serve-static-core'; |
| 21 | import { AccountDetails, HttpStatusCode } from 'jami-web-common'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 22 | import { Container } from 'typedi'; |
| 23 | |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 24 | import { Jamid } from '../jamid/jamid.js'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 25 | import { authenticateToken } from '../middleware/auth.js'; |
| 26 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 27 | interface SendAccountTextMessageApi { |
| 28 | from?: string; |
| 29 | to?: string; |
| 30 | type?: string; |
| 31 | data?: string; |
| 32 | } |
| 33 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 34 | const jamid = Container.get(Jamid); |
| 35 | |
| 36 | export const accountRouter = Router(); |
| 37 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 38 | accountRouter.use(authenticateToken); |
| 39 | |
| 40 | // TODO: If tokens can be generated on one daemon and used on another (transferrable between daemons), |
| 41 | // then add middleware to check that the currently logged-in accountId is stored in this daemon instance |
| 42 | |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame^] | 43 | // TODO: Do we really need this route to return the default moderators? |
| 44 | // It would be cleaner just to GET /default-moderators for this |
| 45 | accountRouter.get( |
| 46 | '/', |
| 47 | asyncHandler(async (_req, res) => { |
| 48 | const accountId = res.locals.accountId; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 49 | |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame^] | 50 | // Add usernames for default moderators |
| 51 | const defaultModeratorUris = jamid.getDefaultModeratorUris(accountId); |
| 52 | const namedDefaultModerators = []; |
| 53 | for (const defaultModeratorUri of defaultModeratorUris) { |
| 54 | const { username } = await jamid.lookupAddress(defaultModeratorUri, accountId); |
| 55 | namedDefaultModerators.push({ |
| 56 | uri: defaultModeratorUri, |
| 57 | registeredName: username, |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | res.json({ |
| 62 | id: accountId, |
| 63 | details: jamid.getAccountDetails(accountId), |
| 64 | volatileDetails: jamid.getVolatileAccountDetails(accountId), |
| 65 | defaultModerators: namedDefaultModerators, |
| 66 | devices: jamid.getDevices(accountId), |
| 67 | }); |
| 68 | }) |
| 69 | ); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 70 | |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 71 | accountRouter.patch('/', (req, res) => { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 72 | const accountId = res.locals.accountId; |
| 73 | const currentAccountDetails = jamid.getAccountDetails(accountId); |
| 74 | const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body }; |
| 75 | jamid.setAccountDetails(res.locals.accountId, newAccountDetails); |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 76 | res.sendStatus(HttpStatusCode.NoContent); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 77 | }); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 78 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 79 | accountRouter.post( |
| 80 | '/send-account-message', |
| 81 | (req: Request<ParamsDictionary, string, SendAccountTextMessageApi>, res) => { |
| 82 | const { from, to, type, data } = req.body; |
| 83 | if (!from || !to || !type || !data) { |
| 84 | res.status(HttpStatusCode.BadRequest).send('Missing arguments in request'); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | jamid.sendAccountTextMessage(from, to, type, data); |
| 89 | res.end(); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 90 | } |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 91 | ); |