From 738a8c53fa8ae3f74f88cb0398b646f61ef8f8d8 Mon Sep 17 00:00:00 2001 From: Evert Date: Sun, 10 Sep 2017 12:42:12 +0300 Subject: [PATCH] some cleanup and stuff --- server/api/external.js | 110 +++++++++++++++-------------------------- server/api/index.js | 4 +- server/api/news.js | 2 +- 3 files changed, 44 insertions(+), 72 deletions(-) diff --git a/server/api/external.js b/server/api/external.js index 60fabf1..d6311c3 100644 --- a/server/api/external.js +++ b/server/api/external.js @@ -44,6 +44,34 @@ const API = { await await models.External.query().insert(data) return true }, + newUser: async (service, identifier, data) => { + let udataLimited = Object.assign({ + activated: 1, + created_at: new Date(), + updated_at: new Date() + }, data) + + // Check if the username is already taken + if (await UAPI.User.get(udataLimited.username) != null) { + udataLimited.username = udataLimited.username + UAPI.Hash(4) + } + + // Check if the email given to us is already registered, if so, + // associate an external node with the user bearing the email + if (udataLimited.email && udataLimited.email !== '') { + let getByEmail = await UAPI.User.get(udataLimited.email) + if (getByEmail) { + await API.Common.new(service, identifier, getByEmail) + return {error: null, user: getByEmail} + } + } + + // Create a new user based on the information we got from Facebook + let newUser = await models.User.query().insert(udataLimited) + await API.Common.new(service, identifier, newUser) + + return newUser + }, remove: async (user, service) => { user = await UAPI.User.ensureObject(user, ['password']) let userExterns = await models.External.query().orderBy('created_at', 'asc').where('user_id', user.id) @@ -141,36 +169,16 @@ const API = { } } - // Create a new user - let udataLimited = { + let newUData = { username: fbdata.short_name || 'FB' + UAPI.Hash(4), display_name: fbdata.name, email: fbdata.email || '', avatar_file: profilepic, - activated: 1, - ip_address: data.ip_address, - created_at: new Date(), - updated_at: new Date() + ip_address: data.ip_address } - // Check if the username is already taken - if (await UAPI.User.get(udataLimited.username) != null) { - udataLimited.username = udataLimited.username + UAPI.Hash(4) - } - - // Check if the email Facebook gave us is already registered, if so, - // associate an external node with the user bearing the email - if (udataLimited.email && udataLimited.email !== '') { - let getByEmail = await UAPI.User.get(udataLimited.email) - if (getByEmail) { - await API.Common.new('fb', getByEmail.id, getByEmail) - return {error: null, user: getByEmail} - } - } - - // Create a new user based on the information we got from Facebook - let newUser = await models.User.query().insert(udataLimited) - await API.Common.new('fb', uid, newUser) + let newUser = await API.Common.newUser('fb', uid, newUData) + if (!newUser) return {error: 'Failed to create user.'} return {error: null, user: newUser} } @@ -268,35 +276,16 @@ const API = { } // Create a new user - let udataLimited = { + let newUData = { username: twdata.screen_name, display_name: twdata.name, email: twdata.email || '', avatar_file: profilepic, - activated: 1, - ip_address: ipAddress, - updated_at: new Date(), - created_at: new Date() + ip_address: ipAddress } - // Check if the username is already taken - if (await UAPI.User.get(udataLimited.username) != null) { - udataLimited.username = udataLimited.username + UAPI.Hash(4) - } - - // Check if the email Twitter gave us is already registered, if so, - // associate an external node with the user bearing the email - if (udataLimited.email && udataLimited.email !== '') { - let getByEmail = await UAPI.User.get(udataLimited.email) - if (getByEmail) { - await API.Common.new('twitter', getByEmail.id, getByEmail) - return {error: null, user: getByEmail} - } - } - - // Create a new user based on the information we got from Twitter - let newUser = await models.User.query().insert(udataLimited) - await API.Common.new('twitter', uid, newUser) + let newUser = await API.Common.newUser('twitter', uid, newUData) + if (!newUser) return {error: 'Failed to create user.'} return {error: null, user: newUser} } @@ -401,35 +390,16 @@ const API = { } // Create a new user - let udataLimited = { + let newUData = { username: ddata.username.replace(/\W+/gi, '_'), display_name: ddata.username, email: ddata.email || '', avatar_file: profilepic, - activated: 1, - ip_address: ipAddress, - updated_at: new Date(), - created_at: new Date() + ip_address: ipAddress } - // Check if the username is already taken - if (await UAPI.User.get(udataLimited.username) != null) { - udataLimited.username = udataLimited.username + UAPI.Hash(4) - } - - // Check if the email Discord gave us is already registered, if so, - // associate an external node with the user bearing the email - if (udataLimited.email && udataLimited.email !== '') { - let getByEmail = await UAPI.User.get(udataLimited.email) - if (getByEmail) { - await API.Common.new('discord', uid, getByEmail) - return {error: null, user: getByEmail} - } - } - - // Create a new user based on the information we got from Discord - let newUser = await models.User.query().insert(udataLimited) - await API.Common.new('discord', uid, newUser) + let newUser = await API.Common.newUser('discord', uid, newUData) + if (!newUser) return {error: 'Failed to create user.'} return {error: null, user: newUser} } diff --git a/server/api/index.js b/server/api/index.js index 9f8149e..575225d 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -14,6 +14,7 @@ const fs = Promise.promisifyAll(require('fs')) const emailRe = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ +// Fork a bcrypt process to hash and compare passwords function bcryptTask (data) { return new Promise((resolve, reject) => { let proc = cprog.fork(path.join(__dirname, '../../scripts', 'bcrypt.js')) @@ -39,6 +40,7 @@ function bcryptTask (data) { }) } +// Make sure an object contains the keys specified in `required` function keysAvailable (object, required) { let found = true @@ -52,7 +54,7 @@ function keysAvailable (object, required) { return found } -// Clean up the donation endpoint for ease of use +// Clean up the donation responses for ease of use async function cleanUpDonation (obj, mcOnly, timeframe) { if (timeframe && new Date(obj.created_at).getTime() < timeframe) { return null diff --git a/server/api/news.js b/server/api/news.js index 99db55c..4814d1d 100644 --- a/server/api/news.js +++ b/server/api/news.js @@ -4,7 +4,7 @@ import Models from './models' const perPage = 8 function slugify (title) { - return title.toLowerCase().replace(/\W/g, '-').substring(0, 16) + return title.toLowerCase().replace(/\W/g, '-').substring(0, 32) } async function cleanArticle (entry, shortenContent = false) {