MqDiscussion.php 11 KB

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