]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
respond to whispers
[alttp.git] / app / Models / ChatLog.php
1 <?php
2
3 namespace App\Models;
4
5 use App\TwitchBot\TokenizedMessage;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8 use Illuminate\Support\Arr;
9 use Illuminate\Support\Str;
10 use LanguageDetection\Language;
11
12 class ChatLog extends Model {
13
14         use HasFactory;
15
16         public function channel() {
17                 return $this->belongsTo(Channel::class);
18         }
19
20         public function user() {
21                 return $this->belongsTo(User::class);
22         }
23
24         public function tokenize() {
25                 return TokenizedMessage::fromLog($this);
26         }
27
28         public function isReply() {
29                 return !empty($this->tags['reply-parent-msg-body']);
30         }
31
32         public function getReplyParent() {
33                 return str_replace('\\s', ' ', $this->tags['reply-parent-msg-body']);
34         }
35
36         public function getReplyParentUser() {
37                 return $this->tags['reply-parent-display-name'];
38         }
39
40         public function getText() {
41                 return $this->params[1];
42         }
43
44         public function getTextWithoutEmotes() {
45                 $text = $this->params[1];
46                 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
47                         $emotes = explode('/', $this->tags['emotes']);
48                         foreach ($emotes as $emote) {
49                                 $set = explode(':', $emote);
50                                 $positions = explode(',', $set[1]);
51                                 foreach ($positions as $position) {
52                                         $coords = explode('-', $position);
53                                         $text = mb_substr($text, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($text, $coords[1] + 1);
54                                 }
55                         }
56                 }
57                 return trim(preg_replace('/\s+/', ' ', $text));
58         }
59
60         public function getTextWithoutReply() {
61                 if ($this->isReply()) {
62                         return mb_substr($this->params[1], mb_strlen($this->getReplyParentUser()) + 2);
63                 }
64                 return $this->params[1];
65         }
66
67         public function evaluate() {
68                 $this->evaluateUser();
69                 $this->evaluateChannel();
70
71                 if (is_null($this->nick)) {
72                         $this->type = 'system';
73                         return;
74                 }
75                 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
76                         $this->type = 'self';
77                         return;
78                 }
79
80                 if ($this->command == 'PRIVMSG' || $this->command == 'WHISPER') {
81                         if (static::isKnownBot($this->nick)) {
82                                 $this->type = 'bot';
83                         } else if (substr($this->params[0], 0, 1) == '#') {
84                                 $this->type = 'chat';
85                         } else {
86                                 $this->type = 'dm';
87                         }
88                         $this->text_content = $this->getTextWithoutReply();
89                         $this->detectLanguage();
90                         $tokenized = $this->tokenize();
91                         if ($tokenized->isSpammy()) {
92                                 $this->banned = true;
93                         }
94                         $this->classification = $tokenized->classify();
95                         return;
96                 }
97
98                 throw new \Exception('unidentified message');
99         }
100
101         public static function isKnownBot($nick) {
102                 return in_array(strtolower($nick), [
103                         'a_n_i_v',
104                         'birrellthesquirrel',
105                         'funtoon',
106                         'nidbot2000',
107                         'nightbot',
108                         'pokemoncommunitygame',
109                         'sery_bot',
110                         'speedgaming',
111                         'starbase47',
112                         'streamelements',
113                         'wizebot',
114                         'zockerstuebchen',
115                 ]);
116         }
117
118         protected function evaluateUser() {
119         }
120
121         protected function evaluateChannel() {
122                 if (empty($this->params)) {
123                         return;
124                 }
125                 $cname = $this->params[0];
126                 if (substr($cname, 0, 1) != '#') {
127                         $cname = '#'.$cname;
128                 }
129                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
130                 if (!is_null($channel)) {
131                         $this->channel()->associate($channel);
132                         if (empty($this->twitch_category) && now()->sub(15, 'minute')->isBefore($this->created_at)) {
133                                 $this->twitch_category = $channel->twitch_category;
134                         }
135                 }
136         }
137
138         protected function detectLanguage() {
139                 $languages = ['de', 'en', 'es', 'fr'];
140                 if (!is_null($this->channel)) {
141                         $languages = array_values($this->channel->languages);
142                         if (!in_array('en', $languages)) {
143                                 $languages[] = 'en';
144                         }
145                 }
146                 $detector = (new Language($languages))->detect($this->getTextWithoutEmotes());
147                 $scores = $detector->close();
148                 $lang = strval($detector);
149                 //var_dump($scores, $lang, $this->text_content);
150                 if (!empty($lang) && $scores[$lang] > 0.4) {
151                         $this->detected_language = $lang;
152                 }
153         }
154
155         protected $casts = [
156                 'banned' => 'boolean',
157                 'params' => 'array',
158                 'tags' => 'array',
159                 'user_id' => 'string',
160         ];
161
162         protected $fillable = [
163                 'command',
164                 'nick',
165                 'params',
166                 'tags',
167         ];
168
169 }