mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-09 16:24:51 +00:00
Update autospam config, use config_cache
This commit is contained in:
parent
da0e0ffabf
commit
a76cb5f4f8
3 changed files with 65 additions and 60 deletions
|
@ -183,7 +183,7 @@ class StatusEntityLexer implements ShouldQueue
|
||||||
'photo:video:album',
|
'photo:video:album',
|
||||||
];
|
];
|
||||||
|
|
||||||
if (config_cache('pixelfed.bouncer.enabled')) {
|
if ((bool) config_cache('pixelfed.bouncer.enabled')) {
|
||||||
Bouncer::get($status);
|
Bouncer::get($status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,77 +2,82 @@
|
||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Util\Lexer\Classifier;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use App\Util\Lexer\Classifier;
|
|
||||||
|
|
||||||
class AutospamService
|
class AutospamService
|
||||||
{
|
{
|
||||||
const CHCKD_CACHE_KEY = 'pf:services:autospam:nlp:checked';
|
const CHCKD_CACHE_KEY = 'pf:services:autospam:nlp:checked';
|
||||||
const MODEL_CACHE_KEY = 'pf:services:autospam:nlp:model-cache';
|
|
||||||
const MODEL_FILE_PATH = 'nlp/active-training-data.json';
|
|
||||||
const MODEL_SPAM_PATH = 'nlp/spam.json';
|
|
||||||
const MODEL_HAM_PATH = 'nlp/ham.json';
|
|
||||||
|
|
||||||
public static function check($text)
|
const MODEL_CACHE_KEY = 'pf:services:autospam:nlp:model-cache';
|
||||||
{
|
|
||||||
if(!$text || strlen($text) == 0) {
|
|
||||||
false;
|
|
||||||
}
|
|
||||||
if(!self::active()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$model = self::getCachedModel();
|
|
||||||
$classifier = new Classifier;
|
|
||||||
$classifier->import($model['documents'], $model['words']);
|
|
||||||
return $classifier->most($text) === 'spam';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function eligible()
|
const MODEL_FILE_PATH = 'nlp/active-training-data.json';
|
||||||
{
|
|
||||||
return Cache::remember(self::CHCKD_CACHE_KEY, 86400, function() {
|
|
||||||
if(!config_cache('pixelfed.bouncer.enabled') || !config('autospam.enabled')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!Storage::exists(self::MODEL_SPAM_PATH)) {
|
const MODEL_SPAM_PATH = 'nlp/spam.json';
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!Storage::exists(self::MODEL_HAM_PATH)) {
|
const MODEL_HAM_PATH = 'nlp/ham.json';
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!Storage::exists(self::MODEL_FILE_PATH)) {
|
public static function check($text)
|
||||||
return false;
|
{
|
||||||
} else {
|
if (! $text || strlen($text) == 0) {
|
||||||
if(Storage::size(self::MODEL_FILE_PATH) < 1000) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
}
|
||||||
});
|
if (! self::active()) {
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
|
$model = self::getCachedModel();
|
||||||
|
$classifier = new Classifier;
|
||||||
|
$classifier->import($model['documents'], $model['words']);
|
||||||
|
|
||||||
public static function active()
|
return $classifier->most($text) === 'spam';
|
||||||
{
|
}
|
||||||
return config_cache('autospam.nlp.enabled') && self::eligible();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getCachedModel()
|
public static function eligible()
|
||||||
{
|
{
|
||||||
if(!self::active()) {
|
return Cache::remember(self::CHCKD_CACHE_KEY, 86400, function () {
|
||||||
return null;
|
if (! (bool) config_cache('pixelfed.bouncer.enabled') || ! (bool) config_cache('autospam.enabled')) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return Cache::remember(self::MODEL_CACHE_KEY, 86400, function() {
|
if (! Storage::exists(self::MODEL_SPAM_PATH)) {
|
||||||
$res = Storage::get(self::MODEL_FILE_PATH);
|
return false;
|
||||||
if(!$res || empty($res)) {
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return json_decode($res, true);
|
if (! Storage::exists(self::MODEL_HAM_PATH)) {
|
||||||
});
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! Storage::exists(self::MODEL_FILE_PATH)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
if (Storage::size(self::MODEL_FILE_PATH) < 1000) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function active()
|
||||||
|
{
|
||||||
|
return config_cache('autospam.nlp.enabled') && self::eligible();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCachedModel()
|
||||||
|
{
|
||||||
|
if (! self::active()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Cache::remember(self::MODEL_CACHE_KEY, 86400, function () {
|
||||||
|
$res = Storage::get(self::MODEL_FILE_PATH);
|
||||||
|
if (! $res || empty($res)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_decode($res, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -815,7 +815,7 @@
|
||||||
<tr>
|
<tr>
|
||||||
<td><span class="badge badge-primary">PIXELFED</span></td>
|
<td><span class="badge badge-primary">PIXELFED</span></td>
|
||||||
<td><strong>PF_BOUNCER_ENABLED</strong></td>
|
<td><strong>PF_BOUNCER_ENABLED</strong></td>
|
||||||
<td><span>{{config_cache('pixelfed.bouncer.enabled') ? '✅ true' : '❌ false' }}</span></td>
|
<td><span>{{(bool) config_cache('pixelfed.bouncer.enabled') ? '✅ true' : '❌ false' }}</span></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><span class="badge badge-primary">PIXELFED</span></td>
|
<td><span class="badge badge-primary">PIXELFED</span></td>
|
||||||
|
|
Loading…
Reference in a new issue