@rjakobsson here below is my send method
send:function(msg){
let st = process.hrtime();
if (! this.userId) {
throw new Meteor.Error('error-not-authorized','User need to login', {method: "send"})
}
//check for the validation
NonEmptyString = Match.Where(function (x) {
check(x, String);
return x.length > 0;
});
check(msg, {
id: String,
msg: NonEmptyString,
rid: String,
lts: String
});
let u = Meteor.user()
let r = Rooms.findOne({_id: msg.rid})
if(! r){
throw new Meteor.Error("error-room-invalid", "Invalid Room id", msg.id);
}
//find the recepient user and check if he is online or not, if not send a push notification.
let ru = Meteor.users.findOne({$and: [{username: {$in: r.users}},{username: {$ne: u.username}}]})
if(! ru){
throw new Meteor.Error("error-invalid-recipient","No recipent user found", msg.id);
}
// TODO: Check for message limit
let record = {
_id: msg.id,
msg: msg.msg,
rid: r._id,
u: u.username,
ts: new Date(),
lts: msg.lts
}
let result = null;
try {
result = Chats.insert(record);
if(result){
//increase the msg counter
Rooms.update({_id: r._id}, {$set: {mc: r.mc+1}})
if((ru && ru.status) ? !ru.status.online : true){
Meteor.defer(function() {
Meteor.call('sendNotification',record,ru._id)
})
}
}
} catch (e) {
if (e.name === 'MongoError' && e.code === 11000)
result = record._id;
else
throw new Meteor.Error('error-500','Something went wrong', e)
}
let et = process.hrtime(st);
return {_id: result};
}