hyperf-chat/app/Service/TalkSessionService.php

196 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2021-07-08 23:44:43 +08:00
<?php
2021-08-29 22:24:45 +08:00
declare(strict_types=1);
2021-07-08 23:44:43 +08:00
namespace App\Service;
use App\Cache\LastMessage;
use App\Cache\ServerRunID;
2021-07-21 23:31:41 +08:00
use App\Cache\UnreadTalkCache;
2022-01-22 20:08:19 +08:00
use App\Constant\TalkModeConstant;
2022-01-16 14:22:53 +08:00
use App\Model\Talk\TalkSession;
2022-01-22 16:04:54 +08:00
use App\Repository\Talk\TalkSessionRepository;
2021-07-13 22:53:13 +08:00
use Carbon\Carbon;
2021-07-08 23:44:43 +08:00
2022-01-22 16:04:54 +08:00
class TalkSessionService extends BaseService
2021-07-08 23:44:43 +08:00
{
2022-01-22 16:04:54 +08:00
/**
* @var TalkSessionRepository
*/
private $repository;
public function __construct(TalkSessionRepository $repository)
{
$this->repository = $repository;
}
2021-07-08 23:44:43 +08:00
/**
* 创建聊天列表记录
*
2021-08-29 22:24:45 +08:00
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 创建类型[1:私聊;2:群聊;]
* @param bool $is_robot
2021-07-08 23:44:43 +08:00
* @return array
*/
2021-08-29 22:24:45 +08:00
public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false): array
2021-07-08 23:44:43 +08:00
{
2022-01-22 16:04:54 +08:00
$result = $this->repository->updateOrCreate([
2021-07-08 23:44:43 +08:00
'talk_type' => $talk_type,
'user_id' => $user_id,
'receiver_id' => $receiver_id,
], [
'is_top' => 0,
'is_delete' => 0,
'is_disturb' => 0,
2021-07-25 22:57:41 +08:00
'is_robot' => intval($is_robot),
2021-07-08 23:44:43 +08:00
]);
return [
'id' => $result->id,
'talk_type' => $result->talk_type,
'receiver_id' => $result->receiver_id,
];
}
/**
* 聊天对话列表置顶操作
*
2022-01-22 16:04:54 +08:00
* @param int $user_id 用户ID
* @param int $session_id 会话列表ID
* @param bool $is_top 是否置顶true: false:否)
2021-07-08 23:44:43 +08:00
* @return bool
*/
2022-01-22 16:04:54 +08:00
public function top(int $user_id, int $session_id, bool $is_top = true): bool
2021-07-08 23:44:43 +08:00
{
2022-01-22 16:04:54 +08:00
return (bool)$this->repository->update(['id' => $session_id, 'user_id' => $user_id], ['is_top' => $is_top ? 1 : 0]);
2021-07-08 23:44:43 +08:00
}
/**
* 删除会话列表
*
2022-01-22 16:04:54 +08:00
* @param int $user_id 用户ID
* @param int $session_id 会话列表ID
2021-07-08 23:44:43 +08:00
* @return bool
*/
2022-01-22 16:04:54 +08:00
public function delete(int $user_id, int $session_id): bool
2021-07-08 23:44:43 +08:00
{
2022-01-22 16:04:54 +08:00
return (bool)$this->repository->update(['id' => $session_id, 'user_id' => $user_id], ['is_delete' => 1]);
2021-07-08 23:44:43 +08:00
}
/**
* 删除会话列表
*
* @param int $user_id 用户ID
* @param int $receiver_id 接受者ID
* @param int $talk_type 对话类型
* @return bool
*/
2021-08-29 22:24:45 +08:00
public function deleteByType(int $user_id, int $receiver_id, int $talk_type): bool
2021-07-08 23:44:43 +08:00
{
2022-01-22 16:04:54 +08:00
return (bool)$this->repository->update(['user_id' => $user_id, 'talk_type' => $talk_type, 'receiver_id' => $receiver_id], ['is_delete' => 1]);
2021-07-08 23:44:43 +08:00
}
/**
* 获取用户的聊天列表
*
* @param int $user_id 用户ID
* @return array
*/
2021-08-29 22:24:45 +08:00
public function getTalkList(int $user_id): array
2021-07-08 23:44:43 +08:00
{
$filed = [
2021-09-12 16:23:43 +08:00
'list.id', 'list.talk_type', 'list.receiver_id', 'list.updated_at', 'list.is_disturb', 'list.is_top', 'list.is_robot',
2021-07-08 23:44:43 +08:00
'users.avatar as user_avatar', 'users.nickname',
'group.group_name', 'group.avatar as group_avatar'
];
2022-01-16 14:22:53 +08:00
$rows = TalkSession::from('talk_session as list')
2021-07-08 23:44:43 +08:00
->leftJoin('users', function ($join) {
2021-07-20 23:12:18 +08:00
$join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkModeConstant::PRIVATE_CHAT);
2021-07-08 23:44:43 +08:00
})
->leftJoin('group', function ($join) {
2021-07-20 23:12:18 +08:00
$join->on('group.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkModeConstant::GROUP_CHAT);
2021-07-08 23:44:43 +08:00
})
->where('list.user_id', $user_id)
->where('list.is_delete', 0)
->orderBy('list.updated_at', 'desc')
->get($filed)
->toArray();
if (!$rows) return [];
2021-07-12 22:00:48 +08:00
$runIdAll = ServerRunID::getInstance()->getServerRunIdAll();
return array_map(function ($item) use ($user_id, $runIdAll) {
2022-01-16 14:22:53 +08:00
$data = TalkSession::item([
2021-07-12 22:00:48 +08:00
'id' => $item['id'],
'talk_type' => $item['talk_type'],
'receiver_id' => $item['receiver_id'],
'is_top' => $item['is_top'],
'is_disturb' => $item['is_disturb'],
2021-09-12 16:23:43 +08:00
'is_robot' => $item['is_robot'],
2021-07-13 22:53:13 +08:00
'updated_at' => Carbon::parse($item['updated_at'])->toDateTimeString(),
2021-07-12 22:00:48 +08:00
]);
2021-07-08 23:44:43 +08:00
2021-07-20 23:12:18 +08:00
if ($item['talk_type'] == TalkModeConstant::PRIVATE_CHAT) {
2021-07-08 23:44:43 +08:00
$data['name'] = $item['nickname'];
$data['avatar'] = $item['user_avatar'];
2021-07-21 23:31:41 +08:00
$data['unread_num'] = UnreadTalkCache::getInstance()->read($item['receiver_id'], $user_id);
2021-09-12 16:23:43 +08:00
$data['is_online'] = (int)di()->get(SocketClientService::class)->isOnlineAll($item['receiver_id'], $runIdAll);
2021-07-23 21:34:29 +08:00
$data['remark_name'] = di()->get(UserFriendService::class)->getFriendRemark($user_id, $item['receiver_id']);
2021-07-20 23:12:18 +08:00
} else if (TalkModeConstant::GROUP_CHAT) {
2021-07-08 23:44:43 +08:00
$data['name'] = strval($item['group_name']);
$data['avatar'] = $item['group_avatar'];
}
$records = LastMessage::getInstance()->read($data['talk_type'], $user_id, $data['receiver_id']);
if ($records) {
$data['msg_text'] = $records['text'];
$data['updated_at'] = $records['created_at'];
}
return $data;
}, $rows);
}
/**
* 设置消息免打扰
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
2021-07-10 00:18:40 +08:00
* @param int $talk_type 对话类型[1:私信;2:群聊;]
2021-07-08 23:44:43 +08:00
* @param int $is_disturb 是否免打扰[0:;1:;]
* @return boolean
*/
2021-08-29 22:24:45 +08:00
public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb): bool
2021-07-08 23:44:43 +08:00
{
2022-01-22 16:04:54 +08:00
$result = $this->repository->first([
'user_id' => $user_id,
'talk_type' => $talk_type,
'receiver_id' => $receiver_id,
], ['id', 'is_disturb']);
2021-07-08 23:44:43 +08:00
if (!$result || $is_disturb == $result->is_disturb) {
return false;
}
2022-01-22 16:04:54 +08:00
return (bool)$this->repository->update(['id' => $result->id], ['is_disturb' => $is_disturb]);
2021-07-08 23:44:43 +08:00
}
2021-07-10 00:18:40 +08:00
/**
* 判断是否消息免打扰
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 对话类型[1:私信;2:群聊;]
* @return bool
*/
2021-08-29 22:24:45 +08:00
public function isDisturb(int $user_id, int $receiver_id, int $talk_type): bool
2021-07-10 00:18:40 +08:00
{
2022-01-22 16:04:54 +08:00
return (bool)$this->repository->value([
'user_id' => $user_id,
'talk_type' => $talk_type,
'receiver_id' => $receiver_id,
], "is_disturb");
2021-07-10 00:18:40 +08:00
}
2021-07-08 23:44:43 +08:00
}