kpiplus
Version:
KPI+
1 lines • 3.44 kB
JavaScript
var model=require("../../models/comment"),Follow=require("../../models/follow"),controller=require("../../controllers/controller"),email=require("../../libs/email"),notify=require("../../models/notification"),loadTemplate=require("../../libs/load-template"),underscore=require("underscore"),adminUsers=require("../../configs").admins;module.exports=function(e){var t=new controller(e,model,"comment",{require_id_app:!1,sort:{date_created:1}});t.deleting=function(e,t,r){e.email==t.user_created||underscore.contains(adminUsers,e.email)?r(null,t):r("Bạn không thể xóa nội dung này")},t.updating=function(e,t,r,n){e.email==r.user_created||underscore.contains(adminUsers,e.email)?n(null,t,r):n("Bạn không thể cập nhật nội dung này")},t.created=function(e,t,r){if(t.is_reply){var n={};underscore.extend(n,t),/<[a-z][\s\S]*>/i.test(n.content)||(n.content=n.content.replace(/\n/g,"<br/>"));var o={$or:[{_id:n.id_topic}],_id:{$ne:n._id},user_created:{$ne:n.user_created}};n.id_topic&&(o.$or.push({id_topic:n.id_topic}),n.url_topic="https://ungdungquanly.vn/#/comment/view/"+n.id_topic),n.url_topic||(n.url_topic=""),model.find(o,{user_created:1}).lean().exec(function(r,o){if(r)return console.log(r);o=underscore.pluck(o,"user_created");var c=n.content.match(/(@[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);c&&c.forEach(function(e){o.push(e.substring(1))}),n.attends&&n.attends.forEach(function(e){o.push(e)}),(o=underscore.uniq(o)).forEach(function(t){loadTemplate("reply-comment.html",{comment:n,user:e},function(e,r){if(e)return console.log(e);email.sendHtml({to:{name:t,address:t},subject:"Trả lời chủ đề '"+n.title+"' bạn tham gia hoặc được nhắc tới",html:r},function(e,r){e&&console.error("Khong the gui email thong bao tra loi comment cho email "+t+"\n"+e)})}),notify.createNotification(n.user_created,t,"Trả lời chủ đề '"+n.title+"'",n.content+"\n<hr/><p>Xem chủ đề <a href='"+n.url_topic+"'>tại đây</a></p>")}),t.user_created_obj&&t.user_created_obj!=t.user_created&&!underscore.contains(o,t.user_created_obj)&&(loadTemplate("reply-comment.html",{comment:n,user:e},function(e,r){if(e)return console.log(e);email.sendHtml({to:{name:t.user_created_obj,address:t.user_created_obj},subject:"Trả lời chủ đề '"+n.title+"' do bạn tạo ra",html:r},function(e,t){e&&console.error("Khong the gui email thong bao comment\n"+e)})}),notify.createNotification(n.user_created,n.user_created_obj,"Trả lời chủ đề '"+n.title+"'",n.content+"\n<hr/><p>Xem chủ đề <a href='"+n.url_topic+"'>tại đây</a></p>")),t.id_product&&Follow.find({id_object:t.id_product,user_created:{$ne:t.user_created}},{user_created:1}).lean().exec(function(r,c){c&&c.forEach(function(r){r.user_created==t.user_created_obj||underscore.contains(o,r.user_created)||(loadTemplate("reply-comment.html",{comment:n,user:e},function(e,t){if(e)return console.log(e);email.sendHtml({to:{name:r.user_created,address:r.user_created},subject:"Trả lời vào chủ đề '"+n.title+"' bạn đã theo dõi",html:t},function(e,t){e&&console.error("Khong the gui email thong bao comment\n"+e)})}),notify.createNotification(n.user_created,r.user_created,"Trả lời chủ đề '"+n.title+"'",n.content+"\n<hr/><p>Xem chủ đề <a href='"+n.url_topic+"'>tại đây</a></p>"))})})})}r(null,t)},t.on("deleted",function(e){model.remove({id_topic:e._id},function(e){e&&console.log(e)})}),t.route()};