From b8aade1e1231fed642cb44ad8cb80fba2461515f Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 12 May 2018 21:56:57 +0100 Subject: [PATCH] Delete likes --- src/models/likes.rs | 17 ++++++++++++++++- src/models/users.rs | 13 ++++++++++++- src/routes/blogs.rs | 1 - src/routes/instance.rs | 1 - src/routes/likes.rs | 19 +++++++++++++------ src/routes/posts.rs | 1 + src/routes/user.rs | 1 - templates/posts/details.tera | 8 +++++++- 8 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/models/likes.rs b/src/models/likes.rs index 513a22f8..2591d584 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -1,9 +1,11 @@ use chrono; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; +use models::posts::Post; +use models::users::User; use schema::likes; -#[derive(Queryable)] +#[derive(Queryable, Identifiable)] pub struct Like { pub id: i32, pub user_id: i32, @@ -33,4 +35,17 @@ impl Like { .expect("Error loading like by ID") .into_iter().nth(0) } + + pub fn for_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option { + likes::table.filter(likes::post_id.eq(post.id)) + .filter(likes::user_id.eq(user.id)) + .limit(1) + .load::(conn) + .expect("Error loading like for user and post") + .into_iter().nth(0) + } + + pub fn delete(&self, conn: &PgConnection) { + diesel::delete(self).execute(conn).unwrap(); + } } diff --git a/src/models/users.rs b/src/models/users.rs index 5d38f616..5580d5e6 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -31,7 +31,7 @@ use schema::users; pub const AUTH_COOKIE: &'static str = "user_id"; -#[derive(Queryable, Identifiable, Serialize)] +#[derive(Queryable, Identifiable, Serialize, Clone)] pub struct User { pub id: i32, pub username: String, @@ -231,6 +231,17 @@ impl User { users::table.filter(users::id.eq(any(follows))).load::(conn).unwrap() } + pub fn has_liked(&self, conn: &PgConnection, post: &Post) -> bool { + use schema::likes; + use models::likes::Like; + likes::table + .filter(likes::post_id.eq(post.id)) + .filter(likes::user_id.eq(self.id)) + .load::(conn) + .expect("Couldn't load likes") + .len() > 0 + } + pub fn get_keypair(&self) -> PKey { PKey::from_rsa(Rsa::private_key_from_pem(self.private_key.clone().unwrap().as_ref()).unwrap()).unwrap() } diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index b81d2bef..3b6ad886 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -5,7 +5,6 @@ use serde_json; use activity_pub::ActivityPub; use activity_pub::actor::Actor; -use activity_pub::object::Object; use activity_pub::outbox::Outbox; use db_conn::DbConn; use models::blog_authors::*; diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 8925fc7c..f6ff3841 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -4,7 +4,6 @@ use rocket_contrib::Template; use serde_json; use BASE_URL; -use activity_pub::object::Object; use db_conn::DbConn; use models::posts::Post; use models::users::User; diff --git a/src/routes/likes.rs b/src/routes/likes.rs index ff558cfd..22b8a363 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -10,12 +10,19 @@ use models::users::User; #[get("/~///like")] fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { let post = Post::find_by_slug(&*conn, slug.clone()).unwrap(); - likes::Like::insert(&*conn, likes::NewLike { - post_id: post.id, - user_id: user.id - }); - let act = Like::new(&user, &post, &*conn); - broadcast(&*conn, &user, act, user.get_followers(&*conn)); + if !user.has_liked(&*conn, &post) { + likes::Like::insert(&*conn, likes::NewLike { + post_id: post.id, + user_id: user.id + }); + let act = Like::new(&user, &post, &*conn); + broadcast(&*conn, &user, act, user.get_followers(&*conn)); + } else { + let like = likes::Like::for_user_on_post(&*conn, &user, &post).unwrap(); + like.delete(&*conn); + // TODO: send Delete to AP + } + Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref()) } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index e98d9e39..6b3b9bdd 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -32,6 +32,7 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option) -> Temp }) }).collect::>(), "n_likes": post.get_likes(&*conn).len(), + "has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false), "account": user })) } diff --git a/src/routes/user.rs b/src/routes/user.rs index 1b31b314..833d29a3 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -6,7 +6,6 @@ use serde_json; use activity_pub::{activity, activity_pub, ActivityPub, context}; use activity_pub::actor::Actor; use activity_pub::inbox::Inbox; -use activity_pub::object::Object; use activity_pub::outbox::Outbox; use db_conn::DbConn; use models::follows::*; diff --git a/templates/posts/details.tera b/templates/posts/details.tera index 412e5a70..278bffb9 100644 --- a/templates/posts/details.tera +++ b/templates/posts/details.tera @@ -21,7 +21,13 @@

{{ n_likes }} like{{ n_likes | pluralize }}

- Add yours + + {% if has_liked %} + I don't like this anymore + {% else %} + Add yours + {% endif %} +

Comments