UNPKG

flexbiz-server

Version:

Flexible Server

8 lines (7 loc) 3.05 kB
'use strict';const model=global.getModel("news"),newsfeed=global.getModel("newsfeed"),controller=require("../../controllers/controller"),googleTranslate=require("google-translate"),async=require("async"),translateText=async($text$$,$source$$="vi",$target$$="en")=>{const {GOOGLE_TRANSLATE_API_KEY:$GOOGLE_TRANSLATE_API_KEY$$}=configs;if(!$GOOGLE_TRANSLATE_API_KEY$$)return $text$$;const $trans$$=googleTranslate($GOOGLE_TRANSLATE_API_KEY$$,{});return await new Promise(($resolve$$,$reject$$)=>{$trans$$.translate($text$$, $source$$,$target$$,function($err$$,$translation$$){if($err$$)return $reject$$($err$$);$resolve$$($translation$$.translatedText)})})}; module.exports=function($router$$){(new controller($router$$,model,"news",{notNeedRight:($user$$,$options$$)=>"view"==$options$$.action?!0:!1,sort:{stt:1,date_created:-1},onFinding:($user$$,$condition$$,$next$$,$options$$)=>{$condition$$.ngon_ngu&&"vi"!==$condition$$.ngon_ngu&&$condition$$.isnews&&($options$$.req.ngon_ngu=$condition$$.ngon_ngu,$condition$$.ngon_ngu="vi");$condition$$.slug&&($options$$.req.slug=$condition$$.slug,$condition$$.$and=$condition$$.$and||[],$condition$$.$and.push({$or:[{slug:$condition$$.slug}, {"trans.slug":$condition$$.slug}]}),delete $condition$$.slug);$next$$(null,$condition$$)},onView:async($user$$,$items$$,$next$$,$options$$)=>{const $ngon_ngu$$=$options$$.req.ngon_ngu,$slug$$=$options$$.req.slug;async.map($items$$,async $item$$=>{if($ngon_ngu$$&&"vi"!=$ngon_ngu$$){var $tran_tran$$=($item$$.trans||[]).find($t$$=>$t$$.ngon_ngu===$ngon_ngu$$);if($tran_tran$$)$item$$.title=$tran_tran$$.title,$item$$.content=$tran_tran$$.content,$item$$.slug=$tran_tran$$.slug;else try{const $title$$=$item$$.title= await translateText($item$$.title,"vi",$ngon_ngu$$),$slug$$=$item$$.slug=await translateText($item$$.slug||$item$$.title,"vi",$ngon_ngu$$),$content$$=$item$$.content=await translateText($item$$.content,"vi",$ngon_ngu$$);$tran_tran$$={title:$title$$,content:$content$$,slug:$slug$$,ngon_ngu:$ngon_ngu$$};var $tran$jscomp$2_trans$$=$item$$.trans=$item$$.trans||[];$tran$jscomp$2_trans$$.push($tran_tran$$);model.updateOne({_id:$item$$._id},{trans:$tran$jscomp$2_trans$$})}catch($e$$){}}else $slug$$&&$slug$$!= $item$$.slug&&($tran$jscomp$2_trans$$=($item$$.trans||[]).find($t$$=>$t$$.slug===$slug$$))&&($item$$.title=$tran$jscomp$2_trans$$.title,$item$$.content=$tran$jscomp$2_trans$$.content,$item$$.slug=$tran$jscomp$2_trans$$.slug)},$e$$=>{$next$$($e$$,$items$$)})},onCreated:async($user$$,$obj$$,$next$$)=>{if($obj$$.newsfeed)try{await newsfeed.add("news",$obj$$),$next$$(null,$obj$$)}catch($e$$){$next$$($e$$.message)}else $next$$(null,$obj$$)},onUpdated:async($user$$,$obj$$,$next$$)=>{if($obj$$.newsfeed)try{await newsfeed.add("news", $obj$$),$next$$(null,$obj$$)}catch($e$$){$next$$($e$$.message)}else{try{await newsfeed.delete($obj$$._id)}catch($e$$){console.error($e$$)}$next$$(null,$obj$$)}},onDeleted:async($user$$,$obj$$,$next$$)=>{try{await newsfeed.delete($obj$$._id)}catch($e$$){console.error($e$$)}$next$$(null,$obj$$)}})).route()};