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();
return $this->morphTo();
}
+ public function user() {
+ return $this->belongsTo(User::class);
+ }
+
}
$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();
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);
}
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();
});
--- /dev/null
+<?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');
+ });
+ }
+};
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', {
});
};
+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();
className="text-muted"
title={moment(entry.created_at).format('LLLL')}
>
- {getEntryDate(entry)}
+ {getEntryInfo(entry, t)}
</div>
</div>
</ListGroup.Item>;
+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);
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) {
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;
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: {
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: {
--- /dev/null
+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');
+ });
+});