mastodon-admintools/job-auto-suspend-account.php

60 lines
2 KiB
PHP

<?php
use Vazaha\Mastodon\Factories\ApiClientFactory;
require_once("bootstrap.php");
$factory = new ApiClientFactory();
$client = $factory->build()
->setBaseUri('https://'.$conf['instance']);
$client->setAccessToken($conf['token']);
$badidfile=__DIR__."/cache/latest-auto-suspend-id";
//$pending = $client->methods()->admin()->accounts()->v2('local','pending');
// get 1000 recent statuses from the latest we had and search for bad message patterns :
if (is_file($badidfile))
$latest=intval(file_get_contents($badidfile));
else
$latest=0;
echo "starting at $latest\n";
$suspend_stmt = $db->prepare('SELECT username,domain, suspended_at from accounts WHERE id=?');
$sql= 'select id, account_id, text from statuses ';
if ($latest) $sql.=' WHERE id> '.$latest.' ';
$sql.=' order by id desc limit 10000;';
$stmt = $db->prepare($sql);
$stmt->execute();
$first=true;
while ($status = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($first) $latest=$status['id'];
if (preg_match('#/share.google#',$status['text'])) {
preg_match_all('#mention">@<span>([^<]*)</span>#',$status['text'],$mat);
if (isset($mat[1]) && count($mat[1])>10) {
// more than 10 mentions + /share.google = suspend this account.
$suspend_stmt->execute([$status['account_id']]);
$badboy = $suspend_stmt->fetch(PDO::FETCH_ASSOC);
if (!$badboy) {
echo "WEIRD: found account ".$status['account_id']." in the statuses but not in the DB !\n";
continue;
}
if ($badboy['suspended_at']) {
echo "found account ".$badboy['username']."@".$badboy['domain']." but already suspended, skip\n";
} else {
echo "found account ".$badboy['username']."@".$badboy['domain']." will suspend it\n";
$client->methods()->admin()->accounts()->action( id: $status['account_id'], type: 'suspend', text: 'Automatic suspention from the Admin Bot Case #1');
}
}
}
$first=false;
}
file_put_contents($badidfile,$latest);