Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (request.query.conversation) {
let me = result.users.find(function (user) {
return user._id.toString() === request.auth.credentials.user._id.toString()
})
if (!me) {
return reply(Boom.badRequest('Current user is not part of this conversation.'));
}
return result
}
else {
// EXPL: If the conversation doesn't exist, create it
if (!result.docs[0]) {
newConversation = true;
let promises = [];
let users = [request.auth.credentials.user._id, request.query.user];
promises.push(RestHapi.create(Conversation, { users, chatType: CHAT_TYPES.DIRECT }, Log));
promises.push(RestHapi.list(User, { _id: users, $select: ['_id', 'firstName', 'lastName', 'profileImageUrl'] }, Log));
return Q.all(promises);
}
else {
return result.docs[0];
}
}
})
.then(function(result) {
const recordVisitorHandler = async function(request, h) {
try {
// Specify the iplocation hosts to prevent issues (Ex: docker cant ping "https://ipaip.co/" by default)
// let hosts = ['freegeoip.net', 'ipapi.co']
// NOTE: Sign up for free access key at https://ipstack.com/
let host =
'http://api.ipstack.com/*?access_key=' +
Config.get('/ipstackAccessKey') +
'&format=1'
let result = await iplocation(server.methods.getIP(request), [host])
const agent = useragent.parse(request.headers['user-agent'])
const visitor = Object.assign(result, { browser: agent.family })
return RestHapi.create(Visitor, visitor, Log)
} catch (err) {
errorHelper.handleError(err, Log)
}
}
.then(function(result) {
const agent = useragent.parse(request.headers['user-agent']);
const visitor = Object.assign(result, {browser: agent.family});
return RestHapi.create(Visitor, visitor, Log)
})
.then(function(result) {
if (rand > 0 && rand <= 0.4) {
browser = 'Chrome'
} else if (rand > 0.4 && rand <= 0.7) {
browser = 'Firefox'
} else if (rand > 0.7 && rand <= 0.8) {
browser = 'Safari'
} else if (rand > 0.8 && rand <= 0.95) {
browser = 'IE'
}
visitor.browser = browser
if (visitor.error || visitor.message || !visitor.ip) {
Log.debug('iplocation failed:', visitor)
} else {
return RestHapi.create(models.visitor, visitor, Log)
}
}
secondaryPayload.primaryUser = payload.connectedUser
}
if (payload.primaryUser) {
secondaryPayload.connectedUser = payload.primaryUser
}
if (payload.isContact) {
secondaryPayload.isContact = payload.isContact
}
if (payload.isFollowed) {
secondaryPayload.isFollowing = payload.isFollowed
}
if (payload.isFollowing) {
secondaryPayload.isFollowed = payload.isFollowing
}
await RestHapi.create(Connection, secondaryPayload, Log)
Notification.createConnectionNotification(
payload,
payload,
request.server,
Log
)
return payload
} else {
delete payload.isSecondary
return payload
}
} catch (err) {
errorHelper.handleError(err, Log)
}
}
const Log = logger.bind()
try {
const Notification = mongoose.model('notification')
const User = mongoose.model('user')
let notification = {
primaryUser: connnection.connectedUser,
actingUser: connnection.primaryUser
}
if (connectionPayload.isContact) {
notification.type = NOTIFICATION_TYPES.CONTACT
} else if (connectionPayload.isFollowing) {
notification.type = NOTIFICATION_TYPES.FOLLOW
}
if (notification.type) {
let promises = []
promises.push(RestHapi.create(Notification, notification, Log))
promises.push(
RestHapi.find(
User,
notification.actingUser,
{ $select: ['firstName', 'lastName', 'profileImageUrl'] },
Log
)
)
let result = await Promise.all(promises)
let notification = result[0]
notification.actingUser = result[1]
server.publish(
'/notification/' + notification.primaryUser,
notification
)
}
const postMessageHandler = function (request, reply) {
const Message = mongoose.model('message');
const Conversation = mongoose.model('conversation');
const User = mongoose.model('user');
const promises = [];
const payload = {
text: request.payload.text,
conversation: request.params.conversationId,
user: request.auth.credentials.user._id
}
promises.push(RestHapi.find(Conversation, payload.conversation, {}, Log));
promises.push(RestHapi.create(Message, payload, Log));
return Q.all(promises)
.then(function (result) {
let conversation = result[0];
let message = result[1];
Log.debug("MESSAGE:", message);
conversation.users.forEach(function(userId) {
server.publish('/chat/' + userId, message);
})
return reply('published');
})
.catch(function (error) {
Log.error(error);
return reply(RestHapi.errorHelper.formatResponse(error));
});
};
.then(function (result) {
keyHash = result;
user = request.payload.user;
originalPassword = user.password;
user.role = request.pre.role._id;
user.isActive = false;
user.activateAccount = {
token: keyHash.hash,
expires: Date.now() + 10000000 //TODO: set token expiration in config
};
return RestHapi.create(User, user, Log);
})
.then(function (result) {
name: USER_ROLES.USER,
rank: 2,
description: 'A standard user account.'
},
{
name: USER_ROLES.ADMIN,
rank: 1,
description: 'A user with advanced permissions.'
},
{
name: USER_ROLES.SUPER_ADMIN,
rank: 0,
description: 'A user with full permissions.'
}
]
return RestHapi.create(models.role, roles, Log)
})
.then(function (result) {
Log.log('seeding groups')
groups = [
{
name: 'Read Only',
description: 'Group that excludes all permissions except for Admin level read permissions.'
},
{
name: 'Editor',
description: 'Group that forbids all creating.'
},
{
name: 'Super User',
description: 'Group with full permissions except root. Role restrictions remain.'
}
]
return RestHapi.create(models.group, groups, Log)
})
.then(function (result) {