Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/structs replace jsonvalue #18

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/poller/edit_types/add_relationship.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
pub entity0: Option<Entity0>,
pub entity1: Option<Entity1>,
#[serde(rename = "entity_id")]
pub entity_id: Option<i64>,
#[serde(rename = "link_type")]
pub link_type: Option<LinkType>,
pub type0: Option<String>,
pub type1: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity0 {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity1 {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkType {
pub id: Option<i64>,
#[serde(rename = "link_phrase")]
pub link_phrase: Option<String>,
#[serde(rename = "long_link_phrase")]
pub long_link_phrase: Option<String>,
pub name: Option<String>,
#[serde(rename = "reverse_link_phrase")]
pub reverse_link_phrase: Option<String>,
}
104 changes: 104 additions & 0 deletions src/poller/edit_types/edit_relationship.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
pub link: Option<Link>,
pub new: Option<New>,
pub old: Option<Old>,
#[serde(rename = "relationship_id")]
pub relationship_id: Option<i64>,
pub type0: Option<String>,
pub type1: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Link {
pub attributes: Option<Vec<Value>>,
#[serde(rename = "begin_date")]
pub begin_date: Option<BeginDate>,
#[serde(rename = "end_date")]
pub end_date: Option<EndDate>,
pub ended: Option<i64>,
pub entity0: Option<Entity0>,
pub entity1: Option<Entity1>,
#[serde(rename = "link_type")]
pub link_type: Option<LinkType>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BeginDate {
pub day: Option<Value>,
pub month: Option<Value>,
pub year: Option<Value>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EndDate {
pub day: Option<Value>,
pub month: Option<Value>,
pub year: Option<Value>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity0 {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity1 {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkType {
pub id: Option<i64>,
#[serde(rename = "link_phrase")]
pub link_phrase: Option<String>,
#[serde(rename = "long_link_phrase")]
pub long_link_phrase: Option<String>,
pub name: Option<String>,
#[serde(rename = "reverse_link_phrase")]
pub reverse_link_phrase: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct New {
pub entity1: Option<Entity12>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity12 {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Old {
pub entity1: Option<Entity13>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity13 {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}
32 changes: 32 additions & 0 deletions src/poller/edit_types/edit_url.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
pub entity: Option<Entity>,
#[serde(rename = "is_merge")]
pub is_merge: Option<i64>,
pub new: Option<New>,
pub old: Option<Old>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Entity {
pub gid: Option<String>,
pub id: Option<i64>,
pub name: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct New {
pub url: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Old {
pub url: Option<String>,
}
3 changes: 3 additions & 0 deletions src/poller/edit_types/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod add_relationship;
pub mod edit_relationship;
pub mod edit_url;
pub mod remove_relationship;
38 changes: 38 additions & 0 deletions src/poller/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use serde_json::json;

#[test]
fn test_extract_urls_from_text() {
Expand Down Expand Up @@ -549,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())
);
}
82 changes: 43 additions & 39 deletions src/poller/utils.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -90,52 +93,53 @@ fn extract_url_from_remove_relationship(json: &JsonValue) -> Option<String> {
}

fn extract_url_from_add_relationship(json: &JsonValue) -> Option<String> {
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<AddRelationship> = 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<String> {
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<String> {
let edit_relationship: Option<EditRelationship> = 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<String> {
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<String> {
let edit_url: Option<EditUrl> = serde_json::from_value(json.clone()).ok();
edit_url?.new?.url
}

fn extract_url_from_any_annotation(json: &JsonValue) -> Option<Vec<String>> {
Expand Down