blob: 415c2d177705b9e77225a01278023e30f8b0b273 [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';
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050020import { HttpStatusCode, RegisteredNameFoundState } from 'jami-web-common';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040021import { 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) {
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050037 case RegisteredNameFoundState.Found:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050038 res.send(result);
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040039 break;
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050040 case RegisteredNameFoundState.InvalidResponse:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050041 res.status(HttpStatusCode.BadRequest).send('Invalid username');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040042 break;
43 default:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050044 res.status(HttpStatusCode.NotFound).send('No such username found');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040045 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) {
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050055 case RegisteredNameFoundState.Found:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050056 res.send(result);
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040057 break;
Misha Krieger-Raynauld99e5a702022-11-30 22:36:52 -050058 case RegisteredNameFoundState.InvalidResponse:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050059 res.status(HttpStatusCode.BadRequest).send('Invalid address');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040060 break;
61 default:
Misha Krieger-Raynauldcb11bba2022-11-11 18:08:33 -050062 res.status(HttpStatusCode.NotFound).send('No such address found');
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040063 break;
64 }
65 })
66);