+ public function randomOfClass($class) {
+ if (is_array($class)) {
+ return $this->queryChatlog()
+ ->whereIn('classification', $class)
+ ->first();
+ }
+ return $this->queryChatlog()
+ ->where('classification', '=', $class)
+ ->first();
+ }
+
+ public function queryChatlog() {
+ $min_age = $this->getChatSetting('min_age', 1);
+ $query = ChatLog::where('type', '=', 'chat')
+ ->where('banned', '=', false)
+ ->where('created_at', '<', now()->sub($min_age, 'day'))
+ ->where(function ($query) {
+ $query->whereNull('detected_language');
+ $query->orWhereIn('detected_language', $this->getPreferredLanguages());
+ })
+ ->inRandomOrder();
+ $source = $this->getChatSetting('source', 'any');
+ if (in_array($source, ['catchan', 'chan'])) {
+ $query->where('channel_id', $this->id);
+ }
+ if (in_array($source, ['cat', 'catchan'])) {
+ $query->where('twitch_category', $this->twitch_category);
+ }
+ return $query;
+ }
+
+ public function getPreferredLanguages() {
+ $setting = $this->getChatSetting('language');
+ if ($setting) {
+ return [$setting];
+ }
+ if (!empty($this->languages)) {
+ return $this->languages;
+ }
+ return ['de'];
+ }
+
+ public function getChatSetting($name, $default = null) {
+ if (array_key_exists($name, $this->chat_settings)) {
+ return $this->chat_settings[$name];
+ }
+ return $default;
+ }
+