MqDiscussion.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\MdRender;
  4. use App\Http\Api\Mq;
  5. use App\Http\Api\UserApi;
  6. use App\Http\Controllers\NotificationController;
  7. use App\Models\Article;
  8. use App\Models\DhammaTerm;
  9. use App\Models\Discussion;
  10. use App\Models\Sentence;
  11. use App\Models\Wbw;
  12. use App\Models\WbwBlock;
  13. use App\Models\WebHook;
  14. use App\Tools\Tools;
  15. use App\Tools\WebHook as WebHookSend;
  16. use Illuminate\Console\Command;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Str;
  19. class MqDiscussion extends Command
  20. {
  21. /**
  22. * The name and signature of the console command.
  23. * php artisan mq:discussion
  24. *
  25. * @var string
  26. */
  27. protected $signature = 'mq:discussion';
  28. /**
  29. * The console command description.
  30. *
  31. * @var string
  32. */
  33. protected $description = 'Command description';
  34. /**
  35. * Create a new command instance.
  36. *
  37. * @return void
  38. */
  39. public function __construct()
  40. {
  41. parent::__construct();
  42. }
  43. /**
  44. * Execute the console command.
  45. *
  46. * @return int
  47. */
  48. public function handle()
  49. {
  50. if (Tools::isStop()) {
  51. return 0;
  52. }
  53. $exchange = 'router';
  54. $queue = 'discussion';
  55. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  56. Log::info('discussion worker start .');
  57. Mq::worker($exchange, $queue, function ($message) {
  58. Log::info('mq discussion receive {message}', ['message' => json_encode($message, JSON_UNESCAPED_UNICODE)]);
  59. $result = 0;
  60. $msgParam = [];
  61. $msgParam['nickname'] = $message->editor->nickName;
  62. $link = config('app.url').'/pcd/discussion/topic/';
  63. if ($message->parent) {
  64. $msgParam['topic-title'] = Discussion::where('id', $message->parent)->value('title');
  65. $id = $message->id;
  66. $msgParam['link'] = $link.$message->parent.'#'.$id;
  67. $msgParam['card_title'] = '回复讨论';
  68. $type = 'reply';
  69. } else {
  70. $msgParam['title'] = $message->title;
  71. $msgParam['link'] = $link.$message->id;
  72. $msgParam['card_title'] = '创建讨论';
  73. $type = 'create';
  74. }
  75. if ($message->content) {
  76. $msgParam['content'] = $message->content;
  77. }
  78. switch ($message->res_type) {
  79. case 'sentence':
  80. $sentence = Sentence::where('uid', $message->res_id)->first();
  81. if (! $sentence) {
  82. Log::error('invalid sentence id '.$message->res_id);
  83. $result = 1;
  84. break;
  85. }
  86. // 站内信
  87. try {
  88. $sendTo = [];
  89. // 句子的channel拥有者
  90. // $sendTo[] = $prData->channel->studio_id;
  91. // 句子的作者
  92. if (! in_array($sentence->editor_uid, $sendTo)) {
  93. $sendTo[] = $sentence->editor_uid;
  94. }
  95. // 句子的采纳者
  96. if (! empty($sentence->acceptor_uid) && ! in_array($sentence->acceptor_uid, $sendTo)) {
  97. $sendTo[] = $sentence->acceptor_uid;
  98. }
  99. $this->notification(
  100. $message->editor->id,
  101. $sendTo,
  102. 'discussion',
  103. $message->id,
  104. $sentence->channel_uid
  105. );
  106. } catch (\Exception $e) {
  107. Log::error('send notification failed', ['exception' => $e]);
  108. }
  109. // webhook
  110. $contentHtml = MdRender::render(
  111. $sentence->content,
  112. [$sentence->channel_uid],
  113. null,
  114. 'read',
  115. 'translation',
  116. $sentence->content_type
  117. );
  118. $contentTxt = strip_tags($contentHtml);
  119. /**生成消息内容 */
  120. $msgParam['anchor-content'] = $contentTxt;
  121. $WebHookResId = $sentence->channel_uid;
  122. $this->WebHook($msgParam, $type, $WebHookResId);
  123. break;
  124. case 'wbw':
  125. $wbw = Wbw::where('uid', $message->res_id)->first();
  126. if (! $wbw) {
  127. Log::error('invalid wbw id '.$message->res_id);
  128. $result = 1;
  129. break;
  130. }
  131. $wbwBlock = WbwBlock::where('uid', $wbw->block_uid)->first();
  132. if (! $wbwBlock) {
  133. Log::error('invalid wbw-block id '.$message->res_id);
  134. $result = 1;
  135. break;
  136. }
  137. // 站内信
  138. try {
  139. $sendTo = [];
  140. // channel拥有者
  141. // $sendTo[] = $prData->channel->studio_id;
  142. // 作者
  143. if (! in_array($wbw->creator_uid, $sendTo)) {
  144. $sendTo[] = $wbw->creator_uid;
  145. }
  146. // 提问者
  147. if (! empty($message->parent)) {
  148. $topicEditor = Discussion::where('id', $message->parent)
  149. ->value('editor_uid');
  150. if (! empty($topicEditor) && ! in_array($topicEditor, $sendTo)) {
  151. $sendTo[] = $topicEditor;
  152. Log::debug('发送给提问者', ['data' => $topicEditor]);
  153. }
  154. }
  155. $this->notification(
  156. $message->editor->id,
  157. $sendTo,
  158. 'discussion',
  159. $message->id,
  160. $wbwBlock->channel_uid
  161. );
  162. } catch (\Exception $e) {
  163. Log::error('send notification failed', ['exception' => $e]);
  164. }
  165. $msgParam['anchor-content'] = $wbw->word;
  166. $WebHookResId = $wbwBlock->channel_uid;
  167. $this->WebHook($msgParam, $type, $WebHookResId);
  168. break;
  169. case 'term':
  170. $term = DhammaTerm::where('guid', $message->res_id)->first();
  171. if (! $term) {
  172. Log::error('invalid term id '.$message->res_id);
  173. $result = 1;
  174. break;
  175. }
  176. if (empty($term->channal) || ! Str::isUuid($term->channal)) {
  177. break;
  178. }
  179. // 站内信
  180. try {
  181. $sendTo = [];
  182. // 拥有者
  183. $sendTo[] = $term->term;
  184. // 作者
  185. $editor = UserApi::getById($term->editor_id);
  186. if ($editor['id'] !== 0 && ! in_array($editor['id'], $sendTo)) {
  187. $sendTo[] = $editor['id'];
  188. }
  189. $this->notification(
  190. $message->editor->id,
  191. $sendTo,
  192. 'discussion',
  193. $message->id,
  194. $term->channal
  195. );
  196. } catch (\Exception $e) {
  197. Log::error('send notification failed', ['exception' => $e]);
  198. }
  199. // webhook
  200. $msgParam['anchor-content'] = $term->meaning.'('.$term->word.')';
  201. $WebHookResId = $term->channal;
  202. $this->WebHook($msgParam, 'term', $WebHookResId);
  203. break;
  204. default:
  205. // code...
  206. break;
  207. }
  208. return $result;
  209. });
  210. return 0;
  211. }
  212. private function WebHook($msgParam, $type, $resId)
  213. {
  214. $rootId = UserApi::getById(0)['id'];
  215. $articleTitle = "webhook://discussion/{$type}/zh-hans";
  216. $tpl = Article::where('owner', $rootId)
  217. ->where('title', $articleTitle)
  218. ->value('content');
  219. if (empty($tpl)) {
  220. Log::error('mq:discussion 模版不能为空', ['tpl_title' => $articleTitle]);
  221. return 1;
  222. }
  223. $m = new \Mustache_Engine([
  224. 'entity_flags' => ENT_QUOTES,
  225. 'delimiters' => '{% %}',
  226. ]);
  227. $msgContent = $m->render($tpl, $msgParam);
  228. $webhooks = WebHook::where('res_id', $resId)
  229. ->where('status', 'active')
  230. ->get();
  231. $result = 0;
  232. foreach ($webhooks as $key => $hook) {
  233. $event = json_decode($hook->event);
  234. if (is_array($event)) {
  235. if (! in_array('discussion', $event)) {
  236. continue;
  237. }
  238. } else {
  239. continue;
  240. }
  241. $command = '';
  242. $whSend = new WebHookSend;
  243. $ok = 0;
  244. switch ($hook->receiver) {
  245. case 'dingtalk':
  246. $ok = $whSend->dingtalk($hook->url, $msgParam['card_title'], $msgContent);
  247. break;
  248. case 'wechat':
  249. $ok = $whSend->wechat($hook->url, null, $msgContent);
  250. break;
  251. default:
  252. $ok = 2;
  253. break;
  254. }
  255. $result += $ok;
  256. $logMsg = "{$command} ok={$ok}";
  257. if ($ok === 0) {
  258. $this->info($logMsg);
  259. } else {
  260. $this->error($logMsg);
  261. }
  262. if ($ok === 0) {
  263. Log::debug('mq:discussion: send success {url}', ['url' => $hook->url]);
  264. WebHook::where('id', $hook->id)->increment('success');
  265. } else {
  266. Log::error('mq:discussion: send fail {url}', ['url' => $hook->url]);
  267. WebHook::where('id', $hook->id)->increment('fail');
  268. }
  269. }
  270. }
  271. private function notification($from, $to, $resType, $resId, $channel)
  272. {
  273. // 发送站内信
  274. try {
  275. $sendCount = NotificationController::insert(
  276. $from,
  277. $to,
  278. $resType,
  279. $resId,
  280. $channel
  281. );
  282. $this->info('send notification success to ['.$sendCount.'] users');
  283. } catch (\Exception $e) {
  284. Log::error('send notification failed', ['exception' => $e]);
  285. }
  286. }
  287. }