推拉函数与queue
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Post;
|
||||
|
||||
use App\Exceptions\ModelException;
|
||||
use App\Models\Base\BaseModel;
|
||||
use App\Tools\Tools;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class PostModel extends BaseModel
|
||||
@ -15,7 +17,9 @@ class PostModel extends BaseModel
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'type',
|
||||
'uuid',
|
||||
'post_batch_sn',
|
||||
'mid',
|
||||
'uid',
|
||||
'media',
|
||||
@ -24,25 +28,65 @@ class PostModel extends BaseModel
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
const TYPE_POST = PostPushBoxModel::TYPE_POST;
|
||||
const TYPE_REPOST = PostPushBoxModel::TYPE_REPOST;
|
||||
|
||||
const TYPE = [
|
||||
self::TYPE_POST => '推文',
|
||||
self::TYPE_REPOST => '转发',
|
||||
];
|
||||
|
||||
protected function media(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn(string $value = '') => Tools::JonsDecode($value),
|
||||
set: fn(array $value = []) => Tools::JonsEncode($value),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ModelException
|
||||
*/
|
||||
function addPost($uid, $content = null, $media = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
|
||||
function addPost($uid, $type = self::TYPE_POST, $content = null, $media = null, $mid = null, $sBachSn = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
|
||||
{
|
||||
if(!$content && !$media) throw new ModelException('addPost params error');
|
||||
if (!in_array($type, [self::TYPE_POST, self::TYPE_REPOST])) throw new ModelException('type params error');
|
||||
$uuid = Tools::genUuid();
|
||||
$bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid());
|
||||
if (!$content && !$media) throw new ModelException('addPost params error');
|
||||
$aItem['type'] = $type;
|
||||
$aItem['uid'] = $uid;
|
||||
$aItem['uuid'] = Tools::genUuid();
|
||||
$aItem['uuid'] = $uuid;
|
||||
$aItem['post_batch_sn'] = $bsn;
|
||||
$aItem['mid'] = $mid;
|
||||
$aItem['media'] = $media;
|
||||
$aItem['content'] = $content;
|
||||
$sDateTime = date('Y-m-d H:i:s');
|
||||
$aItem['created_at'] = $sDateTime;
|
||||
$res = $this->addItem($aItem);
|
||||
if($res){
|
||||
|
||||
if ($res) {
|
||||
$this->pushToQueue(self::TYPE_POST, $res->id);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function addBatchPost(array $aPostList): void
|
||||
{
|
||||
$mid = null;
|
||||
$bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid());
|
||||
foreach ($aPostList as $aPostItem) {
|
||||
$oPost = $this->addPost($aPostItem['uid'], $aPostItem['type'], $aPostItem['content'], $aPostItem['media'], $mid, $bsn);
|
||||
if ($oPost) {
|
||||
$mid = $oPost->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//发送到消息队列处理新增post
|
||||
function pushToQueue($type, $id): void
|
||||
{
|
||||
PostPushBoxModel::addPostQueueProducer(['type' => $type, 'id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ModelException
|
||||
*/
|
||||
@ -50,9 +94,9 @@ class PostModel extends BaseModel
|
||||
{
|
||||
$oPost = $this->findItem($id);
|
||||
$res = $this->delItem($id);
|
||||
if($res){
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
@ -62,18 +106,18 @@ class PostModel extends BaseModel
|
||||
*/
|
||||
function delPostByUuid($uuid)
|
||||
{
|
||||
$oPost = $this->findItemByWhere(['uuid'=>$uuid]);
|
||||
$oPost = $this->findItemByWhere(['uuid' => $uuid]);
|
||||
$res = $this->newQuery()->where('uuid', $uuid)->delete();
|
||||
if($res){
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function getPostListByUid($uid): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
return $this->getItemsByWhere(['uid'=>$uid]);
|
||||
return $this->getItemsByWhere(['uid' => $uid]);
|
||||
}
|
||||
|
||||
function getPostById($id): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
|
||||
@ -86,13 +130,13 @@ class PostModel extends BaseModel
|
||||
*/
|
||||
function updatePostById($aItem): bool|int
|
||||
{
|
||||
if(!isset($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
if(empty($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
if (!isset($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
if (empty($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
$oPost = $this->findItem($aItem['id']);
|
||||
$res = $this->updateItem($aItem);
|
||||
if($res){
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
@ -102,31 +146,32 @@ class PostModel extends BaseModel
|
||||
*/
|
||||
function updatePostByUuid($aItem): bool|int
|
||||
{
|
||||
if(!isset($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
if(empty($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
$oPost = $this->findItemByWhere(['uuid'=>$aItem['uuid']]);
|
||||
$res = $this->updateItem($aItem,'uuid');
|
||||
if($res){
|
||||
if (!isset($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
if (empty($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
$oPost = $this->findItemByWhere(['uuid' => $aItem['uuid']]);
|
||||
$res = $this->updateItem($aItem, 'uuid');
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function getPostListByMid($mid): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
return $this->getItemsByWhere(['mid'=>$mid]);
|
||||
return $this->getItemsByWhere(['mid' => $mid]);
|
||||
}
|
||||
|
||||
|
||||
function getPostListByUidMid($uid,$mid): \Illuminate\Database\Eloquent\Collection|array
|
||||
function getPostListByUidMid($uid, $mid): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
return $this->getItemsByWhere(['uid'=>$uid,'mid'=>$mid]);
|
||||
return $this->getItemsByWhere(['uid' => $uid, 'mid' => $mid]);
|
||||
}
|
||||
function getPostListByUids($uids,$sDateLimit = null): \Illuminate\Database\Eloquent\Collection|array
|
||||
|
||||
function getPostListByUids($uids, $sDateLimit = null): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
if($sDateLimit == null) $sDateLimit = date('Y-m-d H:i:s',strtotime('-3 day'));
|
||||
return $this->newQuery()->where('created_at',$sDateLimit)->whereIn('uid',$uids)->get();
|
||||
if ($sDateLimit == null) $sDateLimit = date('Y-m-d H:i:s', strtotime('-3 day'));
|
||||
return $this->newQuery()->where('created_at', $sDateLimit)->whereIn('uid', $uids)->get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
16
app/Models/Post/Structs/PostParamsStruct.php
Normal file
16
app/Models/Post/Structs/PostParamsStruct.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace App\Models\Post\Structs;
|
||||
class PostParamsStruct{
|
||||
const REPOST_ORG_USER_ID = 'repostOrgUserId';
|
||||
const REPOST_ORG_USERNAME = 'repostOrgUsername';
|
||||
const REPOST_ORG_USER_NICKNAME = 'repostOrgUserNickname';
|
||||
const REPOST_ORG_POST_ID = 'repostOrgPostId'; //原推文id
|
||||
|
||||
const COMMENT_ID = 'CommentId';
|
||||
const COMMENT_USER_ID = 'CommentUserId';
|
||||
const COMMENT_USERNAME = 'CommentUsername';
|
||||
const COMMENT_POST_ID = 'CommentPostId';
|
||||
const COMMENT_CONTEXT= 'CommentContext';
|
||||
const COMMENT_CONTEXT_CREATE_TIME= 'CommentContextCreateTime';
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user