blob: 9ba97a79354effecceee529f422b652a2f941728 [file] [log] [blame]
Adrien Béraud0cb76c92021-04-07 19:59:08 -04001const express = require('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éraud0cb76c92021-04-07 19:59:08 -04009 const router = express.Router({mergeParams: true})
Adrien Béraud6ecaa402021-04-06 17:37:25 -040010
11 // Accounts
12 router.get(['/accounts'], (req, res, next) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -040013 console.log("Get account list")
Adrien Béraud6ecaa402021-04-06 17:37:25 -040014 res.json(this.jami.getAccountList().map(account => account.getSummary()))
Adrien Béraud0cb76c92021-04-07 19:59:08 -040015 })
Adrien Béraud6ecaa402021-04-06 17:37:25 -040016
17 router.get(['/accounts/:accountId'], (req, res, next) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -040018 console.log(`Get account ${req.params.accountId}`)
19 const account = this.jami.getAccount(req.params.accountId)
Adrien Béraud6ecaa402021-04-06 17:37:25 -040020 if (account)
21 res.json(account.getObject())
22 else
23 res.sendStatus(404)
Adrien Béraud0cb76c92021-04-07 19:59:08 -040024 })
25
26 // Contacts
27 router.get(['/accounts/:accountId/contacts'], (req, res, next) => {
28 console.log(`Get account ${req.params.accountId}`)
29 const account = this.jami.getAccount(req.params.accountId)
30 if (account)
31 res.json(account.getContacts())
32 else
33 res.sendStatus(404)
34 })
Adrien Béraud6ecaa402021-04-06 17:37:25 -040035
36 // Conversations
Adrien Béraud0cb76c92021-04-07 19:59:08 -040037 const conversationRouter = express.Router({mergeParams: true})
Adrien Béraud6ecaa402021-04-06 17:37:25 -040038 conversationRouter.get('/', (req, res, next) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -040039 console.log(`Get conversations for account ${req.params.accountId}`)
40 const account = this.jami.getAccount(req.params.accountId)
41 if (!account)
42 res.sendStatus(404)
43 res.json(account.getConversationIds())
Adrien Béraud6ecaa402021-04-06 17:37:25 -040044 })
45
46 conversationRouter.get('/:conversationId', (req, res, next) => {
Adrien Béraud0cb76c92021-04-07 19:59:08 -040047 console.log(`Get conversation ${req.params.conversationId} for account ${req.params.accountId}`)
48 const account = this.jami.getAccount(req.params.accountId)
49 if (!account)
50 res.sendStatus(404)
51 res.json(account.getConversation(req.params.conversationId))
Adrien Béraud6ecaa402021-04-06 17:37:25 -040052 })
53
Adrien Béraud0cb76c92021-04-07 19:59:08 -040054 router.use('/accounts/:accountId/conversations', conversationRouter)
55 return router
Adrien Béraud6ecaa402021-04-06 17:37:25 -040056 }
57}
58
Adrien Béraud0cb76c92021-04-07 19:59:08 -040059module.exports = JamiRestApi