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