From e690e310ae78c6e52420efa1e669f94f5705f56a Mon Sep 17 00:00:00 2001 From: Bas950 Date: Sun, 4 Dec 2022 19:04:29 +0100 Subject: [PATCH 1/2] fix(PMD-94): make it work for multi presence input --- .../v4/fields/availablePresenceLanguages.ts | 106 +++++++++++------- 1 file changed, 68 insertions(+), 38 deletions(-) diff --git a/worker/src/v4/fields/availablePresenceLanguages.ts b/worker/src/v4/fields/availablePresenceLanguages.ts index 26e3b26f..95a01500 100644 --- a/worker/src/v4/fields/availablePresenceLanguages.ts +++ b/worker/src/v4/fields/availablePresenceLanguages.ts @@ -12,59 +12,89 @@ export const schema = gql` Presence, e.g. 'Netflix' """ presence: StringOrStringArray - ): [Language!]! + ): [PresenceLanguage!]! + } + + type PresenceLanguage { + """ + Presence, e.g. 'Netflix' + """ + presence: String! + """ + The available languages for the presence + """ + languages: [Language!]! } `; export async function resolver( _: any, args: { - presence?: string | string[]; + presence: string | string[]; }, { dataSources: { strings } }: { dataSources: { strings: Strings } } ) { - const fetchedStrings = await strings.get(args), - englishStrings = fetchedStrings.find(s => s.lang === "en-US") ?? { - translations: {} - }, - // String count of the English strings by first name - englishStringCount: { - [key: string]: number; - } = {}; + args.presence = Array.isArray(args.presence) + ? args.presence + : [args.presence]; - for (const key of Object.keys(englishStrings.translations)) { - const [firstName] = key.split("."); - englishStringCount[firstName] = (englishStringCount[firstName] ?? 0) + 1; - } + const returnObject: { + [service: string]: { + lang: string; + nativeName: string; + direction: "ltr" | "rtl"; + }[]; + } = {}; - return fetchedStrings - .filter(s => { - const fetchedStringCount: { + for (const presence of args.presence) { + const fetchedStrings = await strings.get({ presence }), + englishStrings = fetchedStrings.find(s => s.lang === "en-US") ?? { + translations: {} + }, + // String count of the English strings by first name + englishStringCount: { [key: string]: number; } = {}; - for (const key of Object.keys(s.translations)) { - const [firstName] = key.split("."); - fetchedStringCount[firstName] = - (fetchedStringCount[firstName] ?? 0) + 1; - } + for (const key of Object.keys(englishStrings.translations)) { + const [firstName] = key.split("."); + englishStringCount[firstName] = (englishStringCount[firstName] ?? 0) + 1; + } + + returnObject[presence] = fetchedStrings + .filter(s => { + const fetchedStringCount: { + [key: string]: number; + } = {}; + + for (const key of Object.keys(s.translations)) { + const [firstName] = key.split("."); + fetchedStringCount[firstName] = + (fetchedStringCount[firstName] ?? 0) + 1; + } - // Check if the fetched language has 60% of the strings translated per first name - for (const firstName of Object.keys(englishStringCount)) { - const sixtyPercent = englishStringCount[firstName] * 0.6; + // Check if the fetched language has 60% of the strings translated per first name + for (const firstName of Object.keys(englishStringCount)) { + const sixtyPercent = englishStringCount[firstName] * 0.6; - if ( - !(firstName in fetchedStringCount) || - fetchedStringCount[firstName] < sixtyPercent - ) - return false; - } + if ( + !(firstName in fetchedStringCount) || + fetchedStringCount[firstName] < sixtyPercent + ) + return false; + } + + return true; + }) + .map(s => ({ + lang: s.lang, + nativeName: s.nativeName, + direction: s.direction + })); + } - return true; - }) - .map(s => ({ - lang: s.lang, - nativeName: s.nativeName, - direction: s.direction - })); + return Object.entries(returnObject).map(([presence, languages]) => ({ + presence, + languages + })); } From be0309e0f26654cd0096258bf9001b13eff34730 Mon Sep 17 00:00:00 2001 From: Bas950 Date: Sun, 4 Dec 2022 19:04:36 +0100 Subject: [PATCH 2/2] chore: bump version --- master/package.json | 2 +- package.json | 2 +- worker/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/master/package.json b/master/package.json index 81bd0b7c..abd672a7 100644 --- a/master/package.json +++ b/master/package.json @@ -1,6 +1,6 @@ { "name": "PreMiD-API", - "version": "4.0.0", + "version": "4.2.0", "main": "index.js", "license": "MPL-2.0", "scripts": { diff --git a/package.json b/package.json index 4273825d..e2f634b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "PreMiD-API", - "version": "4.0.1", + "version": "4.2.0", "main": "index.js", "license": "MPL-2.0", "scripts": { diff --git a/worker/package.json b/worker/package.json index cbbd1205..2fc954d6 100644 --- a/worker/package.json +++ b/worker/package.json @@ -1,6 +1,6 @@ { "name": "PreMiD-API", - "version": "4.1.0", + "version": "4.2.0", "main": "index.js", "license": "MPL-2.0", "scripts": {