GroupMsg.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * 群组消息
  4. * @author solu
  5. */
  6. class GroupMsg extends Model {
  7. protected $tableName = 'group_msg';
  8. protected $dbKey = 'dw_chat';
  9. const MAX_REPEAL_TIME = 180; // 三分钟撤销时间
  10. /**
  11. * 撤销消息
  12. * @author solu
  13. * @param $from
  14. * @param $group_id
  15. * @param $hash
  16. * @return bool
  17. * @throws Exception
  18. */
  19. public function repeal($from, $group_id, $hash) {
  20. $where = compact('hash', 'group_id');
  21. $msg = $this->objTable->getRow($where);
  22. if (!$msg) {
  23. throw new Exception('message not found', CODE_NORMAL_ERROR);
  24. }
  25. $isAdmin = (new UserGroup())->isAdmin($group_id, $from);
  26. if (!$isAdmin) {
  27. if ($msg['from'] != $from) {
  28. throw new Exception('no permission', CODE_NO_PERMITION);
  29. }
  30. if ((time() - strtotime($msg['create_time']) > self::MAX_REPEAL_TIME)) {
  31. throw new Exception('repeal message time out', CODE_NORMAL_ERROR);
  32. }
  33. }
  34. $newData = ['state' => 0];
  35. $msg['is_pin'] && $newData['is_pin'] = 0;
  36. $this->objTable->updateObject($newData, $where);
  37. // 如果撤销的是最后一条
  38. if ($msg['msg_num'] == Session::getLastMsgNum($group_id)) {
  39. Session::setLastMsg($group_id, Session::MSG_TYPE_REPEAL, '', $from, '');
  40. }
  41. ThirdApi::pushGroupEvent($group_id, [
  42. 'type' => 'repeal',
  43. 'group_id' => $group_id,
  44. 'from' => $from,
  45. 'nick_name' => User::getUserNameById($from),
  46. 'hash' => $hash,
  47. ]);
  48. $msg['is_pin'] && ThirdApi::pushGroupEvent($group_id, [
  49. 'type' => 'unpin_msg',
  50. 'group_id' => $group_id,
  51. 'from' => $from,
  52. 'hash' => $hash,
  53. ]);
  54. return true;
  55. }
  56. }