From e8eb106aa5adab6dd992390cb3836589e4163e72 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 19 Jan 2024 13:53:33 +0100 Subject: [PATCH] chat log evaluation --- app/Console/Commands/EvaluateChatCommand.php | 51 +++++++++ app/Console/Kernel.php | 1 + app/Models/ChatLog.php | 102 ++++++++++++++++++ .../2024_01_19_090216_chat_evaluation.php | 45 ++++++++ 4 files changed, 199 insertions(+) create mode 100644 app/Console/Commands/EvaluateChatCommand.php create mode 100644 database/migrations/2024_01_19_090216_chat_evaluation.php diff --git a/app/Console/Commands/EvaluateChatCommand.php b/app/Console/Commands/EvaluateChatCommand.php new file mode 100644 index 0000000..8c5cd24 --- /dev/null +++ b/app/Console/Commands/EvaluateChatCommand.php @@ -0,0 +1,51 @@ +argument('amount'); + + $logs = ChatLog::whereNull('evaluated_at')->orderBy('created_at')->limit($amount)->get(); + + foreach ($logs as $line) { + try { + $line->evaluate(); + $line->evaluated_at = now(); + $line->save(); + } catch (\Exception $e) { + $this->error('unable to evaluate line '.$line->id.': '.$e->getMessage()); + $line->type = 'error'; + $line->text_content = $e->getMessage(); + $line->evaluated_at = now(); + $line->save(); + } + } + + return Command::SUCCESS; + } + +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index c3d9aff..4f8719f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -19,6 +19,7 @@ class Kernel extends ConsoleKernel $schedule->command('sync:sra')->everyFifteenMinutes(); $schedule->command('sync:avatars')->everyFiveMinutes(); $schedule->command('sync:racetime')->everyFiveMinutes(); + $schedule->command('chat:evaluate 100')->everyMinute(); } /** diff --git a/app/Models/ChatLog.php b/app/Models/ChatLog.php index 9e50add..aa4e7ae 100644 --- a/app/Models/ChatLog.php +++ b/app/Models/ChatLog.php @@ -9,9 +9,111 @@ class ChatLog extends Model { use HasFactory; + public function channel() { + return $this->belongsTo(Channel::class); + } + + public function user() { + return $this->belongsTo(User::class); + } + + public function evaluate() { + $this->evaluateUser(); + $this->evaluateChannel(); + + if (is_null($this->nick)) { + $this->type = 'system'; + return; + } + if ($this->nick == 'localhorsttv') { + $this->type = 'self'; + return; + } + + if ($this->command == 'PRIVMSG') { + if ($this->isKnownBot()) { + $this->type = 'bot'; + } else if (substr($this->params[0], 0, 1) == '#') { + $this->type = 'chat'; + } else { + $this->type = 'dm'; + } + $this->text_content = $this->params[1]; + if ($this->scanForSpam()) { + $this->banned = true; + } + return; + } + + throw new \Exception('unidentified message'); + } + + public function isKnownBot() { + return in_array(strtolower($this->nick), [ + 'funtoon', + 'nightbot', + 'pokemoncommunitygame', + 'streamelements', + 'wizebot', + 'zockerstuebchen', + ]); + } + + protected function evaluateUser() { + } + + protected function evaluateChannel() { + if (empty($this->params)) { + $this->channel()->associate(null); + return; + } + $cname = $this->params[0]; + if (substr($cname, 0, 1) != '#') { + $cname = '#'.$cname; + } + $channel = Channel::firstWhere('twitch_chat', '=', $cname); + $this->channel()->associate($channel); + } + + protected function scanForSpam() { + if (substr($this->text_content, 0, 1) == '!') { + return true; + } + if (strpos($this->text_content, '$') !== false) { + return true; + } + if (strpos($this->text_content, '€') !== false) { + return true; + } + if (strpos($this->text_content, '@') !== false) { + return true; + } + if (strpos($this->text_content, '://') !== false) { + return true; + } + if (is_numeric($this->text_content)) { + return true; + } + if (strpos($this->text_content, 'followers') !== false) { + return true; + } + if (strpos($this->text_content, 'promotion') !== false) { + return true; + } + if (strpos($this->text_content, 'viewers') !== false) { + return true; + } + if (strpos($this->text_content, 'view ers') !== false) { + return true; + } + return false; + } + protected $casts = [ + 'banned' => 'boolean', 'params' => 'array', 'tags' => 'array', + 'user_id' => 'string', ]; protected $fillable = [ diff --git a/database/migrations/2024_01_19_090216_chat_evaluation.php b/database/migrations/2024_01_19_090216_chat_evaluation.php new file mode 100644 index 0000000..840950c --- /dev/null +++ b/database/migrations/2024_01_19_090216_chat_evaluation.php @@ -0,0 +1,45 @@ +timestamp('evaluated_at')->nullable()->default(null); + $table->foreignId('user_id')->nullable()->default(null)->constrained(); + $table->foreignId('channel_id')->nullable()->default(null)->constrained(); + $table->string('type')->default(''); + $table->text('text_content')->nullable()->default(null); + $table->boolean('banned')->default(false); + $table->index(['type', 'banned']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('chat_logs', function (Blueprint $table) { + $table->dropColumn('evaluated_at'); + $table->dropForeign(['user_id']); + $table->dropColumn('user_id'); + $table->dropForeign(['channel_id']); + $table->dropColumn('channel_id'); + $table->dropColumn('type'); + $table->dropColumn('text_content'); + $table->dropColumn('banned'); + }); + } +}; -- 2.39.2