2019-03-20 17:56:17 +01:00
|
|
|
use rocket::response::{Flash, Redirect};
|
2018-12-06 18:54:16 +01:00
|
|
|
use rocket_i18n::I18n;
|
2018-05-10 18:38:03 +02:00
|
|
|
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::routes::errors::ErrorPage;
|
2019-04-17 19:31:47 +02:00
|
|
|
use plume_common::activity_pub::broadcast;
|
2019-03-20 17:56:17 +01:00
|
|
|
use plume_common::utils;
|
2019-04-17 19:31:47 +02:00
|
|
|
use plume_models::{
|
2019-10-07 19:08:20 +02:00
|
|
|
blogs::Blog, inbox::inbox, likes, posts::Post, timeline::*, users::User, Error, PlumeRocket,
|
2021-01-11 21:27:52 +01:00
|
|
|
CONFIG,
|
2019-04-17 19:31:47 +02:00
|
|
|
};
|
2018-05-10 18:38:03 +02:00
|
|
|
|
2018-07-11 21:27:47 +02:00
|
|
|
#[post("/~/<blog>/<slug>/like")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn create(
|
|
|
|
blog: String,
|
|
|
|
slug: String,
|
|
|
|
user: User,
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-03-20 17:56:17 +01:00
|
|
|
) -> Result<Redirect, ErrorPage> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let conn = &*rockets.conn;
|
|
|
|
let b = Blog::find_by_fqn(&rockets, &blog)?;
|
2018-11-26 10:21:52 +01:00
|
|
|
let post = Post::find_by_slug(&*conn, &slug, b.id)?;
|
2018-05-10 18:38:03 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
if !user.has_liked(&*conn, &post)? {
|
2019-03-20 17:56:17 +01:00
|
|
|
let like = likes::Like::insert(&*conn, likes::NewLike::new(&post, &user))?;
|
2018-12-29 09:36:07 +01:00
|
|
|
like.notify(&*conn)?;
|
2018-05-19 00:04:30 +02:00
|
|
|
|
2019-10-07 19:08:20 +02:00
|
|
|
Timeline::add_to_all_timelines(&rockets, &post, Kind::Like(&user))?;
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
let dest = User::one_by_instance(&*conn)?;
|
|
|
|
let act = like.to_activity(&*conn)?;
|
2021-01-11 21:27:52 +01:00
|
|
|
rockets
|
|
|
|
.worker
|
|
|
|
.execute(move || broadcast(&user, act, dest, CONFIG.proxy().cloned()));
|
2018-05-12 22:56:57 +02:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
let like = likes::Like::find_by_user_on_post(&*conn, user.id, post.id)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
let delete_act = like.build_undo(&*conn)?;
|
|
|
|
inbox(
|
|
|
|
&rockets,
|
|
|
|
serde_json::to_value(&delete_act).map_err(Error::from)?,
|
|
|
|
)?;
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
let dest = User::one_by_instance(&*conn)?;
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets
|
|
|
|
.worker
|
2021-01-11 21:27:52 +01:00
|
|
|
.execute(move || broadcast(&user, delete_act, dest, CONFIG.proxy().cloned()));
|
2018-05-12 22:56:57 +02:00
|
|
|
}
|
2018-05-13 12:44:05 +02:00
|
|
|
|
2021-01-15 17:13:45 +01:00
|
|
|
Ok(Redirect::to(uri!(
|
|
|
|
super::posts::details: blog = blog,
|
|
|
|
slug = slug,
|
|
|
|
responding_to = _
|
|
|
|
)))
|
2018-05-10 18:38:03 +02:00
|
|
|
}
|
2018-06-04 21:57:03 +02:00
|
|
|
|
2018-07-11 21:27:47 +02:00
|
|
|
#[post("/~/<blog>/<slug>/like", rank = 2)]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
|
2018-09-08 01:11:27 +02:00
|
|
|
utils::requires_login(
|
2019-04-06 17:41:57 +02:00
|
|
|
&i18n!(i18n.catalog, "To like a post, you need to be logged in"),
|
2019-03-20 17:56:17 +01:00
|
|
|
uri!(create: blog = blog, slug = slug),
|
2018-09-08 01:11:27 +02:00
|
|
|
)
|
2018-06-04 21:57:03 +02:00
|
|
|
}
|