Skip to content

Commit

Permalink
cypher datetime fn
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan0x6C1 committed Mar 29, 2024
1 parent 0702843 commit 902d8fa
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions imessage-exporter/src/exporters/cypher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,29 @@ impl<'a> Exporter<'a> for CYPHER<'a> {
}

async fn index_to_cypher(message: &HydratedMessage, graph: &Graph) -> Node {
let iso_datetime = iso8601_datetime__from__timestamp_str(&message.timestamp);
let query_string = format!(
"MERGE (t:Thread {{name: \"{}\"}})
CREATE (m:Message {{
timestamp: '{}',
isotime: datetime('{}'),
sender: '{}',
isFromMe: {},
isImessage: {},
subject: {},
text: '{}',
attachments: {},
reactions: {}
reactions: {},
guid: '{}',
created_at: datetime()
}})-[:BELONGS_TO]->(t)
RETURN id(m) AS msg_id, m",
match &message.parent_thread_name {
Some(parent_thread_name) => parent_thread_name,
None => "Unknown",
},
message.timestamp,
iso_datetime,
message.sender,
message.is_from_me,
message.is_imessage,
Expand All @@ -191,18 +196,50 @@ async fn index_to_cypher(message: &HydratedMessage, graph: &Graph) -> Node {
message.text.replace("'", "\\'"),
format!("{:?}", message.attachments),
format!("{:?}", message.reactions),
message.guid
);

println!("\n\n{}\n\n", query_string);

let mut mutres = graph.execute(query(query_string.as_str())).await.unwrap();
let mirow = mutres.next().await.unwrap().unwrap();
let value: Node = mirow.get("m").unwrap();
// println!("{:?}", value);

value
}

/// converts a string like "Jan 30, 2022 1:23:11 PM" to "2022-01-30T13:23:11"
fn iso8601_datetime__from__timestamp_str(timestamp: &str) -> String {
let dt = chrono::NaiveDateTime::parse_from_str(timestamp, "%b %d, %Y %I:%M:%S %p").unwrap();
dt.format("%Y-%m-%dT%H:%M:%S").to_string()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn isodatetime_from_timestamp_str_converts_valid_timestamp() {
let timestamp = "Jan 30, 2022 1:23:11 PM";
let expected = "2022-01-30T13:23:11";
assert_eq!(iso8601_datetime__from__timestamp_str(timestamp), expected);
}

#[test]
fn isodatetime_from_timestamp_str_handles_invalid_timestamp() {
let timestamp = "Invalid timestamp";
let result = std::panic::catch_unwind(|| iso8601_datetime__from__timestamp_str(timestamp));
assert!(result.is_err());
}

#[test]
fn isodatetime_from_timestamp_str_handles_empty_string() {
let timestamp = "";
let result = std::panic::catch_unwind(|| iso8601_datetime__from__timestamp_str(timestamp));
assert!(result.is_err());
}
}

#[tokio::test]
async fn test_index_to_cypher() {
let json_message = r#"
Expand Down

0 comments on commit 902d8fa

Please sign in to comment.