diff options
author | Vijay A <vraravam@users.noreply.github.com> | 2022-05-15 01:44:18 -0500 |
---|---|---|
committer | Vijay Aravamudhan <vraravam@users.noreply.github.com> | 2022-05-15 19:05:55 -0500 |
commit | 082b64b6c40fcab3ecf303f53ce83e7753894a32 (patch) | |
tree | 92626869949cc671effce89cfbcd52092243de5c /src/internal-server | |
parent | fix: revert "Typescript conversion" (#153) (diff) | |
download | ferdium-app-082b64b6c40fcab3ecf303f53ce83e7753894a32.tar.gz ferdium-app-082b64b6c40fcab3ecf303f53ce83e7753894a32.tar.zst ferdium-app-082b64b6c40fcab3ecf303f53ce83e7753894a32.zip |
Extract utility functions for JSON parsing
Diffstat (limited to 'src/internal-server')
5 files changed, 28 insertions, 58 deletions
diff --git a/src/internal-server/app/Controllers/Http/RecipeController.js b/src/internal-server/app/Controllers/Http/RecipeController.js index e88a89a0b..eac34aa5c 100644 --- a/src/internal-server/app/Controllers/Http/RecipeController.js +++ b/src/internal-server/app/Controllers/Http/RecipeController.js | |||
@@ -6,6 +6,7 @@ const Env = use('Env'); | |||
6 | const fetch = require('node-fetch'); | 6 | const fetch = require('node-fetch'); |
7 | const debug = require('../../../../preload-safe-debug')('Ferdium:internalServer:RecipeController'); | 7 | const debug = require('../../../../preload-safe-debug')('Ferdium:internalServer:RecipeController'); |
8 | const { LIVE_FERDIUM_API } = require('../../../../config'); | 8 | const { LIVE_FERDIUM_API } = require('../../../../config'); |
9 | const { convertToJSON } = require('../../../../jsUtils'); | ||
9 | const { API_VERSION } = require('../../../../environment-remote'); | 10 | const { API_VERSION } = require('../../../../environment-remote'); |
10 | 11 | ||
11 | const RECIPES_URL = `${LIVE_FERDIUM_API}/${API_VERSION}/recipes`; | 12 | const RECIPES_URL = `${LIVE_FERDIUM_API}/${API_VERSION}/recipes`; |
@@ -14,13 +15,13 @@ class RecipeController { | |||
14 | // List official and custom recipes | 15 | // List official and custom recipes |
15 | async list({ response }) { | 16 | async list({ response }) { |
16 | const recipesUrlFetch = await fetch(RECIPES_URL); | 17 | const recipesUrlFetch = await fetch(RECIPES_URL); |
17 | const officialRecipes = JSON.parse(await recipesUrlFetch.text()); | 18 | const officialRecipes = convertToJSON(await recipesUrlFetch.text()); |
18 | const allRecipes = await Recipe.all(); | 19 | const allRecipes = await Recipe.all(); |
19 | const customRecipesArray = allRecipes.rows; | 20 | const customRecipesArray = allRecipes.rows; |
20 | const customRecipes = customRecipesArray.map(recipe => ({ | 21 | const customRecipes = customRecipesArray.map(recipe => ({ |
21 | id: recipe.recipeId, | 22 | id: recipe.recipeId, |
22 | name: recipe.name, | 23 | name: recipe.name, |
23 | ...JSON.parse(recipe.data), | 24 | ...convertToJSON(recipe.data), |
24 | })); | 25 | })); |
25 | 26 | ||
26 | const recipes = [...officialRecipes, ...customRecipes]; | 27 | const recipes = [...officialRecipes, ...customRecipes]; |
@@ -53,7 +54,7 @@ class RecipeController { | |||
53 | results = dbResults.map(recipe => ({ | 54 | results = dbResults.map(recipe => ({ |
54 | id: recipe.recipeId, | 55 | id: recipe.recipeId, |
55 | name: recipe.name, | 56 | name: recipe.name, |
56 | ...JSON.parse(recipe.data), | 57 | ...convertToJSON(recipe.data), |
57 | })); | 58 | })); |
58 | } else { | 59 | } else { |
59 | let remoteResults = []; | 60 | let remoteResults = []; |
@@ -62,7 +63,7 @@ class RecipeController { | |||
62 | const recipesUrlFetch = await fetch( | 63 | const recipesUrlFetch = await fetch( |
63 | `${RECIPES_URL}/search?needle=${encodeURIComponent(needle)}`, | 64 | `${RECIPES_URL}/search?needle=${encodeURIComponent(needle)}`, |
64 | ); | 65 | ); |
65 | remoteResults = JSON.parse(await recipesUrlFetch.text()); | 66 | remoteResults = convertToJSON(await recipesUrlFetch.text()); |
66 | } | 67 | } |
67 | 68 | ||
68 | debug('remoteResults:', remoteResults); | 69 | debug('remoteResults:', remoteResults); |
@@ -74,7 +75,7 @@ class RecipeController { | |||
74 | const localResults = localResultsArray.map(recipe => ({ | 75 | const localResults = localResultsArray.map(recipe => ({ |
75 | id: recipe.recipeId, | 76 | id: recipe.recipeId, |
76 | name: recipe.name, | 77 | name: recipe.name, |
77 | ...JSON.parse(recipe.data), | 78 | ...convertToJSON(recipe.data), |
78 | })); | 79 | })); |
79 | 80 | ||
80 | debug('localResults:', localResults); | 81 | debug('localResults:', localResults); |
@@ -96,7 +97,7 @@ class RecipeController { | |||
96 | response, | 97 | response, |
97 | }) { | 98 | }) { |
98 | const recipesUrlFetch = await fetch(`${RECIPES_URL}/popular`); | 99 | const recipesUrlFetch = await fetch(`${RECIPES_URL}/popular`); |
99 | const featuredRecipes = JSON.parse(await recipesUrlFetch.text()); | 100 | const featuredRecipes = convertToJSON(await recipesUrlFetch.text()); |
100 | return response.send(featuredRecipes); | 101 | return response.send(featuredRecipes); |
101 | } | 102 | } |
102 | 103 | ||
diff --git a/src/internal-server/app/Controllers/Http/ServiceController.js b/src/internal-server/app/Controllers/Http/ServiceController.js index f2f6e7028..81c03e5ff 100644 --- a/src/internal-server/app/Controllers/Http/ServiceController.js +++ b/src/internal-server/app/Controllers/Http/ServiceController.js | |||
@@ -4,6 +4,7 @@ const Env = use('Env'); | |||
4 | 4 | ||
5 | const { v4: uuid } = require('uuid'); | 5 | const { v4: uuid } = require('uuid'); |
6 | const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); | 6 | const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); |
7 | const { convertToJSON } = require('../../../../jsUtils'); | ||
7 | const { API_VERSION } = require('../../../../environment-remote'); | 8 | const { API_VERSION } = require('../../../../environment-remote'); |
8 | const moveIcon = require('../../ImageHelper'); | 9 | const moveIcon = require('../../ImageHelper'); |
9 | 10 | ||
@@ -72,10 +73,7 @@ class ServiceController { | |||
72 | const services = allServices.rows; | 73 | const services = allServices.rows; |
73 | // Convert to array with all data Franz wants | 74 | // Convert to array with all data Franz wants |
74 | const servicesArray = services.map(service => { | 75 | const servicesArray = services.map(service => { |
75 | const settings = | 76 | const settings = convertToJSON(service.settings); |
76 | typeof service.settings === 'string' | ||
77 | ? JSON.parse(service.settings) | ||
78 | : service.settings; | ||
79 | 77 | ||
80 | return { | 78 | return { |
81 | customRecipe: false, | 79 | customRecipe: false, |
@@ -88,7 +86,7 @@ class ServiceController { | |||
88 | order: DEFAULT_SERVICE_ORDER, | 86 | order: DEFAULT_SERVICE_ORDER, |
89 | spellcheckerLanguage: '', | 87 | spellcheckerLanguage: '', |
90 | workspaces: [], | 88 | workspaces: [], |
91 | ...JSON.parse(service.settings), | 89 | ...settings, |
92 | iconUrl: settings.iconId | 90 | iconUrl: settings.iconId |
93 | ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` | 91 | ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` |
94 | : null, | 92 | : null, |
@@ -108,10 +106,7 @@ class ServiceController { | |||
108 | const { id } = params; | 106 | const { id } = params; |
109 | const serviceQuery = await Service.query().where('serviceId', id).fetch(); | 107 | const serviceQuery = await Service.query().where('serviceId', id).fetch(); |
110 | const service = serviceQuery.rows[0]; | 108 | const service = serviceQuery.rows[0]; |
111 | const settings = | 109 | const settings = convertToJSON(service.settings); |
112 | typeof service.settings === 'string' | ||
113 | ? JSON.parse(service.settings) | ||
114 | : service.settings; | ||
115 | 110 | ||
116 | const icon = request.file('icon', { | 111 | const icon = request.file('icon', { |
117 | types: ['image'], | 112 | types: ['image'], |
@@ -160,9 +155,7 @@ class ServiceController { | |||
160 | const serviceData = serviceQuery.rows[0]; | 155 | const serviceData = serviceQuery.rows[0]; |
161 | 156 | ||
162 | const settings = { | 157 | const settings = { |
163 | ...(typeof serviceData.settings === 'string' | 158 | ...convertToJSON(serviceData.settings), |
164 | ? JSON.parse(serviceData.settings) | ||
165 | : serviceData.settings), | ||
166 | ...data, | 159 | ...data, |
167 | }; | 160 | }; |
168 | 161 | ||
@@ -205,7 +198,7 @@ class ServiceController { | |||
205 | const serviceData = serviceQuery.rows[0]; | 198 | const serviceData = serviceQuery.rows[0]; |
206 | 199 | ||
207 | const settings = { | 200 | const settings = { |
208 | ...JSON.parse(serviceData.settings), | 201 | ...convertToJSON(serviceData.settings), |
209 | order: data[service], | 202 | order: data[service], |
210 | }; | 203 | }; |
211 | 204 | ||
@@ -222,10 +215,7 @@ class ServiceController { | |||
222 | const services = allServices.rows; | 215 | const services = allServices.rows; |
223 | // Convert to array with all data Franz wants | 216 | // Convert to array with all data Franz wants |
224 | const servicesArray = services.map(service => { | 217 | const servicesArray = services.map(service => { |
225 | const settings = | 218 | const settings = convertToJSON(service.settings); |
226 | typeof service.settings === 'string' | ||
227 | ? JSON.parse(service.settings) | ||
228 | : service.settings; | ||
229 | 219 | ||
230 | return { | 220 | return { |
231 | customRecipe: false, | 221 | customRecipe: false, |
@@ -238,7 +228,7 @@ class ServiceController { | |||
238 | order: DEFAULT_SERVICE_ORDER, | 228 | order: DEFAULT_SERVICE_ORDER, |
239 | spellcheckerLanguage: '', | 229 | spellcheckerLanguage: '', |
240 | workspaces: [], | 230 | workspaces: [], |
241 | ...JSON.parse(service.settings), | 231 | ...settings, |
242 | iconUrl: settings.iconId | 232 | iconUrl: settings.iconId |
243 | ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` | 233 | ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` |
244 | : null, | 234 | : null, |
diff --git a/src/internal-server/app/Controllers/Http/UserController.js b/src/internal-server/app/Controllers/Http/UserController.js index 6bd4f85a7..fb1a19db6 100644 --- a/src/internal-server/app/Controllers/Http/UserController.js +++ b/src/internal-server/app/Controllers/Http/UserController.js | |||
@@ -9,6 +9,7 @@ const fetch = require('node-fetch'); | |||
9 | const { v4: uuid } = require('uuid'); | 9 | const { v4: uuid } = require('uuid'); |
10 | const crypto = require('crypto'); | 10 | const crypto = require('crypto'); |
11 | const { DEFAULT_APP_SETTINGS } = require('../../../../config'); | 11 | const { DEFAULT_APP_SETTINGS } = require('../../../../config'); |
12 | const { convertToJSON } = require('../../../../jsUtils'); | ||
12 | const { API_VERSION } = require('../../../../environment-remote'); | 13 | const { API_VERSION } = require('../../../../environment-remote'); |
13 | const { default: userAgent } = require('../../../../helpers/userAgent-helpers'); | 14 | const { default: userAgent } = require('../../../../helpers/userAgent-helpers'); |
14 | 15 | ||
@@ -87,10 +88,7 @@ class UserController { | |||
87 | async me({ response }) { | 88 | async me({ response }) { |
88 | const user = await User.find(1); | 89 | const user = await User.find(1); |
89 | 90 | ||
90 | const settings = | 91 | const settings = convertToJSON(user.settings); |
91 | typeof user.settings === 'string' | ||
92 | ? JSON.parse(user.settings) | ||
93 | : user.settings; | ||
94 | 92 | ||
95 | return response.send({ | 93 | return response.send({ |
96 | ...DEFAULT_USER_DATA, | 94 | ...DEFAULT_USER_DATA, |
@@ -101,10 +99,7 @@ class UserController { | |||
101 | async updateMe({ request, response }) { | 99 | async updateMe({ request, response }) { |
102 | const user = await User.find(1); | 100 | const user = await User.find(1); |
103 | 101 | ||
104 | let settings = user.settings || {}; | 102 | const settings = convertToJSON(user.settings || {}); |
105 | if (typeof settings === 'string') { | ||
106 | settings = JSON.parse(settings); | ||
107 | } | ||
108 | 103 | ||
109 | const newSettings = { | 104 | const newSettings = { |
110 | ...settings, | 105 | ...settings, |
@@ -304,12 +299,8 @@ class UserController { | |||
304 | .rows.length > 0 | 299 | .rows.length > 0 |
305 | ); | 300 | ); |
306 | 301 | ||
307 | if ( | 302 | if (workspace.services) { |
308 | workspace.services && | 303 | workspace.services = convertToJSON(workspace.services); |
309 | typeof workspace.services === 'string' && | ||
310 | workspace.services.length > 0 | ||
311 | ) { | ||
312 | workspace.services = JSON.parse(workspace.services); | ||
313 | } | 304 | } |
314 | const services = | 305 | const services = |
315 | workspace.services && typeof workspace.services === 'object' | 306 | workspace.services && typeof workspace.services === 'object' |
@@ -317,12 +308,8 @@ class UserController { | |||
317 | oldServiceId => serviceIdTranslation[oldServiceId], | 308 | oldServiceId => serviceIdTranslation[oldServiceId], |
318 | ) | 309 | ) |
319 | : []; | 310 | : []; |
320 | if ( | 311 | if (workspace.data) { |
321 | workspace.data && | 312 | workspace.data = convertToJSON(workspace.data); |
322 | typeof workspace.data === 'string' && | ||
323 | workspace.data.length > 0 | ||
324 | ) { | ||
325 | workspace.data = JSON.parse(workspace.data); | ||
326 | } | 313 | } |
327 | 314 | ||
328 | await Workspace.create({ | 315 | await Workspace.create({ |
@@ -347,12 +334,8 @@ class UserController { | |||
347 | // store the old serviceId as the key for future lookup | 334 | // store the old serviceId as the key for future lookup |
348 | serviceIdTranslation[service.serviceId] = newServiceId; | 335 | serviceIdTranslation[service.serviceId] = newServiceId; |
349 | 336 | ||
350 | if ( | 337 | if (service.settings) { |
351 | service.settings && | 338 | service.settings = convertToJSON(service.settings); |
352 | typeof service.settings === 'string' && | ||
353 | service.settings.length > 0 | ||
354 | ) { | ||
355 | service.settings = JSON.parse(service.settings); | ||
356 | } | 339 | } |
357 | 340 | ||
358 | await Service.create({ | 341 | await Service.create({ |
diff --git a/src/internal-server/app/Controllers/Http/WorkspaceController.js b/src/internal-server/app/Controllers/Http/WorkspaceController.js index ee5731caf..352589567 100644 --- a/src/internal-server/app/Controllers/Http/WorkspaceController.js +++ b/src/internal-server/app/Controllers/Http/WorkspaceController.js | |||
@@ -2,6 +2,7 @@ const Workspace = use('App/Models/Workspace'); | |||
2 | const { validateAll } = use('Validator'); | 2 | const { validateAll } = use('Validator'); |
3 | 3 | ||
4 | const { v4: uuid } = require('uuid'); | 4 | const { v4: uuid } = require('uuid'); |
5 | const { convertToJSON } = require('../../../../jsUtils'); | ||
5 | 6 | ||
6 | class WorkspaceController { | 7 | class WorkspaceController { |
7 | // Create a new workspace for user | 8 | // Create a new workspace for user |
@@ -130,10 +131,7 @@ class WorkspaceController { | |||
130 | id: workspace.workspaceId, | 131 | id: workspace.workspaceId, |
131 | name: workspace.name, | 132 | name: workspace.name, |
132 | order: workspace.order, | 133 | order: workspace.order, |
133 | services: | 134 | services: convertToJSON(workspace.services), |
134 | typeof workspace.services === 'string' | ||
135 | ? JSON.parse(workspace.services) | ||
136 | : workspace.services, | ||
137 | userId: 1, | 135 | userId: 1, |
138 | })); | 136 | })); |
139 | } | 137 | } |
diff --git a/src/internal-server/start/migrate.js b/src/internal-server/start/migrate.js index 403f7cb86..0af33a977 100644 --- a/src/internal-server/start/migrate.js +++ b/src/internal-server/start/migrate.js | |||
@@ -1,3 +1,4 @@ | |||
1 | const { convertToJSON } = require('../../jsUtils'); | ||
1 | const { ferdiumVersion } = require('../../environment-remote'); | 2 | const { ferdiumVersion } = require('../../environment-remote'); |
2 | 3 | ||
3 | /** | 4 | /** |
@@ -26,10 +27,7 @@ module.exports = async () => { | |||
26 | // Create new user | 27 | // Create new user |
27 | await Database.raw('INSERT INTO "users" ("id") VALUES (\'1\');'); | 28 | await Database.raw('INSERT INTO "users" ("id") VALUES (\'1\');'); |
28 | } else { | 29 | } else { |
29 | settings = | 30 | settings = convertToJSON(user.settings); |
30 | typeof user.settings === 'string' | ||
31 | ? JSON.parse(user.settings) | ||
32 | : user.settings; | ||
33 | } | 31 | } |
34 | 32 | ||
35 | if ( | 33 | if ( |