blob: f0ef5663169151708951dc73ddcb5dabda9a8a23 [file] [log] [blame]
idillon18283ac2023-01-07 12:06:42 -05001/*
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 */
18import { Router } from 'express';
19import asyncHandler from 'express-async-handler';
20import { HttpStatusCode, IConversationRequest } from 'jami-web-common';
21import { Container } from 'typedi';
22
23import { Jamid } from '../jamid/jamid.js';
24import { authenticateToken } from '../middleware/auth.js';
25import { ConversationService } from '../services/ConversationService.js';
26
27const jamid = Container.get(Jamid);
28const conversationService = Container.get(ConversationService);
29
30export const conversationRequestRouter = Router();
31
32conversationRequestRouter.use(authenticateToken);
33
34conversationRequestRouter.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
47conversationRequestRouter.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
62conversationRequestRouter.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
70conversationRequestRouter.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});