]> git.localhorst.tv Git - alttp.git/commitdiff
log who sent manual random chats
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 14 Apr 2024 12:12:31 +0000 (14:12 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 14 Apr 2024 12:12:31 +0000 (14:12 +0200)
app/Http/Controllers/ChannelController.php
app/Models/ChatBotLog.php
app/TwitchBot/TwitchChatBot.php
app/TwitchBotCommands/BaseCommand.php
app/TwitchBotCommands/RandomChatCommand.php
database/migrations/2024_04_14_112456_better_chat_bot_log.php [new file with mode: 0644]
resources/js/components/chat-bot-logs/Item.js
resources/js/helpers/User.js
resources/js/i18n/de.js
resources/js/i18n/en.js
tests/js/helpers/User.test.js [new file with mode: 0644]

index 15213b412f675481ca86d854a4ecbf1579f0dfc6..f9cf0d27f50aa3e020fb413aa5428aad2fc8a3e8 100644 (file)
@@ -71,7 +71,7 @@ class ChannelController extends Controller {
        public function chatBotLog(Request $request, Channel $channel) {
                $this->authorize('editRestream', $channel);
                $log = $channel->chat_bot_logs()
-                       ->with('origin')
+                       ->with(['origin', 'user'])
                        ->orderBy('created_at', 'DESC')
                        ->limit(150)
                        ->get();
index 34d2febd42bd0d5a984b27a72537bc7352ff5b04..a4f1f934b52abea3e0f5422ee47524d9a2fc088b 100644 (file)
@@ -17,4 +17,8 @@ class ChatBotLog extends Model {
                return $this->morphTo();
        }
 
+       public function user() {
+               return $this->belongsTo(User::class);
+       }
+
 }
index 442173d8c60c82228d66ffdd52e2b1ff5744ef07..92894262f3b108d42826f76238dca6c909146a94 100644 (file)
@@ -82,6 +82,9 @@ class TwitchChatBot extends TwitchBot {
                $log->channel()->associate($channel);
                if (is_object($text)) {
                        $log->origin()->associate($text);
+                       $log->category = $text->classification;
+               } else {
+                       $log->category = $this->getLastSpecialSent($channel);
                }
                $log->text = $actual_text;
                $log->save();
index d803fb739defe022977c6b4cda7c017797695507..e2df728d0eb76a59970dff23aca17ed0a13705e3 100644 (file)
@@ -44,6 +44,10 @@ abstract class BaseCommand {
                return User::findOrFail($this->getParameter('user'));
        }
 
+       protected function getExecutingUser() {
+               return $this->command->user;
+       }
+
        protected function hasParameter($name) {
                return array_key_exists($name, $this->command->parameters);
        }
index cc49c00e62a788757ae267732d8680d59e344bd7..f3ee928e00fb3f063eadf5b6fce47e3997268269 100644 (file)
@@ -19,13 +19,16 @@ class RandomChatCommand extends BaseCommand {
                return new Promise(function($resolve) {
                        $channel = Channel::findOrFail($this->getParameter('channel'));
                        $text = $channel->randomOfClass($this->getParameter('category'));
-                       $this->bot->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $text->text_content));
+                       $actual_text = is_object($text) ? $text->text_content : $text;
+                       $this->bot->sendIRCMessage(IRCMessage::privmsg($channel->twitch_chat, $actual_text));
                        $log = new ChatBotLog();
                        $log->channel()->associate($channel);
                        if (is_object($text)) {
                                $log->origin()->associate($text);
                        }
-                       $log->text = $text->text_content;
+                       $log->text = $actual_text;
+                       $log->user()->associate($this->getExecutingUser());
+                       $log->category = $this->getParameter('category');
                        $log->save();
                        $resolve();
                });
diff --git a/database/migrations/2024_04_14_112456_better_chat_bot_log.php b/database/migrations/2024_04_14_112456_better_chat_bot_log.php
new file mode 100644 (file)
index 0000000..6f0326f
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+               Schema::table('chat_bot_logs', function (Blueprint $table) {
+                       $table->foreignId('user_id')->nullable()->default(null)->constrained();
+                       $table->string('category')->default('');
+               });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+               Schema::table('chat_bot_logs', function (Blueprint $table) {
+                       $table->dropColumn('user_id');
+                       $table->dropColumn('category');
+               });
+    }
+};
index 8340e4ed9df6097ce3d31cb8e77ee7677901d400..f50d078f27bda27666020367dfc81d14c93528af 100644 (file)
@@ -4,12 +4,9 @@ import React from 'react';
 import { ListGroup } from 'react-bootstrap';
 import { useTranslation } from 'react-i18next';
 
