blob: 6340ddbac9aa63850b098d9d7c9382338546f3f5 [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() {
ervinanoh99655642022-09-01 15:11:31 -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éraud0cb76c92021-04-07 19:59:08 -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) {
24 return next();
25 }
26 res.status(403).end()
27 }
28
29 router.post('/accounts', checkCanCreateAccounts, async (req, res) => {
30 console.log("Create new account")
31 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) {
35 this.jami.registerName(accountId, "", req.body.registerName)
36 .then(result => console.log("Name registrtion result: " + result))
37 }
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))) {
47 return next();
48 }
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())
ervinanohf1758a42022-09-14 14:52:51 -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
idillon531b6f22022-09-16 14:02:00 -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) {
idillon531b6f22022-09-16 14:02:00 -040071 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
idillon531b6f22022-09-16 14:02:00 -040078 accountRouter.get("/contacts", (req, res) => {
ervinanoh99655642022-09-01 15:11:31 -040079 console.log(`Get account ${req.params.accountId}`);
idillon531b6f22022-09-16 14:02:00 -040080 const account = this.jami.getAccount(req.params.accountId);
81 if (account) {
82 let rep = account.getContacts();
idillon531b6f22022-09-16 14:02:00 -040083 res.json(rep);
84 } else res.status(404).end();
85 });
86
87 accountRouter.get("/contacts/:contactId", (req, res) => {
idillon531b6f22022-09-16 14:02:00 -040088 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
ervinanoh8e918042022-09-06 10:30:59 -040097 accountRouter.get("/contacts/details/:contactId", (req, res) => {
98 console.log(
99 `Get contact ${req.params.contactId} details for ${req.params.accountId}`
100 );
101 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 );
ervinanoh99655642022-09-01 15:11:31 -0400107
ervinanoh8e918042022-09-06 10:30:59 -0400108 console.log(rep);
109 res.json(rep);
110 } else res.status(404).end();
111 });
ervinanoh99655642022-09-01 15:11:31 -0400112
ervinanoh8e918042022-09-06 10:30:59 -0400113 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 });
ervinanoh99655642022-09-01 15:11:31 -0400138
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) => {
idillon531b6f22022-09-16 14:02:00 -0400155 console.log(
ervinanoh99655642022-09-01 15:11:31 -0400156 `Removing default moderator to account ${req.params.accountId}`
idillon531b6f22022-09-16 14:02:00 -0400157 );
ervinanoh99655642022-09-01 15:11:31 -0400158 this.jami.removeDefaultModerator(
idillon531b6f22022-09-16 14:02:00 -0400159 req.params.accountId,
160 req.params.contactId
161 );
162 res.status(200).end();
ervinanoh99655642022-09-01 15:11:31 -0400163 }
164 );
idillon531b6f22022-09-16 14:02:00 -0400165
ervinanoh99655642022-09-01 15:11:31 -0400166 // 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(),
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400179 })
ervinanoh99655642022-09-01 15:11:31 -0400180 )
181 )
182 );
183 //res.json(account.getConversations())
184 });
idillon531b6f22022-09-16 14:02:00 -0400185
ervinanoh99655642022-09-01 15:11:31 -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
ervinanoh99655642022-09-01 15:11:31 -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
ervinanoh99655642022-09-01 15:11:31 -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 });
231
232 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 );
249
250 // Calls
251
252 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 });
261
262 accountRouter.get("/calls/:callId", async (req, res) => {
263 console.log(`Get call ${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 });
274
275 // Nameserver
276 const nsRouter = Router({ mergeParams: true });
277 accountRouter.use("/ns", nsRouter); // use account nameserver
278 router.use("/ns", nsRouter); // use default nameserver
279
280 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 });
293
294 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 });
307
308 return router;
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400309 }
310}
311
Adrien Béraude74741b2021-04-19 13:22:54 -0400312export default JamiRestApi