Skip to content

Commit

Permalink
refactor code to use Shrinkwraprs and diesel-derive-newtype (#598)
Browse files Browse the repository at this point in the history
* add shrinkwraprs and implement Id thru it

This also means we can automatically convert Id to String without using
.into()!

* cleanup with the help of clippy!

* cleanup with the help of cargo fmt!

* remove extra block

* Shrinkwrap Page, ContentLen and RemoteForm

* translations
  • Loading branch information
igalic authored and elegaanz committed May 25, 2019
1 parent 59023e9 commit 8c59c82
Show file tree
Hide file tree
Showing 38 changed files with 10,415 additions and 10,334 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ scheduled-thread-pool = "0.2.0"
serde = "1.0"
serde_json = "1.0"
serde_qs = "0.4"
shrinkwraprs = "0.2.1"
validator = "0.8"
validator_derive = "0.8"
webfinger = "0.3.1"
Expand Down
1 change: 1 addition & 0 deletions plume-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ reqwest = "0.9"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
shrinkwraprs = "0.2.1"

[dependencies.chrono]
features = ["serde"]
Expand Down
12 changes: 3 additions & 9 deletions plume-common/src/activity_pub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,12 @@ where
}
}

#[derive(Clone, Serialize, Deserialize)]
#[derive(Shrinkwrap, Clone, Serialize, Deserialize)]
pub struct Id(String);

impl Id {
pub fn new<T: Into<String>>(id: T) -> Id {
Id(id.into())
}
}

impl Into<String> for Id {
fn into(self) -> String {
self.0.clone()
pub fn new(id: impl ToString) -> Id {
Id(id.to_string())
}
}

Expand Down
2 changes: 2 additions & 0 deletions plume-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extern crate reqwest;
extern crate rocket;
extern crate serde;
#[macro_use]
extern crate shrinkwraprs;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
Expand Down
2 changes: 2 additions & 0 deletions plume-models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ tantivy = "0.9.1"
url = "1.7"
webfinger = "0.3.1"
whatlang = "0.7.1"
shrinkwraprs = "0.2.1"
diesel-derive-newtype = "0.1.2"

[dependencies.chrono]
features = ["serde"]
Expand Down
4 changes: 2 additions & 2 deletions plume-models/src/blogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl FromId<PlumeRocket> for Blog {
.icon_image()
.ok()
.and_then(|icon| {
let owner: String = icon.object_props.attributed_to_link::<Id>().ok()?.into();
let owner = icon.object_props.attributed_to_link::<Id>().ok()?;
Media::save_remote(
&c.conn,
icon.object_props.url_string().ok()?,
Expand All @@ -348,7 +348,7 @@ impl FromId<PlumeRocket> for Blog {
.image_image()
.ok()
.and_then(|banner| {
let owner: String = banner.object_props.attributed_to_link::<Id>().ok()?.into();
let owner = banner.object_props.attributed_to_link::<Id>().ok()?;
Media::save_remote(
&c.conn,
banner.object_props.url_string().ok()?,
Expand Down
5 changes: 1 addition & 4 deletions plume-models/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,7 @@ impl FromId<PlumeRocket> for Comment {
})?,
author_id: User::from_id(
c,
&{
let res: String = note.object_props.attributed_to_link::<Id>()?.into();
res
},
&note.object_props.attributed_to_link::<Id>()?,
None,
)
.map_err(|(_, e)| e)?
Expand Down
24 changes: 5 additions & 19 deletions plume-models/src/follows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,11 @@ impl FromId<PlumeRocket> for Follow {
}

fn from_activity(c: &PlumeRocket, follow: FollowAct) -> Result<Self> {
let actor = User::from_id(
c,
&{
let res: String = follow.follow_props.actor_link::<Id>()?.into();
res
},
None,
)
.map_err(|(_, e)| e)?;

let target = User::from_id(
c,
&{
let res: String = follow.follow_props.object_link::<Id>()?.into();
res
},
None,
)
.map_err(|(_, e)| e)?;
let actor =
User::from_id(c, &follow.follow_props.actor_link::<Id>()?, None).map_err(|(_, e)| e)?;

let target = User::from_id(c, &follow.follow_props.object_link::<Id>()?, None)
.map_err(|(_, e)| e)?;
Follow::accept_follow(&c.conn, &actor, &target, follow, actor.id, target.id)
}
}
Expand Down
26 changes: 6 additions & 20 deletions plume-models/src/likes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,12 @@ impl FromId<PlumeRocket> for Like {
let res = Like::insert(
&c.conn,
NewLike {
post_id: Post::from_id(
c,
&{
let res: String = act.like_props.object_link::<Id>()?.into();
res
},
None,
)
.map_err(|(_, e)| e)?
.id,
user_id: User::from_id(
c,
&{
let res: String = act.like_props.actor_link::<Id>()?.into();
res
},
None,
)
.map_err(|(_, e)| e)?
.id,
post_id: Post::from_id(c, &act.like_props.object_link::<Id>()?, None)
.map_err(|(_, e)| e)?
.id,
user_id: User::from_id(c, &act.like_props.actor_link::<Id>()?, None)
.map_err(|(_, e)| e)?
.id,
ap_url: act.object_props.id_string()?,
},
)?;
Expand Down
2 changes: 1 addition & 1 deletion plume-models/src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl FromId<PlumeRocket> for Post {
.attributed_to_link_vec::<Id>()?
.into_iter()
.fold((None, vec![]), |(blog, mut authors), link| {
let url: String = link.into();
let url = link;
match User::from_id(&c, &url, None) {
Ok(u) => {
authors.push(u);
Expand Down
26 changes: 6 additions & 20 deletions plume-models/src/reshares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,12 @@ impl FromId<PlumeRocket> for Reshare {
let res = Reshare::insert(
&c.conn,
NewReshare {
post_id: Post::from_id(
c,
&{
let res: String = act.announce_props.object_link::<Id>()?.into();
res
},
None,
)
.map_err(|(_, e)| e)?
.id,
user_id: User::from_id(
c,
&{
let res: String = act.announce_props.actor_link::<Id>()?.into();
res
},
None,
)
.map_err(|(_, e)| e)?
.id,
post_id: Post::from_id(c, &act.announce_props.object_link::<Id>()?, None)
.map_err(|(_, e)| e)?
.id,
user_id: User::from_id(c, &act.announce_props.actor_link::<Id>()?, None)
.map_err(|(_, e)| e)?
.id,
ap_url: act.object_props.id_string()?,
},
)?;
Expand Down
Loading

0 comments on commit 8c59c82

Please sign in to comment.