blob: 49250c4aa3c8ffe9ff815a3038010f8e109d1fd9 [file] [log] [blame]
Adrien Béraude74741b2021-04-19 13:22:54 -04001import { Router } from 'express'
Adrien Béraud6ecaa402021-04-06 17:37:25 -04002
3class JamiRestApi {
4 constructor(jami) {
5 this.jami = jami
6 }
7
8 getRouter() {
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -04009 const router = Router({ mergeParams: true })
10 //router.use(express.json())
Adrien Béraud6ecaa402021-04-06 17:37:25 -040011
12 // Accounts
Adrien Béraud88a52442021-04-26 12:11:41 -040013 router.get('/accounts', async (req, res) => {
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040014 console.log('Get account list')
Adrien Béraude74741b2021-04-19 13:22:54 -040015 let accounts = this.jami.getAccountList()
16 if (req.user.accountFilter)
17 accounts = accounts.filter(account => req.user.accountFilter(account.getId()))
Adrien Béraud4e287b92021-04-24 16:15:56 -040018 res.json(await Promise.all(accounts.map(async account => await account.getSummary())))
Adrien Béraud0cb76c92021-04-07 19:59:08 -040019 })
Adrien Béraud6ecaa402021-04-06 17:37:25 -040020
Adrien Béraud88a52442021-04-26 12:11:41 -040021 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éraud2b3c2cd2022-09-18 14:24:33 -040024 return next()
Adrien Béraud88a52442021-04-26 12:11:41 -040025 }
26 res.status(403).end()
27 }
28
29 router.post('/accounts', checkCanCreateAccounts, async (req, res) => {
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040030 console.log('Create new account')
Adrien Béraud88a52442021-04-26 12:11:41 -040031 console.log(req.body)
32 try {
Adrien Béraud0561d3c2021-05-02 11:23:54 -040033 const accountId = await this.jami.addAccount(req.body)
34 if (req.body.registerName) {
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040035 this.jami.registerName(accountId, '', req.body.registerName)
36 .then(result => console.log('Name registrtion result: ' + result))
Adrien Béraud0561d3c2021-05-02 11:23:54 -040037 }
38 res.json({ accountId })
Adrien Béraud88a52442021-04-26 12:11:41 -040039 } catch (e) {
40 res.status(400).json({ error: e })
41 }
42 })
43
Adrien Béraude74741b2021-04-19 13:22:54 -040044 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éraud2b3c2cd2022-09-18 14:24:33 -040047 return next()
Adrien Béraude74741b2021-04-19 13:22:54 -040048 }
49 res.status(403).end()
50 }
51
52 const accountRouter = Router({mergeParams: true})
53 router.use('/accounts/:accountId', checkAccount, accountRouter)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040054
Adrien Béraud86986032021-04-25 12:04:53 -040055 accountRouter.get('/', async (req, res) => {
ervinanoh99655642022-09-01 15:11:31 -040056 console.log(`Get account ${req.params.accountId}`)
Adrien Béraud0cb76c92021-04-07 19:59:08 -040057 const account = this.jami.getAccount(req.params.accountId)
Adrien Béraud4e287b92021-04-24 16:15:56 -040058 if (account) {
59 account.defaultModerators = this.jami.getDefaultModerators(account.getId())
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040060 const obj = await account.getObject()
61 obj.devices = this.jami.getDevices(req.params.accountId)
Adrien Béraud4e287b92021-04-24 16:15:56 -040062 res.json(obj)
63 } else
Adrien Béraude74741b2021-04-19 13:22:54 -040064 res.status(404).end()
Adrien Béraud0cb76c92021-04-07 19:59:08 -040065 })
66
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040067 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éraud86986032021-04-25 12:04:53 -040076
Adrien Béraud0cb76c92021-04-07 19:59:08 -040077 // Contacts
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040078 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 })
idillon531b6f22022-09-16 14:02:00 -040086
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040087 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éraud6ecaa402021-04-06 17:37:25 -040096
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040097 accountRouter.get('/contacts/details/:contactId', (req, res) => {
98 console.log(
99 `Get contact ${req.params.contactId} details for ${req.params.accountId}`
ervinanoh99655642022-09-01 15:11:31 -0400100 )
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400101 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 )
ervinanoh99655642022-09-01 15:11:31 -0400183 //res.json(account.getConversations())
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400184 })
idillon531b6f22022-09-16 14:02:00 -0400185
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400186 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éraud35e7d7c2021-04-13 03:28:39 -0400201
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400202 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éraud88a52442021-04-26 12:11:41 -0400213
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400214 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 })
ervinanoh99655642022-09-01 15:11:31 -0400231
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400232 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 )
ervinanoh99655642022-09-01 15:11:31 -0400249
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400250 // Calls
ervinanoh99655642022-09-01 15:11:31 -0400251
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400252 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 })
ervinanoh99655642022-09-01 15:11:31 -0400261
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400262 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 })
ervinanoh99655642022-09-01 15:11:31 -0400274
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400275 // Nameserver
276 const nsRouter = Router({ mergeParams: true })
277 accountRouter.use('/ns', nsRouter) // use account nameserver
278 router.use('/ns', nsRouter) // use default nameserver
ervinanoh99655642022-09-01 15:11:31 -0400279
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400280 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 })
ervinanoh99655642022-09-01 15:11:31 -0400293
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400294 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 })
ervinanoh99655642022-09-01 15:11:31 -0400307
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400308 return router
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400309 }
310}
311
Adrien Béraude74741b2021-04-19 13:22:54 -0400312export default JamiRestApi