UNPKG

flexbiz-server

Version:

Flexible Server

15 lines (14 loc) 6.86 kB
const model=global.getModel("message"),User=global.getModel("user"),async=require("async"),controller=require("../../controllers/controller"),{onAfterCommit}=require("../../libs/sessionContext"); module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,"message",{require_id_app:!1,sort:{date_created:1}});$contr_router$$.route();$contr_router$$.view=function($user$$,$result$$,$fn$$){$result$$.joinModel(void 0,User,[{akey:"email_sender",bkey:"email",fields:[{name:"name_sender",value:"name"},{name:"picture_sender",value:"picture"}]},{akey:"email_receiver",bkey:"email",fields:[{name:"name_receiver",value:"name"},{name:"picture_receiver",value:"picture"}]}], function(){$result$$.forEach(function($r$$){$r$$.picture_sender||($r$$.picture_sender="/images/avatar.jpg");$r$$.name_sender||($r$$.name_sender=$r$$.email_sender);$r$$.picture_receiver||($r$$.picture_receiver="/images/avatar.jpg");$r$$.name_receiver||($r$$.name_receiver=$r$$.email_receiver)});$fn$$(null,$result$$)})};$contr_router$$.getting=function($user$$,$id$$,$next$$,$obj$$){if($obj$$.email_owner!=$user$$.email)return $next$$("B\u1ea1n kh\u00f4ng c\u00f3 quy\u1ec1n th\u1ef1c hi\u1ec7n thao t\u00e1c n\u00e0y"); $obj$$.read||model.findByIdAndUpdate($obj$$._id,{$set:{read:!0}},function($error$$){if($error$$)return Logger.info($error$$);global.alertMessage($user$$.email)});$next$$()};$contr_router$$.finding=function($user$$,$condition$$,$next$$){$condition$$.$or=[{email_owner:$user$$.email}];$next$$(null,$condition$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.email_sender=$user$$.email;$obj$$.email_owner=$user$$.email;$obj$$.read=!0;$next$$(null,$obj$$)};$contr_router$$.created=function($user$$, $obj$$,$next$$){onAfterCommit(()=>{var $obj_receiver$$=new model;$obj_receiver$$.email_sender=$obj$$.email_sender;$obj_receiver$$.email_receiver=$obj$$.email_receiver;$obj_receiver$$.email_owner=$obj$$.email_receiver;$obj_receiver$$.content=$obj$$.content;$obj_receiver$$.image=$obj$$.image;$obj_receiver$$.audio=$obj$$.audio;$obj_receiver$$.video=$obj$$.video;$obj_receiver$$.location=$obj$$.location;$obj_receiver$$.object=$obj$$.object;$obj_receiver$$.id_origin=$obj$$._id.toString();$obj_receiver$$.save(function($error$$){if($error$$)return Logger.info($error$$); model.sendMessage($obj_receiver$$);global.alertMessage($obj$$.email_receiver)});$obj$$.email_receiver.indexOf("guest@")>=0&&configs.supportUsers.forEach(function($u$$){$u$$!==$user$$.email&&model.createMessage($obj$$.email_sender,$u$$,$obj$$.title,$obj$$.content)})});$next$$(null,$obj$$)};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$next$$){return $next$$(Error("Updating message is not allowed"))};$contr_router$$.updated=function($user$$,$obj$$,$next$$){global.alertMessage($obj$$.email_owner); $next$$(null,$obj$$)};$contr_router$$.deleting=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.deleted=function($user$$,$obj$$,$next$$){global.alertMessage($obj$$.email_owner);$next$$(null,$obj$$)};$contr_router$$.router.route($contr_router$$.route_name+"/colleague/latest").get(function($req$$,$res$$,$next$$){model.find({email_receiver:$req$$.user.email,read:!1}).sort({date_created:-1}).lean().exec(function($error$$,$results$$){if($error$$)return $next$$($error$$);if($results$$){var $messages$$= [],$senders$$={};$results$$.forEach(function($k$$){$senders$$[$k$$.email_sender]||($messages$$.push($k$$),$senders$$[$k$$.email_sender]=1)});$messages$$.joinModel(void 0,User,[{akey:"email_sender",bkey:"email",fields:[{name:"name_sender",value:"name"},{name:"picture_sender",value:"picture"}]},{akey:"email_receiver",bkey:"email",fields:[{name:"name_receiver",value:"name"},{name:"picture_receiver",value:"picture"}]}],function(){$messages$$.forEach(function($m$$){$m$$.name_sender||($m$$.name_sender= $m$$.title);$m$$.picture_sender||($m$$.picture_sender="/images/avatar.jpg")});$res$$.send($messages$$)})}else $res$$.send([])})});$contr_router$$.router.route($contr_router$$.route_name+"/chat/:email_dt").get(function($req$$,$res$$,$next$$){var $email_dt_query$$=$req$$.params.email_dt;if($email_dt_query$$==$req$$.user.email)return $res$$.send([]);var $condition$$={email_owner:$req$$.user.email};$condition$$.$or=[{email_receiver:$email_dt_query$$},{email_sender:$email_dt_query$$}];let $sort$$;if($req$$.query.sort)try{$sort$$= JSON.parse($req$$.query.sort)}catch($e$$){$sort$$={date_created:1}}else $sort$$={date_created:1};$email_dt_query$$=model.find($condition$$);$condition$$=$req$$.query.page;let $limit$$=$req$$.query.limit;$limit$$&&=Number($limit$$);$condition$$?($limit$$||=10,$email_dt_query$$.skip((Number($condition$$)-1)*$limit$$).limit($limit$$)):$limit$$&&$email_dt_query$$.limit($limit$$);$email_dt_query$$.sort($sort$$).exec(($error$$,$results$$)=>{if($error$$)return $next$$($error$$);if($results$$){var $messages$$= [];$results$$.forEach(function($r$$){$r$$.read===!1&&($r$$.set("read",!0),$r$$.save(function($error$$){if($error$$)return console($error$$);global.alertMessage($req$$.user.email)}));$messages$$.push($r$$.toObject())});$messages$$.joinModel(void 0,User,[{akey:"email_sender",bkey:"email",fields:[{name:"name_sender",value:"name"},{name:"picture_sender",value:"picture"}]},{akey:"email_receiver",bkey:"email",fields:[{name:"name_receiver",value:"name"},{name:"picture_receiver",value:"picture"}]}],function(){$messages$$.forEach(function($r$$){$r$$.picture_sender|| ($r$$.picture_sender="/images/avatar.jpg");$r$$.picture_receiver||($r$$.picture_receiver="/images/avatar.jpg")});$res$$.send($messages$$)})}else $res$$.send([])})});$contr_router$$.router.route($contr_router$$.route_name+"/colleague/list").get(function($req$$,$res$$,$next$$){model.find({email_owner:$req$$.user.email}).lean().exec(function($error$$,$messages$$){if($error$$)return $next$$($error$$);$messages$$=$messages$$.map($m$$=>{$m$$.email=$m$$.email_sender===$req$$.user.email?$m$$.email_receiver: $m$$.email_sender;return $m$$}).filter($m$$=>$m$$.email!==$m$$.email_owner);$messages$$.groupBy(["email_owner","email"],[],function($e$$,$colls$$){if($e$$)return $next$$($e$$);$colls$$.joinModel2(void 0,User,{where:{email:"email"},fields:["name","picture"]},function(){async.mapLimit($colls$$,20,($coll$$,$callback$$)=>{$coll$$.picture=$coll$$.picture||"/images/avatar.jpg";model.find({email_owner:$req$$.user.email,$or:[{email_sender:$coll$$.email},{email_receiver:$coll$$.email}]}).sort({date_created:-1, read:-1}).limit(1).lean().exec(($e$$,$msg$$)=>{$msg$$&&$msg$$.length===1?($coll$$.latest_message=$msg$$[0].content,$coll$$.latest_message_id=$msg$$[0]._id,$coll$$.latest_message_date=$msg$$[0].date_created,$coll$$.latest_message_read=$msg$$[0].read):($coll$$.latest_message="",$coll$$.latest_message_id="",$coll$$.latest_message_date=null);$callback$$()})},()=>{$res$$.send($colls$$)})},{cache:!1})})})})};