Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -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 { Request, Router } from 'express'; |
| 19 | import asyncHandler from 'express-async-handler'; |
| 20 | import { ParamsDictionary } from 'express-serve-static-core'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 21 | import { |
| 22 | ContactDetails, |
| 23 | HttpStatusCode, |
| 24 | IConversation, |
| 25 | IConversationMember, |
| 26 | NewConversationRequestBody, |
| 27 | NewMessageRequestBody, |
| 28 | } from 'jami-web-common'; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 29 | import { Container } from 'typedi'; |
| 30 | |
| 31 | import { Jamid } from '../jamid/jamid.js'; |
| 32 | import { authenticateToken } from '../middleware/auth.js'; |
| 33 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 34 | const jamid = Container.get(Jamid); |
| 35 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 36 | async function createConversationResponseObject( |
| 37 | accountId: string, |
| 38 | accountUri: string, |
| 39 | conversationId: string |
| 40 | ): Promise<IConversation | undefined> { |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 41 | const infos = jamid.getConversationInfos(accountId, conversationId); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 42 | if (Object.keys(infos).length === 0) { |
| 43 | return undefined; |
| 44 | } |
| 45 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 46 | const members = jamid.getConversationMembers(accountId, conversationId); |
| 47 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 48 | const namedMembers: IConversationMember[] = []; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 49 | for (const member of members) { |
| 50 | // Exclude current user from returned conversation members |
| 51 | if (member.uri === accountUri) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | // Add usernames for conversation members |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 56 | const { username } = await jamid.lookupAddress(member.uri, accountId); |
| 57 | namedMembers.push({ |
| 58 | role: member.role, |
| 59 | contact: { |
| 60 | uri: member.uri, |
| 61 | registeredName: username, |
| 62 | }, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | // TODO: Check if messages actually need to be added to response |
| 67 | // (does the client really need it for all endpoints, or just the /conversations/conversationId/messages endpoint?) |
| 68 | const messages = await jamid.getConversationMessages(accountId, conversationId); |
| 69 | |
| 70 | return { |
| 71 | id: conversationId, |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 72 | members: namedMembers, |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 73 | messages: messages, |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 74 | infos: infos, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | export const conversationRouter = Router(); |
| 79 | |
| 80 | conversationRouter.use(authenticateToken); |
| 81 | |
| 82 | conversationRouter.get( |
| 83 | '/', |
| 84 | asyncHandler(async (_req, res) => { |
| 85 | const accountId = res.locals.accountId; |
| 86 | |
| 87 | // Retrieve the URI of the current account (Account.username actually stores the URI rather than the username) |
| 88 | const accountUri = jamid.getAccountDetails(accountId)['Account.username']; |
| 89 | |
| 90 | const conversationIds = jamid.getConversationIds(accountId); |
| 91 | |
| 92 | const conversations = []; |
| 93 | for (const conversationId of conversationIds) { |
| 94 | const conversation = await createConversationResponseObject(accountId, accountUri, conversationId); |
| 95 | conversations.push(conversation); |
| 96 | } |
| 97 | |
| 98 | res.send(conversations); |
| 99 | }) |
| 100 | ); |
| 101 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 102 | conversationRouter.post( |
| 103 | '/', |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 104 | (req: Request<ParamsDictionary, ContactDetails | string, Partial<NewConversationRequestBody>>, res) => { |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 105 | const { members } = req.body; |
| 106 | if (members === undefined || members.length !== 1) { |
| 107 | res.status(HttpStatusCode.BadRequest).send('Missing members or more than one member in body'); |
| 108 | return; |
| 109 | } |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 110 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 111 | const accountId = res.locals.accountId; |
| 112 | |
| 113 | const contactId = members[0]; |
| 114 | jamid.addContact(accountId, contactId); |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 115 | // We need to manually send a conversation request |
| 116 | jamid.sendTrustRequest(accountId, contactId); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 117 | |
| 118 | const contactDetails = jamid.getContactDetails(accountId, contactId); |
| 119 | if (Object.keys(contactDetails).length === 0) { |
| 120 | res.status(HttpStatusCode.NotFound).send('No such member found'); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | res.send(contactDetails); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 125 | } |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 126 | ); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 127 | |
| 128 | // TODO: Check if we actually need this endpoint to return messages. |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 129 | // At the moment, /conversations does a lot of work returning all the conversations with the same |
| 130 | // level of detail as this, and /conversations/messages returns just the messages. Check whether or not |
| 131 | // this is what we want, and if so, if we can be more economical with client requests. |
| 132 | conversationRouter.get( |
| 133 | '/:conversationId', |
| 134 | asyncHandler(async (req, res) => { |
| 135 | const accountId = res.locals.accountId; |
| 136 | const conversationId = req.params.conversationId; |
| 137 | |
| 138 | // Retrieve the URI of the current account (Account.username actually stores the URI rather than the username) |
| 139 | const accountUri = jamid.getAccountDetails(accountId)['Account.username']; |
| 140 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 141 | const conversation = await createConversationResponseObject(accountId, accountUri, conversationId); |
| 142 | if (conversation === undefined) { |
| 143 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 147 | res.send(conversation); |
| 148 | }) |
| 149 | ); |
| 150 | |
| 151 | conversationRouter.get( |
| 152 | '/:conversationId/messages', |
| 153 | asyncHandler(async (req, res) => { |
| 154 | const accountId = res.locals.accountId; |
| 155 | const conversationId = req.params.conversationId; |
| 156 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 157 | const infos = jamid.getConversationInfos(accountId, conversationId); |
| 158 | if (Object.keys(infos).length === 0) { |
| 159 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
| 163 | const messages = await jamid.getConversationMessages(accountId, conversationId); |
| 164 | res.send(messages); |
| 165 | }) |
| 166 | ); |
| 167 | |
| 168 | conversationRouter.post( |
| 169 | '/:conversationId/messages', |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 170 | (req: Request<ParamsDictionary, string, Partial<NewMessageRequestBody>>, res) => { |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 171 | const { message } = req.body; |
| 172 | if (message === undefined) { |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 173 | res.status(HttpStatusCode.BadRequest).send('Missing message in body'); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 177 | const accountId = res.locals.accountId; |
| 178 | const conversationId = req.params.conversationId; |
| 179 | |
| 180 | const infos = jamid.getConversationInfos(accountId, conversationId); |
| 181 | if (Object.keys(infos).length === 0) { |
| 182 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | jamid.sendConversationMessage(accountId, conversationId, message); |
| 187 | res.sendStatus(HttpStatusCode.NoContent); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 188 | } |
| 189 | ); |