hyperf-chat/app/Service/TalkListService.php

205 lines
6.6 KiB
PHP
Raw Normal View History

2021-07-08 23:44:43 +08:00
<?php
namespace App\Service;
use App\Cache\LastMessage;
use App\Cache\ServerRunID;
use App\Cache\UnreadTalk;
use App\Constants\TalkMode;
2021-07-09 22:57:19 +08:00
use App\Model\Talk\TalkList;
2021-07-13 22:53:13 +08:00
use Carbon\Carbon;
2021-07-08 23:44:43 +08:00
class TalkListService
{
/**
* 创建聊天列表记录
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 创建类型[1:私聊;2:群聊;]
* @return array
*/
public function create(int $user_id, int $receiver_id, int $talk_type)
{
$result = TalkList::updateOrCreate([
'talk_type' => $talk_type,
'user_id' => $user_id,
'receiver_id' => $receiver_id,
], [
'is_top' => 0,
'is_delete' => 0,
'is_disturb' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
return [
'id' => $result->id,
'talk_type' => $result->talk_type,
'receiver_id' => $result->receiver_id,
];
}
/**
* 聊天对话列表置顶操作
*
* @param int $user_id 用户ID
* @param int $list_id 会话列表ID
* @param bool $is_top 是否置顶true: false:否)
* @return bool
*/
public function top(int $user_id, int $list_id, $is_top = true)
{
return (bool)TalkList::query()->where([
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_top' => $is_top ? 1 : 0,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 删除会话列表
*
* @param int $user_id 用户ID
* @param int $list_id 会话列表ID
* @return bool
*/
public function delete(int $user_id, int $list_id)
{
return (bool)TalkList::query()->where([
['id', '=', $list_id],
['user_id', '=', $user_id],
])->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 删除会话列表
*
* @param int $user_id 用户ID
* @param int $receiver_id 接受者ID
* @param int $talk_type 对话类型
* @return bool
*/
public function deleteByType(int $user_id, int $receiver_id, int $talk_type)
{
return (bool)TalkList::query()->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->update([
'is_delete' => 1,
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 获取用户的聊天列表
*
* @param int $user_id 用户ID
* @return array
*/
public function getTalkList(int $user_id)
{
$filed = [
'list.id', 'list.talk_type', 'list.receiver_id', 'list.updated_at', 'list.is_disturb', 'list.is_top',
'users.avatar as user_avatar', 'users.nickname',
'group.group_name', 'group.avatar as group_avatar'
];
$rows = TalkList::from('talk_list as list')
->leftJoin('users', function ($join) {
$join->on('users.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::PRIVATE_CHAT);
})
->leftJoin('group', function ($join) {
$join->on('group.id', '=', 'list.receiver_id')->where('list.talk_type', '=', TalkMode::GROUP_CHAT);
})
->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) {
$data = TalkList::item([
'id' => $item['id'],
'talk_type' => $item['talk_type'],
'receiver_id' => $item['receiver_id'],
'is_top' => $item['is_top'],
'is_disturb' => $item['is_disturb'],
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
if ($item['talk_type'] == TalkMode::PRIVATE_CHAT) {
$data['name'] = $item['nickname'];
$data['avatar'] = $item['user_avatar'];
$data['unread_num'] = UnreadTalk::getInstance()->read($item['receiver_id'], $user_id);
2021-07-12 22:00:48 +08:00
$data['is_online'] = container()->get(SocketClientService::class)->isOnlineAll($item['receiver_id'], $runIdAll);
$data['remark_name'] = container()->get(UserFriendService::class)->getFriendRemark($user_id, $item['receiver_id']);
} else if (TalkMode::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
*/
public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb)
{
$result = TalkList::query()->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->first(['id', 'is_disturb']);
if (!$result || $is_disturb == $result->is_disturb) {
return false;
}
return (bool)TalkList::query()->where('id', $result->id)->update([
'is_disturb' => $is_disturb,
'updated_at' => date('Y-m-d H:i:s')
]);
}
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
*/
public function isDisturb(int $user_id, int $receiver_id, int $talk_type)
{
return (bool)TalkList::query()->where([
['user_id', '=', $user_id],
['talk_type', '=', $talk_type],
['receiver_id', '=', $receiver_id],
])->value('is_disturb');
}
2021-07-08 23:44:43 +08:00
}