ImService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Services\V1;
  3. use App\Generated\V1\DTOs\TimMessageDTO;
  4. use App\Generated\V1\Messages\Im\MarkUserMessagesReadMessage;
  5. use App\Generated\V1\Messages\Im\DeleteMessage;
  6. use App\Generated\V1\Messages\Im\RevokeMessage;
  7. use App\Generated\V1\Messages\Im\SendMessage;
  8. use App\Generated\V1\Messages\Im\GetUsersLatestsMessage;
  9. use App\Http\Controllers\Miniprogram\Auth;
  10. use App\Managers\IMManager;
  11. use App\Utils\ImUtil;
  12. class ImService
  13. {
  14. protected $imUtil;
  15. protected $imManager;
  16. public function __construct(ImUtil $imUtil, IMManager $imManager)
  17. {
  18. $this->imUtil = $imUtil;
  19. $this->imManager = $imManager;
  20. }
  21. public function markUserMessagesRead(MarkUserMessagesReadMessage $message)
  22. {
  23. $timMessage = $message->getMessage();
  24. $msgRandom = $timMessage->getMsgRandom();
  25. $msgTimeStamp = $timMessage->getMsgTimeStamp();
  26. $msgSeq = $timMessage->getMsgSeq();
  27. $uid = Auth::auth();
  28. $map = $this->imManager->mapAccountToUserId([
  29. $fromAccount = $timMessage->getFromAccount(),
  30. $toAccount = $timMessage->getToAccount(),
  31. ]);
  32. $this->imManager->markMessageAsRead($uid, $map[$fromAccount] ?? 0, $map[$toAccount] ?? 0, $msgTimeStamp, $msgRandom, $msgSeq);
  33. }
  34. public function delete(DeleteMessage $message)
  35. {
  36. $timMessage = $message->getMessage();
  37. $msgRandom = $timMessage->getMsgRandom();
  38. $msgTimeStamp = $timMessage->getMsgTimeStamp();
  39. $msgSeq = $timMessage->getMsgSeq();
  40. $uid = Auth::auth();
  41. $map = $this->imManager->mapAccountToUserId([
  42. $fromAccount = $timMessage->getFromAccount(),
  43. $toAccount = $timMessage->getToAccount(),
  44. ]);
  45. $this->imManager->deleteMessage($uid, $map[$fromAccount] ?? 0, $map[$toAccount] ?? 0, $msgTimeStamp, $msgRandom, $msgSeq);
  46. }
  47. public function revoke(RevokeMessage $message)
  48. {
  49. $timMessage = $message->getMessage();
  50. $msgRandom = $timMessage->getMsgRandom();
  51. $msgTimeStamp = $timMessage->getMsgTimeStamp();
  52. $msgSeq = $timMessage->getMsgSeq();
  53. $uid = Auth::auth();
  54. $map = $this->imManager->mapAccountToUserId([
  55. $fromAccount = $timMessage->getFromAccount(),
  56. $toAccount = $timMessage->getToAccount(),
  57. ]);
  58. $this->imManager->revokeMessage($uid, $map[$toAccount] ?? 0, $msgTimeStamp, $msgRandom, $msgSeq);
  59. }
  60. public function send(SendMessage $message)
  61. {
  62. $message->getMessage();
  63. }
  64. public function getUsersLatests(GetUsersLatestsMessage $message)
  65. {
  66. $uids = $message->getUids();
  67. $uid = Auth::auth();
  68. $timMessages = $this->imManager->getUserLatestMessages($uid, $uids);
  69. $message->setResponse(TimMessageDTO::initFromEloquents($timMessages));
  70. }
  71. }