2020-11-10 14:41:56 -08:00
|
|
|
import { Router } from 'express';
|
|
|
|
import { loadTsv } from '../loader';
|
2020-11-10 15:47:44 -08:00
|
|
|
import {buildPronoun, parsePronouns} from "../../src/buildPronoun";
|
2021-06-09 05:47:08 -07:00
|
|
|
import {buildList, handleErrorAsync} from "../../src/helpers";
|
2020-11-10 14:41:56 -08:00
|
|
|
import {Example} from "../../src/classes";
|
2021-12-14 15:45:28 -08:00
|
|
|
import {caches} from '../../src/cache';
|
|
|
|
import md5 from "js-md5";
|
|
|
|
import assert from 'assert';
|
|
|
|
import fetch from "node-fetch";
|
2020-11-10 14:41:56 -08:00
|
|
|
|
|
|
|
const buildExample = e => new Example(
|
|
|
|
Example.parse(e.singular),
|
|
|
|
Example.parse(e.plural || e.singular),
|
|
|
|
e.isHonorific,
|
|
|
|
)
|
|
|
|
|
|
|
|
const requestExamples = r => {
|
|
|
|
if (!r || !r.length) {
|
2020-11-10 15:47:44 -08:00
|
|
|
return loadTsv('pronouns/examples');
|
2020-11-10 14:41:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildList(function* () {
|
|
|
|
for (let rr of r) {
|
|
|
|
let [singular, plural, isHonorific] = rr.split('|');
|
|
|
|
yield { singular, plural, isHonorific: !!isHonorific};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const addExamples = (pronoun, examples) => {
|
|
|
|
return buildList(function* () {
|
|
|
|
for (let example of examples) {
|
|
|
|
yield buildExample(example).format(pronoun);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/pronouns', handleErrorAsync(async (req, res) => {
|
2020-11-10 15:47:44 -08:00
|
|
|
const pronouns = parsePronouns(loadTsv('pronouns/pronouns'));
|
|
|
|
for (let pronoun in pronouns) {
|
|
|
|
if (!pronouns.hasOwnProperty(pronoun)) { continue; }
|
|
|
|
pronouns[pronoun].examples = addExamples(pronouns[pronoun], requestExamples(req.query.examples))
|
2020-11-10 14:41:56 -08:00
|
|
|
}
|
2020-11-10 15:47:44 -08:00
|
|
|
return res.json(pronouns);
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-10 14:41:56 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/pronouns/:pronoun*', handleErrorAsync(async (req, res) => {
|
2020-11-10 15:47:44 -08:00
|
|
|
const pronoun = buildPronoun(
|
|
|
|
parsePronouns(loadTsv('pronouns/pronouns')),
|
2020-11-10 14:41:56 -08:00
|
|
|
req.params.pronoun + req.params[0],
|
|
|
|
);
|
|
|
|
if (pronoun) {
|
|
|
|
pronoun.examples = addExamples(pronoun, requestExamples(req.query.examples))
|
2021-12-14 13:12:40 -08:00
|
|
|
pronoun.name = pronoun.name();
|
2020-11-10 14:41:56 -08:00
|
|
|
}
|
|
|
|
return res.json(pronoun);
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-10 14:41:56 -08:00
|
|
|
|
2021-12-14 13:12:40 -08:00
|
|
|
router.get('/pronouns-name/:pronoun*', handleErrorAsync(async (req, res) => {
|
|
|
|
const pronoun = buildPronoun(
|
|
|
|
parsePronouns(loadTsv('pronouns/pronouns')),
|
|
|
|
req.params.pronoun + req.params[0],
|
|
|
|
);
|
2021-12-14 13:20:36 -08:00
|
|
|
if (!pronoun) {
|
|
|
|
return res.status(404).json({error: 'Not found'});
|
|
|
|
}
|
2021-12-14 13:12:40 -08:00
|
|
|
return res.json(pronoun.name());
|
|
|
|
}));
|
|
|
|
|
2021-12-14 15:45:28 -08:00
|
|
|
if (global.config.locale === '_') {
|
|
|
|
router.get('/remote-pronouns-name/:locale/:pronoun*', handleErrorAsync(async (req, res) => {
|
|
|
|
assert(req.locales.hasOwnProperty(req.params.locale));
|
|
|
|
const pronoun = req.params.pronoun + req.params[0];
|
|
|
|
const name = await caches.pronounNames(`${req.params.locale}/${md5(pronoun)}.txt`).fetch(async () => {
|
|
|
|
const res = await (await fetch(`${req.locales[req.params.locale].url}/api/pronouns-name/${encodeURIComponent(pronoun)}`)).json();
|
|
|
|
if (typeof(res) === 'object' && res.error) { return pronoun; }
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.json(name);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:41:56 -08:00
|
|
|
export default router;
|