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, |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 24 | IConversationMember, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 25 | IConversationSummary, |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 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 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 36 | async function createConversationSummary( |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 37 | accountId: string, |
| 38 | accountUri: string, |
| 39 | conversationId: string |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 40 | ): Promise<IConversationSummary | 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 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 48 | const membersNames = []; |
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); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 57 | membersNames.push(username ?? member.uri); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 58 | } |
| 59 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 60 | const lastMessage = (await jamid.getConversationMessages(accountId, conversationId, '', 1))[0]; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 61 | |
| 62 | return { |
| 63 | id: conversationId, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 64 | avatar: infos.avatar, |
| 65 | title: infos.title, |
| 66 | membersNames, |
| 67 | lastMessage, |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 68 | }; |
| 69 | } |
| 70 | |
| 71 | export const conversationRouter = Router(); |
| 72 | |
| 73 | conversationRouter.use(authenticateToken); |
| 74 | |
| 75 | conversationRouter.get( |
| 76 | '/', |
| 77 | asyncHandler(async (_req, res) => { |
| 78 | const accountId = res.locals.accountId; |
| 79 | |
| 80 | // Retrieve the URI of the current account (Account.username actually stores the URI rather than the username) |
| 81 | const accountUri = jamid.getAccountDetails(accountId)['Account.username']; |
| 82 | |
| 83 | const conversationIds = jamid.getConversationIds(accountId); |
| 84 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 85 | const conversationsSummaries = []; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 86 | for (const conversationId of conversationIds) { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 87 | const conversationSummary = await createConversationSummary(accountId, accountUri, conversationId); |
| 88 | conversationsSummaries.push(conversationSummary); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 89 | } |
| 90 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 91 | res.send(conversationsSummaries); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 92 | }) |
| 93 | ); |
| 94 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 95 | conversationRouter.post( |
| 96 | '/', |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 97 | (req: Request<ParamsDictionary, ContactDetails | string, Partial<NewConversationRequestBody>>, res) => { |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 98 | const { members } = req.body; |
| 99 | if (members === undefined || members.length !== 1) { |
| 100 | res.status(HttpStatusCode.BadRequest).send('Missing members or more than one member in body'); |
| 101 | return; |
| 102 | } |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 103 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 104 | const accountId = res.locals.accountId; |
| 105 | |
| 106 | const contactId = members[0]; |
| 107 | jamid.addContact(accountId, contactId); |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 108 | // We need to manually send a conversation request |
| 109 | jamid.sendTrustRequest(accountId, contactId); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 110 | |
| 111 | const contactDetails = jamid.getContactDetails(accountId, contactId); |
| 112 | if (Object.keys(contactDetails).length === 0) { |
| 113 | res.status(HttpStatusCode.NotFound).send('No such member found'); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | res.send(contactDetails); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 118 | } |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 119 | ); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 120 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 121 | conversationRouter.get( |
| 122 | '/:conversationId', |
| 123 | asyncHandler(async (req, res) => { |
| 124 | const accountId = res.locals.accountId; |
| 125 | const conversationId = req.params.conversationId; |
| 126 | |
| 127 | // Retrieve the URI of the current account (Account.username actually stores the URI rather than the username) |
| 128 | const accountUri = jamid.getAccountDetails(accountId)['Account.username']; |
| 129 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 130 | const conversationSummary = await createConversationSummary(accountId, accountUri, conversationId); |
| 131 | if (conversationSummary === undefined) { |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 132 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 136 | res.send(conversationSummary); |
| 137 | }) |
| 138 | ); |
| 139 | |
| 140 | conversationRouter.get( |
| 141 | '/:conversationId/infos', |
| 142 | asyncHandler(async (req, res) => { |
| 143 | const accountId = res.locals.accountId; |
| 144 | const conversationId = req.params.conversationId; |
| 145 | |
| 146 | const infos = jamid.getConversationInfos(accountId, conversationId); |
| 147 | if (Object.keys(infos).length === 0) { |
| 148 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
| 149 | } |
| 150 | |
| 151 | res.send(infos); |
| 152 | }) |
| 153 | ); |
| 154 | |
| 155 | conversationRouter.get( |
| 156 | '/:conversationId/members', |
| 157 | asyncHandler(async (req, res) => { |
| 158 | const accountId = res.locals.accountId; |
| 159 | const conversationId = req.params.conversationId; |
| 160 | |
| 161 | // Retrieve the URI of the current account (Account.username actually stores the URI rather than the username) |
| 162 | const accountUri = jamid.getAccountDetails(accountId)['Account.username']; |
| 163 | |
| 164 | const infos = jamid.getConversationInfos(accountId, conversationId); |
| 165 | if (Object.keys(infos).length === 0) { |
| 166 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | const members = jamid.getConversationMembers(accountId, conversationId); |
| 171 | |
| 172 | const namedMembers: IConversationMember[] = []; |
| 173 | for (const member of members) { |
| 174 | // Exclude current user from returned conversation members |
| 175 | if (member.uri === accountUri) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | // Add usernames for conversation members |
| 180 | const { username } = await jamid.lookupAddress(member.uri, accountId); |
| 181 | namedMembers.push({ |
| 182 | role: member.role, |
| 183 | contact: { |
| 184 | uri: member.uri, |
| 185 | registeredName: username, |
| 186 | }, |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | res.send(namedMembers); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 191 | }) |
| 192 | ); |
| 193 | |
| 194 | conversationRouter.get( |
| 195 | '/:conversationId/messages', |
| 196 | asyncHandler(async (req, res) => { |
| 197 | const accountId = res.locals.accountId; |
| 198 | const conversationId = req.params.conversationId; |
| 199 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 200 | const infos = jamid.getConversationInfos(accountId, conversationId); |
| 201 | if (Object.keys(infos).length === 0) { |
| 202 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | const messages = await jamid.getConversationMessages(accountId, conversationId); |
| 207 | res.send(messages); |
| 208 | }) |
| 209 | ); |
| 210 | |
| 211 | conversationRouter.post( |
| 212 | '/:conversationId/messages', |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 213 | (req: Request<ParamsDictionary, string, Partial<NewMessageRequestBody>>, res) => { |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 214 | const { message } = req.body; |
| 215 | if (message === undefined) { |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 216 | res.status(HttpStatusCode.BadRequest).send('Missing message in body'); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 220 | const accountId = res.locals.accountId; |
| 221 | const conversationId = req.params.conversationId; |
| 222 | |
| 223 | const infos = jamid.getConversationInfos(accountId, conversationId); |
| 224 | if (Object.keys(infos).length === 0) { |
| 225 | res.status(HttpStatusCode.NotFound).send('No such conversation found'); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | jamid.sendConversationMessage(accountId, conversationId, message); |
| 230 | res.sendStatus(HttpStatusCode.NoContent); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 231 | } |
| 232 | ); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame^] | 233 | |
| 234 | conversationRouter.delete('/:conversationId', (req, res) => { |
| 235 | jamid.removeConversation(res.locals.accountId, req.params.conversationId); |
| 236 | res.sendStatus(HttpStatusCode.NoContent); |
| 237 | }); |