Example #1
0
            collection.update(itemCriteria, changes, options, function (err, count) {

                if (err === null) {

                    if (id) {

                        if (count) {

                            callback(null);
                        }
                        else {

                            callback(Err.database('No document found to update', collectionName, 'updateCriteria', [id, itemCriteria, changes, options]));
                        }
                    }
                    else {

                        callback(null);
                    }
                }
                else {

                    callback(Err.database(err, collectionName, 'updateCriteria', [id, itemCriteria, changes, options]));
                }
            });
Example #2
0
                cursor.toArray(function (err, results) {

                    if (err === null) {

                        if (results) {

                            if (results.length > 0) {

                                if (results.length === 1) {

                                    var result = results[0];
                                    internals.normalize(result);
                                    callback(result, null);
                                }
                                else {

                                    callback(null, Err.database('Found multiple results for unique criteria', collectionName, 'queryUnique', criteria));
                                }
                            }
                            else {

                                // Not found
                                callback(null, null);
                            }
                        }
                        else {

                            callback(null, Err.database('Null result array', collectionName, 'queryUnique', criteria));
                        }
                    }
                    else {

                        callback(null, Err.database(err, collectionName, 'queryUnique', criteria));
                    }
                });
Example #3
0
    Db.queryUnique('invite', { code: code }, function (err, invite) {

        //    { "_id": "4d8629d32d0cba57313953b4",
        //      "code": "emu2011",
        //      "notes": "Eran's friends",
        //      "count": 0,
        //      "limit": 10,
        //      "expires" : 1332173847002 }

        if (err) {
            return callback(err);
        }

        if (!invite) {
            return callback(Hapi.Error.notFound('Invitation code not found'));
        }

        // Check expiration

        if ((invite.expires || Infinity) <= Date.now()) {
            return callback(Hapi.Error.badRequest('Invitation Code expired'));
        }

        // Check count

        if (invite.limit &&
            invite.count &&
            invite.count > invite.limit) {

            return callback(Hapi.Error.badRequest('Invitation code reached limit'));
        }

        return callback(null, invite);
    });
Example #4
0
    Db.get('project', projectId, function (err, item) {

        if (!item) {
            return callback(err || Hapi.Error.notFound());
        }

        var member = null;
        for (var i = 0, il = item.participants.length; i < il; ++i) {
            if (item.participants[i].id &&
                item.participants[i].id === userId) {

                member = item.participants[i];
                if (member.isPending) {
                    item.isPending = true;
                }

                break;
            }
        }

        if (!member) {
            return callback(Hapi.Error.forbidden('Not a project member'));
        }

        if (isWritable &&
            item.isPending) {

            return callback(Hapi.Error.forbidden('Must accept project invitation before making changes'));
        }

        return callback(null, item, member);
    });
Example #5
0
        exports.load(request.params.id, function (err, user) {

            if (err || !user) {
                return request.reply(err);
            }

            // Is set?

            if (!user[request.params.network]) {
                return request.reply(Hapi.Error.badRequest('Account not linked'));
            }

            // Is last (and no email)

            var linkCount = (user.facebook ? 1 : 0) + (user.twitter ? 1 : 0) + (user.yahoo ? 1 : 0);

            if ((!user.emails || !user.emails.length) &&
                linkCount <= 1) {

                return request.reply(Hapi.Error.badRequest('Cannot remove last linked account'));
            }

            var changes = { $unset: {} };
            changes.$unset[request.params.network] = 1;

            Db.update('user', user._id, changes, function (err) {

                if (err) {
                    return request.reply(err);
                }

                Stream.update({ object: 'profile', user: user._id }, request);
                return request.reply({ status: 'ok' });
            });
        });
