]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
try to guess chat language
[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 LanguageDetector\LanguageDetector;
8
9 class ChatLog extends Model {
10
11         use HasFactory;
12
13         public function channel() {
14                 return $this->belongsTo(Channel::class);
15         }
16
17         public function user() {
18                 return $this->belongsTo(User::class);
19         }
20
21         public function evaluate() {
22                 $this->evaluateUser();
23                 $this->evaluateChannel();
24
25                 if (is_null($this->nick)) {
26                         $this->type = 'system';
27                         return;
28                 }
29                 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
30                         $this->type = 'self';
31                         return;
32                 }
33
34                 if ($this->command == 'PRIVMSG') {
35                         if ($this->isKnownBot()) {
36                                 $this->type = 'bot';
37                         } else if (substr($this->params[0], 0, 1) == '#') {
38                                 $this->type = 'chat';
39                         } else {
40                                 $this->type = 'dm';
41                         }
42                         $this->text_content = $this->params[1];
43                         $this->detectLanguage();
44                         if ($this->scanForSpam()) {
45                                 $this->banned = true;
46                         }
47                         return;
48                 }
49
50                 throw new \Exception('unidentified message');
51         }
52
53         public function isKnownBot() {
54                 return in_array(strtolower($this->nick), [
55                         'funtoon',
56                         'nightbot',
57                         'pokemoncommunitygame',
58                         'streamelements',
59                         'wizebot',
60                         'zockerstuebchen',
61                 ]);
62         }
63
64         protected function evaluateUser() {
65         }
66
67         protected function evaluateChannel() {
68                 if (empty($this->params)) {
69                         $this->channel()->associate(null);
70                         return;
71                 }
72                 $cname = $this->params[0];
73                 if (substr($cname, 0, 1) != '#') {
74                         $cname = '#'.$cname;
75                 }
76                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
77                 $this->channel()->associate($channel);
78         }
79
80         protected function detectLanguage() {
81                 $languages = ['de', 'en', 'es', 'fr'];
82                 if (!is_null($this->channel)) {
83                         $languages = array_values($this->channel->languages);
84                         if (!in_array('en', $languages)) {
85                                 $languages[] = 'en';
86                         }
87                 }
88                 $detector = LanguageDetector::detect($this->text_content, $languages);
89                 $scores = $detector->getScores();
90                 $lang = strval($detector->getLanguage());
91                 //var_dump($scores, $lang, $this->text_content);
92                 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
93                         $this->detected_language = $lang;
94                 }
95         }
96
97         protected function scanForSpam() {
98                 if (substr($this->text_content, 0, 1) == '!') {
99                         return true;
100                 }
101                 if (strpos($this->text_content, '$') !== false) {
102                         return true;
103                 }
104                 if (strpos($this->text_content, '€') !== false) {
105                         return true;
106                 }
107                 if (strpos($this->text_content, '@') !== false) {
108                         return true;
109                 }
110                 if (strpos($this->text_content, '://') !== false) {
111                         return true;
112                 }
113                 if (is_numeric($this->text_content)) {
114                         return true;
115                 }
116                 if (strpos($this->text_content, 'followers') !== false) {
117                         return true;
118                 }
119                 if (strpos($this->text_content, 'promotion') !== false) {
120                         return true;
121                 }
122                 if (strpos($this->text_content, 'viewers') !== false) {
123                         return true;
124                 }
125                 if (strpos($this->text_content, 'view ers') !== false) {
126                         return true;
127                 }
128                 return false;
129         }
130
131         protected $casts = [
132                 'banned' => 'boolean',
133                 'params' => 'array',
134                 'tags' => 'array',
135                 'user_id' => 'string',
136         ];
137
138         protected $fillable = [
139                 'command',
140                 'nick',
141                 'params',
142                 'tags',
143         ];
144
145 }