blob: cbe328627b325323345a968d1d3241c9a55a8218 [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
ervinanoh99655642022-09-01 15:11:31 -040097
98
99
100
101 // Default modertors
102 accountRouter.put("/defaultModerators/:contactId", async (req, res) => {
103 console.log(
104 `Adding default moderator ${req.params.contactId} to account ${req.params.accountId}`
105 );
106 this.jami.addDefaultModerator(
107 req.params.accountId,
108 req.params.contactId
109 );
110 res.status(200).end();
111 });
112
113 accountRouter.delete(
114 "/defaultModerators/:contactId",
115 async (req, res) => {
idillon531b6f22022-09-16 14:02:00 -0400116 console.log(
ervinanoh99655642022-09-01 15:11:31 -0400117 `Removing default moderator to account ${req.params.accountId}`
idillon531b6f22022-09-16 14:02:00 -0400118 );
ervinanoh99655642022-09-01 15:11:31 -0400119 this.jami.removeDefaultModerator(
idillon531b6f22022-09-16 14:02:00 -0400120 req.params.accountId,
121 req.params.contactId
122 );
123 res.status(200).end();
ervinanoh99655642022-09-01 15:11:31 -0400124 }
125 );
idillon531b6f22022-09-16 14:02:00 -0400126
ervinanoh99655642022-09-01 15:11:31 -0400127 // Conversations
128 accountRouter.get("/conversations", async (req, res, next) => {
129 console.log(`Get conversations for account ${req.params.accountId}`);
130 const account = this.jami.getAccount(req.params.accountId);
131 if (!account) return res.sendStatus(404);
132 const conversations = account.getConversations();
133 res.json(
134 await Promise.all(
135 Object.keys(conversations).map(
136 async (conversationId) =>
137 await conversations[conversationId].getObject({
138 memberFilter: (member) =>
139 member.contact.getUri() !== account.getUri(),
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400140 })
ervinanoh99655642022-09-01 15:11:31 -0400141 )
142 )
143 );
144 //res.json(account.getConversations())
145 });
idillon531b6f22022-09-16 14:02:00 -0400146
ervinanoh99655642022-09-01 15:11:31 -0400147 accountRouter.post("/conversations", (req, res) => {
148 console.log(
149 `Create conversations for account, contact ${req.params.accountId}`
150 );
151 // console.log(req.body)
152 const account = this.jami.getAccount(req.params.accountId);
153 if (!account) return res.sendStatus(404);
154 if (req.body.members.length === 1) {
155 const details = this.jami.addContact(
156 req.params.accountId,
157 req.body.members[0]
158 );
159 res.json(details);
160 } else res.status(400).end();
161 });
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400162
ervinanoh99655642022-09-01 15:11:31 -0400163 accountRouter.post("/conversations/:conversationId", async (req, res) => {
164 console.log(
165 `Sending message to ${req.params.conversationId} for account ${req.params.accountId}`
166 );
167 this.jami.sendMessage(
168 req.params.accountId,
169 req.params.conversationId,
170 req.body.message
171 );
172 res.status(200).end();
173 });
Adrien Béraud88a52442021-04-26 12:11:41 -0400174
ervinanoh99655642022-09-01 15:11:31 -0400175 accountRouter.get("/conversations/:conversationId", async (req, res) => {
176 console.log(
177 `Get conversation ${req.params.conversationId} for account ${req.params.accountId}`
178 );
179 const account = this.jami.getAccount(req.params.accountId);
180 if (!account) return res.sendStatus(404);
181 const conversation = account.getConversation(req.params.conversationId);
182 if (!conversation) res.status(404).end();
183 else {
184 res.json(
185 await conversation.getObject({
186 memberFilter: (member) =>
187 member.contact.getUri() !== account.getUri(),
188 })
189 );
190 }
191 });
192
193 accountRouter.get(
194 "/conversations/:conversationId/messages",
195 async (req, res) => {
196 console.log(
197 `Get messages for conversation ${req.params.conversationId} for account ${req.params.accountId}`
198 );
199 try {
200 const messages = await this.jami.loadMessages(
201 req.params.accountId,
202 req.params.conversationId
203 );
204 res.json(messages).end();
205 } catch (e) {
206 res.status(400).json({ error: e.message });
207 }
208 }
209 );
210
211 // Calls
212
213 accountRouter.get("/calls", async (req, res) => {
214 console.log(`Get calls for account ${req.params.accountId}`);
215 try {
216 const calls = await this.jami.getCalls(req.params.accountId);
217 res.json(calls).end();
218 } catch (e) {
219 res.status(400).json({ error: e.message });
220 }
221 });
222
223 accountRouter.get("/calls/:callId", async (req, res) => {
224 console.log(`Get call ${callId} for account ${req.params.accountId}`);
225 try {
226 const messages = await this.jami.getCall(
227 req.params.accountId,
228 req.params.callId
229 );
230 res.json(messages).end();
231 } catch (e) {
232 res.status(400).json({ error: e.message });
233 }
234 });
235
236 // Nameserver
237 const nsRouter = Router({ mergeParams: true });
238 accountRouter.use("/ns", nsRouter); // use account nameserver
239 router.use("/ns", nsRouter); // use default nameserver
240
241 nsRouter.get(["/name/:nameQuery"], (req, res, next) => {
242 console.log(`Name lookup ${req.params.nameQuery}`);
243 this.jami
244 .lookupName(req.params.accountId || "", req.params.nameQuery)
245 .then((result) => {
246 if (result.state == 0) res.json(result);
247 else if (result.state == 1) res.status(400).json({});
248 else res.status(404).json({});
249 })
250 .catch((e) => {
251 res.status(404).json({});
252 });
253 });
254
255 nsRouter.get(["/addr/:addrQuery"], (req, res, next) => {
256 console.log(`Address lookup ${req.params.addrQuery}`);
257 this.jami
258 .lookupAddress(req.params.accountId || "", req.params.addrQuery)
259 .then((result) => {
260 if (result.state == 0) res.json(result);
261 else if (result.state == 1) res.status(400).json({});
262 else res.status(404).json({});
263 })
264 .catch((e) => {
265 res.status(404).json({});
266 });
267 });
268
269 return router;
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400270 }
271}
272
Adrien Béraude74741b2021-04-19 13:22:54 -0400273export default JamiRestApi