idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [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'; |
| 19 | import asyncHandler from 'express-async-handler'; |
| 20 | import { HttpStatusCode, IConversationRequest } from 'jami-web-common'; |
| 21 | import { Container } from 'typedi'; |
| 22 | |
| 23 | import { Jamid } from '../jamid/jamid.js'; |
| 24 | import { authenticateToken } from '../middleware/auth.js'; |
| 25 | import { ConversationService } from '../services/ConversationService.js'; |
| 26 | |
| 27 | const jamid = Container.get(Jamid); |
| 28 | const conversationService = Container.get(ConversationService); |
| 29 | |
| 30 | export const conversationRequestRouter = Router(); |
| 31 | |
| 32 | conversationRequestRouter.use(authenticateToken); |
| 33 | |
| 34 | conversationRequestRouter.get( |
| 35 | '/', |
| 36 | asyncHandler(async (_req, res) => { |
| 37 | const accountId = res.locals.accountId; |
| 38 | const jamidRequests = jamid.getConversationRequests(accountId); |
| 39 | Promise.all( |
| 40 | jamidRequests.map((jamidRequest) => conversationService.createConversationRequest(accountId, jamidRequest)) |
| 41 | ) |
| 42 | .then((apiRequests: IConversationRequest[]) => res.send(apiRequests)) |
| 43 | .catch((err) => res.status(HttpStatusCode.InternalServerError).send(err.message)); |
| 44 | }) |
| 45 | ); |
| 46 | |
| 47 | conversationRequestRouter.post( |
| 48 | '/:conversationId', |
| 49 | asyncHandler(async (req, res) => { |
| 50 | const accountId = res.locals.accountId; |
| 51 | const conversationId = req.params.conversationId; |
| 52 | await jamid.acceptConversationRequest(accountId, conversationId); |
| 53 | const conversationSummary = await conversationService.createConversationSummary(accountId, conversationId); |
| 54 | if (conversationSummary === undefined) { |
| 55 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
| 56 | return; |
| 57 | } |
| 58 | res.send(conversationSummary); |
| 59 | }) |
| 60 | ); |
| 61 | |
| 62 | conversationRequestRouter.delete( |
| 63 | '/:conversationId', |
| 64 | asyncHandler(async (req, res) => { |
| 65 | jamid.declineConversationRequest(res.locals.accountId, req.params.conversationId); |
| 66 | res.sendStatus(HttpStatusCode.NoContent); |
| 67 | }) |
| 68 | ); |
| 69 | |
| 70 | conversationRequestRouter.post('/:conversationId/block', (req, res) => { |
| 71 | const accountId = res.locals.accountId; |
| 72 | const conversationId = req.params.conversationId; |
| 73 | const conversationRequests = jamid.getConversationRequests(accountId); |
| 74 | const conversationRequest = conversationRequests.filter((request) => request.id === conversationId)[0]; |
| 75 | if (!conversationRequest) { |
| 76 | res.status(HttpStatusCode.NotFound).send('No such conversation request found'); |
| 77 | } |
| 78 | jamid.blockContact(accountId, conversationRequest.from); |
| 79 | res.sendStatus(HttpStatusCode.NoContent); |
| 80 | }); |