no message

This commit is contained in:
2018-03-04 23:11:36 -05:00
parent 18ef8cd9d7
commit 9cd338d300
2 changed files with 86 additions and 23 deletions

View File

@@ -114,6 +114,22 @@ ProfileSchema.pre('save', function (next) {
const ProfileModel = Mongoose.model('profiles', ProfileSchema);
function getChatImages (id, match, callback) {
callback = callback || (typeof match === 'function' ? match : function(){});
match = typeof match === 'object' ? match : {};
match['messages.image'] = { $exists: true };
ProfileModel
.aggregate([
{ $match: { _id: Mongoose.Types.ObjectId(profileId), 'messages.image': { $exists: true } } },
{ $unwind: '$messages' },
{ $match: match },
{ $replaceRoot: { newRoot: '$messages' } }
])
.project('image -_id')
.exec(callback);
}
module.exports = {
all: (e) => {
@@ -166,32 +182,66 @@ module.exports = {
});
},
allMessageImages: (e, profileId) => {
allChatImages: (e, profileId) => {
const promise = new Promise((resolve, reject) => {
ProfileModel
.aggregate([
{ $match: { _id: Mongoose.Types.ObjectId(profileId), 'messages.image': { $exists: true } } },
{ $unwind: '$messages' },
{ $match: { 'messages.isUser': false, 'messages.image': { $exists: true } } },
{ $replaceRoot: { newRoot: '$messages' } }
])
.project('image -_id')
.exec((err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
getChatImages(profileId, (err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
e.emit('allMessageImages', null, result);
e.emit('allChatImages', null, result);
})
.catch((err) => {
e.emit('allMessageImages', err, null);
e.emit('allChatImages', err, null);
});
},
allChatImagesReceived: (e, profileId) => {
const promise = new Promise((resolve, reject) => {
getChatImages(profileId, { 'messages.isUser': true }, (err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
e.emit('allChatImagesReceived', null, result);
})
.catch((err) => {
e.emit('allChatImagesReceived', err, null);
});
},
allChatImagesSent: (e, profileId) => {
const promise = new Promise((resolve, reject) => {
getChatImages(profileId, { 'messages.isUser': false }, (err, result) => {
if (err) {
reject(err);
}
if (result) {
resolve(result);
}
});
});
promise.then((result) => {
e.emit('allChatImagesSent', null, result);
})
.catch((err) => {
e.emit('allChatImagesSent', err, null);
});
},

View File

@@ -154,12 +154,25 @@ Router.route('/list' + ParamStr)
Profiles.find(ProfileEvents, query);
});
Router.route('/:profileId/messages/images')
Router.route('/:profileId/messages/images/:which?')
.get((req, res) => {
var method;
var ProfileEvents = new EventEmitter();
var profileId = req.params.profileId;
ProfileEvents.once('allMessageImages', (err, result) => {
switch (req.params.which) {
case "all":
method = 'allChatImages';
break;
case "sent":
method = 'allChatImagesSent';
break;
case "recd":
default:
method = 'allChatImagesReceived';
}
ProfileEvents.once(method, (err, result) => {
if (err) {
res.status(500).json({ message: 'Could not get chat images for profile ' + profileId + '. [' + err + ']', err: err });
}
@@ -169,7 +182,7 @@ Router.route('/:profileId/messages/images')
}
});
Profiles.allMessageImages(ProfileEvents, profileId);
Profiles[method](ProfileEvents, profileId);
});
Router.route('/:profileId/messages/:messageId?')