simon | 26e79f7 | 2022-10-05 22:16:08 -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 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | import { Router } from 'express'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 19 | |
| 20 | class JamiRestApi { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 21 | constructor(jami) { |
| 22 | this.jami = jami; |
| 23 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 24 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 25 | getRouter() { |
| 26 | const router = Router({ mergeParams: true }); |
| 27 | //router.use(express.json()) |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 28 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 29 | // Accounts |
| 30 | router.get('/accounts', async (req, res) => { |
| 31 | console.log('Get account list'); |
| 32 | let accounts = this.jami.getAccountList(); |
| 33 | if (req.user.accountFilter) accounts = accounts.filter((account) => req.user.accountFilter(account.getId())); |
| 34 | res.json(await Promise.all(accounts.map(async (account) => await account.getSummary()))); |
| 35 | }); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 36 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | const checkCanCreateAccounts = (req, res, next) => { |
| 38 | console.log(`checkCanCreateAccounts ${req.params.accountId} for ${req.user.id}`); |
| 39 | if (req.user && !req.user.accountFilter) { |
| 40 | return next(); |
| 41 | } |
| 42 | res.status(403).end(); |
| 43 | }; |
| 44 | |
| 45 | router.post('/accounts', checkCanCreateAccounts, async (req, res) => { |
| 46 | console.log('Create new account'); |
| 47 | console.log(req.body); |
| 48 | try { |
| 49 | const accountId = await this.jami.addAccount(req.body); |
| 50 | if (req.body.registerName) { |
| 51 | this.jami |
| 52 | .registerName(accountId, '', req.body.registerName) |
| 53 | .then((result) => console.log('Name registrtion result: ' + result)); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 54 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | res.json({ accountId }); |
| 56 | } catch (e) { |
| 57 | res.status(400).json({ error: e }); |
| 58 | } |
| 59 | }); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 60 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 61 | const checkAccount = (req, res, next) => { |
| 62 | console.log(`checkAccount ${req.params.accountId} for ${req.user.id}`); |
| 63 | if (req.user && (!req.user.accountFilter || req.user.accountFilter(req.params.accountId))) { |
| 64 | return next(); |
| 65 | } |
| 66 | res.status(403).end(); |
| 67 | }; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 68 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 69 | const accountRouter = Router({ mergeParams: true }); |
| 70 | router.use('/accounts/:accountId', checkAccount, accountRouter); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 71 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 72 | accountRouter.get('/', async (req, res) => { |
| 73 | console.log(`Get account ${req.params.accountId}`); |
| 74 | const account = this.jami.getAccount(req.params.accountId); |
| 75 | if (account) { |
| 76 | account.defaultModerators = this.jami.getDefaultModerators(account.getId()); |
| 77 | const obj = await account.getObject(); |
| 78 | obj.devices = this.jami.getDevices(req.params.accountId); |
| 79 | res.json(obj); |
| 80 | } else res.status(404).end(); |
| 81 | }); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 82 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 83 | accountRouter.post('/', async (req, res) => { |
| 84 | console.log(`Set account details ${req.params.accountId}`); |
| 85 | const account = this.jami.getAccount(req.params.accountId); |
| 86 | if (account) { |
| 87 | const newDetails = account.updateDetails(req.body); |
| 88 | this.jami.setAccountDetails(account.getId(), newDetails); |
| 89 | res.status(200).end(); |
| 90 | } else res.status(404).end(); |
| 91 | }); |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 92 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 93 | // Contacts |
| 94 | accountRouter.get('/contacts', (req, res) => { |
| 95 | console.log(`Get account ${req.params.accountId}`); |
| 96 | const account = this.jami.getAccount(req.params.accountId); |
| 97 | if (account) { |
| 98 | let rep = account.getContacts(); |
| 99 | res.json(rep); |
| 100 | } else res.status(404).end(); |
| 101 | }); |
Adrien Béraud | 8698603 | 2021-04-25 12:04:53 -0400 | [diff] [blame] | 102 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 103 | accountRouter.get('/contacts/:contactId', (req, res) => { |
| 104 | console.log(`Get account details fot ${req.params.accountId}`); |
| 105 | const account = this.jami.getAccount(req.params.accountId); |
| 106 | const uri = req.params.uri; |
| 107 | if (account) { |
| 108 | let rep = account.getContactDetails(uri); |
| 109 | res.json(rep); |
| 110 | } else res.status(404).end(); |
| 111 | }); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 112 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 113 | accountRouter.get('/contacts/details/:contactId', (req, res) => { |
| 114 | console.log(`Get contact ${req.params.contactId} details for ${req.params.accountId}`); |
| 115 | const account = this.jami.getAccount(req.params.accountId); |
| 116 | if (account) { |
| 117 | let rep = this.jami.getContactDetails(req.params.accountId, req.params.contactId); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 118 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 119 | console.log(rep); |
| 120 | res.json(rep); |
| 121 | } else res.status(404).end(); |
| 122 | }); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 123 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 124 | accountRouter.delete('/contacts/remove/:contactId', async (req, res) => { |
| 125 | console.log('REMOVED CONTACT: ', req.params.contactId); |
| 126 | const account = this.jami.getAccount(req.params.accountId); |
| 127 | if (account) { |
| 128 | let rep = this.jami.removeContact(req.params.accountId, req.params.contactId); |
| 129 | res.json(rep); |
| 130 | } else res.status(404).end(); |
| 131 | res.status(200).end(); |
| 132 | }); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 133 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 134 | accountRouter.delete('/contacts/block/:contactId/', async (req, res) => { |
| 135 | console.log('REMOVED AND BLOCKED CONTACT: ', req.params.contactId); |
| 136 | const account = this.jami.getAccount(req.params.accountId); |
| 137 | if (account) { |
| 138 | let rep = this.jami.blockContact(req.params.accountId, req.params.contactId); |
| 139 | res.json(rep); |
| 140 | } else res.status(404).end(); |
| 141 | res.status(200).end(); |
| 142 | }); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 143 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 144 | // Default modertors |
| 145 | accountRouter.put('/defaultModerators/:contactId', async (req, res) => { |
| 146 | console.log(`Adding default moderator ${req.params.contactId} to account ${req.params.accountId}`); |
| 147 | this.jami.addDefaultModerator(req.params.accountId, req.params.contactId); |
| 148 | res.status(200).end(); |
| 149 | }); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 150 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 151 | accountRouter.delete('/defaultModerators/:contactId', async (req, res) => { |
| 152 | console.log(`Removing default moderator to account ${req.params.accountId}`); |
| 153 | this.jami.removeDefaultModerator(req.params.accountId, req.params.contactId); |
| 154 | res.status(200).end(); |
| 155 | }); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 156 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 157 | // Conversations |
| 158 | accountRouter.get('/conversations', async (req, res, next) => { |
| 159 | console.log(`Get conversations for account ${req.params.accountId}`); |
| 160 | const account = this.jami.getAccount(req.params.accountId); |
| 161 | if (!account) return res.sendStatus(404); |
| 162 | const conversations = account.getConversations(); |
| 163 | res.json( |
| 164 | await Promise.all( |
| 165 | Object.keys(conversations).map( |
| 166 | async (conversationId) => |
| 167 | await conversations[conversationId].getObject({ |
| 168 | memberFilter: (member) => member.contact.getUri() !== account.getUri(), |
| 169 | }) |
| 170 | ) |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 171 | ) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 172 | ); |
| 173 | //res.json(account.getConversations()) |
| 174 | }); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 175 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 176 | accountRouter.post('/conversations', (req, res) => { |
| 177 | console.log(`Create conversations for account, contact ${req.params.accountId}`); |
| 178 | // console.log(req.body) |
| 179 | const account = this.jami.getAccount(req.params.accountId); |
| 180 | if (!account) return res.sendStatus(404); |
| 181 | if (req.body.members.length === 1) { |
| 182 | const details = this.jami.addContact(req.params.accountId, req.body.members[0]); |
| 183 | res.json(details); |
| 184 | } else res.status(400).end(); |
| 185 | }); |
| 186 | |
| 187 | accountRouter.post('/conversations/:conversationId', async (req, res) => { |
| 188 | console.log(`Sending message to ${req.params.conversationId} for account ${req.params.accountId}`); |
| 189 | this.jami.sendMessage(req.params.accountId, req.params.conversationId, req.body.message); |
| 190 | res.status(200).end(); |
| 191 | }); |
| 192 | |
| 193 | accountRouter.get('/conversations/:conversationId', async (req, res) => { |
| 194 | console.log(`Get conversation ${req.params.conversationId} for account ${req.params.accountId}`); |
| 195 | const account = this.jami.getAccount(req.params.accountId); |
| 196 | if (!account) return res.sendStatus(404); |
| 197 | const conversation = account.getConversation(req.params.conversationId); |
| 198 | if (!conversation) res.status(404).end(); |
| 199 | else { |
| 200 | res.json( |
| 201 | await conversation.getObject({ |
| 202 | memberFilter: (member) => member.contact.getUri() !== account.getUri(), |
| 203 | }) |
| 204 | ); |
| 205 | } |
| 206 | }); |
| 207 | |
| 208 | accountRouter.get('/conversations/:conversationId/messages', async (req, res) => { |
| 209 | console.log(`Get messages for conversation ${req.params.conversationId} for account ${req.params.accountId}`); |
| 210 | try { |
| 211 | const messages = await this.jami.loadMessages(req.params.accountId, req.params.conversationId); |
| 212 | res.json(messages).end(); |
| 213 | } catch (e) { |
| 214 | res.status(400).json({ error: e.message }); |
| 215 | } |
| 216 | }); |
| 217 | |
| 218 | // Calls |
| 219 | |
| 220 | accountRouter.get('/calls', async (req, res) => { |
| 221 | console.log(`Get calls for account ${req.params.accountId}`); |
| 222 | try { |
| 223 | const calls = await this.jami.getCalls(req.params.accountId); |
| 224 | res.json(calls).end(); |
| 225 | } catch (e) { |
| 226 | res.status(400).json({ error: e.message }); |
| 227 | } |
| 228 | }); |
| 229 | |
| 230 | accountRouter.get('/calls/:callId', async (req, res) => { |
| 231 | console.log(`Get call ${req.params.callId} for account ${req.params.accountId}`); |
| 232 | try { |
| 233 | const messages = await this.jami.getCall(req.params.accountId, req.params.callId); |
| 234 | res.json(messages).end(); |
| 235 | } catch (e) { |
| 236 | res.status(400).json({ error: e.message }); |
| 237 | } |
| 238 | }); |
| 239 | |
| 240 | // Nameserver |
| 241 | const nsRouter = Router({ mergeParams: true }); |
| 242 | accountRouter.use('/ns', nsRouter); // use account nameserver |
| 243 | router.use('/ns', nsRouter); // use default nameserver |
| 244 | |
| 245 | nsRouter.get(['/name/:nameQuery'], (req, res, next) => { |
| 246 | console.log(`Name lookup ${req.params.nameQuery}`); |
| 247 | this.jami |
| 248 | .lookupName(req.params.accountId || '', req.params.nameQuery) |
| 249 | .then((result) => { |
| 250 | if (result.state == 0) res.json(result); |
| 251 | else if (result.state == 1) res.status(400).json({}); |
| 252 | else res.status(404).json({}); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 253 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 254 | .catch((e) => { |
| 255 | res.status(404).json({}); |
| 256 | }); |
| 257 | }); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 258 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 259 | nsRouter.get(['/addr/:addrQuery'], (req, res, next) => { |
| 260 | console.log(`Address lookup ${req.params.addrQuery}`); |
| 261 | this.jami |
| 262 | .lookupAddress(req.params.accountId || '', req.params.addrQuery) |
| 263 | .then((result) => { |
| 264 | if (result.state == 0) res.json(result); |
| 265 | else if (result.state == 1) res.status(400).json({}); |
| 266 | else res.status(404).json({}); |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 267 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 268 | .catch((e) => { |
| 269 | res.status(404).json({}); |
| 270 | }); |
| 271 | }); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 272 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 273 | return router; |
| 274 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 275 | } |
| 276 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 277 | export default JamiRestApi; |