pixelfed/app/Util/Sentiment/Bouncer.php

167 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2020-12-11 04:58:56 +00:00
<?php
namespace App\Util\Sentiment;
use App\AccountInterstitial;
use App\Status;
use Cache;
use Illuminate\Support\Str;
use App\Services\NotificationService;
use App\Services\StatusService;
2023-03-13 11:55:56 +00:00
use App\Jobs\ReportPipeline\AutospamNotifyAdminViaEmail;
use App\Notification;
2023-05-17 10:10:07 +00:00
use App\Services\AutospamService;
2020-12-11 04:58:56 +00:00
class Bouncer {
public static function get(Status $status)
{
if($status->uri || $status->scope != 'public') {
return;
}
2021-03-01 09:20:19 +00:00
if($status->profile->user->is_admin == true) {
return;
}
2021-01-24 06:12:05 +00:00
$exemptionKey = 'pf:bouncer_v0:exemption_by_pid:' . $status->profile_id;
$exemptionTtl = now()->addDays(12);
if( $status->in_reply_to_id != null &&
$status->in_reply_to_profile_id == $status->profile_id
) {
return;
}
2021-01-24 06:12:05 +00:00
$exemption = Cache::remember($exemptionKey, $exemptionTtl, function() use($status) {
$uid = $status->profile->user_id;
$ids = AccountInterstitial::whereUserId($uid)
->whereType('post.autospam')
->whereItemType('App\Status')
->whereNotNull('appeal_handled_at')
->latest()
->take(5)
->pluck('item_id');
if($ids->count() == 0) {
return false;
}
$count = Status::select('id', 'scope')
->whereScope('public')
->find($ids)
->count();
return $count >= 1 ? true : false;
2020-12-11 04:58:56 +00:00
});
2021-01-24 06:12:05 +00:00
if($exemption == true) {
return;
}
2021-03-01 09:20:19 +00:00
if( $status->profile->created_at->gt(now()->subMonths(6)) &&
$status->profile->bio &&
$status->profile->website
) {
return (new self)->handle($status);
}
2021-01-24 06:12:05 +00:00
$recentKey = 'pf:bouncer_v0:recent_by_pid:' . $status->profile_id;
$recentTtl = now()->addHours(28);
$recent = Cache::remember($recentKey, $recentTtl, function() use($status) {
return $status
->profile
->created_at
->gt(now()->subMonths(6)) ||
$status
->profile
->statuses()
->whereScope('public')
->count() == 0;
});
2020-12-11 04:58:56 +00:00
if(!$recent) {
return;
}
if($status->profile->followers()->count() > 100) {
return;
}
if(AutospamService::active()) {
if(AutospamService::check($status->caption)) {
return (new self)->handle($status);
}
}
2021-01-24 06:12:05 +00:00
if(!Str::contains($status->caption, [
'https://',
'http://',
'hxxps://',
'hxxp://',
'www.',
'.com',
'.net',
'.org'
])) {
2020-12-11 04:58:56 +00:00
return;
}
return (new self)->handle($status);
}
protected function handle($status)
{
$media = $status->media;
$ai = new AccountInterstitial;
$ai->user_id = $status->profile->user_id;
$ai->type = 'post.autospam';
$ai->view = 'account.moderation.post.autospam';
$ai->item_type = 'App\Status';
$ai->item_id = $status->id;
$ai->has_media = (bool) $media->count();
$ai->blurhash = $media->count() ? $media->first()->blurhash : null;
$ai->meta = json_encode([
'caption' => $status->caption,
'created_at' => $status->created_at,
'type' => $status->type,
'url' => $status->url(),
'is_nsfw' => $status->is_nsfw,
'scope' => $status->scope,
'reblog' => $status->reblog_of_id,
'likes_count' => $status->likes_count,
'reblogs_count' => $status->reblogs_count,
]);
$ai->save();
2023-03-13 11:55:56 +00:00
if(config('instance.reports.email.enabled') && config('instance.reports.email.autospam')) {
AutospamNotifyAdminViaEmail::dispatch($ai);
}
2020-12-11 04:58:56 +00:00
$u = $status->profile->user;
$u->has_interstitial = true;
$u->save();
$status->scope = 'unlisted';
$status->visibility = 'unlisted';
// $status->is_nsfw = true;
2020-12-11 04:58:56 +00:00
$status->save();
$notification = new Notification();
$notification->profile_id = $status->profile_id;
$notification->actor_id = $status->profile_id;
$notification->action = 'autospam.warning';
$notification->item_id = $status->id;
$notification->item_type = "App\Status";
$notification->save();
StatusService::del($status->id);
2021-01-24 06:12:05 +00:00
Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $status->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:' . $status->profile_id);
2020-12-11 04:58:56 +00:00
}
}