Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User.send_message = function (id, body, callback) {
- var error = new Error("Access denied.");
- error.errorCode = 401;
- var paticipants = [];
- if (typeof body.participants === 'undefined' || body.participants === null || !Array.isArray(body.participants) || !body.participants.length) {
- return callback(error);
- } else {
- var userIds = body.participants;
- userIds.push(id);
- User.find({where: {id: {inq: userIds}}}, function (err, users) {
- if (users && users.length && err === null) {
- // found users
- users.forEach(function (user) {
- paticipants.push(user.id);
- });
- // now let do query to mongodb.
- var GroupCollection = User.getDataSource().connector.collection(User.app.models.group.modelName);
- GroupCollection.aggregate([
- {$match: {status: true}},
- {$sort: {updatedAt: 1}},
- {
- $lookup: {localField: "_id", from: "member", foreignField: "groupId", as: "members"}
- },
- {
- $group: {
- _id: "$_id",
- members: {$addToSet: "$members.userId"}
- }
- },
- {
- $project: {
- _id: 1,
- id: 1,
- title: 1,
- members: {$arrayElemAt: ["$members", 0]}
- }
- },
- {
- $match: {
- $and: [
- {members: {$all: paticipants}},
- {members: {$size: paticipants.length}}
- ]
- }
- },
- ], function (err, result) {
- var filterQuery = {
- include: [
- {
- relation: "messages",
- scope: {
- order: ["createdAt DESC"],
- limit: 10
- }
- },
- {
- relation: "members",
- }
- ]
- };
- if (err === null && result && result.length && typeof result[0]._id !== 'undefined' && result[0]._id !== null) {
- // found item.
- // if have message in body we need create it
- if (typeof body.message !== 'undefined' && body.message !== null && typeof body.message.body !== 'undefined' && body.message.body !== null && body.message.body !== "") {
- User.app.models.message.create({
- userId: id,
- body: body.message.body,
- messageType: body.message.messageType ? body.message.messageType : "text",
- groupId: result[0]._id
- }, function (err, newMessage) {
- if (err !== null) {
- return callback(err);
- } else {
- User.app.models.group.findById(result[0]._id, filterQuery, function (err, group) {
- return callback(null, group);
- });
- }
- });
- } else {
- User.app.models.group.findById(result[0]._id, filterQuery, function (err, group) {
- return callback(null, group);
- });
- }
- } else {
- // we need create new group. if message from body is found
- if (typeof body.message !== 'undefined' && body.message !== null && typeof body.message.body !== 'undefined' && body.message.body !== null && body.message.body !== "") {
- User.app.models.group.create({
- userId: id
- }, function (err, group) {
- if (err) {
- return callback(err);
- }
- if (group) {
- var groupMemers = [];
- users.forEach(function (p) {
- if (p.id != id) {
- groupMemers.push({groupId: group.id, userId: p.id, name: p.firstName ? p.firstName : ""});
- }
- });
- group.members.create(groupMemers, function (err, members) {
- group.messages.create({
- userId: id,
- body: body.message.body,
- messageType: body.message.messageType ? body.message.messageType : "text",
- groupId: group.id
- }, function (err, message) {
- User.app.models.group.findById(group.id, filterQuery, function (err, group) {
- return callback(null, group);
- });
- });
- });
- } else {
- return callback(error);
- }
- });
- } else {
- return callback(null, result); // this is empty group. and no message post in body.
- }
- }
- });
- } else {
- return callback(error);
- }
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement