blob: c5f59bba90c2121efbf54793a50beca555be02d8 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
simond47ef9e2022-09-28 22:24:28 -040018import { Router } from 'express';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040019
20class JamiRestApi {
simond47ef9e2022-09-28 22:24:28 -040021 constructor(jami) {
22 this.jami = jami;
23 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040024
simond47ef9e2022-09-28 22:24:28 -040025 getRouter() {
26 const router = Router({ mergeParams: true });
27 //router.use(express.json())
Adrien Béraud6ecaa402021-04-06 17:37:25 -040028
simond47ef9e2022-09-28 22:24:28 -040029 // Accounts
30 router.get('/accounts', async (req, res) => {
31 console.log('Get account list');
32 let accounts = this.jami.getAccountList();
33 if (req.user.accountFilter) accounts = accounts.filter((account) => req.user.accountFilter(account.getId()));
34 res.json(await Promise.all(accounts.map(async (account) => await account.getSummary())));
35 });
Adrien Béraud6ecaa402021-04-06 17:37:25 -040036
simond47ef9e2022-09-28 22:24:28 -040037 const checkCanCreateAccounts = (req, res, next) => {
38 console.log(`checkCanCreateAccounts ${req.params.accountId} for ${req.user.id}`);
39 if (req.user && !req.user.accountFilter) {
40 return next();
41 }
42 res.status(403).end();
43 };
44
45 router.post('/accounts', checkCanCreateAccounts, async (req, res) => {
46 console.log('Create new account');
47 console.log(req.body);
48 try {
49 const accountId = await this.jami.addAccount(req.body);
50 if (req.body.registerName) {
51 this.jami
52 .registerName(accountId, '', req.body.registerName)
53 .then((result) => console.log('Name registrtion result: ' + result));
Adrien Béraud88a52442021-04-26 12:11:41 -040054 }
simond47ef9e2022-09-28 22:24:28 -040055 res.json({ accountId });
56 } catch (e) {
57 res.status(400).json({ error: e });
58 }
59 });
Adrien Béraud88a52442021-04-26 12:11:41 -040060
simond47ef9e2022-09-28 22:24:28 -040061 const checkAccount = (req, res, next) => {
62 console.log(`checkAccount ${req.params.accountId} for ${req.user.id}`);
63 if (req.user && (!req.user.accountFilter || req.user.accountFilter(req.params.accountId))) {
64 return next();
65 }
66 res.status(403).end();
67 };
Adrien Béraud88a52442021-04-26 12:11:41 -040068
simond47ef9e2022-09-28 22:24:28 -040069 const accountRouter = Router({ mergeParams: true });
70 router.use('/accounts/:accountId', checkAccount, accountRouter);
Adrien Béraude74741b2021-04-19 13:22:54 -040071
simond47ef9e2022-09-28 22:24:28 -040072 accountRouter.get('/', async (req, res) => {
73 console.log(`Get account ${req.params.accountId}`);
74 const account = this.jami.getAccount(req.params.accountId);
75 if (account) {
76 account.defaultModerators = this.jami.getDefaultModerators(account.getId());
77 const obj = await account.getObject();
78 obj.devices = this.jami.getDevices(req.params.accountId);
79 res.json(obj);
80 } else res.status(404).end();
81 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040082
simond47ef9e2022-09-28 22:24:28 -040083 accountRouter.post('/', async (req, res) => {
84 console.log(`Set account details ${req.params.accountId}`);
85 const account = this.jami.getAccount(req.params.accountId);
86 if (account) {
87 const newDetails = account.updateDetails(req.body);
88 this.jami.setAccountDetails(account.getId(), newDetails);
89 res.status(200).end();
90 } else res.status(404).end();
91 });
Adrien Béraud0cb76c92021-04-07 19:59:08 -040092
simond47ef9e2022-09-28 22:24:28 -040093 // Contacts
94 accountRouter.get('/contacts', (req, res) => {
95 console.log(`Get account ${req.params.accountId}`);
96 const account = this.jami.getAccount(req.params.accountId);
97 if (account) {
98 let rep = account.getContacts();
99 res.json(rep);
100 } else res.status(404).end();
101 });
Adrien Béraud86986032021-04-25 12:04:53 -0400102
simond47ef9e2022-09-28 22:24:28 -0400103 accountRouter.get('/contacts/:contactId', (req, res) => {
104 console.log(`Get account details fot ${req.params.accountId}`);
105 const account = this.jami.getAccount(req.params.accountId);
106 const uri = req.params.uri;
107 if (account) {
108 let rep = account.getContactDetails(uri);
109 res.json(rep);
110 } else res.status(404).end();
111 });
idillon531b6f22022-09-16 14:02:00 -0400112
simond47ef9e2022-09-28 22:24:28 -0400113 accountRouter.get('/contacts/details/:contactId', (req, res) => {
114 console.log(`Get contact ${req.params.contactId} details for ${req.params.accountId}`);
115 const account = this.jami.getAccount(req.params.accountId);
116 if (account) {
117 let rep = this.jami.getContactDetails(req.params.accountId, req.params.contactId);
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400118
simond47ef9e2022-09-28 22:24:28 -0400119 console.log(rep);
120 res.json(rep);
121 } else res.status(404).end();
122 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400123
simond47ef9e2022-09-28 22:24:28 -0400124 accountRouter.delete('/contacts/remove/:contactId', async (req, res) => {
125 console.log('REMOVED CONTACT: ', req.params.contactId);
126 const account = this.jami.getAccount(req.params.accountId);
127 if (account) {
128 let rep = this.jami.removeContact(req.params.accountId, req.params.contactId);
129 res.json(rep);
130 } else res.status(404).end();
131 res.status(200).end();
132 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400133
simond47ef9e2022-09-28 22:24:28 -0400134 accountRouter.delete('/contacts/block/:contactId/', async (req, res) => {
135 console.log('REMOVED AND BLOCKED CONTACT: ', req.params.contactId);
136 const account = this.jami.getAccount(req.params.accountId);
137 if (account) {
138 let rep = this.jami.blockContact(req.params.accountId, req.params.contactId);
139 res.json(rep);
140 } else res.status(404).end();
141 res.status(200).end();
142 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400143
simond47ef9e2022-09-28 22:24:28 -0400144 // Default modertors
145 accountRouter.put('/defaultModerators/:contactId', async (req, res) => {
146 console.log(`Adding default moderator ${req.params.contactId} to account ${req.params.accountId}`);
147 this.jami.addDefaultModerator(req.params.accountId, req.params.contactId);
148 res.status(200).end();
149 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400150
simond47ef9e2022-09-28 22:24:28 -0400151 accountRouter.delete('/defaultModerators/:contactId', async (req, res) => {
152 console.log(`Removing default moderator to account ${req.params.accountId}`);
153 this.jami.removeDefaultModerator(req.params.accountId, req.params.contactId);
154 res.status(200).end();
155 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400156
simond47ef9e2022-09-28 22:24:28 -0400157 // Conversations
158 accountRouter.get('/conversations', async (req, res, next) => {
159 console.log(`Get conversations for account ${req.params.accountId}`);
160 const account = this.jami.getAccount(req.params.accountId);
161 if (!account) return res.sendStatus(404);
162 const conversations = account.getConversations();
163 res.json(
164 await Promise.all(
165 Object.keys(conversations).map(
166 async (conversationId) =>
167 await conversations[conversationId].getObject({
168 memberFilter: (member) => member.contact.getUri() !== account.getUri(),
169 })
170 )
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400171 )
simond47ef9e2022-09-28 22:24:28 -0400172 );
173 //res.json(account.getConversations())
174 });
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400175
simond47ef9e2022-09-28 22:24:28 -0400176 accountRouter.post('/conversations', (req, res) => {
177 console.log(`Create conversations for account, contact ${req.params.accountId}`);
178 // console.log(req.body)
179 const account = this.jami.getAccount(req.params.accountId);
180 if (!account) return res.sendStatus(404);
181 if (req.body.members.length === 1) {
182 const details = this.jami.addContact(req.params.accountId, req.body.members[0]);
183 res.json(details);
184 } else res.status(400).end();
185 });
186
187 accountRouter.post('/conversations/:conversationId', async (req, res) => {
188 console.log(`Sending message to ${req.params.conversationId} for account ${req.params.accountId}`);
189 this.jami.sendMessage(req.params.accountId, req.params.conversationId, req.body.message);
190 res.status(200).end();
191 });
192
193 accountRouter.get('/conversations/:conversationId', async (req, res) => {
194 console.log(`Get conversation ${req.params.conversationId} for account ${req.params.accountId}`);
195 const account = this.jami.getAccount(req.params.accountId);
196 if (!account) return res.sendStatus(404);
197 const conversation = account.getConversation(req.params.conversationId);
198 if (!conversation) res.status(404).end();
199 else {
200 res.json(
201 await conversation.getObject({
202 memberFilter: (member) => member.contact.getUri() !== account.getUri(),
203 })
204 );
205 }
206 });
207
208 accountRouter.get('/conversations/:conversationId/messages', async (req, res) => {
209 console.log(`Get messages for conversation ${req.params.conversationId} for account ${req.params.accountId}`);
210 try {
211 const messages = await this.jami.loadMessages(req.params.accountId, req.params.conversationId);
212 res.json(messages).end();
213 } catch (e) {
214 res.status(400).json({ error: e.message });
215 }
216 });
217
218 // Calls
219
220 accountRouter.get('/calls', async (req, res) => {
221 console.log(`Get calls for account ${req.params.accountId}`);
222 try {
223 const calls = await this.jami.getCalls(req.params.accountId);
224 res.json(calls).end();
225 } catch (e) {
226 res.status(400).json({ error: e.message });
227 }
228 });
229
230 accountRouter.get('/calls/:callId', async (req, res) => {
231 console.log(`Get call ${req.params.callId} for account ${req.params.accountId}`);
232 try {
233 const messages = await this.jami.getCall(req.params.accountId, req.params.callId);
234 res.json(messages).end();
235 } catch (e) {
236 res.status(400).json({ error: e.message });
237 }
238 });
239
240 // Nameserver
241 const nsRouter = Router({ mergeParams: true });
242 accountRouter.use('/ns', nsRouter); // use account nameserver
243 router.use('/ns', nsRouter); // use default nameserver
244
245 nsRouter.get(['/name/:nameQuery'], (req, res, next) => {
246 console.log(`Name lookup ${req.params.nameQuery}`);
247 this.jami
248 .lookupName(req.params.accountId || '', req.params.nameQuery)
249 .then((result) => {
simon73ef58d2022-10-27 00:25:55 -0400250 if (result.state === 0) res.json(result);
251 else if (result.state === 1) res.status(400).json({});
simond47ef9e2022-09-28 22:24:28 -0400252 else res.status(404).json({});
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400253 })
simond47ef9e2022-09-28 22:24:28 -0400254 .catch((e) => {
255 res.status(404).json({});
256 });
257 });
idillon531b6f22022-09-16 14:02:00 -0400258
simond47ef9e2022-09-28 22:24:28 -0400259 nsRouter.get(['/addr/:addrQuery'], (req, res, next) => {
260 console.log(`Address lookup ${req.params.addrQuery}`);
261 this.jami
262 .lookupAddress(req.params.accountId || '', req.params.addrQuery)
263 .then((result) => {
simon73ef58d2022-10-27 00:25:55 -0400264 if (result.state === 0) res.json(result);
265 else if (result.state === 1) res.status(400).json({});
simond47ef9e2022-09-28 22:24:28 -0400266 else res.status(404).json({});
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400267 })
simond47ef9e2022-09-28 22:24:28 -0400268 .catch((e) => {
269 res.status(404).json({});
270 });
271 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400272
simond47ef9e2022-09-28 22:24:28 -0400273 return router;
274 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400275}
276
simond47ef9e2022-09-28 22:24:28 -0400277export default JamiRestApi;