some cleanup and stuff

This commit is contained in:
Evert Prants 2017-09-10 12:42:12 +03:00
parent a03af7ca0f
commit 738a8c53fa
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 44 additions and 72 deletions

View File

@ -44,6 +44,34 @@ const API = {
await await models.External.query().insert(data) await await models.External.query().insert(data)
return true 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) => { remove: async (user, service) => {
user = await UAPI.User.ensureObject(user, ['password']) user = await UAPI.User.ensureObject(user, ['password'])
let userExterns = await models.External.query().orderBy('created_at', 'asc').where('user_id', user.id) 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 newUData = {
let udataLimited = {
username: fbdata.short_name || 'FB' + UAPI.Hash(4), username: fbdata.short_name || 'FB' + UAPI.Hash(4),
display_name: fbdata.name, display_name: fbdata.name,
email: fbdata.email || '', email: fbdata.email || '',
avatar_file: profilepic, avatar_file: profilepic,
activated: 1, ip_address: data.ip_address
ip_address: data.ip_address,
created_at: new Date(),
updated_at: new Date()
} }
// Check if the username is already taken let newUser = await API.Common.newUser('fb', uid, newUData)
if (await UAPI.User.get(udataLimited.username) != null) { if (!newUser) return {error: 'Failed to create user.'}
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)
return {error: null, user: newUser} return {error: null, user: newUser}
} }
@ -268,35 +276,16 @@ const API = {
} }
// Create a new user // Create a new user
let udataLimited = { let newUData = {
username: twdata.screen_name, username: twdata.screen_name,
display_name: twdata.name, display_name: twdata.name,
email: twdata.email || '', email: twdata.email || '',
avatar_file: profilepic, avatar_file: profilepic,
activated: 1, ip_address: ipAddress
ip_address: ipAddress,
updated_at: new Date(),
created_at: new Date()
} }
// Check if the username is already taken let newUser = await API.Common.newUser('twitter', uid, newUData)
if (await UAPI.User.get(udataLimited.username) != null) { if (!newUser) return {error: 'Failed to create user.'}
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)
return {error: null, user: newUser} return {error: null, user: newUser}
} }
@ -401,35 +390,16 @@ const API = {
} }
// Create a new user // Create a new user
let udataLimited = { let newUData = {
username: ddata.username.replace(/\W+/gi, '_'), username: ddata.username.replace(/\W+/gi, '_'),
display_name: ddata.username, display_name: ddata.username,
email: ddata.email || '', email: ddata.email || '',
avatar_file: profilepic, avatar_file: profilepic,
activated: 1, ip_address: ipAddress
ip_address: ipAddress,
updated_at: new Date(),
created_at: new Date()
} }
// Check if the username is already taken let newUser = await API.Common.newUser('discord', uid, newUData)
if (await UAPI.User.get(udataLimited.username) != null) { if (!newUser) return {error: 'Failed to create user.'}
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)
return {error: null, user: newUser} return {error: null, user: newUser}
} }

View File

@ -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,}))$/ 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) { function bcryptTask (data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let proc = cprog.fork(path.join(__dirname, '../../scripts', 'bcrypt.js')) 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) { function keysAvailable (object, required) {
let found = true let found = true
@ -52,7 +54,7 @@ function keysAvailable (object, required) {
return found 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) { async function cleanUpDonation (obj, mcOnly, timeframe) {
if (timeframe && new Date(obj.created_at).getTime() < timeframe) { if (timeframe && new Date(obj.created_at).getTime() < timeframe) {
return null return null

View File

@ -4,7 +4,7 @@ import Models from './models'
const perPage = 8 const perPage = 8
function slugify (title) { 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) { async function cleanArticle (entry, shortenContent = false) {