aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/ServiceController.js
diff options
context:
space:
mode:
authorLibravatar Ricardo <ricardo@cino.io>2023-10-13 14:12:03 +0200
committerLibravatar GitHub <noreply@github.com>2023-10-13 13:12:03 +0100
commite503468660a13760010a94ecda5f0625c6f47f87 (patch)
treefa532f54fc5f091de08d55405ec6339bd2440a02 /app/Controllers/Http/ServiceController.js
parent1.3.16 [skip ci] (diff)
downloadferdium-server-e503468660a13760010a94ecda5f0625c6f47f87.tar.gz
ferdium-server-e503468660a13760010a94ecda5f0625c6f47f87.tar.zst
ferdium-server-e503468660a13760010a94ecda5f0625c6f47f87.zip
Server re-build with latest AdonisJS framework & Typescript (#47)
* chore: setup first basis structure * chore: ensure styling is loaded correctly * chore: comply to new routing syntax by replace . with / in routes/resource locations * chore: add login controller * chore: correctly use views with slash instead of dot * chore: working login + tests * chore: clean up tests * chore: add password-forgot endpoint and matching test * chore: add delete page test * chore: add logout test * chore: add reset-password route and tests * chore: remove obsolete comment * chore: add account-page and tests * chore: add data page & first step of the test * chore: add transfer/import data feature and tests * chore: add export and basic test * chore: add all static api routes with tests * Regenerate 'pnpm-lock.json' and fix bad merge conflict WIP: - Tests have been commented out since they dont work - Server doesn't start * easier dev and test runs * - remove --require-pragma from reformat-files so formatting works properly - run pnpm reformat-files over codebase - remove .json files from .eslintignore - add invalid.json file to .eslintignore - configure prettier properly in eslint config - add type jsdoc to prettier config - run adonis generate:manifest command to regenerate ace-manifest.json - specify volta in package.json - introduce typecheck npm script - remove unused .mjs extension from npm scripts - install missing type definition dependencies - add pnpm.allowedDeprecatedVersions to package.json - fix invalid extends in tsconfig.json causing TS issues throughout codebase - remove @ts-ignore throughout codebase which is not relevant anymore - enable some of the tsconfig options - remove outdated eslint-disable from codebase - change deprecated faker.company.companyName() to faker.company.name() - fix TS issues inside transfer.spec.ts * - update to latest node and pnpm versions - upgrade all non-major dependencies to latest - install missing @types/luxon dependency - add cuid to pnpm.allowedDeprecatedVersions - add esModuleInterop config option to tsconfig - migrate more deprecated faker methods to new ones - add more temporary ts-ignore to code * - update eslint config - remove trailingComma: all since default in prettier v3 - add typecheck command to prepare-code npm script - upgrade various dependencies to latest major version - update tsconfig to include only useful config options - disable some lint issues and fix others * - add test command to prepare-code - disable strictPropertyInitialization flag in tsconfig which creates issues with adonis models - update precommit hook to excute pnpm prepare-code - remove ts-ignore statements from all models * fix node and pnpm dependency update * add cross env (so that we can develop on windows) * add signup endpoint (TODO: JWT auth) * Add login endpoint * Add me and updateMe endpoints * Add service endpoint * refactor: change endpoints to use jwt * add recipes endpoint * add workspaces endpoint * fix web controllors for login and post import * Update node deps * Change auth middleware (for web) and exempt api from CSRF * Add import endpoint (franz import) * Fix export/import logic * Fix service and workspace data in user/data * Fix partial lint * chore: workaround lint issues * fix: migration naming had two . * Sync back node with recipes repo * Temporarily ignore typescript * Fix adonisrc to handle public folder static assets * Fix issue with production database * add Legacy Password Provider * Fix lint errors * Fix issue on login errors frontend * add Legacy Password Provider * Fix issue with customIcons * Fix issue with auth tokens * Update 'node' to '18.18.0' * make docker work * improve docker entrypoint (test api performance) * Add migration database script * NODE_ENV on recipes * prefer @ts-expect-error over @ts-ignore * small fixes * Update 'pnpm' to '8.7.6' * fix error catch * Automatically generate JWT Public and Private keys * Use custom Adonis5-jwt * Update code to use secret (old way, no breaking changes) * Normalize appKey * Trick to make JWT tokens on client work with new version * Fix error with new JWT logic * Change migration and how we store JWT * Fix 500 response code (needs to be 401) * Improve logic and fix bugs * Fix build and entrypoint logic * Catch error if appKey changes * Add newToken logic * Fix lint (ignore any errors) * Add build for PRs * pnpm reformat-files result * Fix some tests * Fix reset password not working (test failing) * Restore csrfTokens (disabled by accident) * Fix pnpm start command with .env * Disable failing tests on the transfer endpoint (TODO) * Add tests to PR build * Fix build * Remove unnecessary assertStatus * Add typecheck * hash password on UserFactory (fix build) * Add JWT_USE_PEM true by default (increase security) * fix name of github action --------- Co-authored-by: Vijay A <vraravam@users.noreply.github.com> Co-authored-by: Balaji Vijayakumar <kuttibalaji.v6@gmail.com> Co-authored-by: MCMXC <16797721+mcmxcdev@users.noreply.github.com> Co-authored-by: André Oliveira <oliveira.andrerodrigues95@gmail.com>
Diffstat (limited to 'app/Controllers/Http/ServiceController.js')
-rw-r--r--app/Controllers/Http/ServiceController.js347
1 files changed, 0 insertions, 347 deletions
diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js
deleted file mode 100644
index 6a27ef2..0000000
--- a/app/Controllers/Http/ServiceController.js
+++ /dev/null
@@ -1,347 +0,0 @@
1const Service = use('App/Models/Service');
2const { validateAll } = use('Validator');
3const Env = use('Env');
4const Helpers = use('Helpers');
5
6const { v4: uuid } = require('uuid');
7const path = require('path');
8const fs = require('fs-extra');
9const sanitize = require('sanitize-filename');
10
11class ServiceController {
12 // Create a new service for user
13 async create({ request, response, auth }) {
14 try {
15 await auth.getUser();
16 } catch (error) {
17 return response.send('Missing or invalid api token');
18 }
19
20 // Validate user input
21 const validation = await validateAll(request.all(), {
22 name: 'required|string',
23 recipeId: 'required',
24 });
25 if (validation.fails()) {
26 return response.status(401).send({
27 message: 'Invalid POST arguments',
28 messages: validation.messages(),
29 status: 401,
30 });
31 }
32
33 const data = request.all();
34
35 // Get new, unused uuid
36 let serviceId;
37 do {
38 serviceId = uuid();
39 } while (
40 (await Service.query().where('serviceId', serviceId).fetch()).rows
41 .length > 0
42 ); // eslint-disable-line no-await-in-loop
43
44 await Service.create({
45 userId: auth.user.id,
46 serviceId,
47 name: data.name,
48 recipeId: data.recipeId,
49 settings: JSON.stringify(data),
50 });
51
52 return response.send({
53 data: {
54 userId: auth.user.id,
55 id: serviceId,
56 isEnabled: true,
57 isNotificationEnabled: true,
58 isBadgeEnabled: true,
59 isMuted: false,
60 isDarkModeEnabled: '',
61 spellcheckerLanguage: '',
62 order: 1,
63 customRecipe: false,
64 hasCustomIcon: false,
65 workspaces: [],
66 iconUrl: null,
67 ...data,
68 },
69 status: ['created'],
70 });
71 }
72
73 // List all services a user has created
74 async list({ response, auth }) {
75 try {
76 await auth.getUser();
77 } catch (error) {
78 return response.send('Missing or invalid api token');
79 }
80
81 const services = (await auth.user.services().fetch()).rows;
82 // Convert to array with all data Franz wants
83 const servicesArray = services.map(service => {
84 const settings =
85 typeof service.settings === 'string'
86 ? JSON.parse(service.settings)
87 : service.settings;
88
89 return {
90 customRecipe: false,
91 hasCustomIcon: !!settings.iconId,
92 isBadgeEnabled: true,
93 isDarkModeEnabled: '',
94 isEnabled: true,
95 isMuted: false,
96 isNotificationEnabled: true,
97 order: 1,
98 spellcheckerLanguage: '',
99 workspaces: [],
100 ...settings,
101 iconUrl: settings.iconId
102 ? `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`
103 : null,
104 id: service.serviceId,
105 name: service.name,
106 recipeId: service.recipeId,
107 userId: auth.user.id,
108 };
109 });
110
111 return response.send(servicesArray);
112 }
113
114 async edit({ request, response, auth, params }) {
115 try {
116 await auth.getUser();
117 } catch (error) {
118 return response.send('Missing or invalid api token');
119 }
120
121 if (request.file('icon')) {
122 // Upload custom service icon
123 const icon = request.file('icon', {
124 types: ['image'],
125 size: '2mb',
126 });
127 const { id } = params;
128 const service = (
129 await Service.query()
130 .where('serviceId', id)
131 .where('userId', auth.user.id)
132 .fetch()
133 ).rows[0];
134 const settings =
135 typeof service.settings === 'string'
136 ? JSON.parse(service.settings)
137 : service.settings;
138
139 let iconId;
140 do {
141 iconId = uuid() + uuid();
142 // eslint-disable-next-line no-await-in-loop
143 } while (await fs.exists(path.join(Helpers.tmpPath('uploads'), iconId)));
144 iconId = `${iconId}.${icon.extname}`;
145
146 await icon.move(Helpers.tmpPath('uploads'), {
147 name: iconId,
148 overwrite: true,
149 });
150
151 if (!icon.moved()) {
152 return response.status(500).send(icon.error());
153 }
154
155 const newSettings = {
156 ...settings,
157 ...{
158 iconId,
159 customIconVersion:
160 settings && settings.customIconVersion
161 ? settings.customIconVersion + 1
162 : 1,
163 },
164 };
165
166 // Update data in database
167 await Service.query()
168 .where('serviceId', id)
169 .where('userId', auth.user.id)
170 .update({
171 name: service.name,
172 settings: JSON.stringify(newSettings),
173 });
174
175 return response.send({
176 data: {
177 id,
178 name: service.name,
179 ...newSettings,
180 iconUrl: `${Env.get('APP_URL')}/v1/icon/${newSettings.iconId}`,
181 userId: auth.user.id,
182 },
183 status: ['updated'],
184 });
185 }
186 // Update service info
187 const data = request.all();
188 const { id } = params;
189
190 // Get current settings from db
191 const serviceData = (
192 await Service.query()
193 .where('serviceId', id)
194 .where('userId', auth.user.id)
195 .fetch()
196 ).rows[0];
197
198 const settings = {
199 ...(typeof serviceData.settings === 'string'
200 ? JSON.parse(serviceData.settings)
201 : serviceData.settings),
202 ...data,
203 };
204
205 if (settings.customIcon === 'delete') {
206 fs.remove(
207 path.join(Helpers.tmpPath('uploads'), settings.iconId),
208 ).catch(error => {
209 console.error(error);
210 });
211
212 settings.iconId = undefined;
213 settings.customIconVersion = undefined;
214 settings.customIcon = '';
215 }
216
217 // Update data in database
218 await Service.query()
219 .where('serviceId', id)
220 .where('userId', auth.user.id)
221 .update({
222 name: data.name,
223 settings: JSON.stringify(settings),
224 });
225
226 // Get updated row
227 const service = (
228 await Service.query()
229 .where('serviceId', id)
230 .where('userId', auth.user.id)
231 .fetch()
232 ).rows[0];
233
234 return response.send({
235 data: {
236 id,
237 name: service.name,
238 ...settings,
239 iconUrl: `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`,
240 userId: auth.user.id,
241 },
242 status: ['updated'],
243 });
244 }
245
246 async icon({ params, response }) {
247 let { id } = params;
248
249 id = sanitize(id);
250 if (id === '') {
251 return response.status(404).send({
252 status: "Icon doesn't exist",
253 });
254 }
255
256 const iconPath = path.join(Helpers.tmpPath('uploads'), id);
257
258 try {
259 await fs.access(iconPath);
260 } catch (ex) {
261 console.log(ex);
262 // File not available.
263 return response.status(404).send({
264 status: "Icon doesn't exist",
265 });
266 }
267
268 return response.download(iconPath);
269 }
270
271 async reorder({ request, response, auth }) {
272 const data = request.all();
273
274 for (const service of Object.keys(data)) {
275 // Get current settings from db
276 const serviceData = (
277 await Service.query() // eslint-disable-line no-await-in-loop
278 .where('serviceId', service)
279 .where('userId', auth.user.id)
280 .fetch()
281 ).rows[0];
282
283 const settings = {
284 ...(typeof serviceData.settings === 'string'
285 ? JSON.parse(serviceData.settings)
286 : serviceData.settings),
287 order: data[service],
288 };
289
290 // Update data in database
291 await Service.query() // eslint-disable-line no-await-in-loop
292 .where('serviceId', service)
293 .where('userId', auth.user.id)
294 .update({
295 settings: JSON.stringify(settings),
296 });
297 }
298
299 // Get new services
300 const services = (await auth.user.services().fetch()).rows;
301 // Convert to array with all data Franz wants
302 const servicesArray = services.map(service => {
303 const settings =
304 typeof service.settings === 'string'
305 ? JSON.parse(service.settings)
306 : service.settings;
307
308 return {
309 customRecipe: false,
310 hasCustomIcon: !!settings.iconId,
311 isBadgeEnabled: true,
312 isDarkModeEnabled: '',
313 isEnabled: true,
314 isMuted: false,
315 isNotificationEnabled: true,
316 order: 1,
317 spellcheckerLanguage: '',
318 workspaces: [],
319 ...settings,
320 iconUrl: settings.iconId
321 ? `${Env.get('APP_URL')}/v1/icon/${settings.iconId}`
322 : null,
323 id: service.serviceId,
324 name: service.name,
325 recipeId: service.recipeId,
326 userId: auth.user.id,
327 };
328 });
329
330 return response.send(servicesArray);
331 }
332
333 async delete({ params, auth, response }) {
334 // Update data in database
335 await Service.query()
336 .where('serviceId', params.id)
337 .where('userId', auth.user.id)
338 .delete();
339
340 return response.send({
341 message: 'Sucessfully deleted service',
342 status: 200,
343 });
344 }
345}
346
347module.exports = ServiceController;