推拉函数与queue

This commit is contained in:
cano
2024-03-03 09:39:06 +08:00
parent f5f7168009
commit 66d88ea2b3
15 changed files with 476 additions and 108 deletions

View File

@ -3,10 +3,16 @@
namespace App\Models\Post;
use App\Exceptions\ModelException;
use App\Jobs\AddPostQueue;
use App\Models\Base\BaseModel;
use App\Models\Comment\PostCommentModel;
use App\Models\Customer\CustomerUserExtendModel;
use App\Models\Follow\FollowModel;
use App\Models\Post\Structs\PostParamsStruct;
use App\Structs\QueueAddPostStruct;
use App\Tools\CollectOffsetLimit;
use App\Tools\Tools;
use Illuminate\Database\Eloquent\Casts\Attribute;
class PostPushBoxModel extends BaseModel
{
@ -15,18 +21,29 @@ class PostPushBoxModel extends BaseModel
protected $primaryKey = 'id';
protected $fillable = [
'id',
'type',
'uid',
'pid',
'puuid',
'post_params',
'is_like',
'is_repost',
'is_bookmark',
'is_read',
'post_created_at',
'created_at',
'created_box_at',
'deleted_at',
];
const TYPE_POST = 1;
const TYPE_REPOST = 2;
const TYPE_COMMENT = 3;
const TYPE = [
self::TYPE_POST => '推文',
self::TYPE_REPOST => '转发',
self::TYPE_COMMENT => '评论',
];
const IS_LIKE_DEFAULT = 1;
const IS_LIKE_YES = 2;
const IS_LIKE_NO = 3;
@ -50,24 +67,69 @@ class PostPushBoxModel extends BaseModel
self::IS_BOOKMARK_YES => '已收藏',
];
const IS_READ_DEFAULT = 1;
const IS_READ_NO = 1;
const IS_READ_YES = 2;
const IS_READ = [
self::IS_READ_DEFAULT => '默认',
self::IS_READ_NO => '默认',
self::IS_READ_YES => '已收藏',
];
protected function postParams(): Attribute
{
return Attribute::make(
get: fn (string $value = '') => Tools::JonsDecode($value),
set: fn (array $value = []) => Tools::JonsEncode($value),
);
}
public static function addPostQueueProducer(array $params): void
{
AddPostQueue::dispatch($params)->onQueue(QueueAddPostStruct::QUEUE_NAME);
}
/**
* 提交后调用事件
* 提交后调用事件,在消费队列跑推送
* @throws ModelException
*/
function newPostPushTask($id = null, $uuid = null)
function addPostQueueConsumer(array $params)
{
if(empty($params)) return false;
if(isset($params['id'])) return false;
if(isset($params['type'])) return false;
$id = $params['id'];
$type = $params['type'];
if(!in_array($type,[self::TYPE_POST,self::TYPE_REPOST,self::TYPE_COMMENT])) return false;
if(empty($id)) return false;
$oPostModel = new PostModel();
$oPost = null;
if ($id) $oPost = $oPostModel->findItem($id);
if ($uuid) $oPost = $oPostModel->findItemByWhere(['uuid' => $uuid]);
$aPostParams = [];
//判断推送类型
if ($type == self::TYPE_POST) {
$postId = $id;
} elseif ($type == self::TYPE_REPOST) {
$postId = $id;
$oRePostModel = $oPostModel->findItem($id);
if(!$oRePostModel) return false;
$aPostParams[PostParamsStruct::REPOST_ORG_USER_ID] = $oRePostModel->uid;
$aPostParams[PostParamsStruct::REPOST_ORG_POST_ID] = $oRePostModel->mid;
} elseif ($type == self::TYPE_COMMENT) {
$oPostCommentModel = new PostCommentModel();
$oPostCommentModel = $oPostCommentModel->findItem($id);
if(!$oPostCommentModel) return false;
$postId = $oPostCommentModel->pid;
$aPostParams[PostParamsStruct::COMMENT_ID] = $oPostCommentModel->id;
$aPostParams[PostParamsStruct::COMMENT_USER_ID] = $oPostCommentModel->uid;
$aPostParams[PostParamsStruct::COMMENT_POST_ID] = $oPostCommentModel->pid;
$aPostParams[PostParamsStruct::COMMENT_CONTEXT] = $oPostCommentModel->content;
$aPostParams[PostParamsStruct::COMMENT_CONTEXT_CREATE_TIME] = $oPostCommentModel->created_at;
} else {
throw new ModelException('type error');
}
if ($postId) $oPost = $oPostModel->findItem($postId);
if (!$oPost) throw new ModelException('post not found');
$iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000));
@ -80,11 +142,14 @@ class PostPushBoxModel extends BaseModel
$bSendMode = $CustomerUserExtend->fans_num < $iConfigFansPushLimit;
$iTotalCount = $this->countSendFans($bSendMode, $oPost->uid); //计算发送总数
$aPost = $oPost->toArray();
if(empty($aPost)) return false;
$aPost['post_params'] = $aPostParams;
//分批发送
$oCollectOffsetLimit = new CollectOffsetLimit();
$oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($oPost, $bSendMode) {
$oFollowList = $this->getFansListWithPage($bSendMode, $oPost->uid, $offset, $limit);
$this->sendPostToBox($oPost, $oFollowList);
$oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($aPost, $bSendMode) {
$oFollowList = $this->getFansListWithPage($bSendMode, $aPost['uid'], $offset, $limit);
$this->sendPostToBox($aPost, $oFollowList);
});
}
@ -116,17 +181,15 @@ class PostPushBoxModel extends BaseModel
}
//发送到推送信箱
function sendPostToBox($oPost, $oFollowList): void
function sendPostToBox($aPost, $oFollowList): void
{
if (!$oFollowList) return;
$date = date('Y-m-d H:i:s');
foreach ($oFollowList as $oFollow) {
$aItem['uid'] = $oFollow->uid;
$aItem['pid'] = $oPost->id;
$aItem['puuid'] = $oPost->uuid;
$aItem['created_at'] = $oPost->created_at;
$aItem['created_box_at'] = $date;
$this->addItem($aItem);
$aItem['pid'] = $aPost['id'];
$aItem['puuid'] = $aPost['uuid'];
$aItem['post_created_at'] = $aPost['created_at'];
$this->addItemWithCreateTime($aItem);
}
}
@ -138,20 +201,27 @@ class PostPushBoxModel extends BaseModel
* @param $limit
* @return \Illuminate\Database\Eloquent\Collection|array
*/
function getPushBoxList($uid, $last_id = 0, $limit = 20): \Illuminate\Database\Eloquent\Collection|array
function getPushBoxList($uid, $last_id = 0, $limit = 20,$cols = ['*']): \Illuminate\Database\Eloquent\Collection|array
{
//活跃用户直接拉取未读消息
return $this->newQuery()->where('uid', $uid)->where('id', '>', $last_id)->where('is_read', self::IS_READ_DEFAULT)->orderBy('created_at', 'desc')->limit($limit)->get();
return $this->newQuery()
->where('uid', $uid)
// ->where('id', '>', $last_id)
->where('is_read', self::IS_READ_NO)
->orderBy('created_at', 'desc')
->limit($limit)
->get($cols);
//非活跃用户拉取大v消息在用户状态更新时已经调用过此处不用在做处理
}
//非活跃拉取已跟随大v最新文章。
//放在用户状态更新时调用
function pullBigFanMasterPost($uid)
function pullBigFanMasterPostConsumer($uid)
{
//@@获取文章和转发文章和评论文章
//获取大v定义粉丝数,获取大于该粉丝数的大v文章
$iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000));
//@此处需针对大量数据进行分批获取
//@@此处需针对大量数据进行分批获取
$oFollowModel = new FollowModel();
$oFollowList = $oFollowModel->getFollowListWithFansLimit($uid,$iConfigFansPushLimit,['a.follow_uid']);
if ($oFollowList->isEmpty()) return null;
@ -161,7 +231,7 @@ class PostPushBoxModel extends BaseModel
$oPostList = $oPostModel->getPostListByUids($aFollowUid);
//将推文发送到信箱
$this->sendPostToBoxByUid($oPostList,$uid);
self::sendPostToBoxByUid($oPostList,$uid);
}