Skip to content

Commit

Permalink
chathistory: Send 'FAIL CHATHISTORY INVALID_TARGET' when required
Browse files Browse the repository at this point in the history
  • Loading branch information
progval authored and spb committed Apr 14, 2024
1 parent 97f3fc7 commit bab5ff4
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions sable_ircd/src/command/handlers/chathistory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn handle_chathistory(
}

send_history_for_target_reverse(
server, &response, source, &target, from_ts, None, limit,
server, &response, source, subcommand, &target, from_ts, None, limit,
)?;
}
"BEFORE" => {
Expand Down Expand Up @@ -116,6 +116,7 @@ fn handle_chathistory(
server,
&response,
source,
subcommand,
&target,
None,
Some(end_ts),
Expand Down Expand Up @@ -152,6 +153,7 @@ fn handle_chathistory(
server,
&response,
source,
subcommand,
&target,
Some(start_ts),
None,
Expand Down Expand Up @@ -190,6 +192,7 @@ fn handle_chathistory(
server,
&response,
source,
subcommand,
&target,
Some(around_ts),
None,
Expand All @@ -199,6 +202,7 @@ fn handle_chathistory(
server,
&response,
source,
subcommand,
&target,
Some(around_ts),
None,
Expand Down Expand Up @@ -247,6 +251,7 @@ fn handle_chathistory(
server,
&response,
source,
subcommand,
&target,
Some(start_ts),
Some(end_ts),
Expand Down Expand Up @@ -335,6 +340,7 @@ fn send_history_for_target_forward(
server: &ClientServer,
into: impl MessageSink,
source: &wrapper::User,
subcommand: &str,
target: &str,
from_ts: Option<i64>,
to_ts: Option<i64>,
Expand Down Expand Up @@ -364,23 +370,15 @@ fn send_history_for_target_forward(
}
}

let batch = into
.batch("chathistory", ClientCapability::Batch)
.with_arguments(&[target])
.start();

for entry in entries {
entry.send_to(&batch, entry)?;
}

Ok(())
send_history_entries(into, subcommand, target, entries)
}

// As above, but work backwards
fn send_history_for_target_reverse(
server: &ClientServer,
into: impl MessageSink,
source: &wrapper::User,
subcommand: &str,
target: &str,
from_ts: Option<i64>,
to_ts: Option<i64>,
Expand Down Expand Up @@ -410,13 +408,31 @@ fn send_history_for_target_reverse(
}
}

let batch = into
.batch("chathistory", ClientCapability::Batch)
.with_arguments(&[target])
.start();
send_history_entries(into, subcommand, target, entries)
}

for entry in entries.into_iter().rev() {
entry.send_to(&batch, entry)?;
fn send_history_entries(
into: impl MessageSink,
subcommand: &str,
target: &str,
entries: Vec<&HistoryLogEntry>,
) -> CommandResult {
if entries.is_empty() {
into.send(message::Fail::new(
"CHATHISTORY",
"INVALID_TARGET",
&format!("{} {}", subcommand, target),
&format!("Cannot fetch history from {}", target),
));
} else {
let batch = into
.batch("chathistory", ClientCapability::Batch)
.with_arguments(&[target])
.start();

for entry in entries {
entry.send_to(&batch, entry)?;
}
}

Ok(())
Expand Down

0 comments on commit bab5ff4

Please sign in to comment.