diff --git a/components/Source.vue b/components/Source.vue index 084d3548..51c1cd61 100644 --- a/components/Source.vue +++ b/components/Source.vue @@ -1,7 +1,9 @@ @@ -69,6 +84,7 @@ import {pronounLibrary} from "../src/data"; export default { + name: 'Source', props: { source: { required: true }, manage: { type: Boolean }, diff --git a/locale/en/translations.suml b/locale/en/translations.suml index 19089794..bb157312 100644 --- a/locale/en/translations.suml +++ b/locale/en/translations.suml @@ -96,6 +96,7 @@ sources: moderation: 'Submissions will have to get approved before getting published.' key: 'Key' keyInfo: 'Identifier for linking sources between language versions and linking with the dictionary' + otherVersions: 'In other languages' nouns: diff --git a/locale/es/translations.suml b/locale/es/translations.suml index 2fa30534..a3782f79 100644 --- a/locale/es/translations.suml +++ b/locale/es/translations.suml @@ -96,6 +96,7 @@ sources: moderation: 'Los envíos deben ser aprobados antes de ser publicados.' key: 'Key' # TODO keyInfo: 'Identifier for linking sources between language versions and linking with the dictionary' # TODO + otherVersions: 'In other languages' # TODO nouns: header: 'Diccionario' diff --git a/locale/pl/translations.suml b/locale/pl/translations.suml index 0fe6e4e4..973b968e 100644 --- a/locale/pl/translations.suml +++ b/locale/pl/translations.suml @@ -195,6 +195,7 @@ sources: moderation: 'Propozycje będą musiały zostać zatwierdzone przed opublikowaniem.' key: 'Klucz' keyInfo: 'Identyfikator do łączenia tekstów między wersjami językowymi i łączenia ze słownikiem' + otherVersions: 'W innych językach' nouns: header: 'Słowniki' diff --git a/locale/zh/translations.suml b/locale/zh/translations.suml index 2748156a..9e3b1c98 100644 --- a/locale/zh/translations.suml +++ b/locale/zh/translations.suml @@ -94,6 +94,7 @@ sources: moderation: '提交的內容必須先獲得批准才能發布。' key: 'Key' # TODO keyInfo: 'Identifier for linking sources between language versions and linking with the dictionary' # TODO + otherVersions: 'In other languages' # TODO nouns: header: '字典' diff --git a/server/routes/sources.js b/server/routes/sources.js index 859f51df..b2abd118 100644 --- a/server/routes/sources.js +++ b/server/routes/sources.js @@ -18,27 +18,53 @@ const approve = async (db, id) => { `); } +const linkOtherVersions = async (req, sources) => { + const keys = new Set(sources.filter(s => !!s && s.key).map(s => `'` + s.key + `'`)); + + const otherVersions = await req.db.all(SQL` + SELECT s.*, u.username AS submitter FROM sources s + LEFT JOIN users u ON s.submitter_id = u.id + WHERE s.locale != ${req.config.locale} + AND s.deleted = 0 + AND s.approved >= ${req.isGranted('sources') ? 0 : 1} + AND s.key IN (`.append([...keys].join(',')).append(SQL`) + `)); + + const otherVersionsMap = {}; + otherVersions.forEach(version => { + if (otherVersionsMap[version.key] === undefined) { + otherVersionsMap[version.key] = []; + } + otherVersionsMap[version.key].push(version); + }); + + return sources.map(s => { + s.versions = s.key ? otherVersionsMap[s.key] || [] : []; + return s; + }); +}; + const router = Router(); router.get('/sources', async (req, res) => { - return res.json(await req.db.all(SQL` + return res.json(await linkOtherVersions(req, await req.db.all(SQL` SELECT s.*, u.username AS submitter FROM sources s LEFT JOIN users u ON s.submitter_id = u.id WHERE s.locale = ${req.config.locale} AND s.deleted = 0 AND s.approved >= ${req.isGranted('sources') ? 0 : 1} - `)); + `))); }); router.get('/sources/:id', async (req, res) => { - return res.json(await req.db.all(SQL` + return res.json(await linkOtherVersions(req, await req.db.all(SQL` SELECT s.*, u.username AS submitter FROM sources s LEFT JOIN users u ON s.submitter_id = u.id WHERE s.locale = ${req.config.locale} AND s.deleted = 0 AND s.approved >= ${req.isGranted('sources') ? 0 : 1} AND s.id = ${req.params.id} - `)); + `))); }); router.post('/sources/submit', async (req, res) => { diff --git a/src/classes.js b/src/classes.js index 4ecee205..58ab5266 100644 --- a/src/classes.js +++ b/src/classes.js @@ -99,7 +99,12 @@ function clone(mainObject) { } export class Source { - constructor ({id, pronouns, type, author, title, extra, year, fragments = '', comment = null, link = null, key = null, submitter = null, approved, base_id = null,}) { + constructor ({ + id, pronouns, type, author, title, extra, year, fragments = '', + comment = null, link = null, + submitter = null, approved, base_id = null, + key = null, versions = [], locale = config.locale, + }) { this.id = id; this.pronouns = pronouns ? pronouns.split(';') : []; this.type = type; @@ -110,10 +115,12 @@ export class Source { this.fragments = fragments ? fragments.replace(/\|/g, '\n').split('@') : []; this.comment = comment; this.link = link; - this.key = key; this.submitter = submitter; this.approved = approved; this.base_id = base_id; + this.key = key; + this.versions = versions.map(v => new Source(v)); + this.locale = locale; } static get TYPES() {