Implement undo<dislike> for activitypub (#83)

Merge branch 'main' into undo-dislike

Implement undo<dislike> for activitypub

Fix some TODOs in activitypub code

Add post_read back in, do some cleanup

Add some delete functions back in

Merge branch 'main' into remove-dead-code

Replace body of unused db functions with unimplemented!()

Remove dead code

Remove remaining usages of unwrap() from activitypub code

Remove most usage of Option::unwrap() from activitypub code

Co-authored-by: Felix Ableitner <me@nutomic.com>
Reviewed-on: https://yerbamate.dev/LemmyNet/lemmy/pulls/83
This commit is contained in:
nutomic 2020-08-12 14:43:45 +00:00 committed by dessalines
parent bb4da4da02
commit a496d8af65
16 changed files with 184 additions and 123 deletions

View file

@ -178,12 +178,12 @@ impl Likeable<CommentLikeForm> for CommentLike {
.values(comment_like_form) .values(comment_like_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> { fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
use crate::schema::comment_like::dsl::*; use crate::schema::comment_like::dsl;
diesel::delete( diesel::delete(
comment_like dsl::comment_like
.filter(comment_id.eq(comment_like_form.comment_id)) .filter(dsl::comment_id.eq(comment_id))
.filter(user_id.eq(comment_like_form.user_id)), .filter(dsl::user_id.eq(user_id)),
) )
.execute(conn) .execute(conn)
} }
@ -388,7 +388,7 @@ mod tests {
let read_comment = Comment::read(&conn, inserted_comment.id).unwrap(); let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap(); let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap(); let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap(); let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap(); let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
Comment::delete(&conn, inserted_child_comment.id).unwrap(); Comment::delete(&conn, inserted_child_comment.id).unwrap();

View file

@ -688,7 +688,7 @@ mod tests {
read_comment_views_with_user[0].hot_rank = 0; read_comment_views_with_user[0].hot_rank = 0;
read_comment_views_with_user[0].hot_rank_active = 0; read_comment_views_with_user[0].hot_rank_active = 0;
let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap(); let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap(); let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
Post::delete(&conn, inserted_post.id).unwrap(); Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap(); Community::delete(&conn, inserted_community.id).unwrap();

View file

@ -80,7 +80,7 @@ pub trait Likeable<T> {
fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;
fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
where where
Self: Sized; Self: Sized;
} }

View file

@ -1,11 +1,4 @@
use crate::{ use crate::{naive_now, schema::{post, post_like, post_read, post_saved}, Crud, Likeable, Saveable, Readable};
naive_now,
schema::{post, post_like, post_read, post_saved},
Crud,
Likeable,
Readable,
Saveable,
};
use diesel::{dsl::*, result::Error, *}; use diesel::{dsl::*, result::Error, *};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::{ParseError, Url}; use url::{ParseError, Url};
@ -207,12 +200,12 @@ impl Likeable<PostLikeForm> for PostLike {
.values(post_like_form) .values(post_like_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> { fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
use crate::schema::post_like::dsl::*; use crate::schema::post_like::dsl;
diesel::delete( diesel::delete(
post_like dsl::post_like
.filter(post_id.eq(post_like_form.post_id)) .filter(dsl::post_id.eq(post_id))
.filter(user_id.eq(post_like_form.user_id)), .filter(dsl::user_id.eq(user_id)),
) )
.execute(conn) .execute(conn)
} }
@ -437,22 +430,22 @@ mod tests {
// Post Read // Post Read
let post_read_form = PostReadForm { let post_read_form = PostReadForm {
post_id: inserted_post.id, post_id: inserted_post.id,
user_id: inserted_user.id, user_id: inserted_user.id,
}; };
let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap(); let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
let expected_post_read = PostRead { let expected_post_read = PostRead {
id: inserted_post_read.id, id: inserted_post_read.id,
post_id: inserted_post.id, post_id: inserted_post.id,
user_id: inserted_user.id, user_id: inserted_user.id,
published: inserted_post_read.published, published: inserted_post_read.published,
}; };
let read_post = Post::read(&conn, inserted_post.id).unwrap(); let read_post = Post::read(&conn, inserted_post.id).unwrap();
let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap(); let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
let like_removed = PostLike::remove(&conn, &post_like_form).unwrap(); let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap(); let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap(); let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap(); let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();

View file

@ -494,12 +494,6 @@ mod tests {
score: 1, score: 1,
}; };
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
user_id: inserted_user.id,
score: 1,
};
let read_post_listings_with_user = PostQueryBuilder::create(&conn) let read_post_listings_with_user = PostQueryBuilder::create(&conn)
.listing_type(ListingType::Community) .listing_type(ListingType::Community)
.sort(&SortType::New) .sort(&SortType::New)
@ -618,7 +612,7 @@ mod tests {
community_local: true, community_local: true,
}; };
let like_removed = PostLike::remove(&conn, &post_like_form).unwrap(); let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap(); let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap(); Community::delete(&conn, inserted_community.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap(); User_::delete(&conn, inserted_user.id).unwrap();

View file

@ -645,8 +645,11 @@ impl Perform for CreateCommentLike {
}; };
// Remove any likes first // Remove any likes first
let like_form2 = like_form.clone(); let user_id = user.id;
blocking(pool, move |conn| CommentLike::remove(conn, &like_form2)).await??; blocking(pool, move |conn| {
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
// Only add the like if the score isnt 0 // Only add the like if the score isnt 0
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1); let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);

View file

@ -430,8 +430,8 @@ impl Perform for CreatePostLike {
}; };
// Remove any likes first // Remove any likes first
let like_form2 = like_form.clone(); let user_id = user.id;
blocking(pool, move |conn| PostLike::remove(conn, &like_form2)).await??; blocking(pool, move |conn| PostLike::remove(conn, user_id, post_id)).await??;
// Only add the like if the score isnt 0 // Only add the like if the score isnt 0
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1); let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);

