From e369e8c5a99f4f24f94708931fb4b28756d48254 Mon Sep 17 00:00:00 2001 From: yellowhatpro Date: Mon, 18 Nov 2024 10:38:26 +0530 Subject: [PATCH 1/4] feat:added add_relationship, edit_relationship, and edit_url structs --- src/poller/edit_types/add_relationship.rs | 45 +++++++++ src/poller/edit_types/edit_relationship.rs | 104 +++++++++++++++++++++ src/poller/edit_types/edit_url.rs | 32 +++++++ src/poller/edit_types/mod.rs | 3 + 4 files changed, 184 insertions(+) create mode 100644 src/poller/edit_types/add_relationship.rs create mode 100644 src/poller/edit_types/edit_relationship.rs create mode 100644 src/poller/edit_types/edit_url.rs diff --git a/src/poller/edit_types/add_relationship.rs b/src/poller/edit_types/add_relationship.rs new file mode 100644 index 0000000..765d52f --- /dev/null +++ b/src/poller/edit_types/add_relationship.rs @@ -0,0 +1,45 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct AddRelationship { + #[serde(rename = "edit_version")] + pub edit_version: Option, + pub entity0: Option, + pub entity1: Option, + #[serde(rename = "entity_id")] + pub entity_id: Option, + #[serde(rename = "link_type")] + pub link_type: Option, + pub type0: Option, + pub type1: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity0 { + pub gid: Option, + pub id: Option, + pub name: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity1 { + pub gid: Option, + pub id: Option, + pub name: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct LinkType { + pub id: Option, + #[serde(rename = "link_phrase")] + pub link_phrase: Option, + #[serde(rename = "long_link_phrase")] + pub long_link_phrase: Option, + pub name: Option, + #[serde(rename = "reverse_link_phrase")] + pub reverse_link_phrase: Option, +} diff --git a/src/poller/edit_types/edit_relationship.rs b/src/poller/edit_types/edit_relationship.rs new file mode 100644 index 0000000..6f2c9cf --- /dev/null +++ b/src/poller/edit_types/edit_relationship.rs @@ -0,0 +1,104 @@ +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct EditRelationship { + #[serde(rename = "edit_version")] + pub edit_version: Option, + pub link: Option, + pub new: Option, + pub old: Option, + #[serde(rename = "relationship_id")] + pub relationship_id: Option, + pub type0: Option, + pub type1: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Link { + pub attributes: Option>, + #[serde(rename = "begin_date")] + pub begin_date: Option, + #[serde(rename = "end_date")] + pub end_date: Option, + pub ended: Option, + pub entity0: Option, + pub entity1: Option, + #[serde(rename = "link_type")] + pub link_type: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct BeginDate { + pub day: Option, + pub month: Option, + pub year: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct EndDate { + pub day: Option, + pub month: Option, + pub year: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity0 { + pub gid: Option, + pub id: Option, + pub name: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity1 { + pub gid: Option, + pub id: Option, + pub name: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct LinkType { + pub id: Option, + #[serde(rename = "link_phrase")] + pub link_phrase: Option, + #[serde(rename = "long_link_phrase")] + pub long_link_phrase: Option, + pub name: Option, + #[serde(rename = "reverse_link_phrase")] + pub reverse_link_phrase: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct New { + pub entity1: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity12 { + pub gid: Option, + pub id: Option, + pub name: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Old { + pub entity1: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity13 { + pub gid: Option, + pub id: Option, + pub name: Option, +} diff --git a/src/poller/edit_types/edit_url.rs b/src/poller/edit_types/edit_url.rs new file mode 100644 index 0000000..99f252e --- /dev/null +++ b/src/poller/edit_types/edit_url.rs @@ -0,0 +1,32 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct EditUrl { + pub affects: Option, + pub entity: Option, + #[serde(rename = "is_merge")] + pub is_merge: Option, + pub new: Option, + pub old: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Entity { + pub gid: Option, + pub id: Option, + pub name: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct New { + pub url: Option, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Old { + pub url: Option, +} diff --git a/src/poller/edit_types/mod.rs b/src/poller/edit_types/mod.rs index a1bd46c..46cbf9c 100644 --- a/src/poller/edit_types/mod.rs +++ b/src/poller/edit_types/mod.rs @@ -1 +1,4 @@ +pub mod add_relationship; +pub mod edit_relationship; +pub mod edit_url; pub mod remove_relationship; From 1f38dbadd7fa51ab13cddf3634d68b0127640f81 Mon Sep 17 00:00:00 2001 From: yellowhatpro Date: Mon, 18 Nov 2024 10:39:05 +0530 Subject: [PATCH 2/4] refactor: replaced JsonValue with the new structs --- src/poller/utils.rs | 82 ++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/src/poller/utils.rs b/src/poller/utils.rs index 15bf2d3..73aa741 100644 --- a/src/poller/utils.rs +++ b/src/poller/utils.rs @@ -1,8 +1,11 @@ +use crate::poller::edit_types::add_relationship::AddRelationship; +use crate::poller::edit_types::edit_relationship::EditRelationship; +use crate::poller::edit_types::edit_url::EditUrl; use crate::poller::edit_types::remove_relationship::RemoveRelationship; use crate::structs::last_unprocessed_row::LastUnprocessedRow; use linkify::{LinkFinder, LinkKind}; use mb_rs::schema::{EditData, EditNote}; -use serde_json::{json, Value}; +use serde_json::Value; use sqlx::types::JsonValue; use sqlx::{Error, PgPool}; @@ -90,52 +93,53 @@ fn extract_url_from_remove_relationship(json: &JsonValue) -> Option { } fn extract_url_from_add_relationship(json: &JsonValue) -> Option { - if json.get("type0") == Some(&json!("url")) { - if json.get("entity0").is_some() && json["entity0"].get("name").is_some() { - let mut url = json["entity0"]["name"].to_string(); - url = url.replace(['\"', ' '], ""); - return Some(url); - }; - } else if json.get("type1") == Some(&json!("url")) - && json.get("entity1").is_some() - && json["entity1"].get("name").is_some() - { - let mut url = json["entity1"]["name"].to_string(); - url = url.replace(['\"', ' '], ""); - return Some(url); + let add_relationship: Option = serde_json::from_value(json.clone()).ok(); + if let Some(add_relationship) = add_relationship { + match ( + add_relationship.type0.as_deref(), + add_relationship.type1.as_deref(), + ) { + (Some("url"), _) => { + return add_relationship.entity0.and_then(|e| e.name); + } + (_, Some("url")) => { + return add_relationship.entity1.and_then(|e| e.name); + } + _ => {} + } } None } -fn extract_url_from_edit_relationship(json: &JsonValue) -> Option { - if json.get("type0") == Some(&json!("url")) { - if json.get("new").is_some() - && json["new"].get("entity0").is_some() - && json["new"]["entity0"].get("name").is_some() - { - let mut url = json["new"]["entity0"]["name"].to_string(); - url = url.replace(['\"', ' '], ""); - return Some(url); +fn extract_url_from_edit_relationship(json: &Value) -> Option { + let edit_relationship: Option = serde_json::from_value(json.clone()).ok(); + if let Some(edit_relationship) = edit_relationship { + match ( + edit_relationship.type0.as_deref(), + edit_relationship.type1.as_deref(), + ) { + (Some("url"), _) => { + return edit_relationship + .new + .and_then(|new_field| new_field.entity1) + .and_then(|entity| entity.name); + } + (_, Some("url")) => { + return edit_relationship + .new + .and_then(|new_field| new_field.entity1) + .and_then(|entity| entity.name); + } + _ => {} } - } else if json.get("type1") == Some(&json!("url")) - && json.get("new").is_some() - && json["new"].get("entity1").is_some() - && json["new"]["entity1"].get("name").is_some() - { - let mut url = json["new"]["entity1"]["name"].to_string(); - url = url.replace(['\"', ' '], ""); - return Some(url); - }; + } + None } -fn extract_url_from_edit_url(json: &JsonValue) -> Option { - if json.get("new").is_some() && json["new"].get("url").is_some() { - let mut url = json["new"]["url"].to_string(); - url = url.replace(['\"', ' '], ""); - return Some(url); - } - None +fn extract_url_from_edit_url(json: &Value) -> Option { + let edit_url: Option = serde_json::from_value(json.clone()).ok(); + edit_url?.new?.url } fn extract_url_from_any_annotation(json: &JsonValue) -> Option> { From fc9074581975dcb169fc529eae61f665fe9fcec5 Mon Sep 17 00:00:00 2001 From: yellowhatpro Date: Mon, 25 Nov 2024 09:40:18 +0530 Subject: [PATCH 3/4] fix: fixed failing tests by importing serde_json::json --- src/poller/tests/utils.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/poller/tests/utils.rs b/src/poller/tests/utils.rs index c3093e4..e27dfdc 100644 --- a/src/poller/tests/utils.rs +++ b/src/poller/tests/utils.rs @@ -1,4 +1,5 @@ use super::*; +use serde_json::json; #[test] fn test_extract_urls_from_text() { From 7dccc9dabbdad79f1881848c9934a0a0474ce264 Mon Sep 17 00:00:00 2001 From: yellowhatpro Date: Mon, 25 Nov 2024 12:03:35 +0530 Subject: [PATCH 4/4] feat: added test for edit_url edit type --- src/poller/tests/utils.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/poller/tests/utils.rs b/src/poller/tests/utils.rs index e27dfdc..6e41d2a 100644 --- a/src/poller/tests/utils.rs +++ b/src/poller/tests/utils.rs @@ -550,3 +550,40 @@ async fn test_update_last_unprocessed_rows(pool: PgPool) -> Result<(), Error> { Ok(()) } + +#[test] +fn test_extract_url_from_edit_url() { + let json = json!({ + "affects": 1, + "entity": { + "gid": "9af7c6df-0aa5-4ef4-8d2e-9fc50627fb2c", + "id": 13724363, + "name": "https://www.rriiccee.com/index.htm" + }, + "is_merge": 1, + "new": { + "url": "https://www.rriiccee.com/" + }, + "old": { + "url": "https://www.rriiccee.com/index.htm" + } + }); + + let json_with_no_url = json!({ + "affects": 1, + "entity": { + "gid": "9af7c6df-0aa5-4ef4-8d2e-9fc50627fb2c", + "id": 13724363, + "name": "Test Entity" + }, + "is_merge": 1, + "new": {}, + }); + + assert_eq!(extract_url_from_edit_url(&json_with_no_url), None); + + assert_eq!( + extract_url_from_edit_url(&json), + Some("https://www.rriiccee.com/".to_string()) + ); +}