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 | */ |
| 18 | import { Router } from 'express'; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame^] | 19 | import { AccountDetails } from 'jami-web-common'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 20 | import { Container } from 'typedi'; |
| 21 | |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 22 | import { Jamid } from '../jamid/jamid.js'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 23 | import { authenticateToken } from '../middleware/auth.js'; |
| 24 | |
| 25 | const jamid = Container.get(Jamid); |
| 26 | |
| 27 | export const accountRouter = Router(); |
| 28 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame^] | 29 | accountRouter.use(authenticateToken); |
| 30 | |
| 31 | // TODO: If tokens can be generated on one daemon and used on another (transferrable between daemons), |
| 32 | // then add middleware to check that the currently logged-in accountId is stored in this daemon instance |
| 33 | |
| 34 | accountRouter.get('/', (_req, res) => { |
| 35 | const accountId = res.locals.accountId; |
| 36 | |
| 37 | res.json({ |
| 38 | id: accountId, |
| 39 | details: jamid.getAccountDetails(accountId), |
| 40 | volatileDetails: jamid.getVolatileAccountDetails(accountId), |
| 41 | defaultModerators: jamid.getDefaultModerators(accountId), |
| 42 | devices: jamid.getDevices(accountId), |
| 43 | }); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 44 | }); |
| 45 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame^] | 46 | accountRouter.post('/', (req, res) => { |
| 47 | const accountId = res.locals.accountId; |
| 48 | const currentAccountDetails = jamid.getAccountDetails(accountId); |
| 49 | const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body }; |
| 50 | jamid.setAccountDetails(res.locals.accountId, newAccountDetails); |
| 51 | res.end(); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 52 | }); |