diff --git a/models/profile.js b/models/profile.js index cb75fdc..3cf3d01 100644 --- a/models/profile.js +++ b/models/profile.js @@ -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); }); }, diff --git a/routes/profiles.js b/routes/profiles.js index 8cbe2ab..a03f27b 100644 --- a/routes/profiles.js +++ b/routes/profiles.js @@ -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?')