ImService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Services\V1_1;
  3. use App\Generated\V1_1\DTOs\TimMessageDTO;
  4. use App\Generated\V1_1\Messages\Im\MarkUserMessagesReadMessage;
  5. use App\Generated\V1_1\Messages\Im\DeleteMessage;
  6. use App\Generated\V1_1\Messages\Im\RevokeMessage;
  7. use App\Generated\V1_1\Messages\Im\SendMessage;
  8. use App\Generated\V1_1\Messages\Im\GetUsersLatestsMessage;
  9. use App\Http\Controllers\Miniprogram\Auth;
  10. use App\Managers\FriendManager;
  11. use App\Managers\IMManager;
  12. use App\Services\Pair\PairFirstMatch;
  13. use App\Utils\ImUtil;
  14. use Illuminate\Support\Arr;
  15. class ImService
  16. {
  17. protected $imUtil;
  18. protected $imManager;
  19. public function __construct(ImUtil $imUtil, IMManager $imManager)
  20. {
  21. $this->imUtil = $imUtil;
  22. $this->imManager = $imManager;
  23. }
  24. public function markUserMessagesRead(MarkUserMessagesReadMessage $message)
  25. {
  26. $timMessage = $message->getMessage();
  27. $msgRandom = $timMessage->getMsgRandom();
  28. $msgTimeStamp = $timMessage->getMsgTimeStamp();
  29. $msgSeq = $timMessage->getMsgSeq();
  30. $uid = Auth::auth();
  31. $map = $this->imManager->mapAccountToUserId([
  32. $fromAccount = $timMessage->getFromAccount(),
  33. $toAccount = $timMessage->getToAccount(),
  34. ]);
  35. $this->imManager->markMessageAsRead($uid, $map[$fromAccount] ?? 0, $map[$toAccount] ?? 0, $msgTimeStamp, $msgRandom, $msgSeq);
  36. $friendManager = new FriendManager();
  37. $friendManager->makeZeroUnreadMsg($uid, $map[$toAccount]);
  38. }
  39. public function delete(DeleteMessage $message)
  40. {
  41. $timMessage = $message->getMessage();
  42. $msgRandom = $timMessage->getMsgRandom();
  43. $msgTimeStamp = $timMessage->getMsgTimeStamp();
  44. $msgSeq = $timMessage->getMsgSeq();
  45. $uid = Auth::auth();
  46. $map = $this->imManager->mapAccountToUserId([
  47. $fromAccount = $timMessage->getFromAccount(),
  48. $toAccount = $timMessage->getToAccount(),
  49. ]);
  50. $this->imManager->deleteMessage($uid, $map[$fromAccount] ?? 0, $map[$toAccount] ?? 0, $msgTimeStamp, $msgRandom, $msgSeq);
  51. }
  52. public function revoke(RevokeMessage $message)
  53. {
  54. $timMessage = $message->getMessage();
  55. $msgRandom = $timMessage->getMsgRandom();
  56. $msgTimeStamp = $timMessage->getMsgTimeStamp();
  57. $msgSeq = $timMessage->getMsgSeq();
  58. $uid = Auth::auth();
  59. $map = $this->imManager->mapAccountToUserId([
  60. $fromAccount = $timMessage->getFromAccount(),
  61. $toAccount = $timMessage->getToAccount(),
  62. ]);
  63. $this->imManager->revokeMessage($uid, $map[$toAccount] ?? 0, $msgTimeStamp, $msgRandom, $msgSeq);
  64. }
  65. public function send(SendMessage $message)
  66. {
  67. $pairFirstMatch = new PairFirstMatch(1);
  68. $pairFirstMatch->run();
  69. }
  70. public function getUsersLatests(GetUsersLatestsMessage $message)
  71. {
  72. $uids = $message->getUids();
  73. $uid = Auth::auth();
  74. $timMessages = $this->imManager->getUserLatestMessages($uid, $uids);
  75. $timMessages->map(function (&$item) {
  76. $content = json_decode($item->content, true);
  77. $item->fromAccount = Arr::get($content, 'From_Account');
  78. $item->toAccount = Arr::get($content, 'To_Account');
  79. $item->msgSeq = $item->seq;
  80. $item->msgRandom = $item->msg_random;
  81. $item->msgTimeStamp = $item->msg_time_stamp;
  82. return $item;
  83. });
  84. $message->setResponse(TimMessageDTO::initFromModels($timMessages->toArray()));
  85. }
  86. }