From 29b8334b060dc0c05a509d523ead4b3a30229fef Mon Sep 17 00:00:00 2001 From: vantezzen Date: Thu, 5 Sep 2019 11:22:49 +0200 Subject: Add eslint --- app/Controllers/Http/DashboardController.js | 61 ++--- app/Controllers/Http/RecipeController.js | 159 ++++++------- app/Controllers/Http/ServiceController.js | 200 ++++++++-------- app/Controllers/Http/StaticController.js | 347 ++++++++++++++-------------- app/Controllers/Http/UserController.js | 213 ++++++++--------- app/Controllers/Http/WorkspaceController.js | 104 ++++----- 6 files changed, 530 insertions(+), 554 deletions(-) (limited to 'app/Controllers') diff --git a/app/Controllers/Http/DashboardController.js b/app/Controllers/Http/DashboardController.js index aa8127f..49f6cc0 100644 --- a/app/Controllers/Http/DashboardController.js +++ b/app/Controllers/Http/DashboardController.js @@ -1,7 +1,6 @@ -'use strict' const { - validateAll + validateAll, } = use('Validator'); const crypto = require('crypto'); @@ -11,7 +10,7 @@ class DashboardController { request, response, auth, - session + session, }) { const validation = await validateAll(request.all(), { mail: 'required|email', @@ -20,25 +19,25 @@ class DashboardController { if (validation.fails()) { session.withErrors({ type: 'danger', - message: 'Invalid mail or password' + message: 'Invalid mail or password', }).flashExcept(['password']); return response.redirect('back'); } - let { + const { mail, - password - } = request.all() + password, + } = request.all(); const hashedPassword = crypto.createHash('sha256').update(password).digest('base64'); try { - await auth.authenticator('session').attempt(mail, hashedPassword) + await auth.authenticator('session').attempt(mail, hashedPassword); } catch (error) { session.flash({ type: 'danger', - message: 'Invalid mail or password' - }) + message: 'Invalid mail or password', + }); return response.redirect('back'); } return response.redirect('/user/account'); @@ -46,17 +45,18 @@ class DashboardController { async account({ auth, - view + view, + response, }) { try { - await auth.check() + await auth.check(); } catch (error) { return response.redirect('/user/login'); } return view.render('dashboard.account', { username: auth.user.username, - email: auth.user.email + email: auth.user.email, }); } @@ -65,11 +65,11 @@ class DashboardController { request, session, view, - response + response, }) { let validation = await validateAll(request.all(), { username: 'required', - email: 'required' + email: 'required', }); if (validation.fails()) { session.withErrors(validation.messages()).flashExcept(['password']); @@ -80,19 +80,19 @@ class DashboardController { if (request.input('username') !== auth.user.username) { validation = await validateAll(request.all(), { username: 'required|unique:users,username', - email: 'required' + email: 'required', }); if (validation.fails()) { session.withErrors(validation.messages()).flashExcept(['password']); return response.redirect('back'); } - } + } // Check new email if (request.input('email') !== auth.user.email) { validation = await validateAll(request.all(), { username: 'required', - email: 'required|email|unique:users,email' + email: 'required|email|unique:users,email', }); if (validation.fails()) { session.withErrors(validation.messages()).flashExcept(['password']); @@ -101,24 +101,25 @@ class DashboardController { } // Update user account - auth.user.username = request.input('username'); - auth.user.email = request.input('email'); - if (!!request.input('password')) { + const { user } = auth; + user.username = request.input('username'); + user.email = request.input('email'); + if (request.input('password')) { const hashedPassword = crypto.createHash('sha256').update(request.input('password')).digest('base64'); - auth.user.password = hashedPassword; + user.password = hashedPassword; } - auth.user.save(); + user.save(); return view.render('dashboard.account', { - username: auth.user.username, - email: auth.user.email, - success: true + username: user.username, + email: user.email, + success: true, }); } async data({ auth, - view + view, }) { const general = auth.user; const services = (await auth.user.services().fetch()).toJSON(); @@ -136,7 +137,7 @@ class DashboardController { logout({ auth, - response + response, }) { auth.authenticator('session').logout(); return response.redirect('/user/login'); @@ -144,7 +145,7 @@ class DashboardController { delete({ auth, - response + response, }) { auth.user.delete(); auth.authenticator('session').logout(); @@ -152,4 +153,4 @@ class DashboardController { } } -module.exports = DashboardController +module.exports = DashboardController; diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js index fd9ed83..217b05b 100644 --- a/app/Controllers/Http/RecipeController.js +++ b/app/Controllers/Http/RecipeController.js @@ -1,61 +1,58 @@ -'use strict' const Recipe = use('App/Models/Recipe'); -const Helpers = use('Helpers') -const Drive = use('Drive') +const Helpers = use('Helpers'); +const Drive = use('Drive'); const { - validateAll + validateAll, } = use('Validator'); -const Env = use('Env') +const Env = use('Env'); const fetch = require('node-fetch'); const targz = require('targz'); const path = require('path'); const fs = require('fs-extra'); -const compress = (src, dest) => { - return new Promise((resolve, reject) => { - targz.compress({ - src, - dest - }, function (err) { - if (err) { - reject(err); - } else { - resolve(dest); - } - }); - }) -} +const compress = (src, dest) => new Promise((resolve, reject) => { + targz.compress({ + src, + dest, + }, (err) => { + if (err) { + reject(err); + } else { + resolve(dest); + } + }); +}); class RecipeController { // List official and custom recipes async list({ - response + response, }) { const officialRecipes = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes')).text()); const customRecipesArray = (await Recipe.all()).rows; - const customRecipes = customRecipesArray.map(recipe => ({ - "id": recipe.recipeId, - "name": recipe.name, - ...JSON.parse(recipe.data) - })) + const customRecipes = customRecipesArray.map((recipe) => ({ + id: recipe.recipeId, + name: recipe.name, + ...JSON.parse(recipe.data), + })); const recipes = [ ...officialRecipes, ...customRecipes, - ] + ]; - return response.send(recipes) + return response.send(recipes); } // Create a new recipe using the new.html page async create({ request, - response + response, }) { // Check if recipe creation is enabled - if (Env.get('IS_CREATION_ENABLED') == 'false') { + if (Env.get('IS_CREATION_ENABLED') == 'false') { // eslint-disable-line eqeqeq return response.send('This server doesn\'t allow the creation of new recipes.'); } @@ -69,10 +66,10 @@ class RecipeController { }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const data = request.all(); @@ -90,16 +87,16 @@ class RecipeController { await fs.emptyDir(Helpers.tmpPath('recipe')); // Move uploaded files to temporary path - const files = request.file('files') - await files.moveAll(Helpers.tmpPath('recipe')) + const files = request.file('files'); + await files.moveAll(Helpers.tmpPath('recipe')); // Compress files to .tar.gz file const source = Helpers.tmpPath('recipe'); - const destination = path.join(Helpers.appRoot(), '/recipes/' + data.id + '.tar.gz'); - + const destination = path.join(Helpers.appRoot(), `/recipes/${data.id}.tar.gz`); + compress( source, - destination + destination, ); // Create recipe in db @@ -107,74 +104,73 @@ class RecipeController { name: data.name, recipeId: data.id, data: JSON.stringify({ - "author": data.author, - "featured": false, - "version": "1.0.0", - "icons": { - "png": data.png, - "svg": data.svg - } - }) - }) - - return response.send('Created new recipe') + author: data.author, + featured: false, + version: '1.0.0', + icons: { + png: data.png, + svg: data.svg, + }, + }), + }); + + return response.send('Created new recipe'); } // Search official and custom recipes async search({ request, - response + response, }) { // Validate user input const validation = await validateAll(request.all(), { - needle: 'required' + needle: 'required', }); if (validation.fails()) { return response.status(401).send({ - "message": "Please provide a needle", - "messages": validation.messages(), - "status": 401 - }) + message: 'Please provide a needle', + messages: validation.messages(), + status: 401, + }); } - const needle = request.input('needle') + const needle = request.input('needle'); // Get results let remoteResults = []; - if (Env.get('CONNECT_WITH_FRANZ') == 'true') { - remoteResults = JSON.parse(await (await fetch('https://api.franzinfra.com/v1/recipes/search?needle=' + encodeURIComponent(needle))).text()); + if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq + remoteResults = JSON.parse(await (await fetch(`https://api.franzinfra.com/v1/recipes/search?needle=${encodeURIComponent(needle)}`)).text()); } - const localResultsArray = (await Recipe.query().where('name', 'LIKE', '%' + needle + '%').fetch()).toJSON(); - const localResults = localResultsArray.map(recipe => ({ - "id": recipe.recipeId, - "name": recipe.name, - ...JSON.parse(recipe.data) - })) + const localResultsArray = (await Recipe.query().where('name', 'LIKE', `%${needle}%`).fetch()).toJSON(); + const localResults = localResultsArray.map((recipe) => ({ + id: recipe.recipeId, + name: recipe.name, + ...JSON.parse(recipe.data), + })); const results = [ ...localResults, ...remoteResults, - ] + ]; return response.send(results); } // Download a recipe async download({ - request, response, - params + params, }) { // Validate user input const validation = await validateAll(params, { - recipe: 'required|accepted' + recipe: 'required|accepted', }); if (validation.fails()) { return response.status(401).send({ - "message": "Please provide a recipe ID", - "messages": validation.messages(), - "status": 401 - }) + message: 'Please provide a recipe ID', + messages: validation.messages(), + status: 401, + }); } const service = params.recipe; @@ -185,17 +181,16 @@ class RecipeController { } // Check if recipe exists in recipes folder - if (await Drive.exists(service + '.tar.gz')) { - response.send(await Drive.get(service + '.tar.gz')) - } else if(Env.get('CONNECT_WITH_FRANZ') == 'true') { - response.redirect('https://api.franzinfra.com/v1/recipes/download/' + service) - } else { - return response.status(400).send({ - "message": "Recipe not found", - "code": "recipe-not-found" - }) + if (await Drive.exists(`${service}.tar.gz`)) { + response.send(await Drive.get(`${service}.tar.gz`)); + } else if (Env.get('CONNECT_WITH_FRANZ') == 'true') { // eslint-disable-line eqeqeq + response.redirect(`https://api.franzinfra.com/v1/recipes/download/${service}`); } + return response.status(400).send({ + message: 'Recipe not found', + code: 'recipe-not-found', + }); } } -module.exports = RecipeController +module.exports = RecipeController; diff --git a/app/Controllers/Http/ServiceController.js b/app/Controllers/Http/ServiceController.js index e887e3a..309ae09 100644 --- a/app/Controllers/Http/ServiceController.js +++ b/app/Controllers/Http/ServiceController.js @@ -1,9 +1,6 @@ -'use strict' - -const User = use('App/Models/User'); const Service = use('App/Models/Service'); const { - validateAll + validateAll, } = use('Validator'); const uuid = require('uuid/v4'); @@ -13,12 +10,12 @@ class ServiceController { async create({ request, response, - auth + auth, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } // Validate user input @@ -28,10 +25,10 @@ class ServiceController { }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const data = request.all(); @@ -40,83 +37,82 @@ class ServiceController { let serviceId; do { serviceId = uuid(); - } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0) + } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop - const service = await Service.create({ + await Service.create({ userId: auth.user.id, serviceId, name: data.name, recipeId: data.recipeId, - settings: JSON.stringify(data) + settings: JSON.stringify(data), }); return response.send({ - "data": { + data: { userId: auth.user.id, id: serviceId, - "isEnabled": true, - "isNotificationEnabled": true, - "isBadgeEnabled": true, - "isMuted": false, - "isDarkModeEnabled": "", - "spellcheckerLanguage": "", - "order": 1, - "customRecipe": false, - "hasCustomIcon": false, - "workspaces": [], - "iconUrl": null, + isEnabled: true, + isNotificationEnabled: true, + isBadgeEnabled: true, + isMuted: false, + isDarkModeEnabled: '', + spellcheckerLanguage: '', + order: 1, + customRecipe: false, + hasCustomIcon: false, + workspaces: [], + iconUrl: null, ...data, }, - "status": ["created"] - }) + status: ['created'], + }); } // List all services a user has created async list({ - request, response, - auth + auth, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } const services = (await auth.user.services().fetch()).rows; // Convert to array with all data Franz wants - const servicesArray = services.map(service => ({ - "customRecipe": false, - "hasCustomIcon": false, - "isBadgeEnabled": true, - "isDarkModeEnabled": "", - "isEnabled": true, - "isMuted": false, - "isNotificationEnabled": true, - "order": 1, - "spellcheckerLanguage": "", - "workspaces": [], - "iconUrl": null, + const servicesArray = services.map((service) => ({ + customRecipe: false, + hasCustomIcon: false, + isBadgeEnabled: true, + isDarkModeEnabled: '', + isEnabled: true, + isMuted: false, + isNotificationEnabled: true, + order: 1, + spellcheckerLanguage: '', + workspaces: [], + iconUrl: null, ...JSON.parse(service.settings), - "id": service.serviceId, - "name": service.name, - "recipeId": service.recipeId, - "userId": auth.user.id, - })) + id: service.serviceId, + name: service.name, + recipeId: service.recipeId, + userId: auth.user.id, + })); - return response.send(servicesArray) + return response.send(servicesArray); } async edit({ request, response, auth, - params + params, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } // Validate user input @@ -125,15 +121,15 @@ class ServiceController { }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const data = request.all(); const { - id + id, } = params; // Get current settings from db @@ -141,7 +137,7 @@ class ServiceController { .where('serviceId', id) .where('userId', auth.user.id).fetch()).rows[0]; - let settings = { + const settings = { ...JSON.parse(serviceData.settings), ...data, }; @@ -150,9 +146,9 @@ class ServiceController { await (Service.query() .where('serviceId', id) .where('userId', auth.user.id)).update({ - name: data.name, - settings: JSON.stringify(settings) - }); + name: data.name, + settings: JSON.stringify(settings), + }); // Get updated row const service = (await Service.query() @@ -160,88 +156,86 @@ class ServiceController { .where('userId', auth.user.id).fetch()).rows[0]; return response.send({ - "id": service.serviceId, - "name": data.name, + id: service.serviceId, + name: data.name, ...settings, - "userId": auth.user.id - }) + userId: auth.user.id, + }); } async reorder({ request, response, - auth + auth, }) { const data = request.all(); - for (const service in data) { + for (const service of Object.keys(data)) { // Get current settings from db - const serviceData = (await Service.query() + const serviceData = (await Service.query() // eslint-disable-line no-await-in-loop .where('serviceId', service) .where('userId', auth.user.id).fetch()).rows[0]; - let settings = { + const settings = { ...JSON.parse(serviceData.settings), - order: data[service] + order: data[service], }; // Update data in database - await (Service.query() + await (Service.query() // eslint-disable-line no-await-in-loop .where('serviceId', service) .where('userId', auth.user.id)) - .update({ - settings: JSON.stringify(settings) - }); + .update({ + settings: JSON.stringify(settings), + }); } // Get new services const services = (await auth.user.services().fetch()).rows; // Convert to array with all data Franz wants - const servicesArray = services.map(service => ({ - "customRecipe": false, - "hasCustomIcon": false, - "isBadgeEnabled": true, - "isDarkModeEnabled": "", - "isEnabled": true, - "isMuted": false, - "isNotificationEnabled": true, - "order": 1, - "spellcheckerLanguage": "", - "workspaces": [], - "iconUrl": null, + const servicesArray = services.map((service) => ({ + customRecipe: false, + hasCustomIcon: false, + isBadgeEnabled: true, + isDarkModeEnabled: '', + isEnabled: true, + isMuted: false, + isNotificationEnabled: true, + order: 1, + spellcheckerLanguage: '', + workspaces: [], + iconUrl: null, ...JSON.parse(service.settings), - "id": service.serviceId, - "name": service.name, - "recipeId": service.recipeId, - "userId": auth.user.id, - })) + id: service.serviceId, + name: service.name, + recipeId: service.recipeId, + userId: auth.user.id, + })); - return response.send(servicesArray) + return response.send(servicesArray); } update({ - request, - response + response, }) { - return response.send([]) + return response.send([]); } async delete({ - request, params, auth, - response + response, }) { // Update data in database await (Service.query() .where('serviceId', params.id) - .where('userId', auth.user.id)).delete() + .where('userId', auth.user.id)).delete(); return response.send({ - "message": "Sucessfully deleted service", - "status": 200 - }) + message: 'Sucessfully deleted service', + status: 200, + }); } } -module.exports = ServiceController +module.exports = ServiceController; diff --git a/app/Controllers/Http/StaticController.js b/app/Controllers/Http/StaticController.js index 17b641f..b16e6cb 100644 --- a/app/Controllers/Http/StaticController.js +++ b/app/Controllers/Http/StaticController.js @@ -1,4 +1,4 @@ -'use strict' + /** * Controller for routes with static responses */ @@ -6,220 +6,219 @@ class StaticController { // Enable all features features({ - response + response, }) { return response.send({ - "needToWaitToProceed": false, - "isSpellcheckerPremiumFeature": true, - "isServiceProxyEnabled": true, - "isServiceProxyPremiumFeature": true, - "isWorkspacePremiumFeature": true, - "isWorkspaceEnabled": true, - "isAnnouncementsEnabled": true, - "isSettingsWSEnabled": false, - "isServiceLimitEnabled": false, - "serviceLimitCount": 0, - "isCommunityRecipesPremiumFeature": false - }) + needToWaitToProceed: false, + isSpellcheckerPremiumFeature: true, + isServiceProxyEnabled: true, + isServiceProxyPremiumFeature: true, + isWorkspacePremiumFeature: true, + isWorkspaceEnabled: true, + isAnnouncementsEnabled: true, + isSettingsWSEnabled: false, + isServiceLimitEnabled: false, + serviceLimitCount: 0, + isCommunityRecipesPremiumFeature: false, + }); } // Return an empty array emptyArray({ - response + response, }) { - return response.send([]) + return response.send([]); } // Payment plans availible plans({ - response + response, }) { return response.send({ - "month": { - "id": "franz-supporter-license", - "price": 99 - }, - "year": { - "id": "franz-supporter-license-year-2019", - "price": 99 - } - }) + month: { + id: 'franz-supporter-license', + price: 99, + }, + year: { + id: 'franz-supporter-license-year-2019', + price: 99, + }, + }); } // Return list of popular recipes (copy of the response Franz's API is returning) popularRecipes({ - response + response, }) { return response.send([{ - "author": "Stefan Malzner ", - "featured": false, - "id": "slack", - "name": "Slack", - "version": "1.0.4", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/slack/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/slack/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'slack', + name: 'Slack', + version: '1.0.4', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/slack/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/slack/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "whatsapp", - "name": "WhatsApp", - "version": "1.0.1", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/whatsapp/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/whatsapp/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'whatsapp', + name: 'WhatsApp', + version: '1.0.1', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/whatsapp/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/whatsapp/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "messenger", - "name": "Messenger", - "version": "1.0.6", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/messenger/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/messenger/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'messenger', + name: 'Messenger', + version: '1.0.6', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/messenger/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/messenger/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "telegram", - "name": "Telegram", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/telegram/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/telegram/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'telegram', + name: 'Telegram', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/telegram/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/telegram/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "gmail", - "name": "Gmail", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/gmail/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/gmail/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'gmail', + name: 'Gmail', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/gmail/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/gmail/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "skype", - "name": "Skype", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/skype/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/skype/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'skype', + name: 'Skype', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/skype/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/skype/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "hangouts", - "name": "Hangouts", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/hangouts/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/hangouts/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'hangouts', + name: 'Hangouts', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/hangouts/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/hangouts/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "discord", - "name": "Discord", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/discord/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/discord/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'discord', + name: 'Discord', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/discord/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/discord/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "tweetdeck", - "name": "Tweetdeck", - "version": "1.0.1", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/tweetdeck/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/tweetdeck/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'tweetdeck', + name: 'Tweetdeck', + version: '1.0.1', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/tweetdeck/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/tweetdeck/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "hipchat", - "name": "HipChat", - "version": "1.0.1", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/hipchat/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/hipchat/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'hipchat', + name: 'HipChat', + version: '1.0.1', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/hipchat/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/hipchat/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "gmailinbox", - "name": "Inbox by Gmail", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/gmailinbox/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/gmailinbox/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'gmailinbox', + name: 'Inbox by Gmail', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/gmailinbox/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/gmailinbox/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "rocketchat", - "name": "Rocket.Chat", - "version": "1.0.1", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/rocketchat/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/rocketchat/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'rocketchat', + name: 'Rocket.Chat', + version: '1.0.1', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/rocketchat/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/rocketchat/src/icon.svg', + }, }, { - "author": "Brian Gilbert ", - "featured": false, - "id": "gitter", - "name": "Gitter", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/gitter/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/gitter/src/icon.svg" - } + author: 'Brian Gilbert ', + featured: false, + id: 'gitter', + name: 'Gitter', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/gitter/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/gitter/src/icon.svg', + }, }, { - "author": "Stefan Malzner ", - "featured": false, - "id": "mattermost", - "name": "Mattermost", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/mattermost/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/mattermost/src/icon.svg" - } + author: 'Stefan Malzner ', + featured: false, + id: 'mattermost', + name: 'Mattermost', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/mattermost/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/mattermost/src/icon.svg', + }, }, { - "author": "Franz ", - "featured": false, - "id": "toggl", - "name": "toggl", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/toggl/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/toggl/src/icon.svg" - } + author: 'Franz ', + featured: false, + id: 'toggl', + name: 'toggl', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/toggl/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/toggl/src/icon.svg', + }, }, { - "author": "Stuart Clark ", - "featured": false, - "id": "twist", - "name": "twist", - "version": "1.0.0", - "icons": { - "png": "https://cdn.franzinfra.com/recipes/dist/twist/src/icon.png", - "svg": "https://cdn.franzinfra.com/recipes/dist/twist/src/icon.svg" - } - }]) + author: 'Stuart Clark ', + featured: false, + id: 'twist', + name: 'twist', + version: '1.0.0', + icons: { + png: 'https://cdn.franzinfra.com/recipes/dist/twist/src/icon.png', + svg: 'https://cdn.franzinfra.com/recipes/dist/twist/src/icon.svg', + }, + }]); } // Show announcements announcement({ response, - params }) { return response.send('No announcement found.'); } } -module.exports = StaticController +module.exports = StaticController; diff --git a/app/Controllers/Http/UserController.js b/app/Controllers/Http/UserController.js index ced27bb..1e67092 100644 --- a/app/Controllers/Http/UserController.js +++ b/app/Controllers/Http/UserController.js @@ -1,12 +1,10 @@ -'use strict' - const User = use('App/Models/User'); const Service = use('App/Models/Service'); const Workspace = use('App/Models/Workspace'); const { - validateAll + validateAll, } = use('Validator'); -const Env = use('Env') +const Env = use('Env'); const atob = require('atob'); const btoa = require('btoa'); @@ -14,49 +12,44 @@ const fetch = require('node-fetch'); const uuid = require('uuid/v4'); const crypto = require('crypto'); -const franzRequest = async (route, method, auth) => { - return new Promise(async (resolve, reject) => { - const base = 'https://api.franzinfra.com/v1/'; - 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'; - - try { - const rawResponse = await fetch(base + route, { - method, - headers: { - 'Authorization': 'Bearer ' + auth, - 'User-Agent': user - }, - }); - const content = await rawResponse.json(); - - resolve(content); - } catch (e) { - reject(); - } - }) -} +const franzRequest = (route, method, auth) => new Promise((resolve, reject) => { + const base = 'https://api.franzinfra.com/v1/'; + 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'; + + try { + fetch(base + route, { + method, + headers: { + Authorization: `Bearer ${auth}`, + 'User-Agent': user, + }, + }) + .then((data) => data.json()) + .then((json) => resolve(json)); + } catch (e) { + reject(); + } +}); class UserController { - // Register a new user async signup({ request, response, auth, - session }) { // Validate user input const validation = await validateAll(request.all(), { firstname: 'required', email: 'required|email|unique:users,email', - password: 'required' + password: 'required', }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const data = request.only(['firstname', 'email', 'password']); @@ -67,21 +60,21 @@ class UserController { user = await User.create({ email: data.email, password: data.password, - username: data.firstname + username: data.firstname, }); } catch (e) { return response.status(401).send({ - "message": "E-Mail Address already in use", - "status": 401 - }) + message: 'E-Mail Address already in use', + status: 401, + }); } // Generate new auth token - const token = await auth.generate(user) + const token = await auth.generate(user); return response.send({ - "message": "Successfully created account", - "token": token.token + message: 'Successfully created account', + token: token.token, }); } @@ -89,115 +82,112 @@ class UserController { async login({ request, response, - auth + auth, }) { if (!request.header('Authorization')) { return response.status(401).send({ - "message": "Please provide authorization", - "status": 401 - }) + message: 'Please provide authorization', + status: 401, + }); } // Get auth data from auth token const authHeader = atob(request.header('Authorization').replace('Basic ', '')).split(':'); // Check if user with email exists - let user = (await User.query().where('email', authHeader[0]).first()); + const user = (await User.query().where('email', authHeader[0]).first()); if (!user || !user.email) { return response.status(401).send({ - "message": "User credentials not valid (Invalid mail)", - "code": "invalid-credentials", - "status": 401 + message: 'User credentials not valid (Invalid mail)', + code: 'invalid-credentials', + status: 401, }); } // Try to login let token; try { - token = await auth.attempt(user.email, authHeader[1]) + token = await auth.attempt(user.email, authHeader[1]); } catch (e) { return response.status(401).send({ - "message": "User credentials not valid", - "code": "invalid-credentials", - "status": 401 + message: 'User credentials not valid', + code: 'invalid-credentials', + status: 401, }); } return response.send({ - "message": "Successfully logged in", - "token": token.token + message: 'Successfully logged in', + token: token.token, }); } // Return information about the current user async me({ - request, response, auth, - session }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - response.send('Missing or invalid api token') + response.send('Missing or invalid api token'); } return response.send({ - accountType: "individual", + accountType: 'individual', beta: false, donor: {}, email: auth.user.email, emailValidated: true, features: {}, - firstname: "Franz", - id: "82c1cf9d-ab58-4da2-b55e-aaa41d2142d8", + firstname: 'Franz', + id: '82c1cf9d-ab58-4da2-b55e-aaa41d2142d8', isPremium: true, isSubscriptionOwner: true, - lastname: "Franz", - locale: "en-US" + lastname: 'Franz', + locale: 'en-US', }); } - async import({ request, - response + response, }) { // Validate user input const validation = await validateAll(request.all(), { email: 'required|email|unique:users,email', - password: 'required' + password: 'required', }); if (validation.fails()) { - let errorMessage = "There was an error while trying to import your account:\n"; + let errorMessage = 'There was an error while trying to import your account:\n'; for (const message of validation.messages()) { - if (message.validation == 'required') { - errorMessage += '- Please make sure to supply your ' + message.field + '\n' - } else if (message.validation == 'unique') { - errorMessage += '- There is already a user with this email.\n' + if (message.validation === 'required') { + errorMessage += `- Please make sure to supply your ${message.field}\n`; + } else if (message.validation === 'unique') { + errorMessage += '- There is already a user with this email.\n'; } else { - errorMessage += message.message + '\n'; + errorMessage += `${message.message}\n`; } } - return response.status(401).send(errorMessage) + return response.status(401).send(errorMessage); } const { email, - password - } = request.all() + password, + } = request.all(); const hashedPassword = crypto.createHash('sha256').update(password).digest('base64'); - - if(Env.get('CONNECT_WITH_FRANZ') == 'false') { + + if (Env.get('CONNECT_WITH_FRANZ') == 'false') { // eslint-disable-line eqeqeq await User.create({ email, password: hashedPassword, - username: 'Franz' + username: 'Franz', }); - return response.send('Your account has been created but due to this server\'s configuration, we could not import your Franz account data.\n\nIf you are the server owner, please set CONNECT_WITH_FRANZ to true to enable account imports.') + return response.send('Your account has been created but due to this server\'s configuration, we could not import your Franz account data.\n\nIf you are the server owner, please set CONNECT_WITH_FRANZ to true to enable account imports.'); } const base = 'https://api.franzinfra.com/v1/'; @@ -206,42 +196,41 @@ class UserController { // Try to get an authentication token let token; try { - const basicToken = btoa(email + ':' + hashedPassword) + const basicToken = btoa(`${email}:${hashedPassword}`); - const rawResponse = await fetch(base + 'auth/login', { + const rawResponse = await fetch(`${base}auth/login`, { method: 'POST', headers: { - 'Authorization': 'Basic ' + basicToken, - 'User-Agent': userAgent + Authorization: `Basic ${basicToken}`, + 'User-Agent': userAgent, }, }); const content = await rawResponse.json(); if (!content.message || content.message !== 'Successfully logged in') { const errorMessage = 'Could not login into Franz with your supplied credentials. Please check and try again'; - return response.status(401).send(errorMessage) + return response.status(401).send(errorMessage); } token = content.token; } catch (e) { return response.status(401).send({ - "message": "Cannot login to Franz", - "error": e - }) + message: 'Cannot login to Franz', + error: e, + }); } // Get user information let userInf = false; try { - userInf = await franzRequest('me', 'GET', token) - console.log('A', userInf) + userInf = await franzRequest('me', 'GET', token); } catch (e) { - const errorMessage = 'Could not get your user info from Franz. Please check your credentials or try again later.\nError: ' + e; - return response.status(401).send(errorMessage) + const errorMessage = `Could not get your user info from Franz. Please check your credentials or try again later.\nError: ${e}`; + return response.status(401).send(errorMessage); } if (!userInf) { - const errorMessage = 'Could not get your user info from Franz. Please check your credentials or try again later.\nError: ' + e; - return response.status(401).send(errorMessage) + const errorMessage = 'Could not get your user info from Franz. Please check your credentials or try again later'; + return response.status(401).send(errorMessage); } // Create user in DB @@ -250,69 +239,69 @@ class UserController { user = await User.create({ email: userInf.email, password: hashedPassword, - username: userInf.firstname + username: userInf.firstname, }); } catch (e) { - const errorMessage = 'Could not create your user in our system.\nError: ' + e; - return response.status(401).send(errorMessage) + const errorMessage = `Could not create your user in our system.\nError: ${e}`; + return response.status(401).send(errorMessage); } - let serviceIdTranslation = {}; + const serviceIdTranslation = {}; // Import services try { - const services = await franzRequest('me/services', 'GET', token) + const services = await franzRequest('me/services', 'GET', token); for (const service of services) { // Get new, unused uuid let serviceId; do { serviceId = uuid(); - } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0) + } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop - await Service.create({ + await Service.create({ // eslint-disable-line no-await-in-loop userId: user.id, serviceId, name: service.name, recipeId: service.recipeId, - settings: JSON.stringify(service) + settings: JSON.stringify(service), }); serviceIdTranslation[service.id] = serviceId; } } catch (e) { - const errorMessage = 'Could not import your services into our system.\nError: ' + e; - return response.status(401).send(errorMessage) + const errorMessage = `Could not import your services into our system.\nError: ${e}`; + return response.status(401).send(errorMessage); } // Import workspaces try { - const workspaces = await franzRequest('workspace', 'GET', token) + const workspaces = await franzRequest('workspace', 'GET', token); for (const workspace of workspaces) { let workspaceId; do { workspaceId = uuid(); - } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0) + } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop - const services = workspace.services.map(service => serviceIdTranslation[service]) + const services = workspace.services.map((service) => serviceIdTranslation[service]); - await Workspace.create({ - userId: auth.user.id, + await Workspace.create({ // eslint-disable-line no-await-in-loop + userId: user.id, workspaceId, name: workspace.name, order: workspace.order, services: JSON.stringify(services), - data: JSON.stringify({}) + data: JSON.stringify({}), }); } } catch (e) { - const errorMessage = 'Could not import your workspaces into our system.\nError: ' + e; - return response.status(401).send(errorMessage) + const errorMessage = `Could not import your workspaces into our system.\nError: ${e}`; + return response.status(401).send(errorMessage); } - return response.send('Your account has been imported. You can now use your Franz account in Ferdi.') + return response.send('Your account has been imported. You can now use your Franz account in Ferdi.'); } } -module.exports = UserController +module.exports = UserController; diff --git a/app/Controllers/Http/WorkspaceController.js b/app/Controllers/Http/WorkspaceController.js index b64d858..ecf79af 100644 --- a/app/Controllers/Http/WorkspaceController.js +++ b/app/Controllers/Http/WorkspaceController.js @@ -1,8 +1,7 @@ -'use strict' const Workspace = use('App/Models/Workspace'); const { - validateAll + validateAll, } = use('Validator'); const uuid = require('uuid/v4'); @@ -12,12 +11,12 @@ class WorkspaceController { async create({ request, response, - auth + auth, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } // Validate user input @@ -26,10 +25,10 @@ class WorkspaceController { }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const data = request.all(); @@ -38,7 +37,7 @@ class WorkspaceController { let workspaceId; do { workspaceId = uuid(); - } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0) + } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop const order = (await auth.user.workspaces().fetch()).rows.length; @@ -48,7 +47,7 @@ class WorkspaceController { name: data.name, order, services: JSON.stringify([]), - data: JSON.stringify(data) + data: JSON.stringify(data), }); return response.send({ @@ -57,37 +56,37 @@ class WorkspaceController { id: workspaceId, order, workspaces: [], - }) + }); } async edit({ request, response, auth, - params + params, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } // Validate user input const validation = await validateAll(request.all(), { name: 'required|alpha', - services: 'required|array' + services: 'required|array', }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const data = request.all(); const { - id + id, } = params; // Update data in database @@ -95,7 +94,7 @@ class WorkspaceController { .where('workspaceId', id) .where('userId', auth.user.id)).update({ name: data.name, - services: JSON.stringify(data.services) + services: JSON.stringify(data.services), }); // Get updated row @@ -104,24 +103,24 @@ class WorkspaceController { .where('userId', auth.user.id).fetch()).rows[0]; return response.send({ - "id": workspace.workspaceId, - "name": data.name, - "order": workspace.order, - "services": data.services, - "userId": auth.user.id - }) + id: workspace.workspaceId, + name: data.name, + order: workspace.order, + services: data.services, + userId: auth.user.id, + }); } async delete({ request, response, auth, - params + params, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } // Validate user input @@ -130,14 +129,14 @@ class WorkspaceController { }); if (validation.fails()) { return response.status(401).send({ - "message": "Invalid POST arguments", - "messages": validation.messages(), - "status": 401 - }) + message: 'Invalid POST arguments', + messages: validation.messages(), + status: 401, + }); } const { - id + id, } = params; // Update data in database @@ -146,38 +145,37 @@ class WorkspaceController { .where('userId', auth.user.id)).delete(); return response.send({ - "message": "Successfully deleted workspace", - }) + message: 'Successfully deleted workspace', + }); } // List all workspaces a user has created async list({ - request, response, - auth + auth, }) { try { - await auth.getUser() + await auth.getUser(); } catch (error) { - return response.send('Missing or invalid api token') + return response.send('Missing or invalid api token'); } const workspaces = (await auth.user.workspaces().fetch()).rows; // Convert to array with all data Franz wants let workspacesArray = []; - if(workspaces) { - workspacesArray = workspaces.map(workspace => ({ - "id": workspace.workspaceId, - "name": workspace.name, - "order": workspace.order, - "services": JSON.parse(workspace.services), - "userId": auth.user.id - })) + if (workspaces) { + workspacesArray = workspaces.map((workspace) => ({ + id: workspace.workspaceId, + name: workspace.name, + order: workspace.order, + services: JSON.parse(workspace.services), + userId: auth.user.id, + })); } - - return response.send(workspacesArray) + + return response.send(workspacesArray); } } -module.exports = WorkspaceController +module.exports = WorkspaceController; -- cgit v1.2.3-54-g00ecf