Skip to content

Commit

Permalink
Log system events and improve their handling (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick authored Oct 10, 2024
1 parent 48a482f commit acd5bb1
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 323 deletions.
11 changes: 4 additions & 7 deletions crates/steel_core/src/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Message {
}
}

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, PartialEq)]
pub enum ChatState {
#[default]
Left,
Expand All @@ -159,9 +159,9 @@ pub struct Chat {
}

impl Chat {
pub fn new(name: String) -> Self {
pub fn new(name: &str) -> Self {
Self {
name,
name: name.to_owned(),
messages: Vec::new(),
state: ChatState::Left,
}
Expand All @@ -175,11 +175,8 @@ impl Chat {
self.name.chat_type()
}

pub fn set_state(&mut self, state: ChatState, reason: Option<&str>) {
pub fn set_state(&mut self, state: ChatState) {
self.state = state;
if let Some(reason) = reason {
self.push(Message::new_system(reason));
}
}
}

Expand Down
16 changes: 2 additions & 14 deletions crates/steel_core/src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@ impl CoreClient {
}

impl CoreClient {
pub fn channel_opened(&self, channel: &str) {
pub fn chat_opened(&self, chat: &str) {
self.server
.send(AppMessageIn::UIChannelOpened(channel.to_owned()))
.unwrap();
}

pub fn private_chat_opened(&self, chat: &str) {
self.server
.send(AppMessageIn::UIPrivateChatOpened(chat.to_owned()))
.unwrap();
}

pub fn channel_join_requested(&self, channel: &str) {
self.server
.send(AppMessageIn::UIChannelJoinRequested(channel.to_owned()))
.send(AppMessageIn::UIChatOpened(chat.to_owned()))
.unwrap();
}

Expand Down
5 changes: 2 additions & 3 deletions crates/steel_core/src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ pub enum AppMessageIn {
ChatMessageReceived { target: String, message: Message },
ServerMessageReceived { content: String },
ChannelJoined(String),
DateChanged(chrono::DateTime<chrono::Local>, String),

UIConnectRequested,
UIDisconnectRequested,
UIExitRequested,
UIChannelOpened(String),
UIChannelJoinRequested(String),
UIPrivateChatOpened(String),
UIChatOpened(String),
UIChatClosed(String),
UIChatCleared(String),
UIChatSwitchRequested(String, Option<usize>),
Expand Down
15 changes: 4 additions & 11 deletions crates/steel_core/src/ipc/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ use crate::settings::Settings;
pub enum UIMessageIn {
SettingsChanged(Settings),
ConnectionStatusChanged(ConnectionStatus),
NewMessageReceived {
target: String,
message: Message,
},
NewSystemMessage { target: String, message: Message },
NewMessageReceived { target: String, message: Message },
NewServerMessageReceived(String),
NewChatStatusReceived {
target: String,
state: ChatState,
details: String,
},
NewChatRequested(String, ChatState, bool),
NewChatStateReceived { target: String, state: ChatState },
NewChatRequested { target: String, switch: bool },
ChatSwitchRequested(String, Option<usize>),
ChannelJoined(String),
ChatClosed(String),
ChatCleared(String),
ChatModeratorAdded(String),
Expand Down
15 changes: 11 additions & 4 deletions crates/steel_core/src/settings/journal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use serde::{Deserialize, Serialize};

use crate::DEFAULT_DATETIME_FORMAT;

pub const DEFAULT_LOG_DIRECTORY: &str = "./chat-logs";

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Journal {
Expand Down Expand Up @@ -41,16 +45,19 @@ pub struct ChatEvents {
pub enabled: bool,
pub directory: String,
pub format: String,
pub with_system_events: bool,
pub log_system_events: bool,
}

impl Default for ChatEvents {
fn default() -> Self {
Self {
enabled: true,
directory: "./chat-logs".to_owned(),
format: "[{date:%Y-%m-%d %H:%M:%S}] <{username}> {text}".to_owned(),
with_system_events: true,
directory: DEFAULT_LOG_DIRECTORY.to_owned(),
format: format!(
"{{date:{}}} <{{username}}> {{text}}",
DEFAULT_DATETIME_FORMAT
),
log_system_events: true,
}
}
}
40 changes: 40 additions & 0 deletions src/app/date_announcer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::time::Duration;

use chrono::DurationRound;
use steel_core::{ipc::server::AppMessageIn, DEFAULT_DATE_FORMAT};
use tokio::sync::mpsc::UnboundedSender;

use crate::actor::ActorHandle;

pub(super) struct DateAnnouncer {
_thread: std::thread::JoinHandle<()>,
}

impl ActorHandle for DateAnnouncer {}

impl DateAnnouncer {
pub(super) fn new(app_queue: UnboundedSender<AppMessageIn>) -> Self {
Self {
_thread: std::thread::spawn(|| announcer(app_queue)),
}
}
}

fn announcer(app_queue: UnboundedSender<AppMessageIn>) {
let mut then = chrono::Local::now();
loop {
let now = chrono::Local::now();
if then.date_naive() < now.date_naive() {
then = now;
let round_date = now.duration_trunc(chrono::Duration::days(1)).unwrap();
let message = format!(
"A new day is born ({})",
round_date.format(DEFAULT_DATE_FORMAT),
);
app_queue
.send(AppMessageIn::DateChanged(round_date, message))
.unwrap();
}
std::thread::sleep(Duration::from_millis(100));
}
}
Loading

0 comments on commit acd5bb1

Please sign in to comment.