diff --git a/NOTES.md b/NOTES.md index ea5a2c9..47245ff 100644 --- a/NOTES.md +++ b/NOTES.md @@ -1,5 +1,7 @@ # Doing +Reset session information in config on logout + Show error when choosing file "chooseFile" Show filename in chat bot list Add styling to choose file button diff --git a/app.go b/app.go index 36b3c64..a594854 100644 --- a/app.go +++ b/app.go @@ -215,10 +215,77 @@ func (a *App) UpdateChatMessage(cid string, cm config.ChatMessage) (map[string]c return a.cfg.Channels[cid].ChatBot.Messages, nil } -func (a *App) NewChatBot(cid string, username string, password string, streamUrl string) error { +type NewChatBotResponse struct { + LoggedIn bool `json:"logged_in"` + StreamUrl string `json:"stream_url"` + Username string `json:"username"` +} + +func (a *App) GetChatBot(cid string) (NewChatBotResponse, error) { + if a.cb == nil { + return NewChatBotResponse{}, fmt.Errorf("Chat bot not initalized.") + } + + loggedIn, err := a.cb.LoggedIn() + if err != nil { + a.logError.Println("error checking if chat bot is logged in:", err) + return NewChatBotResponse{}, fmt.Errorf("Error checking if chat bot is logged in. Try again.") + } + + return NewChatBotResponse{loggedIn, a.cb.Cfg.Session.Client.StreamUrl, a.cb.Cfg.Session.Username}, nil +} + +func (a *App) NewChatBot(cid string) (NewChatBotResponse, error) { a.cbMu.Lock() defer a.cbMu.Unlock() + if a.cb != nil { + err := a.resetChatBot() + if err != nil { + a.logError.Println("error resetting chat bot:", err) + return NewChatBotResponse{}, fmt.Errorf("Error creating chat bot. Try Again.") + } + } + channel, exists := a.cfg.Channels[cid] + if !exists { + a.logError.Println("channel does not exist:", cid) + return NewChatBotResponse{}, fmt.Errorf("Channel does not exist.") + } + + if channel.ChatBot.Session.Client.StreamUrl == "" { + return NewChatBotResponse{}, nil + } + + var err error + a.cb, err = chatbot.NewChatBot(a.ctx, channel.ChatBot, a.logError) + if err != nil { + a.logError.Println("error creating new chat bot:", err) + return NewChatBotResponse{}, fmt.Errorf("Error creating new chat bot. Try again.") + } + + loggedIn, err := a.cb.LoggedIn() + if err != nil { + a.logError.Println("error checking if chat bot is logged in:", err) + return NewChatBotResponse{}, fmt.Errorf("Error checking if chat bot is logged in. Try again.") + } + + if loggedIn { + err = a.cb.StartChatStream() + if err != nil { + a.logError.Println("error starting chat stream:", err) + return NewChatBotResponse{}, fmt.Errorf("Error connecting to chat. Try again.") + } + } + + return NewChatBotResponse{loggedIn, channel.ChatBot.Session.Client.StreamUrl, channel.ChatBot.Session.Username}, nil +} + +func (a *App) LoginChatBot(cid string, username string, password string, streamUrl string) error { + a.cbMu.Lock() + defer a.cbMu.Unlock() + a.cfgMu.Lock() + defer a.cfgMu.Unlock() + if a.cb != nil { err := a.resetChatBot() if err != nil { @@ -231,35 +298,151 @@ func (a *App) NewChatBot(cid string, username string, password string, streamUrl a.logError.Println("channel does not exist:", cid) return fmt.Errorf("Channel does not exist.") } + channel.ChatBot.Session.Client.StreamUrl = streamUrl var err error - a.cb, err = chatbot.NewChatBot(a.ctx, streamUrl, channel.ChatBot, a.logError) + a.cb, err = chatbot.NewChatBot(a.ctx, channel.ChatBot, a.logError) if err != nil { a.logError.Println("error creating new chat bot:", err) return fmt.Errorf("Error creating new chat bot. Try again.") } - err = a.cb.Login(username, password) + cookies, err := a.cb.Login(username, password) if err != nil { a.logError.Println("error logging into chat bot:", err) return fmt.Errorf("Error logging in. Try again.") } + channel.ChatBot.Session = config.ChatBotSession{ + Client: rumblelivestreamlib.NewClientOptions{ + Cookies: cookies, + StreamUrl: streamUrl, + }, + Username: username, + } + a.cfg.Channels[cid] = channel + err = a.cfg.Save() + if err != nil { + a.logError.Println("error saving config:", err) + return fmt.Errorf("Error saving session information. Try again.") + } + + a.cb.Cfg.Session = channel.ChatBot.Session + err = a.cb.StartChatStream() if err != nil { a.logError.Println("error starting chat stream:", err) return fmt.Errorf("Error connecting to chat. Try again.") } - // a.cb = cb return nil } -func (a *App) ResetChatBot() error { +func (a *App) StopAllChatBot(cid string) error { + err := a.cb.StopAllMessages() + if err != nil { + a.logError.Println("error stopping all chat bot messages:", err) + return fmt.Errorf("Error stopping messages.") + } + + return nil +} + +func (a *App) StartAllChatBot(cid string) error { + err := a.cb.StartAllMessages() + if err != nil { + a.logError.Println("error starting all chat bot messages:", err) + return fmt.Errorf("Error starting messages.") + } + + return nil +} + +func (a *App) UpdateChatBotUrl(cid string, streamUrl string) error { a.cbMu.Lock() defer a.cbMu.Unlock() + a.cfgMu.Lock() + defer a.cfgMu.Unlock() + + if a.cb == nil { + return fmt.Errorf("Chat bot not initalized.") + } err := a.resetChatBot() + if err != nil { + a.logError.Println("error resetting chat bot:", err) + return fmt.Errorf("Error creating chat bot. Try Again.") + } + + channel, exists := a.cfg.Channels[cid] + if !exists { + a.logError.Println("channel does not exist:", cid) + return fmt.Errorf("Channel does not exist.") + } + channel.ChatBot.Session.Client.StreamUrl = streamUrl + + a.cb, err = chatbot.NewChatBot(a.ctx, channel.ChatBot, a.logError) + if err != nil { + a.logError.Println("error creating new chat bot:", err) + return fmt.Errorf("Error creating new chat bot. Try again.") + } + + a.cfg.Channels[cid] = channel + err = a.cfg.Save() + if err != nil { + a.logError.Println("error saving config:", err) + return fmt.Errorf("Error saving session information. Try again.") + } + + a.cb.Cfg.Session.Client.StreamUrl = streamUrl + + err = a.cb.StartChatStream() + if err != nil { + a.logError.Println("error starting chat stream:", err) + return fmt.Errorf("Error connecting to chat. Try again.") + } + + return nil +} + +func (a *App) ResetChatBot(cid string, logout bool) error { + a.cbMu.Lock() + defer a.cbMu.Unlock() + a.cfgMu.Lock() + defer a.cfgMu.Unlock() + + if a.cb == nil { + return nil + } + + err := a.cb.StopAllMessages() + if err != nil { + return fmt.Errorf("error stopping all chat bot messages: %v", err) + } + + if logout { + err := a.cb.Logout() + if err != nil { + return fmt.Errorf("error logging out of chat bot: %v", err) + } + + //TODO: reset session in config + channel, exists := a.cfg.Channels[cid] + if !exists { + a.logError.Println("channel does not exist:", cid) + return fmt.Errorf("Channel does not exist.") + } + + channel.ChatBot.Session = config.ChatBotSession{} + a.cfg.Channels[cid] = channel + err = a.cfg.Save() + if err != nil { + a.logError.Println("error saving config:", err) + return fmt.Errorf("Error saving session information. Try again.") + } + } + + err = a.resetChatBot() if err != nil { a.logError.Println("error resetting chat bot:", err) return fmt.Errorf("Error resetting chat bot. Try Again.") @@ -284,11 +467,6 @@ func (a *App) resetChatBot() error { return fmt.Errorf("error stopping chat stream: %v", err) } - err = a.cb.Logout() - if err != nil { - return fmt.Errorf("error logging out of chat bot: %v", err) - } - a.cb = nil return nil diff --git a/frontend/src/assets/icons/index.jsx b/frontend/src/assets/icons/index.jsx index 2c394db..b814454 100644 --- a/frontend/src/assets/icons/index.jsx +++ b/frontend/src/assets/icons/index.jsx @@ -6,8 +6,10 @@ import heart from './heart-fill.png'; import house from './house.png'; import pause from './pause-fill.png'; import play from './play-fill.png'; +import play_green from './play-fill-green.png'; import plus_circle from './plus-circle-fill.png'; import star from './star-fill.png'; +import stop from './stop-fill.png'; import thumbs_down from './hand-thumbs-down.png'; import thumbs_up from './hand-thumbs-up.png'; import x_lg from './x-lg.png'; @@ -20,8 +22,10 @@ export const Heart = heart; export const House = house; export const Pause = pause; export const Play = play; +export const PlayGreen = play_green; export const PlusCircle = plus_circle; export const Star = star; +export const Stop = stop; export const ThumbsDown = thumbs_down; export const ThumbsUp = thumbs_up; export const XLg = x_lg; diff --git a/frontend/src/assets/icons/play-fill-green.png b/frontend/src/assets/icons/play-fill-green.png new file mode 100644 index 0000000..6b7294a Binary files /dev/null and b/frontend/src/assets/icons/play-fill-green.png differ diff --git a/frontend/src/assets/icons/stop-fill.png b/frontend/src/assets/icons/stop-fill.png new file mode 100644 index 0000000..6dccccf Binary files /dev/null and b/frontend/src/assets/icons/stop-fill.png differ diff --git a/frontend/src/components/ChatBot.css b/frontend/src/components/ChatBot.css index 332cb42..6b879db 100644 --- a/frontend/src/components/ChatBot.css +++ b/frontend/src/components/ChatBot.css @@ -44,4 +44,21 @@ padding: 10px; resize: none; width: 100%; +} + +.chat-bot-description { + align-items: center; + display: flex; + flex-direction: row; + justify-content: start; + padding-top: 10px; + width: 100%; +} + +.chat-bot-description-label { + color: white; + font-family: sans-serif; + font-size: 20px; + padding-bottom: 5px; + padding-right: 5px; } \ No newline at end of file diff --git a/frontend/src/components/ChatBot.jsx b/frontend/src/components/ChatBot.jsx index 8e1354e..d7b2174 100644 --- a/frontend/src/components/ChatBot.jsx +++ b/frontend/src/components/ChatBot.jsx @@ -2,18 +2,19 @@ import { useEffect, useState } from 'react'; import { Modal, SmallModal } from './Modal'; -import { NewChatBot } from '../../wailsjs/go/main/App'; +import { LoginChatBot, UpdateChatBotUrl } from '../../wailsjs/go/main/App'; import './ChatBot.css'; export function ChatBotModal(props) { const [error, setError] = useState(''); + const [loggedIn, setLoggedIn] = useState(props.loggedIn); const [password, setPassword] = useState(''); const [saving, setSaving] = useState(false); const updatePassword = (event) => setPassword(event.target.value); - const [url, setUrl] = useState(''); + const [url, setUrl] = useState(props.streamUrl); const updateUrl = (event) => setUrl(event.target.value); - const [username, setUsername] = useState(''); + const [username, setUsername] = useState(props.username); const updateUsername = (event) => setUsername(event.target.value); useEffect(() => { @@ -22,21 +23,36 @@ export function ChatBotModal(props) { // let p = password; // let u = url; // props.onSubmit(user, p, u); - NewChatBot(props.cid, username, password, url) - .then(() => { - reset(); - props.onClose(); - }) - .catch((error) => { - setSaving(false); - setError(error); - console.log('Error creating new chat bot:', error); - }); + // NewChatBot(props.cid, username, password, url) + if (loggedIn) { + UpdateChatBotUrl(props.cid, url) + .then(() => { + reset(); + props.onUpdate(url); + }) + .catch((error) => { + setSaving(false); + setError(error); + console.log('Error updating chat bot:', error); + }); + } else { + LoginChatBot(props.cid, username, password, url) + .then(() => { + reset(); + props.onLogin(); + }) + .catch((error) => { + setSaving(false); + setError(error); + console.log('Error creating new chat bot:', error); + }); + } } }, [saving]); const reset = () => { setError(''); + setLoggedIn(false); setPassword(''); setSaving(false); setUrl(''); @@ -48,13 +64,18 @@ export function ChatBotModal(props) { props.onClose(); }; + const logout = () => { + reset(); + props.onLogout(); + }; + const submit = () => { if (username === '') { setError('Add username'); return; } - if (password === '') { + if (password === '' && !loggedIn) { setError('Add password'); return; } @@ -78,8 +99,10 @@ export function ChatBotModal(props) { onClose={close} show={props.show} style={{ minWidth: '300px', maxWidth: '400px' }} - cancelButton={'Cancel'} + cancelButton={loggedIn ? '' : 'Cancel'} onCancel={close} + deleteButton={loggedIn ? 'Logout' : ''} + onDelete={logout} submitButton={saving ? 'Saving' : 'Save'} onSubmit={ saving @@ -91,27 +114,40 @@ export function ChatBotModal(props) { title={'Chat Bot'} >