]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
792e9e7a9b59c3aa212edf5b59786c7dfec6b6ae
[alttp.git] / app / Models / ChatLog.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Arr;
8 use Illuminate\Support\Str;
9 use LanguageDetector\LanguageDetector;
10
11 class ChatLog extends Model {
12
13         use HasFactory;
14
15         public function channel() {
16                 return $this->belongsTo(Channel::class);
17         }
18
19         public function user() {
20                 return $this->belongsTo(User::class);
21         }
22
23         public function evaluate() {
24                 $this->evaluateUser();
25                 $this->evaluateChannel();
26
27                 if (is_null($this->nick)) {
28                         $this->type = 'system';
29                         return;
30                 }
31                 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
32                         $this->type = 'self';
33                         return;
34                 }
35
36                 if ($this->command == 'PRIVMSG') {
37                         if (static::isKnownBot($this->nick)) {
38                                 $this->type = 'bot';
39                         } else if (substr($this->params[0], 0, 1) == '#') {
40                                 $this->type = 'chat';
41                         } else {
42                                 $this->type = 'dm';
43                         }
44                         $this->text_content = $this->params[1];
45                         $this->detectLanguage();
46                         if ($this->scanForSpam()) {
47                                 $this->banned = true;
48                         }
49                         $this->classification = static::classify($this->text_content);
50                         return;
51                 }
52
53                 throw new \Exception('unidentified message');
54         }
55
56         public static function isKnownBot($nick) {
57                 return in_array(strtolower($nick), [
58                         'birrellthesquirrel',
59                         'funtoon',
60                         'nidbot2000',
61                         'nightbot',
62                         'pokemoncommunitygame',
63                         'speedgaming',
64                         'starbase47',
65                         'streamelements',
66                         'wizebot',
67                         'zockerstuebchen',
68                 ]);
69         }
70
71         public static function classify($text) {
72                 if (empty($text)) {
73                         return 'unclassified';
74                 }
75                 if (is_numeric(trim($text))) {
76                         return 'number';
77                 }
78                 $rawText = strtolower(preg_replace('/[^\w]/', '', $text));
79                 $tokenizedText = preg_split('/\s+/', strtolower(trim($text)));
80                 if (Str::startsWith($rawText, 'gg') || Str::endsWith($rawText, 'gg')) {
81                         return 'gg';
82                 }
83                 if (Str::contains($rawText, ['glgl', 'glhf', 'hfgl'])) {
84                         return 'gl';
85                 }
86                 if (Str::contains($rawText, ['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul', 'xd'])) {
87                         return 'lol';
88                 }
89                 if (Str::startsWith($rawText, ['ahoi', 'hallo', 'hello', 'hi ', 'huhu']) || Str::endsWith($rawText, ['hi', 'wave'])) {
90                         return 'hi';
91                 }
92                 if (Str::contains($rawText, ['pog', 'wow'])) {
93                         return 'pog';
94                 }
95                 if (Str::contains($rawText, ['hype'])) {
96                         return 'hype';
97                 }
98                 if (Str::startsWith($rawText, 'o7') || Str::endsWith($rawText, 'o7') || Str::contains($rawText, 'salut')) {
99                         return 'o7';
100                 }
101                 return 'unclassified';
102         }
103
104         protected function evaluateUser() {
105         }
106
107         protected function evaluateChannel() {
108                 if (empty($this->params)) {
109                         $this->channel()->associate(null);
110                         return;
111                 }
112                 $cname = $this->params[0];
113                 if (substr($cname, 0, 1) != '#') {
114                         $cname = '#'.$cname;
115                 }
116                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
117                 $this->channel()->associate($channel);
118         }
119
120         protected function detectLanguage() {
121                 $languages = ['de', 'en', 'es', 'fr'];
122                 if (!is_null($this->channel)) {
123                         $languages = array_values($this->channel->languages);
124                         if (!in_array('en', $languages)) {
125                                 $languages[] = 'en';
126                         }
127                 }
128                 $detector = LanguageDetector::detect($this->text_content, $languages);
129                 $scores = $detector->getScores();
130                 $lang = strval($detector->getLanguage());
131                 //var_dump($scores, $lang, $this->text_content);
132                 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
133                         $this->detected_language = $lang;
134                 }
135         }
136
137         public static function spammyText($raw_text) {
138                 $text = strtolower($raw_text);
139                 if (substr($text, 0, 1) == '!') {
140                         return true;
141                 }
142                 if (strpos($text, '$') !== false) {
143                         return true;
144                 }
145                 if (strpos($text, '€') !== false) {
146                         return true;
147                 }
148                 if (strpos($text, '@') !== false) {
149                         return true;
150                 }
151                 if (strpos($text, '://') !== false) {
152                         return true;
153                 }
154                 if (strpos($text, 'followers') !== false) {
155                         return true;
156                 }
157                 if (strpos($text, 'horstie') !== false) {
158                         return true;
159                 }
160                 if (strpos($text, 'promotion') !== false) {
161                         return true;
162                 }
163                 if (strpos($text, 'viewers') !== false) {
164                         return true;
165                 }
166                 if (strpos($text, 'view ers') !== false) {
167                         return true;
168                 }
169                 if (strpos($text, 'vielen dank für den raid') !== false) {
170                         return true;
171                 }
172                 if (strpos($text, 'willkommen auf starbase 47') !== false) {
173                         return true;
174                 }
175                 return false;
176         }
177
178         protected function scanForSpam() {
179                 if (is_numeric($this->text_content)) {
180                         return true;
181                 }
182                 return static::spammyText($this->text_content);
183         }
184
185         protected $casts = [
186                 'banned' => 'boolean',
187                 'params' => 'array',
188                 'tags' => 'array',
189                 'user_id' => 'string',
190         ];
191
192         protected $fillable = [
193                 'command',
194                 'nick',
195                 'params',
196                 'tags',
197         ];
198
199 }