Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
else if (request.query.user) {
if (request.query.user === request.auth.credentials.user._id.toString()) {
return reply(Boom.badRequest('No chatting with yourself.'))
}
// EXPL: The query below searches for the direct chat conversation between the current user and the user provided
query.$where = {
$and: [
{ users: { $elemMatch: { $eq: request.auth.credentials.user._id } } },
{ users: { $elemMatch: { $eq: request.query.user } } },
{ chatType: { $eq: CHAT_TYPES.DIRECT } }
]
};
promise = RestHapi.list(Conversation, query, Log)
}
else {
return reply(Boom.badRequest('Must provide either conversation or user query params.'));
}
return promise
.then(function(result) {
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
}
const dashboardStatsHandler = function (request, reply) {
let promises = [];
let stats = {};
promises.push(RestHapi.list(User, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Document, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Image, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Message, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Visitor, { isDeleted: false, $count: true }, Log))
return Q.all(promises)
.then(function(result) {
stats = {
userCount: result[0],
documentCount: result[1],
imageCount: result[2],
messageCount: result[3],
visitorCount: result[4]
}
promises = [];
let step = {};
// MONGO AGGREGATION PIPELINE EXAMPLE
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 dashboardStatsHandler = function (request, reply) {
let promises = [];
let stats = {};
promises.push(RestHapi.list(User, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Document, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Image, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Message, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Visitor, { isDeleted: false, $count: true }, Log))
return Q.all(promises)
.then(function(result) {
stats = {
userCount: result[0],
documentCount: result[1],
imageCount: result[2],
messageCount: result[3],
visitorCount: result[4]
}
promises = [];
let step = {};
pre: async function(payload, request, logger) {
const Log = logger.bind()
try {
const Image = mongoose.model('image')
payload.owner = request.auth.credentials.user._id
let result = await RestHapi.list(
Image,
{ owner: payload.owner, $sort: ['index'] },
Log
)
if (result.docs[0]) {
payload.index = result.docs[0].index + 1
} else {
payload.index = 0
}
return payload
} catch (err) {
errorHelper.handleError(err, Log)
}
}
}
.then(function (result) {
Log.log('setting associations')
return RestHapi.list(models.permission, {}, Log)
.then(function (result) {
permissions = result.docs
permissionNames = permissions.map(function(p) { return p.name })
promises = []
let userBasePermissionNames = [
'readUser',
'readVisitor',
'createConnection',
'readConnection',
'updateConnection',
'readUserConnectionStats',
'receiveChatMessages',
'markConversationAsRead',
'markConversationAsUnread',
const getConversationsHandler = function (request, reply) {
const Conversation = mongoose.model('conversation');
const query = { $where: { users: { $elemMatch: { $eq: request.auth.credentials.user._id } } } }
query.$embed = ['users', 'lastMessage.user', 'userData'];
query.$sort = ['-updatedAt'];
return RestHapi.list(Conversation, query, Log)
.then(function (result) {
result.docs.forEach(function(conversation) {
formatConversation(request, conversation);
})
return reply(result)
})
.catch(function (error) {
Log.error(error);
return reply(RestHapi.errorHelper.formatResponse(error));
});
};
const dashboardStatsHandler = function (request, reply) {
let promises = [];
let stats = {};
promises.push(RestHapi.list(User, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Document, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Image, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Message, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Visitor, { isDeleted: false, $count: true }, Log))
return Q.all(promises)
.then(function(result) {
stats = {
userCount: result[0],
documentCount: result[1],
imageCount: result[2],
messageCount: result[3],
visitorCount: result[4]
}
promises = [];
let step = {};
const dashboardStatsHandler = function (request, reply) {
let promises = [];
let stats = {};
promises.push(RestHapi.list(User, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Document, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Image, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Message, { isDeleted: false, $count: true }, Log))
promises.push(RestHapi.list(Visitor, { isDeleted: false, $count: true }, Log))
return Q.all(promises)
.then(function(result) {
stats = {
userCount: result[0],
documentCount: result[1],
imageCount: result[2],
messageCount: result[3],
visitorCount: result[4]
}
promises = [];
.then(function (result) {
user = result[0] ? result[0] : result[1];
if (user) {
user.facebookId = facebookProfile.id;
reply(user);
throw 'Found User';
}
else {
return RestHapi.list(Role, { name: USER_ROLES.USER }, Log)
}
})
.then(function(role) {