no message
This commit is contained in:
@@ -114,6 +114,22 @@ ProfileSchema.pre('save', function (next) {
|
|||||||
|
|
||||||
const ProfileModel = Mongoose.model('profiles', ProfileSchema);
|
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 = {
|
module.exports = {
|
||||||
|
|
||||||
all: (e) => {
|
all: (e) => {
|
||||||
@@ -166,32 +182,66 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
allMessageImages: (e, profileId) => {
|
allChatImages: (e, profileId) => {
|
||||||
const promise = new Promise((resolve, reject) => {
|
const promise = new Promise((resolve, reject) => {
|
||||||
ProfileModel
|
getChatImages(profileId, (err, result) => {
|
||||||
.aggregate([
|
if (err) {
|
||||||
{ $match: { _id: Mongoose.Types.ObjectId(profileId), 'messages.image': { $exists: true } } },
|
reject(err);
|
||||||
{ $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) {
|
if (result) {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
promise.then((result) => {
|
promise.then((result) => {
|
||||||
e.emit('allMessageImages', null, result);
|
e.emit('allChatImages', null, result);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.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);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -154,12 +154,25 @@ Router.route('/list' + ParamStr)
|
|||||||
Profiles.find(ProfileEvents, query);
|
Profiles.find(ProfileEvents, query);
|
||||||
});
|
});
|
||||||
|
|
||||||
Router.route('/:profileId/messages/images')
|
Router.route('/:profileId/messages/images/:which?')
|
||||||
.get((req, res) => {
|
.get((req, res) => {
|
||||||
|
var method;
|
||||||
var ProfileEvents = new EventEmitter();
|
var ProfileEvents = new EventEmitter();
|
||||||
var profileId = req.params.profileId;
|
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) {
|
if (err) {
|
||||||
res.status(500).json({ message: 'Could not get chat images for profile ' + profileId + '. [' + err + ']', err: 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?')
|
Router.route('/:profileId/messages/:messageId?')
|
||||||
|
|||||||
Reference in New Issue
Block a user