From 5587d56598fac20611ffcb8179eb6d5ed185c76f Mon Sep 17 00:00:00 2001 From: Avris Date: Tue, 7 Sep 2021 21:01:41 +0200 Subject: [PATCH] #264 [sources] handle multiple translations of the same source --- server/routes/sources.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/server/routes/sources.js b/server/routes/sources.js index 563a3b9d..059479d6 100644 --- a/server/routes/sources.js +++ b/server/routes/sources.js @@ -19,28 +19,38 @@ const approve = async (db, id) => { `); } -const linkOtherVersions = async (req, sources) => { - const keys = new Set(sources.filter(s => !!s && s.key).map(s => `'` + clearKey(s.key) + `'`)); +const keyGetMain = (key) => { + return key.split('/')[0]; +} - const otherVersions = await req.db.all(SQL` +const linkOtherVersions = async (req, sources) => { + const keys = new Set(sources.filter(s => !!s && s.key).map(s => keyGetMain(clearKey(s.key)))); + + let sql = SQL` SELECT s.*, u.username AS submitter FROM sources s LEFT JOIN users u ON s.submitter_id = u.id WHERE s.locale != ${global.config.locale} AND s.deleted = 0 AND s.approved >= ${req.isGranted('sources') ? 0 : 1} - AND s.key IN (`.append([...keys].join(',')).append(SQL`) - `)); + AND (`; + for (let key of keys) { + sql = sql.append(SQL`s.key = ${key} OR s.key LIKE ${key + '/%'} OR `) + } + sql = sql.append(SQL`0=1)`); + + const otherVersions = await req.db.all(sql); const otherVersionsMap = {}; otherVersions.forEach(version => { - if (otherVersionsMap[version.key] === undefined) { - otherVersionsMap[version.key] = []; + const k = keyGetMain(version.key); + if (otherVersionsMap[k] === undefined) { + otherVersionsMap[k] = []; } - otherVersionsMap[version.key].push(version); + otherVersionsMap[k].push(version); }); return sources.map(s => { - s.versions = s.key ? otherVersionsMap[s.key] || [] : []; + s.versions = s.key ? otherVersionsMap[keyGetMain(s.key)] || [] : []; return s; }); };