Skip to content

Commit

Permalink
Use Url type for ap_id fields in database (fixes #1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jan 26, 2021
1 parent b2d6b55 commit c51f750
Show file tree
Hide file tree
Showing 48 changed files with 433 additions and 243 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions crates/api/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ impl Perform for CreateCommunity {
}

// Double check for duplicate community actor_ids
let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string();
let actor_id = make_apub_endpoint(EndpointType::Community, &data.name);
let actor_id_cloned = actor_id.to_owned();
let community_dupe = blocking(context.pool(), move |conn| {
Community::read_from_apub_id(conn, &actor_id_cloned)
Community::read_from_apub_id(conn, &actor_id_cloned.into())
})
.await?;
if community_dupe.is_ok() {
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Perform for CreateCommunity {
deleted: None,
nsfw: data.nsfw,
updated: None,
actor_id: Some(actor_id),
actor_id: Some(actor_id.into()),
local: true,
private_key: Some(keypair.private_key),
public_key: Some(keypair.public_key),
Expand Down Expand Up @@ -506,9 +506,9 @@ impl Perform for FollowCommunity {
} else if data.follow {
// Dont actually add to the community followers here, because you need
// to wait for the accept
user.send_follow(&community.actor_id()?, context).await?;
user.send_follow(&community.actor_id(), context).await?;
} else {
user.send_unfollow(&community.actor_id()?, context).await?;
user.send_unfollow(&community.actor_id(), context).await?;
let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() {
return Err(APIError::err("community_follower_already_exists").into());
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Perform for Register {
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: Some(make_apub_endpoint(EndpointType::User, &data.username).to_string()),
actor_id: Some(make_apub_endpoint(EndpointType::User, &data.username).into()),
bio: None,
local: true,
private_key: Some(user_keypair.private_key),
Expand Down Expand Up @@ -247,7 +247,7 @@ impl Perform for Register {
deleted: None,
updated: None,
actor_id: Some(
make_apub_endpoint(EndpointType::Community, default_community_name).to_string(),
make_apub_endpoint(EndpointType::Community, default_community_name).into(),
),
local: true,
private_key: Some(main_community_keypair.private_key),
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/activities/receive/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) async fn receive_create_comment(
let note = NoteExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;

let comment = Comment::from_apub(&note, context, user.actor_id()?, request_counter).await?;
let comment = Comment::from_apub(&note, context, user.actor_id(), request_counter).await?;

let post_id = comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(crate) async fn receive_update_comment(
.context(location_info!())?;
let user = get_actor_as_user(&update, context, request_counter).await?;

let comment = Comment::from_apub(&note, context, user.actor_id()?, request_counter).await?;
let comment = Comment::from_apub(&note, context, user.actor_id(), request_counter).await?;

let comment_id = comment.id;
let post_id = comment.post_id;
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/activities/receive/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) async fn receive_remove_community(
.single_xsd_any_uri()
.context(location_info!())?;
let community = blocking(context.pool(), move |conn| {
Community::read_from_apub_id(conn, community_uri.as_str())
Community::read_from_apub_id(conn, &community_uri.into())
})
.await??;

Expand Down Expand Up @@ -137,7 +137,7 @@ pub(crate) async fn receive_undo_remove_community(
.single_xsd_any_uri()
.context(location_info!())?;
let community = blocking(context.pool(), move |conn| {
Community::read_from_apub_id(conn, community_uri.as_str())
Community::read_from_apub_id(conn, &community_uri.into())
})
.await??;

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/activities/receive/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) async fn receive_create_post(
let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;

let post = Post::from_apub(&page, context, user.actor_id()?, request_counter).await?;
let post = Post::from_apub(&page, context, user.actor_id(), request_counter).await?;

// Refetch the view
let post_id = post.id;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(crate) async fn receive_update_post(
let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;

let post = Post::from_apub(&page, context, user.actor_id()?, request_counter).await?;
let post = Post::from_apub(&page, context, user.actor_id(), request_counter).await?;

let post_id = post.id;
// Refetch the view
Expand Down
84 changes: 59 additions & 25 deletions crates/apub/src/activities/send/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl ApubObjectType for Comment {

let maa = collect_non_local_mentions(&self, &community, context).await?;

let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
let mut create = Create::new(
creator.actor_id.to_owned().into_inner(),
note.into_any_base()?,
);
create
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(CreateType::Create)?)
Expand Down Expand Up @@ -89,7 +92,10 @@ impl ApubObjectType for Comment {

let maa = collect_non_local_mentions(&self, &community, context).await?;

let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
let mut update = Update::new(
creator.actor_id.to_owned().into_inner(),
note.into_any_base()?,
);
update
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(UpdateType::Update)?)
Expand All @@ -113,12 +119,15 @@ impl ApubObjectType for Comment {
})
.await??;

let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut delete = Delete::new(
creator.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(delete, &creator, &community, context).await?;
Ok(())
Expand All @@ -139,20 +148,26 @@ impl ApubObjectType for Comment {
.await??;

// Generate a fake delete activity, with the correct object
let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut delete = Delete::new(
creator.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
let mut undo = Undo::new(
creator.actor_id.to_owned().into_inner(),
delete.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(undo, &creator, &community, context).await?;
Ok(())
Expand All @@ -168,12 +183,15 @@ impl ApubObjectType for Comment {
})
.await??;

let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut remove = Remove::new(
mod_.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
remove
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(remove, &mod_, &community, context).await?;
Ok(())
Expand All @@ -190,20 +208,26 @@ impl ApubObjectType for Comment {
.await??;

// Generate a fake delete activity, with the correct object
let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut remove = Remove::new(
mod_.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
remove
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

// Undo that fake activity
let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
let mut undo = Undo::new(
mod_.actor_id.to_owned().into_inner(),
remove.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(undo, &mod_, &community, context).await?;
Ok(())
Expand All @@ -222,12 +246,15 @@ impl ApubLikeableType for Comment {
})
.await??;

let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut like = Like::new(
creator.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
like
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(like, &creator, &community, context).await?;
Ok(())
Expand All @@ -243,12 +270,15 @@ impl ApubLikeableType for Comment {
})
.await??;

let mut dislike = Dislike::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut dislike = Dislike::new(
creator.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
dislike
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(dislike, &creator, &community, context).await?;
Ok(())
Expand All @@ -268,20 +298,26 @@ impl ApubLikeableType for Comment {
})
.await??;

let mut like = Like::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
let mut like = Like::new(
creator.actor_id.to_owned().into_inner(),
self.ap_id.to_owned().into_inner(),
);
like
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
let mut undo = Undo::new(
creator.actor_id.to_owned().into_inner(),
like.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()?]);
.set_many_ccs(vec![community.actor_id()]);

send_to_community(undo, &creator, &community, context).await?;
Ok(())
Expand Down Expand Up @@ -313,7 +349,7 @@ async fn collect_non_local_mentions(
context: &LemmyContext,
) -> Result<MentionsAndAddresses, LemmyError> {
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
let mut addressed_ccs = vec![community.actor_id()?, parent_creator.actor_id()?];
let mut addressed_ccs = vec![community.actor_id(), parent_creator.actor_id()];
// Note: dont include community inbox here, as we send to it separately with `send_to_community()`
let mut inboxes = vec![parent_creator.get_shared_inbox_url()?];

Expand Down Expand Up @@ -393,7 +429,5 @@ async fn fetch_webfinger_url(mention: &MentionData, client: &Client) -> Result<U
link
.href
.to_owned()
.map(|u| Url::parse(&u))
.transpose()?
.ok_or_else(|| anyhow!("No href found.").into())
}
Loading

0 comments on commit c51f750

Please sign in to comment.