View file

@ -297,7 +297,7 @@ impl ApubObjectType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DeleteType::Delete)?) .set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -331,7 +331,7 @@ impl ApubObjectType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DeleteType::Delete)?) .set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity // 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(), delete.into_any_base()?);
@ -339,7 +339,7 @@ impl ApubObjectType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -372,7 +372,7 @@ impl ApubObjectType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(RemoveType::Remove)?) .set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&mod_, &mod_,
@ -406,7 +406,7 @@ impl ApubObjectType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(RemoveType::Remove)?) .set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity // 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(), remove.into_any_base()?);
@ -414,7 +414,7 @@ impl ApubObjectType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&mod_, &mod_,
@ -450,7 +450,7 @@ impl ApubLikeableType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -483,7 +483,7 @@ impl ApubLikeableType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DislikeType::Dislike)?) .set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -516,7 +516,7 @@ impl ApubLikeableType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DislikeType::Dislike)?) .set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity // 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(), like.into_any_base()?);
@ -524,7 +524,7 @@ impl ApubLikeableType for Comment {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -540,7 +540,7 @@ impl ApubLikeableType for Comment {
} }
struct MentionsAndAddresses { struct MentionsAndAddresses {
addressed_ccs: Vec<String>, addressed_ccs: Vec<Url>,
inboxes: Vec<Url>, inboxes: Vec<Url>,
tags: Vec<Mention>, tags: Vec<Mention>,
} }
@ -564,7 +564,7 @@ async fn collect_non_local_mentions_and_addresses(
client: &Client, client: &Client,
pool: &DbPool, pool: &DbPool,
) -> Result<MentionsAndAddresses, LemmyError> { ) -> Result<MentionsAndAddresses, LemmyError> {
let mut addressed_ccs = vec![community.get_followers_url()]; let mut addressed_ccs = vec![community.get_followers_url()?];
// Add the mention tag // Add the mention tag
let mut tags = Vec::new(); let mut tags = Vec::new();
@ -581,7 +581,7 @@ async fn collect_non_local_mentions_and_addresses(
// TODO should it be fetching it every time? // TODO should it be fetching it every time?
if let Ok(actor_id) = fetch_webfinger_url(mention, client).await { if let Ok(actor_id) = fetch_webfinger_url(mention, client).await {
debug!("mention actor_id: {}", actor_id); debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_owned().to_string()); addressed_ccs.push(actor_id.to_owned().to_string().parse()?);
let mention_user = get_or_fetch_and_upsert_user(&actor_id, client, pool).await?; let mention_user = get_or_fetch_and_upsert_user(&actor_id, client, pool).await?;
let shared_inbox = mention_user.get_shared_inbox_url()?; let shared_inbox = mention_user.get_shared_inbox_url()?;

View file

@ -95,7 +95,7 @@ impl ToApub for Community {
ap_actor ap_actor
.set_preferred_username(self.title.to_owned()) .set_preferred_username(self.title.to_owned())
.set_outbox(self.get_outbox_url()?) .set_outbox(self.get_outbox_url()?)
.set_followers(self.get_followers_url().parse()?) .set_followers(self.get_followers_url()?)
.set_following(self.get_following_url().parse()?) .set_following(self.get_following_url().parse()?)
.set_liked(self.get_liked_url().parse()?) .set_liked(self.get_liked_url().parse()?)
.set_endpoints(Endpoints { .set_endpoints(Endpoints {
@ -174,7 +174,7 @@ impl ActorType for Community {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DeleteType::Delete)?) .set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![self.get_followers_url()]); .set_many_ccs(vec![self.get_followers_url()?]);
insert_activity(self.creator_id, delete.clone(), true, pool).await?; insert_activity(self.creator_id, delete.clone(), true, pool).await?;
@ -200,16 +200,14 @@ impl ActorType for Community {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DeleteType::Delete)?) .set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![self.get_followers_url()]); .set_many_ccs(vec![self.get_followers_url()?]);
// TODO
// 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(), delete.into_any_base()?);
undo undo
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![self.get_followers_url()]); .set_many_ccs(vec![self.get_followers_url()?]);
insert_activity(self.creator_id, undo.clone(), true, pool).await?; insert_activity(self.creator_id, undo.clone(), true, pool).await?;
@ -235,7 +233,7 @@ impl ActorType for Community {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(RemoveType::Remove)?) .set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![self.get_followers_url()]); .set_many_ccs(vec![self.get_followers_url()?]);
insert_activity(mod_.id, remove.clone(), true, pool).await?; insert_activity(mod_.id, remove.clone(), true, pool).await?;
@ -261,7 +259,7 @@ impl ActorType for Community {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(RemoveType::Remove)?) .set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![self.get_followers_url()]); .set_many_ccs(vec![self.get_followers_url()?]);
// Undo that fake activity // 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(), remove.into_any_base()?);
@ -269,7 +267,7 @@ impl ActorType for Community {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![self.get_followers_url()]); .set_many_ccs(vec![self.get_followers_url()?]);
insert_activity(mod_.id, undo.clone(), true, pool).await?; insert_activity(mod_.id, undo.clone(), true, pool).await?;
@ -474,8 +472,7 @@ pub async fn get_apub_community_followers(
let mut collection = UnorderedCollection::new(); let mut collection = UnorderedCollection::new();
collection collection
.set_context(context()) .set_context(context())
// TODO: this needs its own ID .set_id(community.get_followers_url()?)
.set_id(community.actor_id.parse()?)
.set_total_items(community_followers.len() as u64); .set_total_items(community_followers.len() as u64);
Ok(create_apub_response(&collection)) Ok(create_apub_response(&collection))
} }
@ -522,7 +519,7 @@ pub async fn do_announce(
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(AnnounceType::Announce)?) .set_id(generate_activity_id(AnnounceType::Announce)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
insert_activity(community.creator_id, announce.clone(), true, pool).await?; insert_activity(community.creator_id, announce.clone(), true, pool).await?;

View file

@ -146,8 +146,6 @@ pub async fn search_by_apub_id(
let community = get_or_fetch_and_upsert_community(community_uri, client, pool).await?; let community = get_or_fetch_and_upsert_community(community_uri, client, pool).await?;
// TODO Maybe at some point in the future, fetch all the history of a community
// fetch_community_outbox(&c, conn)?;
response.communities = vec![ response.communities = vec![
blocking(pool, move |conn| { blocking(pool, move |conn| {
CommunityView::read(conn, community.id, None) CommunityView::read(conn, community.id, None)
@ -166,24 +164,8 @@ pub async fn search_by_apub_id(
response response
} }
SearchAcceptedObjects::Comment(c) => { SearchAcceptedObjects::Comment(c) => {
let post_url = c
.in_reply_to()
.as_ref()
.context(location_info!())?
.as_many()
.context(location_info!())?;
// TODO: also fetch parent comments if any
let x = post_url
.first()
.context(location_info!())?
.as_xsd_any_uri()
.context(location_info!())?;
let post = fetch_remote_object(client, x).await?;
let post_form = PostForm::from_apub(&post, client, pool, Some(query_url.clone())).await?;
let comment_form = CommentForm::from_apub(&c, client, pool, Some(query_url)).await?; let comment_form = CommentForm::from_apub(&c, client, pool, Some(query_url)).await?;
blocking(pool, move |conn| upsert_post(&post_form, conn)).await??;
let c = blocking(pool, move |conn| upsert_comment(&comment_form, conn)).await??; let c = blocking(pool, move |conn| upsert_comment(&comment_form, conn)).await??;
response.comments = response.comments =
vec![blocking(pool, move |conn| CommentView::read(conn, c.id, None)).await??]; vec![blocking(pool, move |conn| CommentView::read(conn, c.id, None)).await??];

View file

@ -72,8 +72,9 @@ async fn receive_dislike_post(
user_id: user.id, user_id: user.id,
score: -1, score: -1,
}; };
let user_id = user.id;
blocking(pool, move |conn| { blocking(pool, move |conn| {
PostLike::remove(conn, &like_form)?; PostLike::remove(conn, user_id, post_id)?;
PostLike::like(conn, &like_form) PostLike::like(conn, &like_form)
}) })
.await??; .await??;
@ -121,8 +122,9 @@ async fn receive_dislike_comment(
user_id: user.id, user_id: user.id,
score: -1, score: -1,
}; };
let user_id = user.id;
blocking(pool, move |conn| { blocking(pool, move |conn| {
CommentLike::remove(conn, &like_form)?; CommentLike::remove(conn, user_id, comment_id)?;
CommentLike::like(conn, &like_form) CommentLike::like(conn, &like_form)
}) })
.await??; .await??;

View file

@ -66,8 +66,9 @@ async fn receive_like_post(
user_id: user.id, user_id: user.id,
score: 1, score: 1,
}; };
let user_id = user.id;
blocking(pool, move |conn| { blocking(pool, move |conn| {
PostLike::remove(conn, &like_form)?; PostLike::remove(conn, user_id, post_id)?;
PostLike::like(conn, &like_form) PostLike::like(conn, &like_form)
}) })
.await??; .await??;
@ -109,8 +110,9 @@ async fn receive_like_comment(
user_id: user.id, user_id: user.id,
score: 1, score: 1,
}; };
let user_id = user.id;
blocking(pool, move |conn| { blocking(pool, move |conn| {
CommentLike::remove(conn, &like_form)?; CommentLike::remove(conn, user_id, comment_id)?;
CommentLike::like(conn, &like_form) CommentLike::like(conn, &like_form)
}) })
.await??; .await??;

View file

@ -30,12 +30,12 @@ use activitystreams::{
use actix_web::{client::Client, HttpResponse}; use actix_web::{client::Client, HttpResponse};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db::{ use lemmy_db::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, comment::{Comment, CommentForm, CommentLike},
comment_view::CommentView, comment_view::CommentView,
community::{Community, CommunityForm}, community::{Community, CommunityForm},
community_view::CommunityView, community_view::CommunityView,
naive_now, naive_now,
post::{Post, PostForm, PostLike, PostLikeForm}, post::{Post, PostForm, PostLike},
post_view::PostView, post_view::PostView,
Crud, Crud,
Likeable, Likeable,
@ -145,21 +145,23 @@ async fn receive_undo_like(
async fn receive_undo_dislike( async fn receive_undo_dislike(
undo: Undo, undo: Undo,
_client: &Client, client: &Client,
_pool: &DbPool, pool: &DbPool,
_chat_server: ChatServerParam, chat_server: ChatServerParam,
) -> Result<HttpResponse, LemmyError> { ) -> Result<HttpResponse, LemmyError> {
let dislike = Dislike::from_any_base(undo.object().to_owned().one().context(location_info!())?)? let dislike = Dislike::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?; .context(location_info!())?;
check_is_undo_valid(&undo, &dislike)?; check_is_undo_valid(&undo, &dislike)?;
// TODO: need to implement Undo<Dislike>
let type_ = dislike let type_ = dislike
.object() .object()
.as_single_kind_str() .as_single_kind_str()
.context(location_info!())?; .context(location_info!())?;
Err(anyhow!("Undo Delete type {} not supported", type_).into()) match type_ {
"Note" => receive_undo_dislike_comment(undo, &dislike, client, pool, chat_server).await,
"Page" => receive_undo_dislike_post(undo, &dislike, client, pool, chat_server).await,
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
}
} }
async fn receive_undo_delete_comment( async fn receive_undo_delete_comment(
@ -543,13 +545,11 @@ async fn receive_undo_like_comment(
.await? .await?
.id; .id;
let like_form = CommentLikeForm { let user_id = user.id;
comment_id, blocking(pool, move |conn| {
post_id: comment.post_id, CommentLike::remove(conn, user_id, comment_id)
user_id: user.id, })
score: 0, .await??;
};
blocking(pool, move |conn| CommentLike::remove(conn, &like_form)).await??;
// Refetch the view // Refetch the view
let comment_view = let comment_view =
@ -590,12 +590,100 @@ async fn receive_undo_like_post(
.await? .await?
.id; .id;
let like_form = PostLikeForm { let user_id = user.id;
post_id, blocking(pool, move |conn| PostLike::remove(conn, user_id, post_id)).await??;
user_id: user.id,
score: 1, // Refetch the view
}; let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
blocking(pool, move |conn| PostLike::remove(conn, &like_form)).await??;
let res = PostResponse { post: post_view };
chat_server.do_send(SendPost {
op: UserOperation::CreatePostLike,
post: res,
my_id: None,
});
announce_if_community_is_local(undo, &user, client, pool).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_dislike_comment(
undo: Undo,
dislike: &Dislike,
client: &Client,
pool: &DbPool,
chat_server: ChatServerParam,
) -> Result<HttpResponse, LemmyError> {
let user = get_user_from_activity(dislike, client, pool).await?;
let note = Note::from_any_base(
dislike
.object()
.to_owned()
.one()
.context(location_info!())?,
)?
.context(location_info!())?;
let comment = CommentForm::from_apub(&note, client, pool, None).await?;
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
.await?
.id;
let user_id = user.id;
blocking(pool, move |conn| {
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
// Refetch the view
let comment_view =
blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
let res = CommentResponse {
comment: comment_view,
recipient_ids,
form_id: None,
};
chat_server.do_send(SendComment {
op: UserOperation::CreateCommentLike,
comment: res,
my_id: None,
});
announce_if_community_is_local(undo, &user, client, pool).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_dislike_post(
undo: Undo,
dislike: &Dislike,
client: &Client,
pool: &DbPool,
chat_server: ChatServerParam,
) -> Result<HttpResponse, LemmyError> {
let user = get_user_from_activity(dislike, client, pool).await?;
let page = PageExt::from_any_base(
dislike
.object()
.to_owned()
.one()
.context(location_info!())?,
)?
.context(location_info!())?;
let post = PostForm::from_apub(&page, client, pool, None).await?;
let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
.await?
.id;
let user_id = user.id;
blocking(pool, move |conn| PostLike::remove(conn, user_id, post_id)).await??;
// Refetch the view // Refetch the view
let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??; let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;

View file

@ -328,8 +328,8 @@ pub trait ActorType {
Url::parse(&format!("{}/outbox", &self.actor_id_str())) Url::parse(&format!("{}/outbox", &self.actor_id_str()))
} }
fn get_followers_url(&self) -> String { fn get_followers_url(&self) -> Result<Url, ParseError> {
format!("{}/followers", &self.actor_id_str()) Url::parse(&format!("{}/followers", &self.actor_id_str()))
} }
fn get_following_url(&self) -> String { fn get_following_url(&self) -> String {

View file

@ -316,7 +316,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(CreateType::Create)?) .set_id(generate_activity_id(CreateType::Create)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
creator, creator,
@ -347,7 +347,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UpdateType::Update)?) .set_id(generate_activity_id(UpdateType::Update)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
creator, creator,
@ -377,7 +377,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DeleteType::Delete)?) .set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
creator, creator,
@ -407,7 +407,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DeleteType::Delete)?) .set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity // 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(), delete.into_any_base()?);
@ -415,7 +415,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
creator, creator,
@ -445,7 +445,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(RemoveType::Remove)?) .set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
mod_, mod_,
@ -475,7 +475,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(RemoveType::Remove)?) .set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity // 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(), remove.into_any_base()?);
@ -483,7 +483,7 @@ impl ApubObjectType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
mod_, mod_,
@ -516,7 +516,7 @@ impl ApubLikeableType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -546,7 +546,7 @@ impl ApubLikeableType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(DislikeType::Dislike)?) .set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,
@ -576,7 +576,7 @@ impl ApubLikeableType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(LikeType::Like)?) .set_id(generate_activity_id(LikeType::Like)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity // 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(), like.into_any_base()?);
@ -584,7 +584,7 @@ impl ApubLikeableType for Post {
.set_context(context()) .set_context(context())
.set_id(generate_activity_id(UndoType::Undo)?) .set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public()) .set_to(public())
.set_many_ccs(vec![community.get_followers_url()]); .set_many_ccs(vec![community.get_followers_url()?]);
send_activity_to_community( send_activity_to_community(
&creator, &creator,

View file

@ -80,7 +80,7 @@ impl ToApub for User_ {
let mut ap_actor = ApActor::new(self.get_inbox_url()?, person); let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
ap_actor ap_actor
.set_outbox(self.get_outbox_url()?) .set_outbox(self.get_outbox_url()?)
.set_followers(self.get_followers_url().parse()?) .set_followers(self.get_followers_url()?)
.set_following(self.get_following_url().parse()?) .set_following(self.get_following_url().parse()?)
.set_liked(self.get_liked_url().parse()?) .set_liked(self.get_liked_url().parse()?)
.set_endpoints(Endpoints { .set_endpoints(Endpoints {