diff options
author | Vijay A <avijayr@protonmail.com> | 2021-08-23 09:34:46 +0530 |
---|---|---|
committer | Vijay A <avijayr@protonmail.com> | 2021-08-23 09:34:46 +0530 |
commit | e5e50f4d7a8a24cb1061b563759108ffd0140825 (patch) | |
tree | 58df2e8fc2ee6ba3a0ef95cd7d8fc4032d95f4ce /src | |
parent | 5.6.1-nightly.36 [skip ci] (diff) | |
download | ferdium-app-e5e50f4d7a8a24cb1061b563759108ffd0140825.tar.gz ferdium-app-e5e50f4d7a8a24cb1061b563759108ffd0140825.tar.zst ferdium-app-e5e50f4d7a8a24cb1061b563759108ffd0140825.zip |
refactor: Reuse constants and utility functions to remove hardcoding/duplication
Diffstat (limited to 'src')
4 files changed, 20 insertions, 20 deletions
diff --git a/src/internal-server/app/Controllers/Http/RecipeController.js b/src/internal-server/app/Controllers/Http/RecipeController.js index df889c7f4..1a7595a9d 100644 --- a/src/internal-server/app/Controllers/Http/RecipeController.js +++ b/src/internal-server/app/Controllers/Http/RecipeController.js | |||
@@ -8,8 +8,9 @@ const Env = use('Env'); | |||
8 | const fetch = require('node-fetch'); | 8 | const fetch = require('node-fetch'); |
9 | const debug = require('debug')('Ferdi:internalServer:RecipeController'); | 9 | const debug = require('debug')('Ferdi:internalServer:RecipeController'); |
10 | const { LIVE_FERDI_API } = require('../../../../config'); | 10 | const { LIVE_FERDI_API } = require('../../../../config'); |
11 | const { API_VERSION } = require('../../../../environment'); | ||
11 | 12 | ||
12 | const RECIPES_URL = `${LIVE_FERDI_API}/v1/recipes`; | 13 | const RECIPES_URL = `${LIVE_FERDI_API}/${API_VERSION}/recipes`; |
13 | 14 | ||
14 | class RecipeController { | 15 | class RecipeController { |
15 | // List official and custom recipes | 16 | // List official and custom recipes |
diff --git a/src/internal-server/app/Controllers/Http/ServiceController.js b/src/internal-server/app/Controllers/Http/ServiceController.js index 312306abf..f2af9d411 100644 --- a/src/internal-server/app/Controllers/Http/ServiceController.js +++ b/src/internal-server/app/Controllers/Http/ServiceController.js | |||
@@ -6,6 +6,7 @@ const uuid = require('uuid/v4'); | |||
6 | const path = require('path'); | 6 | const path = require('path'); |
7 | const fs = require('fs-extra'); | 7 | const fs = require('fs-extra'); |
8 | const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); | 8 | const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); |
9 | const { API_VERSION } = require('../../../../environment'); | ||
9 | 10 | ||
10 | const hostname = LOCAL_HOSTNAME; | 11 | const hostname = LOCAL_HOSTNAME; |
11 | const port = Env.get('PORT'); | 12 | const port = Env.get('PORT'); |
@@ -88,7 +89,7 @@ class ServiceController { | |||
88 | workspaces: [], | 89 | workspaces: [], |
89 | ...JSON.parse(service.settings), | 90 | ...JSON.parse(service.settings), |
90 | iconUrl: settings.iconId | 91 | iconUrl: settings.iconId |
91 | ? `http://${hostname}:${port}/v1/icon/${settings.iconId}` | 92 | ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` |
92 | : null, | 93 | : null, |
93 | id: service.serviceId, | 94 | id: service.serviceId, |
94 | name: service.name, | 95 | name: service.name, |
@@ -156,7 +157,7 @@ class ServiceController { | |||
156 | id, | 157 | id, |
157 | name: service.name, | 158 | name: service.name, |
158 | ...newSettings, | 159 | ...newSettings, |
159 | iconUrl: `http://${hostname}:${port}/v1/icon/${ | 160 | iconUrl: `http://${hostname}:${port}/${API_VERSION}/icon/${ |
160 | newSettings.iconId | 161 | newSettings.iconId |
161 | }`, | 162 | }`, |
162 | userId: 1, | 163 | userId: 1, |
@@ -196,7 +197,7 @@ class ServiceController { | |||
196 | id, | 197 | id, |
197 | name: service.name, | 198 | name: service.name, |
198 | ...settings, | 199 | ...settings, |
199 | iconUrl: `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`, | 200 | iconUrl: `${Env.get('APP_URL')}/${API_VERSION}/icon/${settings.iconId}`, |
200 | userId: 1, | 201 | userId: 1, |
201 | }, | 202 | }, |
202 | status: ['updated'], | 203 | status: ['updated'], |
@@ -262,7 +263,7 @@ class ServiceController { | |||
262 | workspaces: [], | 263 | workspaces: [], |
263 | ...JSON.parse(service.settings), | 264 | ...JSON.parse(service.settings), |
264 | iconUrl: settings.iconId | 265 | iconUrl: settings.iconId |
265 | ? `http://${hostname}:${port}/v1/icon/${settings.iconId}` | 266 | ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` |
266 | : null, | 267 | : null, |
267 | id: service.serviceId, | 268 | id: service.serviceId, |
268 | name: service.name, | 269 | name: service.name, |
diff --git a/src/internal-server/app/Controllers/Http/UserController.js b/src/internal-server/app/Controllers/Http/UserController.js index d8ed9abc5..57aea6fc1 100644 --- a/src/internal-server/app/Controllers/Http/UserController.js +++ b/src/internal-server/app/Controllers/Http/UserController.js | |||
@@ -9,18 +9,16 @@ const btoa = require('btoa'); | |||
9 | const fetch = require('node-fetch'); | 9 | const fetch = require('node-fetch'); |
10 | const uuid = require('uuid/v4'); | 10 | const uuid = require('uuid/v4'); |
11 | const crypto = require('crypto'); | 11 | const crypto = require('crypto'); |
12 | const { DEFAULT_APP_SETTINGS } = require('../../../../environment'); | 12 | const { DEFAULT_APP_SETTINGS, API_VERSION } = require('../../../../environment'); |
13 | const { default: userAgent } = require('../../../../helpers/userAgent-helpers'); | ||
13 | 14 | ||
14 | const apiRequest = (url, route, method, auth) => new Promise((resolve, reject) => { | 15 | const apiRequest = (url, route, method, auth) => new Promise((resolve, reject) => { |
15 | const base = `${url}/v1/`; | ||
16 | const user = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Ferdi/5.3.0-beta.1 Chrome/69.0.3497.128 Electron/4.2.4 Safari/537.36'; | ||
17 | |||
18 | try { | 16 | try { |
19 | fetch(base + route, { | 17 | fetch(`${url}/${API_VERSION}/${route}`, { |
20 | method, | 18 | method, |
21 | headers: { | 19 | headers: { |
22 | Authorization: `Bearer ${auth}`, | 20 | Authorization: `Bearer ${auth}`, |
23 | 'User-Agent': user, | 21 | 'User-Agent': userAgent(), |
24 | }, | 22 | }, |
25 | }) | 23 | }) |
26 | .then(data => data.json()) | 24 | .then(data => data.json()) |
@@ -30,6 +28,8 @@ const apiRequest = (url, route, method, auth) => new Promise((resolve, reject) = | |||
30 | } | 28 | } |
31 | }); | 29 | }); |
32 | 30 | ||
31 | const LOGIN_SUCCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGZXJkaSBJbnRlcm5hbCBTZXJ2ZXIiLCJpYXQiOjE1NzEwNDAyMTUsImV4cCI6MjUzMzk1NDE3ODQ0LCJhdWQiOiJnZXRmZXJkaS5jb20iLCJzdWIiOiJmZXJkaUBsb2NhbGhvc3QiLCJ1c2VySWQiOiIxIn0.9_TWFGp6HROv8Yg82Rt6i1-95jqWym40a-HmgrdMC6M'; | ||
32 | |||
33 | class UserController { | 33 | class UserController { |
34 | // Register a new user | 34 | // Register a new user |
35 | async signup({ | 35 | async signup({ |
@@ -52,7 +52,7 @@ class UserController { | |||
52 | 52 | ||
53 | return response.send({ | 53 | return response.send({ |
54 | message: 'Successfully created account', | 54 | message: 'Successfully created account', |
55 | token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGZXJkaSBJbnRlcm5hbCBTZXJ2ZXIiLCJpYXQiOjE1NzEwNDAyMTUsImV4cCI6MjUzMzk1NDE3ODQ0LCJhdWQiOiJnZXRmZXJkaS5jb20iLCJzdWIiOiJmZXJkaUBsb2NhbGhvc3QiLCJ1c2VySWQiOiIxIn0.9_TWFGp6HROv8Yg82Rt6i1-95jqWym40a-HmgrdMC6M', | 55 | token: LOGIN_SUCCESS_TOKEN, |
56 | }); | 56 | }); |
57 | } | 57 | } |
58 | 58 | ||
@@ -70,7 +70,7 @@ class UserController { | |||
70 | 70 | ||
71 | return response.send({ | 71 | return response.send({ |
72 | message: 'Successfully logged in', | 72 | message: 'Successfully logged in', |
73 | token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGZXJkaSBJbnRlcm5hbCBTZXJ2ZXIiLCJpYXQiOjE1NzEwNDAyMTUsImV4cCI6MjUzMzk1NDE3ODQ0LCJhdWQiOiJnZXRmZXJkaS5jb20iLCJzdWIiOiJmZXJkaUBsb2NhbGhvc3QiLCJ1c2VySWQiOiIxIn0.9_TWFGp6HROv8Yg82Rt6i1-95jqWym40a-HmgrdMC6M', | 73 | token: LOGIN_SUCCESS_TOKEN, |
74 | }); | 74 | }); |
75 | } | 75 | } |
76 | 76 | ||
@@ -168,19 +168,16 @@ class UserController { | |||
168 | 168 | ||
169 | const hashedPassword = crypto.createHash('sha256').update(password).digest('base64'); | 169 | const hashedPassword = crypto.createHash('sha256').update(password).digest('base64'); |
170 | 170 | ||
171 | const base = `${server}/v1/`; | ||
172 | const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Ferdi/5.3.0-beta.1 Chrome/69.0.3497.128 Electron/4.2.4 Safari/537.36'; | ||
173 | |||
174 | // Try to get an authentication token | 171 | // Try to get an authentication token |
175 | let token; | 172 | let token; |
176 | try { | 173 | try { |
177 | const basicToken = btoa(`${email}:${hashedPassword}`); | 174 | const basicToken = btoa(`${email}:${hashedPassword}`); |
178 | 175 | ||
179 | const rawResponse = await fetch(`${base}auth/login`, { | 176 | const rawResponse = await fetch(`${server}/${API_VERSION}/auth/login`, { |
180 | method: 'POST', | 177 | method: 'POST', |
181 | headers: { | 178 | headers: { |
182 | Authorization: `Basic ${basicToken}`, | 179 | Authorization: `Basic ${basicToken}`, |
183 | 'User-Agent': userAgent, | 180 | 'User-Agent': userAgent(), |
184 | }, | 181 | }, |
185 | }); | 182 | }); |
186 | const content = await rawResponse.json(); | 183 | const content = await rawResponse.json(); |
diff --git a/src/internal-server/start/routes.js b/src/internal-server/start/routes.js index 6668f3062..b32b094ee 100644 --- a/src/internal-server/start/routes.js +++ b/src/internal-server/start/routes.js | |||
@@ -8,6 +8,7 @@ | |||
8 | /** @type {typeof import('@adonisjs/framework/src/Route/Manager')} */ | 8 | /** @type {typeof import('@adonisjs/framework/src/Route/Manager')} */ |
9 | const Route = use('Route'); | 9 | const Route = use('Route'); |
10 | 10 | ||
11 | const { API_VERSION } = require('../../environment'); | ||
11 | // Run latest database migration | 12 | // Run latest database migration |
12 | const migrate = require('./migrate'); | 13 | const migrate = require('./migrate'); |
13 | 14 | ||
@@ -67,11 +68,11 @@ Route.group(() => { | |||
67 | Route.get('services', 'StaticController.emptyArray'); | 68 | Route.get('services', 'StaticController.emptyArray'); |
68 | Route.get('news', 'StaticController.emptyArray'); | 69 | Route.get('news', 'StaticController.emptyArray'); |
69 | Route.get('announcements/:version', 'StaticController.announcement'); | 70 | Route.get('announcements/:version', 'StaticController.announcement'); |
70 | }).prefix('v1').middleware(OnlyAllowFerdi); | 71 | }).prefix(API_VERSION).middleware(OnlyAllowFerdi); |
71 | 72 | ||
72 | Route.group(() => { | 73 | Route.group(() => { |
73 | Route.get('icon/:id', 'ServiceController.icon'); | 74 | Route.get('icon/:id', 'ServiceController.icon'); |
74 | }).prefix('v1'); | 75 | }).prefix(API_VERSION); |
75 | 76 | ||
76 | // Franz account import | 77 | // Franz account import |
77 | Route.post('import', 'UserController.import'); | 78 | Route.post('import', 'UserController.import'); |