Skip to content

Commit

Permalink
Simplify FUZZ_SECONDS and separate into distinct values for history…
Browse files Browse the repository at this point in the history
… insertion and chathistory purposes.
  • Loading branch information
andymandias committed Oct 30, 2024
1 parent d3bb475 commit c7d175f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
6 changes: 4 additions & 2 deletions data/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,16 @@ impl History {
/// of the incoming message. Either message IDs match, or server times
/// have an exact match + target & content.
pub fn insert_message(messages: &mut Vec<Message>, message: Message) {
const FUZZ_SECONDS: chrono::Duration = chrono::Duration::seconds(1);

if messages.is_empty() {
messages.push(message);

return;
}

let start = message::fuzz_start_server_time(message.server_time);
let end = message::fuzz_end_server_time(message.server_time);
let start = message.server_time - FUZZ_SECONDS;
let end = message.server_time + FUZZ_SECONDS;

let start_index = match messages.binary_search_by(|stored| stored.server_time.cmp(&start)) {
Ok(match_index) => match_index,
Expand Down
8 changes: 5 additions & 3 deletions data/src/isupport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use irc::proto;

use crate::{message, Message};
use crate::Message;

// Utilized ISUPPORT parameters should have an associated Kind enum variant
// returned by Operation::kind() and Parameter::kind()
Expand Down Expand Up @@ -722,10 +722,12 @@ const DEFAULT_DEAF_LETTER: char = 'D';

const DEFAULT_INVITE_EXCEPTION_LETTER: char = 'I';

const FUZZ_SECONDS: chrono::Duration = chrono::Duration::seconds(5);

pub fn fuzz_start_message_reference(message_reference: MessageReference) -> MessageReference {
match message_reference {
MessageReference::Timestamp(start_server_time) => {
MessageReference::Timestamp(message::fuzz_start_server_time(start_server_time))
MessageReference::Timestamp(start_server_time - FUZZ_SECONDS)
}
_ => message_reference,
}
Expand All @@ -734,7 +736,7 @@ pub fn fuzz_start_message_reference(message_reference: MessageReference) -> Mess
pub fn fuzz_end_message_reference(message_reference: MessageReference) -> MessageReference {
match message_reference {
MessageReference::Timestamp(end_server_time) => {
MessageReference::Timestamp(message::fuzz_end_server_time(end_server_time))
MessageReference::Timestamp(end_server_time + FUZZ_SECONDS)
}
_ => message_reference,
}
Expand Down
16 changes: 1 addition & 15 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::hash::{DefaultHasher, Hash as _, Hasher};
use std::iter;

use chrono::{DateTime, TimeDelta, Utc};
use chrono::{DateTime, Utc};
use const_format::concatcp;
use irc::proto;
use irc::proto::Command;
Expand Down Expand Up @@ -853,20 +853,6 @@ pub fn server_time(message: &Encoded) -> DateTime<Utc> {
.unwrap_or_else(Utc::now)
}

pub const FUZZ_SECONDS: i64 = 1;

pub fn fuzz_start_server_time(server_time: DateTime<Utc>) -> DateTime<Utc> {
TimeDelta::try_seconds(FUZZ_SECONDS)
.and_then(|time_delta| server_time.checked_sub_signed(time_delta))
.map_or(server_time, |fuzzed_server_time| fuzzed_server_time)
}

pub fn fuzz_end_server_time(server_time: DateTime<Utc>) -> DateTime<Utc> {
TimeDelta::try_seconds(FUZZ_SECONDS)
.and_then(|time_delta| server_time.checked_add_signed(time_delta))
.map_or(server_time, |fuzzed_server_time| fuzzed_server_time)
}

fn content<'a>(
message: &Encoded,
our_nick: &Nick,
Expand Down

0 comments on commit c7d175f

Please sign in to comment.