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