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 | */ |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 18 | import { Router } from 'express'; |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 19 | import asyncHandler from 'express-async-handler'; |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 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 | |
| 26 | const jamid = Container.get(Jamid); |
| 27 | |
| 28 | export const accountRouter = Router(); |
| 29 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 30 | accountRouter.use(authenticateToken); |
| 31 | |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 32 | // TODO: Do we really need this route to return the default moderators? |
| 33 | // It would be cleaner just to GET /default-moderators for this |
| 34 | accountRouter.get( |
| 35 | '/', |
| 36 | asyncHandler(async (_req, res) => { |
| 37 | const accountId = res.locals.accountId; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 38 | |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 39 | // 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-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 50 | res.send({ |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 51 | 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-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 59 | |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 60 | accountRouter.patch('/', (req, res) => { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 61 | const accountId = res.locals.accountId; |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 62 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 63 | const currentAccountDetails = jamid.getAccountDetails(accountId); |
| 64 | const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body }; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 65 | jamid.setAccountDetails(accountId, newAccountDetails); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 66 | |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 67 | res.sendStatus(HttpStatusCode.NoContent); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 68 | }); |