Skip to content

Commit

Permalink
forget command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardicus committed Oct 14, 2024
1 parent 04c0399 commit 52b07ee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
42 changes: 42 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ struct RewindCommand {
pub entry: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
struct ForgetCommand {
pub entry: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
struct ExitCommand {}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -136,6 +140,7 @@ enum InputCommand {
Help(HelpCommand),
Remind(RemindCommand),
Rewind(RewindCommand),
Forget(ForgetCommand),
}

impl InputCommand {
Expand Down Expand Up @@ -175,6 +180,14 @@ impl InputCommand {
let cmd = RewindCommand { entry };
Some(InputCommand::Rewind(cmd))
}
Some("forget") => {
let entry = match parts.next() {
Some(entry) => entry.parse::<usize>().unwrap(),
None => 0,
};
let cmd = ForgetCommand { entry };
Some(InputCommand::Forget(cmd))
}
_ => None,
}
}
Expand All @@ -192,6 +205,9 @@ impl InputCommand {
println_message_str(1, " rewind [entry]").await;
println_message_str(1, " - Decrypts and displays previous chat sessions").await;
println_message_str(1, " enumerated as per 'remind'.").await;
println_message_str(1, " forget [entry]").await;
println_message_str(1, " - Delete the record of a previous chat session").await;
println_message_str(1, " enumerated as per 'remind'.").await;
println_message_str(1, " exit").await;
println_message_str(1, " - Exit the program.").await;
}
Expand Down Expand Up @@ -792,6 +808,32 @@ async fn launch_terminal_program(
}
}
}
Some(InputCommand::Forget(cmd)) => {
let entry = cmd.entry;
let ids = session.get_reminded_session_ids().await;
if ids.len() == 0 || entry > ids.len() || entry <= 0 {
println_message_str(
1,
"There is no memory of that session ¯\\_(ツ)_/¯...",
)
.await;
} else {
let id = &ids[entry - 1];
let last_active = session.get_reminded_last_active(id).await;
if last_active.is_ok() {
println_message_str(1,
&format!("-- Are you sure you want to delete this session memory from {}? [y/n]",
last_active.unwrap()),
)
.await;
}
let r = InputCommand::read_yes_or_no(1, ">> ", &mut rx).await;
if r.is_ok() && r.unwrap() {
let _ = session.remove_memory_entry(&ids[entry - 1]).await;
println_message_str(1, "The memory has been removed!").await;
}
}
}

None => {
// Send this input to listeners
Expand Down
7 changes: 6 additions & 1 deletion src/session/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ impl Memory {
None => Err(()),
}
}

pub fn delete_session(&mut self, session_id: &str) -> Result<usize, ()> {
match self.session_log.remove(session_id) {
Some(entry) => Ok(entry.messages.len()),
None => Err(()),
}
}
/// Returns a tuple with the encrypted session key and a vector of session messages
/// for the given session_id. Returns Err(()) if the session_id does not exist.
pub fn get_session_log(&self, session_id: &str) -> Result<(String, Vec<SessionMessage>), ()> {
Expand Down
12 changes: 8 additions & 4 deletions src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl Session<ChaCha20Poly1305EnDeCrypt, PGPEnDeCrypt> {
return Err(());
}
let cert = cert.unwrap();
let fingerprint = cert.fingerprint().to_string();
let _fingerprint = cert.fingerprint().to_string();

{
let mut hm = self.sessions.lock().await;
Expand Down Expand Up @@ -735,7 +735,7 @@ impl Session<ChaCha20Poly1305EnDeCrypt, PGPEnDeCrypt> {
}
while keep_running {
{
let mut sessions;
let sessions;
let session_ids: Vec<String>;
{
sessions = sessions_clone.lock().await;
Expand Down Expand Up @@ -1246,7 +1246,7 @@ impl Session<ChaCha20Poly1305EnDeCrypt, PGPEnDeCrypt> {
return Ok(None);
}
for message in msg.messages {
self.handle_message(message, topic, relay).await;
let _ = self.handle_message(message, topic, relay).await;
}
return Ok(None);
}
Expand Down Expand Up @@ -1659,7 +1659,8 @@ impl Session<ChaCha20Poly1305EnDeCrypt, PGPEnDeCrypt> {
let pub_key = session_data.pub_key.clone();
self.call_callbacks_chat(&pub_key, &msg.message).await;
if self.memory_active {
self.memory
let _ = self
.memory
.lock()
.await
.add_entry_message(&session_id.clone(), incoming_message.clone());
Expand Down Expand Up @@ -1849,6 +1850,9 @@ impl Session<ChaCha20Poly1305EnDeCrypt, PGPEnDeCrypt> {
pub async fn get_reminded_last_active(&self, session_id: &str) -> Result<String, ()> {
self.memory.lock().await.get_last_active(session_id)
}
pub async fn remove_memory_entry(&self, session_id: &str) -> Result<usize, ()> {
self.memory.lock().await.delete_session(session_id)
}
pub async fn get_reminded_session_log(
&self,
session_id: &str,
Expand Down

0 comments on commit 52b07ee

Please sign in to comment.