Example #6
0
exports.subscribe = function (req, reply) {

    // Lookup socket

    if (internals.socketsById[req.params.id] &&
        internals.socketsById[req.params.id].socket) {

        if (internals.socketsById[req.params.id].userId) {

            if (internals.socketsById[req.params.id].userId === req.hapi.userId) {

                var socket = internals.socketsById[req.params.id].socket;

                // Lookup project

                Project.load(req.params.project, req.hapi.userId, false, function (project, member, err) {

                    if (err === null) {

                        // Add to subscriber list

                        internals.idsByProject[project._id] = internals.idsByProject[project._id] || {};
                        internals.idsByProject[project._id][req.params.id] = true;

                        // Add to cleanup list

                        internals.projectsById[req.params.id] = internals.projectsById[req.params.id] || {};
                        internals.projectsById[req.params.id][project._id] = true;

                        // Send ack via the stream

                        socket.json.send({ type: 'subscribe', project: project._id });

                        // Send ack via the request

                        reply({ status: 'ok' });
                    }
                    else {

                        reply(err);
                    }
                });
            }
            else {

                reply(Hapi.Error.forbidden());
            }
        }
        else {

            reply(Hapi.Error.badRequest('Stream not initialized'));
        }
    }
    else {

        reply(Hapi.Error.notFound('Stream not found'));
    }
};
Example #7
0
 data.read(function(err, result) {
   if (err) {
     request.reply(Hapi.Error.badRequest(err));
   } else if (!result) {
     request.reply(Hapi.Error.badRequest('UnknownToken'));
   } else {
     request.reply({ success: true, data: result.value.data, casid: result.casid });
   }
 });
Example #8
0
            internals.checkUsername(request.payload.username, function (err, lookupUser) {

                if (!err ||
                    err.code !== Hapi.Error.notFound().code) {

                    return request.reply(typeof err === 'string' ? Hapi.Error.badRequest('Invalid username: ' + err) : err);
                }

                return createAccount();
            });
Example #9
0
exports.unsubscribe = function (req, reply) {

    // Lookup socket

    if (internals.socketsById[req.params.id] &&
        internals.socketsById[req.params.id].socket) {

        if (internals.socketsById[req.params.id].userId) {

            if (internals.socketsById[req.params.id].userId === req.hapi.userId) {

                var socket = internals.socketsById[req.params.id].socket;

                // Remove from subscriber list

                if (internals.idsByProject[req.params.project] &&
                    internals.idsByProject[req.params.project][req.params.id]) {

                    delete internals.idsByProject[req.params.project][req.params.id];

                    // Remove from cleanup list

                    if (internals.projectsById[req.params.id]) {

                        delete internals.projectsById[req.params.id][req.params.project];
                    }

                    // Send ack via the stream

                    socket.json.send({ type: 'unsubscribe', project: req.params.project });

                    // Send ack via the request

                    reply({ status: 'ok' });
                }
                else {

                    reply(Hapi.Error.notFound('Project subscription not found'));
                }
            }
            else {

                reply(Hapi.Error.forbidden());
            }
        }
        else {

            reply(Hapi.Error.badRequest('Stream not initialized'));
        }
    }
    else {

        reply(Hapi.Error.notFound('Stream not found'));
    }
};
Example #10
0
            internals.checkUsername(req.hapi.payload.username, function (lookupUser, err) {

                if (err &&
                    err.code === Hapi.Error.notFound().code) {

                    createAccount();
                }
                else {

                    reply(typeof err === 'string' ? Hapi.Error.badRequest('Invalid username: ' + err) : err);
                }
            });
Example #11
0
    Db.get('project', projectId, function (item, err) {

        if (item) {

            var member = null;
            for (var i = 0, il = item.participants.length; i < il; ++i) {

                if (item.participants[i].id &&
                    item.participants[i].id === userId) {

                    member = item.participants[i];
                    if (member.isPending) {

                        item.isPending = true;
                    }

                    break;
                }
            }

            if (member) {

                if (isWritable === false ||
                    item.isPending !== true) {

                    callback(item, member, null);
                }
                else {

                    // Invitation pending
                    callback(null, null, Hapi.Error.forbidden('Must accept project invitation before making changes'));
                }
            }
            else {

                // Not allowed
                callback(null, null, Hapi.Error.forbidden('Not a project member'));
            }
        }
        else {

            if (err === null) {

                callback(null, null, Hapi.Error.notFound());
            }
            else {

                callback(null, null, err);
            }
        }
    });
