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