]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
switch SNES default port
[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 ($this->isKnownBot()) {
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 function isKnownBot() {
57                 return in_array(strtolower($this->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         protected function scanForSpam() {
138                 if (substr($this->text_content, 0, 1) == '!') {
139                         return true;
140                 }
141                 if (strpos($this->text_content, '$') !== false) {
142                         return true;
143                 }
144                 if (strpos($this->text_content, '€') !== false) {
145                         return true;
146                 }
147                 if (strpos($this->text_content, '@') !== false) {
148                         return true;
149                 }
150                 if (strpos($this->text_content, '://') !== false) {
151                         return true;
152                 }
153                 if (is_numeric($this->text_content)) {
154                         return true;
155                 }
156                 if (strpos($this->text_content, 'followers') !== false) {
157                         return true;
158                 }
159                 if (strpos($this->text_content, 'promotion') !== false) {
160                         return true;
161                 }
162                 if (strpos($this->text_content, 'viewers') !== false) {
163                         return true;
164                 }
165                 if (strpos($this->text_content, 'view ers') !== false) {
166                         return true;
167                 }
168                 return false;
169         }
170
171         protected $casts = [
172                 'banned' => 'boolean',
173                 'params' => 'array',
174                 'tags' => 'array',
175                 'user_id' => 'string',
176         ];
177
178         protected $fillable = [
179                 'command',
180                 'nick',
181                 'params',
182                 'tags',
183         ];
184
185 }