Example #12
0
    handler: function (request) {

        if (request.params.type === 'username') {
            internals.checkUsername(request.params.id, function (err, lookupUser) {

                if (!lookupUser) {
                    return request.reply(typeof err === 'string' ? Hapi.Error.badRequest(err) : err);
                }

                return request.reply({ user: lookupUser._id });
            });
        }
        else if (request.params.type === 'email') {
            if (!Email.checkAddress(request.params.id)) {
                return request.reply(Hapi.Error.badRequest('Invalid email address'));
            }

            Db.queryUnique('user', { 'emails.address': request.params.id }, function (err, item) {

                if (err) {
                    return request.reply(err);
                }

                if (!item) {
                    return request.reply(Hapi.Error.notFound());
                }

                return request.reply({ user: item._id });
            });
        }
        else if (['twitter', 'facebook', 'yahoo'].indexOf(request.params.type) !== -1) {
            var criteria = {};
            criteria[request.params.type] = request.params.id;

            Db.queryUnique('user', criteria, function (err, item) {

                if (err) {
                    return request.reply(err);
                }

                if (!item) {
                    return request.reply(Hapi.Error.notFound());
                }

                return request.reply({ user: item._id });
            });
        }
        else {
            return request.reply(Hapi.Error.badRequest('Unknown network type'));
        }
    }
Example #13
0
exports.query = function (collectionName, criteria, callback) {

    var collection = internals.collections[collectionName];
    if (collection) {

        internals.idifyString(criteria);
        collection.find(criteria, function (err, cursor) {

            if (err === null) {

                cursor.toArray(function (err, results) {

                    if (err === null) {

                        internals.normalize(results);
                        callback(results, null);
                    }
                    else {

                        callback(null, Err.database(err, collectionName, 'query', criteria));
                    }
                });
            }
            else {

                callback(null, Err.database(err, collectionName, 'query', criteria));
            }
        });
    }
    else {

        callback(null, Err.database('Collection not found', collectionName, 'query', criteria));
    }
};
Example #14
0
        exports.load(request.params.id, request.session.user, false, function (err, project, member) {

            if (err || !project) {
                return request.reply(err);
            }

            // Verify user is pending

            if (!member.isPending) {
                return request.reply(Hapi.Error.badRequest('Already a member of the project'));
            }

            Db.updateCriteria('project', project._id, { 'participants.id': request.session.user }, { $unset: { 'participants.$.isPending': 1 } }, function (err) {

                if (err) {
                    return request.reply(err);
                }

                // Return success

                Stream.update({ object: 'project', project: project._id }, request);
                Stream.update({ object: 'projects', user: request.session.user }, request);

                return request.reply({ status: 'ok' });
            });
        });
Example #15
0
    exports.load(req.params.id, req.hapi.userId, false, function (project, member, err) {

        if (project) {

            // Verify user is pending

            if (member.isPending) {

                Db.updateCriteria('project', project._id, { 'participants.id': req.hapi.userId }, { $unset: { 'participants.$.isPending': 1} }, function (err) {

                    if (err === null) {

                        // Return success

                        Stream.update({ object: 'project', project: project._id }, req);
                        Stream.update({ object: 'projects', user: req.hapi.userId }, req);

                        reply({ status: 'ok' });
                    }
                    else {

                        reply(err);
                    }
                });
            }
            else {

                reply(Hapi.Error.badRequest('Already a member of the project'));
            }
        }
        else {

            reply(err);
        }
    });
Example #16
0
exports.get = function (collectionName, id, callback) {

    var collection = internals.collections[collectionName];
    if (collection) {

        var dbId = internals.getDbId(id);
        if (dbId) {

            collection.findOne(dbId, function (err, result) {

                if (err === null) {

                    internals.normalize(result);
                    callback(result, null);
                }
                else {

                    callback(null, Err.database(err, collectionName, 'get', id));
                }
            });
        }
        else {

            callback(null, null);
        }
    }
    else {

        callback(null, Err.database('Collection not found', collectionName, 'get', id));
    }
};
Example #17
0
exports.sendReminder = function (user, callback) {

    if (!user ||
        !user.emails ||
        !user.emails[0] ||
        !user.emails[0].address) {

        return callback(Hapi.Error.internal('User has no email address'));
    }

    var options = { action: { type: 'reminder' }, expiresInMin: 20160 };                      // Two weeks
    exports.generateTicket(user, user.emails[0].address, options, function (err, ticket) {

        if (err || !ticket) {
            return callback(err);
        }

        var subject = 'Help signing into ' + Config.product.name;
        var text = 'Hey ' + (user.name || user.username || user.emails[0].address) + ',\n\n' +
               'Use this link to sign into ' + Config.product.name + ': \n\n' +
               '    ' + Config.host.uri('web') + '/t/' + ticket;

        internals.sendEmail(user.emails[0].address, subject, text);
        return callback(null);
    });
};
Example #18
0
        Db.queryUnique('user', criteria, function (user, err) {

            if (err === null) {

                if (user) {

                    Email.sendReminder(user, function (err) {

                        if (err === null) {

                            reply({ result: 'ok' });
                        }
                        else {

                            reply(err);
                        }
                    });
                }
                else {

                    reply(Hapi.Error.notFound());
                }
            }
            else {

                reply(err);
            }
        });