-const getEntryDate = entry => {
-       const dateStr = moment(entry.created_at).fromNow();
-       return entry.user
-               ? `${entry.user.username} ${dateStr}`
-               : dateStr;
-};
+import { getUserName } from '../../helpers/User';
+
+const getEntryDate = entry => moment(entry.created_at).fromNow();
 
 const getEntryOrigin = (entry, t) => {
        return t('chatBotLog.origin.chatLog', {
@@ -19,6 +16,29 @@ const getEntryOrigin = (entry, t) => {
        });
 };
 
+const getEntryInfo = (entry, t) => {
+       if (entry.user && entry.category) {
+               return t('chatBotLog.info.userCat', {
+                       category: t(`twitchBot.chatCategories.${entry.category}`),
+                       date: getEntryDate(entry),
+                       user: getUserName(entry.user),
+               });
+       }
+       if (entry.category) {
+               return t('chatBotLog.info.cat', {
+                       category: t(`twitchBot.chatCategories.${entry.category}`),
+                       date: getEntryDate(entry),
+               });
+       }
+       if (entry.user) {
+               return t('chatBotLog.info.user', {
+                       date: getEntryDate(entry),
+                       user: getUserName(entry.user),
+               });
+       }
+       return getEntryDate(entry);
+};
+
 const Item = ({ entry }) => {
        const { t } = useTranslation();
 
@@ -38,7 +58,7 @@ const Item = ({ entry }) => {
                                className="text-muted"
                                title={moment(entry.created_at).format('LLLL')}
                        >
-                               {getEntryDate(entry)}
+                               {getEntryInfo(entry, t)}
                        </div>
                </div>
        </ListGroup.Item>;
index 0705bed2d35013f25bcd14154a2361a861d8f56f..b00c853542d6eb739ec6fec3cec8f0771473ae49 100644 (file)
@@ -1,3 +1,18 @@
+export const getUserName = user => (user &&
+       (user.nickname || user.discord_nickname || user.username)) || '';
+
+export const compareUsername = (a, b) => {
+       const a_name = getUserName(a);
+       const b_name = getUserName(b);
+       return a_name.localeCompare(b_name);
+};
+
+export const findResult = (user, round) => {
+       if (!user || !user.id) return null;
+       if (!round || !round.results || !round.results.length) return null;
+       return round.results.find(result => result.user_id == user.id);
+};
+
 export const compareFinished = round => (a, b) => {
        const a_result = findResult(a, round);
        const b_result = findResult(b, round);
@@ -34,18 +49,6 @@ export const compareResult = round => (a, b) => {
        return compareUsername(a, b);
 };
 
-export const compareUsername = (a, b) => {
-       const a_name = getUserName(a);
-       const b_name = getUserName(b);
-       return a_name.localeCompare(b_name);
-};
-
-export const findResult = (user, round) => {
-       if (!user || !user.id) return null;
-       if (!round || !round.results || !round.results.length) return null;
-       return round.results.find(result => result.user_id == user.id);
-};
-
 export const getAvatarUrl = user => {
        if (user && user.avatar) {
                if (user.avatar_cached) {
@@ -56,9 +59,6 @@ export const getAvatarUrl = user => {
        return '/default-avatar.png';
 };
 
-export const getUserName = user => (user &&
-       (user.nickname || user.discord_nickname || user.username)) || '';
-
 export const hasFinishedRound = (user, round) => {
        const result = findResult(user, round);
        return result && result.has_finished;
index 14f18fa30ba015370b9bb0112e1da23826331541..21275f05a4e887886c453be315e73288b8710f1b 100644 (file)
@@ -100,8 +100,13 @@ export default {
                chatBotLog: {
                        empty: 'Noch keine Nachrichten erfasst',
                        heading: 'Chat Bot Protokoll',
+                       info: {
+                               cat: '{{ category }} {{ date }}',
+                               user: '{{ user }} {{ date }}',
+                               userCat: '{{ category }} von {{ user }} {{ date }}',
+                       },
                        origin: {
-                               chatLog: '{{ nick }} in {{ channel }} am {{ date, L LT }}',
+                               chatLog: 'Quelle: {{ nick }} in {{ channel }} am {{ date, L LT }}',
                        },
                },
                content: {
index f6965043a84a355e9a380c04307284694e97577b..2645cc99b61da132164b4f94d263afd587ebecbc 100644 (file)
@@ -100,8 +100,13 @@ export default {
                chatBotLog: {
                        empty: 'No messages on protocol yet',
                        heading: 'Chat Bot Log',
+                       info: {
+                               cat: '{{ category }} {{ date }}',
+                               user: '{{ user }} {{ date }}',
+                               userCat: '{{ category }} from {{ user }} {{ date }}',
+                       },
                        origin: {
-                               chatLog: '{{ nick }} in {{ channel }} on {{ date, L LT }}',
+                               chatLog: 'Source: {{ nick }} in {{ channel }} on {{ date, L LT }}',
                        },
                },
                content: {
diff --git a/tests/js/helpers/User.test.js b/tests/js/helpers/User.test.js
new file mode 100644 (file)
index 0000000..86233f8
--- /dev/null
@@ -0,0 +1,36 @@
+import {
+       getUserName,
+} from 'helpers/User';
+
+describe('getUserName', () => {
+       test('empty on missing user', () => {
+               expect(getUserName()).toEqual('');
+       });
+       test('nickname if available', () => {
+               expect(getUserName({
+                       nickname: 'Holy',
+               })).toEqual('Holy');
+               expect(getUserName({
+                       nickname: 'Holy',
+                       discord_nickname: 'HolySmoke',
+               })).toEqual('Holy');
+               expect(getUserName({
+                       nickname: 'Holy',
+                       username: 'holysmoke86',
+               })).toEqual('Holy');
+       });
+       test('discord_nickname if no nickname', () => {
+               expect(getUserName({
+                       discord_nickname: 'HolySmoke',
+               })).toEqual('HolySmoke');
+               expect(getUserName({
+                       discord_nickname: 'HolySmoke',
+                       username: 'holysmoke86',
+               })).toEqual('HolySmoke');
+       });
+       test('username if no nicknames', () => {
+               expect(getUserName({
+                       username: 'holysmoke86',
+               })).toEqual('holysmoke86');
+       });
+});