diff options
author | vantezzen <properly@protonmail.com> | 2019-08-22 11:12:36 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-08-22 11:12:36 +0200 |
commit | b018adf240679ec59a7344e30be39400f1ecd8af (patch) | |
tree | c076635761667dad302716b498088f1047281e46 /app/Controllers/Http/ServiceController.js | |
download | ferdium-server-b018adf240679ec59a7344e30be39400f1ecd8af.tar.gz ferdium-server-b018adf240679ec59a7344e30be39400f1ecd8af.tar.zst ferdium-server-b018adf240679ec59a7344e30be39400f1ecd8af.zip |
Initial commit
Diffstat (limited to 'app/Controllers/Http/ServiceController.js')
-rw-r--r-- | app/Controllers/Http/ServiceController.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js new file mode 100644 index 0000000..47a8a6f --- /dev/null +++ b/app/Controllers/Http/ServiceController.js | |||
@@ -0,0 +1,94 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const User = use('App/Models/User'); | ||
4 | const Service = use('App/Models/Service'); | ||
5 | |||
6 | class ServiceController { | ||
7 | // Create a new service for user | ||
8 | async create({ | ||
9 | request, | ||
10 | response, | ||
11 | auth | ||
12 | }) { | ||
13 | try { | ||
14 | await auth.getUser() | ||
15 | } catch (error) { | ||
16 | return response.send('Missing or invalid api token') | ||
17 | } | ||
18 | |||
19 | const data = request.all(); | ||
20 | |||
21 | const service = await Service.create({ | ||
22 | userId: auth.user.id, | ||
23 | name: data.name, | ||
24 | recipeId: data.recipeId, | ||
25 | settings: JSON.stringify(data) | ||
26 | }); | ||
27 | |||
28 | return response.send({ | ||
29 | "data": { | ||
30 | userId: auth.user.id, | ||
31 | id: service.id, | ||
32 | "isEnabled": true, | ||
33 | "isNotificationEnabled": true, | ||
34 | "isBadgeEnabled": true, | ||
35 | "isMuted": false, | ||
36 | "isDarkModeEnabled": "", | ||
37 | "spellcheckerLanguage": "", | ||
38 | "order": 1, | ||
39 | "customRecipe": false, | ||
40 | "hasCustomIcon": false, | ||
41 | "workspaces": [], | ||
42 | "iconUrl": null, | ||
43 | ...data, | ||
44 | }, | ||
45 | "status": ["created"] | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | // List all services a user has created | ||
50 | async list({ | ||
51 | request, | ||
52 | response, | ||
53 | auth | ||
54 | }) { | ||
55 | try { | ||
56 | await auth.getUser() | ||
57 | } catch (error) { | ||
58 | return response.send('Missing or invalid api token') | ||
59 | } | ||
60 | |||
61 | |||
62 | const services = (await auth.user.services().fetch()).rows; | ||
63 | // Convert to array with all data Franz wants | ||
64 | const servicesArray = services.map(service => ({ | ||
65 | "customRecipe": false, | ||
66 | "hasCustomIcon": false, | ||
67 | "isBadgeEnabled": true, | ||
68 | "isDarkModeEnabled": "", | ||
69 | "isEnabled": true, | ||
70 | "isMuted": false, | ||
71 | "isNotificationEnabled": true, | ||
72 | "order": 1, | ||
73 | "spellcheckerLanguage": "", | ||
74 | "workspaces": [], | ||
75 | "iconUrl": null, | ||
76 | ...JSON.parse(service.settings), | ||
77 | "id": service.id, | ||
78 | "name": service.name, | ||
79 | "recipeId": service.recipeId, | ||
80 | "userId": auth.user.id, | ||
81 | })) | ||
82 | |||
83 | return response.send(servicesArray) | ||
84 | } | ||
85 | |||
86 | // Download a recipe (currently simply redirects to Franz's API) | ||
87 | download({ request, response, params }) { | ||
88 | const service = params.recipe; | ||
89 | |||
90 | response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service) | ||
91 | } | ||
92 | } | ||
93 | |||
94 | module.exports = ServiceController | ||