Example #19
0
        var validate = function () {

            // Look for email address

            email = (request.payload.email ? request.payload.email : (projectPid && projectPid.email ? projectPid.email : null));
            isEmailVerified = (projectPid && projectPid.email && projectPid.email === email ? true : false);

            // Check for at least one identifier

            if (!request.payload.network &&
                !email) {

                return request.reply(Hapi.Error.badRequest('Must include either a network id or email address'));
            }

            if (!email) {
                return validateNetwork();
            }

            Db.count('user', { 'emails.address': email }, function (err, count) {

                if (err) {
                    return request.reply(err);
                }

                if (count) {
                    return request.reply(Hapi.Error.badRequest('Email address already linked to an existing user'));
                }

                return validateNetwork();
            });
        };
Example #20
0
                Db.count('user', { 'emails.address': address }, function (err, count) {

                    if (err) {
                        return request.reply(err);
                    }

                    if (count) {
                        return request.reply(Hapi.Error.badRequest('Email already assigned to another account'));
                    }

                    // Save

                    Db.update('user', user._id, { '$push': { emails: { address: address, isVerified: false } } }, function (err) {

                        if (err) {
                            return request.reply(err);
                        }

                        Email.sendValidation(user, address, function (err) {

                            // Ignore errors

                            Stream.update({ object: 'profile', user: user._id }, request);
                            return request.reply({ status: 'ok' });
                        });
                    });
                });
Example #21
0
                    Db.count('user', criteria, function (count, err) {

                        if (err === null) {

                            if (count === 0) {

                                var changes = { $set: {} };
                                changes.$set[req.params.network] = req.hapi.payload.id;

                                Db.update('user', user._id, changes, function (err) {

                                    if (err === null) {

                                        Stream.update({ object: 'profile', user: user._id }, req);
                                        reply({ status: 'ok' });
                                    }
                                    else {

                                        reply(err);
                                    }
                                });
                            }
                            else {

                                reply(Hapi.Error.badRequest('Network id already linked to another user'));
                            }
                        }
                        else {

                            reply(err);
                        }
                    });
Example #22
0
exports.load = function (userId, callback) {

    if (userId) {

        Db.get('user', userId, function (item, err) {

            if (item) {

                callback(item, null);
            }
            else {

                if (err === null) {

                    callback(null, Hapi.Error.notFound());
                }
                else {

                    callback(null, err);
                }
            }
        });
    }
    else {

        callback(null, Hapi.Error.internal('Missing user id'));
    }
};
Example #23
0
 data.write(request.payload.data, request.payload.casid || 0, function(err) {
   if (err) {
     request.reply(Hapi.Error.badRequest(err));
   } else {
     request.reply({ success: true });
   }
 });
Example #24
0
            Db.insert('user', user, function (err, items) {

                if (err) {
                    return request.reply(err);
                }

                if (items.length !== 1 ||
                    !items[0]._id) {

                    return request.reply(Hapi.Error.internal('Failed to retrieve new user id', user));
                }

                var userId = items[0]._id;
                if (inviteId) {
                    // Decrease invitation count
                    Db.update('invite', inviteId, { $inc: { count: 1 } }, function (err) { });
                }

                if (!projectPid) {
                    return sendWelcome(items[0]);
                }

                // Update project with new participant

                Project.replacePid(projectPid.project, projectPid.pid, userId, function (err) {

                    if (err) {
                        return request.reply(err);
                    }

                    Stream.update({ object: 'project', project: projectPid.project._id }, request);
                    return sendWelcome(items[0]);
                });
            });
