作者 邓超

重新同步

... ... @@ -15,80 +15,90 @@ class AutoMail {
$this->start();
}
/**
* @var Lib\Es\Es
*/
public $es;
/**
* @author:dc
* @time 2024/7/18 14:04
*/
private function start(){
echo "start ";
echo date('Y-m-d H:i:s');
echo "\n";
if(redis()->get('resync_to_es_inbox') == 1){
$post_ids = fob_mysql()->all("select `post_id` from `e_mail_binds` group by `post_id`");
$this->es = es();
foreach ($post_ids as $post_id){
$total = $this->essss(0,$post_id,0);
$autoTotal = $this->essss(0,$post_id,1);
echo "postid {$post_id} inbox {$total} auto {$autoTotal}\n";
}
$old = redis()->get('prev_auto_mail_keyword',[]);
$filter = isAiAutoMail(true,true);
if(!$filter) return 0;
redis()->set('prev_auto_mail_keyword',$filter,86400);
$t = function ($vs){
return array_map(function ($v){
return $v[0].":".$v[1];
},$vs);
};
// 找到新增的
$q = array_diff($t($filter),$t($old));
if(!$q){
return 0;
}
$es = es('email_lists_copy');
echo "end ";
echo date('Y-m-d H:i:s');
echo "\n";
$not_must = [];
foreach ($q as $f){
list($t,$str) = explode(':',$f,2);
if($t==2){
$not_must[] = ["match_phrase"=>["subject"=>$str]];
}elseif ($t==1){
$not_must[] = ["match_phrase"=>["from.email"=>$str]];
}
public function essss($id,$post_id,$is_auto){
$total = 0;
while ( true){
// 查询自动回的邮件
$this->es->setIndex('email_lists_branch_'.$post_id);
$must = [
["term"=>["folder_as_int" => 1]], // 收件箱
["term"=>["is_auto" => $is_auto]], // 非auto
["term"=>["is_hots" => 0]], // 非预热
["term"=>["deleted" => 0]], // 非删除
["term"=>["source" => 2]], // 非删除
["range"=>[
'uuid'=>[
"egt"=>$id
]
]]
];
if($is_auto){
$must[] = [
"match_phrase" =>[
"subject"=>"Re:"
]
];
}
}
// 查询自动回的邮件
$lists = $es->search([
"_source" => ["uuid","subject","from"],
"query"=>[
"constant_score"=>[
"filter"=>[
"bool"=>[
"must"=>[
["term"=>["folder_as_int" => 1]], // 收件箱
["term"=>["is_auto" => 0]], // 非auto
["term"=>["is_hots" => 0]], // 非预热
["term"=>["deleted" => 0]], // 非删除
["term"=>["source" => 2]], // 非删除
// 自动关键字
[
"bool" => [
"should"=> $not_must,
"minimum_should_match" => 1
]
]
$lists = $this->es->search([
"_source" => ["uuid"],
"query"=>[
"constant_score"=>[
"filter"=>[
"bool"=>[
"must"=> $must
]
]
]
]
]
],0,1000,['udate'=>'desc'],1000);
foreach ($lists['hits']['hits']??[] as $list){
$list = $list['_source'];
$id = intval($list['uuid']??0);
if($id && isAiAutoMail($list['from']['email'],$list['subject'])){
_echo("id = ".$id);
redis()->rPush('sync_to_es',$id);
],0,1000,['uuid'=>'asc'],1000);
if(empty($lists['hits']['hits'])){
return $total;
}
foreach ($lists['hits']['hits']??[] as $list){
$total++;
$id = $list['_source']['uuid'];
redis()->rPush('sync_to_es',$list['_source']['uuid'].'.1');
}
}
}
}
... ...
... ... @@ -1126,6 +1126,25 @@ class Home extends Base {
}
/**
* 标记一下 把邮件重新同步
* @throws \Lib\Err
* @author:dc
* @time 2025/9/18 16:50
*/
public function resync_to_es(){
if(redis()->add('resync_to_es_inbox',1,600)){
app()->e('请求成功,请等待5-15分钟');
}else{
app()->e('失败,请等待10分钟后在重试');
}
}
}
... ...
... ... @@ -53,6 +53,8 @@ return [
// v2 版本的
'v2/sync' => [\Controller\v2\Home::class, 'sync'],
'v3/sync' => [\Controller\v3\Home::class, 'sync'],
// 重新同步收件箱到es库
'resync_to_es' => [\Controller\Home::class, 'resync_to_es'],
// 标记为已读
'seen_2_unseen' => [\Controller\Home::class, 'seen_2_unseen'],
// 标记为已回复/未回复
... ...