blob: 0e41a8931404b2471b920237a90ee5553dd1b4f5 [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001import { Router } from 'express';
Adrien Béraud6ecaa402021-04-06 17:37:25 -04002
3class JamiRestApi {
simond47ef9e2022-09-28 22:24:28 -04004 constructor(jami) {
5 this.jami = jami;
6 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -04007
simond47ef9e2022-09-28 22:24:28 -04008 getRouter() {
9 const router = Router({ mergeParams: true });
10 //router.use(express.json())
Adrien Béraud6ecaa402021-04-06 17:37:25 -040011
simond47ef9e2022-09-28 22:24:28 -040012 // 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éraud6ecaa402021-04-06 17:37:25 -040019
simond47ef9e2022-09-28 22:24:28 -040020 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éraud88a52442021-04-26 12:11:41 -040037 }
simond47ef9e2022-09-28 22:24:28 -040038 res.json({ accountId });
39 } catch (e) {
40 res.status(400).json({ error: e });
41 }
42 });
Adrien Béraud88a52442021-04-26 12:11:41 -040043
simond47ef9e2022-09-28 22:24:28 -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))) {
47 return next();
48 }
49 res.status(403).end();
50 };
Adrien Béraud88a52442021-04-26 12:11:41 -040051
simond47ef9e2022-09-28 22:24:28 -040052 const accountRouter = Router({ mergeParams: true });
53 router.use('/accounts/:accountId', checkAccount, accountRouter);
Adrien Béraude74741b2021-04-19 13:22:54 -040054
simond47ef9e2022-09-28 22:24:28 -040055 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éraud35e7d7c2021-04-13 03:28:39 -040065
simond47ef9e2022-09-28 22:24:28 -040066 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éraud0cb76c92021-04-07 19:59:08 -040075
simond47ef9e2022-09-28 22:24:28 -040076 // 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éraud86986032021-04-25 12:04:53 -040085
simond47ef9e2022-09-28 22:24:28 -040086 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 });
idillon531b6f22022-09-16 14:02:00 -040095
simond47ef9e2022-09-28 22:24:28 -040096 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éraud6ecaa402021-04-06 17:37:25 -0400101
simond47ef9e2022-09-28 22:24:28 -0400102 console.log(rep);
103 res.json(rep);
104 } else res.status(404).end();
105 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400106
simond47ef9e2022-09-28 22:24:28 -0400107 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éraud2b3c2cd2022-09-18 14:24:33 -0400116
simond47ef9e2022-09-28 22:24:28 -0400117 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éraud2b3c2cd2022-09-18 14:24:33 -0400126
simond47ef9e2022-09-28 22:24:28 -0400127 // 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éraud2b3c2cd2022-09-18 14:24:33 -0400133
simond47ef9e2022-09-28 22:24:28 -0400134 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éraud2b3c2cd2022-09-18 14:24:33 -0400139
simond47ef9e2022-09-28 22:24:28 -0400140 // 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éraud2b3c2cd2022-09-18 14:24:33 -0400154 )
simond47ef9e2022-09-28 22:24:28 -0400155 );
156 //res.json(account.getConversations())
157 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400158
simond47ef9e2022-09-28 22:24:28 -0400159 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éraud2b3c2cd2022-09-18 14:24:33 -0400236 })
simond47ef9e2022-09-28 22:24:28 -0400237 .catch((e) => {
238 res.status(404).json({});
239 });
240 });
idillon531b6f22022-09-16 14:02:00 -0400241
simond47ef9e2022-09-28 22:24:28 -0400242 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éraud2b3c2cd2022-09-18 14:24:33 -0400250 })
simond47ef9e2022-09-28 22:24:28 -0400251 .catch((e) => {
252 res.status(404).json({});
253 });
254 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400255
simond47ef9e2022-09-28 22:24:28 -0400256 return router;
257 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400258}
259
simond47ef9e2022-09-28 22:24:28 -0400260export default JamiRestApi;