Example #25
0
 db.storePayload(id, payload, function(err, timestamp) {
   if (err) return request.reply(Hapi.Error.internal("Could not store payload: " + err));
   request.reply({
     success: true,
     updated: timestamp
   });
 });
Example #26
0
    handler: function (request) {

        var isEmail = request.payload.account.indexOf('@') !== -1;
        var account = request.payload.account.toLowerCase();

        if (isEmail &&
            !Email.checkAddress(account)) {

            return request.reply(Hapi.Error.badRequest());
        }

        var criteria = {};
        criteria[isEmail ? 'emails.address' : 'username'] = account;

        Db.queryUnique('user', criteria, function (err, user) {

            if (err) {
                return request.reply(err);
            }

            if (!user) {
                return request.reply(Hapi.Error.notFound());
            }

            Email.sendReminder(user, function (err) {

                return request.reply(err || { result: 'ok' });
            });
        });
    }
Example #27
0
exports.all = function (collectionName, callback) {

    var collection = internals.collections[collectionName];
    if (collection) {

        collection.find(function (err, cursor) {

            if (err === null) {

                cursor.toArray(function (err, results) {

                    if (err === null) {

                        internals.normalize(results);
                        callback(results, null);
                    }
                    else {

                        callback(null, Err.database(err, collectionName, 'all'));
                    }
                });
            }
            else {

                callback(null, Err.database(err, collectionName, 'all'));
            }
        });
    }
    else {

        callback(null, Err.database('Collection not found', collectionName, 'all'));
    }
};
Example #28
0
File: task.js Project: lpw/postmile
    Db.get('task', taskId, function (item, err) {

        if (item) {

            Project.load(item.project, userId, isWritable, function (project, member, err) {

                if (project) {

                    callback(item, null, project);
                }
                else {

                    callback(null, err, null);
                }
            });
        }
        else {

            if (err === null) {

                callback(null, Hapi.Error.notFound(), null);
            }
            else {

                callback(null, err, null);
            }
        }
    });
Example #29
0
    handler: function (request) {

        // Lookup socket

        if (!internals.socketsById[request.params.id] ||
            !internals.socketsById[request.params.id].socket) {

            return request.reply(Hapi.Error.notFound('Stream not found'));
        }

        if (!internals.socketsById[request.params.id].userId) {
            return request.reply(Hapi.Error.badRequest('Stream not initialized'));
        }

        if (internals.socketsById[request.params.id].userId !== request.auth.credentials.user) {
            return request.reply(Hapi.Error.forbidden());
        }

        var socket = internals.socketsById[request.params.id].socket;

        // Lookup project

        Project.load(request.params.project, request.auth.credentials.user, false, function (err, project, member) {

            if (err) {
                return request.reply(err);
            }

            // Add to subscriber list

            internals.idsByProject[project._id] = internals.idsByProject[project._id] || {};
            internals.idsByProject[project._id][request.params.id] = true;

            // Add to cleanup list

            internals.projectsById[request.params.id] = internals.projectsById[request.params.id] || {};
            internals.projectsById[request.params.id][project._id] = true;

            // Send ack via the stream

            socket.json.send({ type: 'subscribe', project: project._id });

            // Send ack via the request

            return request.reply({ status: 'ok' });
        });
    }
Example #30
0
File: task.js Project: lpw/postmile
    Project.load(request.params.id, request.userId, true, function (project, member, err) {

        if (project) {

            if (request.query.suggestion) {

                // From suggestion

                if (!request.rawBody) {

                    Suggestions.get(request.query.suggestion, function (suggestion) {

                        if (suggestion) {

                            var task = { title: suggestion.title, origin: { type: 'suggestion', suggestion: suggestion._id} };
                            addTask(task);
                        }
                        else {

                            reply(Hapi.Error.badRequest('Suggestion not found'));
                        }
                    });
                }
                else {

                    reply(Hapi.Error.badRequest('New task cannot have both body and suggestion id'));
                }
            }
            else {

                // From body

                if (request.payload.title) {

                    addTask(request.payload);
                }
                else {

                    reply(Hapi.Error.badRequest('New task must include a title or a suggestion id'));
                }
            }
        }
        else {

            reply(err);
        }
    });