12efe721cc
* Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
use rocket::response::{Flash, Redirect};
|
|
use rocket_i18n::I18n;
|
|
|
|
use plume_common::activity_pub::broadcast;
|
|
use plume_common::utils;
|
|
use plume_models::{
|
|
blogs::Blog, inbox::inbox, likes, posts::Post, users::User, Error, PlumeRocket,
|
|
};
|
|
use routes::errors::ErrorPage;
|
|
|
|
#[post("/~/<blog>/<slug>/like")]
|
|
pub fn create(
|
|
blog: String,
|
|
slug: String,
|
|
user: User,
|
|
rockets: PlumeRocket,
|
|
) -> Result<Redirect, ErrorPage> {
|
|
let conn = &*rockets.conn;
|
|
let b = Blog::find_by_fqn(&rockets, &blog)?;
|
|
let post = Post::find_by_slug(&*conn, &slug, b.id)?;
|
|
|
|
if !user.has_liked(&*conn, &post)? {
|
|
let like = likes::Like::insert(&*conn, likes::NewLike::new(&post, &user))?;
|
|
like.notify(&*conn)?;
|
|
|
|
let dest = User::one_by_instance(&*conn)?;
|
|
let act = like.to_activity(&*conn)?;
|
|
rockets.worker.execute(move || broadcast(&user, act, dest));
|
|
} else {
|
|
let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id)?;
|
|
let delete_act = like.build_undo(&*conn)?;
|
|
inbox(
|
|
&rockets,
|
|
serde_json::to_value(&delete_act).map_err(Error::from)?,
|
|
)?;
|
|
|
|
let dest = User::one_by_instance(&*conn)?;
|
|
rockets
|
|
.worker
|
|
.execute(move || broadcast(&user, delete_act, dest));
|
|
}
|
|
|
|
Ok(Redirect::to(
|
|
uri!(super::posts::details: blog = blog, slug = slug, responding_to = _),
|
|
))
|
|
}
|
|
|
|
#[post("/~/<blog>/<slug>/like", rank = 2)]
|
|
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
|
|
utils::requires_login(
|
|
&i18n!(i18n.catalog, "To like a post, you need to be logged in"),
|
|
uri!(create: blog = blog, slug = slug),
|
|
)
|
|
}
|