blob: 6e49b5221c14c8a93547107e29866788f3a5378a [file] [log] [blame]
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -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 */
18import { Router } from 'express';
19import asyncHandler from 'express-async-handler';
20import { HttpStatusCode } from 'jami-web-common';
21import { Container } from 'typedi';
22
23import { Jamid } from '../jamid/jamid.js';
24import { authenticateOptionalToken } from '../middleware/auth.js';
25
26const jamid = Container.get(Jamid);
27
28export const nameserverRouter = Router();
29
30nameserverRouter.use(authenticateOptionalToken);
31
32nameserverRouter.get(
33 '/username/:username',
34 asyncHandler(async (req, res) => {
35 const result = await jamid.lookupUsername(req.params.username, res.locals.accountId);
36 switch (result.state) {
37 case 0:
38 res.json(result);
39 break;
40 case 1:
41 res.sendStatus(HttpStatusCode.BadRequest);
42 break;
43 default:
44 res.sendStatus(HttpStatusCode.NotFound);
45 break;
46 }
47 })
48);
49
50nameserverRouter.get(
51 '/address/:address',
52 asyncHandler(async (req, res) => {
53 const result = await jamid.lookupAddress(req.params.address, res.locals.accountId);
54 switch (result.state) {
55 case 0:
56 res.json(result);
57 break;
58 case 1:
59 res.sendStatus(HttpStatusCode.BadRequest);
60 break;
61 default:
62 res.sendStatus(HttpStatusCode.NotFound);
63 break;
64 }
65 })
66);