Delete likes

This commit is contained in:
Bat 2018-05-12 21:56:57 +01:00
parent b68e481b3f
commit b8aade1e12
8 changed files with 49 additions and 12 deletions

View file

@ -1,9 +1,11 @@
use chrono; use chrono;
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use models::posts::Post;
use models::users::User;
use schema::likes; use schema::likes;
#[derive(Queryable)] #[derive(Queryable, Identifiable)]
pub struct Like { pub struct Like {
pub id: i32, pub id: i32,
pub user_id: i32, pub user_id: i32,
@ -33,4 +35,17 @@ impl Like {
.expect("Error loading like by ID") .expect("Error loading like by ID")
.into_iter().nth(0) .into_iter().nth(0)
} }
pub fn for_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Like> {
likes::table.filter(likes::post_id.eq(post.id))
.filter(likes::user_id.eq(user.id))
.limit(1)
.load::<Like>(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();
}
} }

View file

@ -31,7 +31,7 @@ use schema::users;
pub const AUTH_COOKIE: &'static str = "user_id"; pub const AUTH_COOKIE: &'static str = "user_id";
#[derive(Queryable, Identifiable, Serialize)] #[derive(Queryable, Identifiable, Serialize, Clone)]
pub struct User { pub struct User {
pub id: i32, pub id: i32,
pub username: String, pub username: String,
@ -231,6 +231,17 @@ impl User {
users::table.filter(users::id.eq(any(follows))).load::<User>(conn).unwrap() users::table.filter(users::id.eq(any(follows))).load::<User>(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::<Like>(conn)
.expect("Couldn't load likes")
.len() > 0
}
pub fn get_keypair(&self) -> PKey<Private> { pub fn get_keypair(&self) -> PKey<Private> {
PKey::from_rsa(Rsa::private_key_from_pem(self.private_key.clone().unwrap().as_ref()).unwrap()).unwrap() PKey::from_rsa(Rsa::private_key_from_pem(self.private_key.clone().unwrap().as_ref()).unwrap()).unwrap()
} }

View file

@ -5,7 +5,6 @@ use serde_json;
use activity_pub::ActivityPub; use activity_pub::ActivityPub;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use activity_pub::object::Object;
use activity_pub::outbox::Outbox; use activity_pub::outbox::Outbox;
use db_conn::DbConn; use db_conn::DbConn;
use models::blog_authors::*; use models::blog_authors::*;

View file

@ -4,7 +4,6 @@ use rocket_contrib::Template;
use serde_json; use serde_json;
use BASE_URL; use BASE_URL;
use activity_pub::object::Object;
use db_conn::DbConn; use db_conn::DbConn;
use models::posts::Post; use models::posts::Post;
use models::users::User; use models::users::User;

View file

@ -10,12 +10,19 @@ use models::users::User;
#[get("/~/<blog>/<slug>/like")] #[get("/~/<blog>/<slug>/like")]
fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect {
let post = Post::find_by_slug(&*conn, slug.clone()).unwrap(); 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()) Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref())
} }

View file

@ -32,6 +32,7 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Temp
}) })
}).collect::<Vec<serde_json::Value>>(), }).collect::<Vec<serde_json::Value>>(),
"n_likes": post.get_likes(&*conn).len(), "n_likes": post.get_likes(&*conn).len(),
"has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
"account": user "account": user
})) }))
} }

View file

@ -6,7 +6,6 @@ use serde_json;
use activity_pub::{activity, activity_pub, ActivityPub, context}; use activity_pub::{activity, activity_pub, ActivityPub, context};
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use activity_pub::inbox::Inbox; use activity_pub::inbox::Inbox;
use activity_pub::object::Object;
use activity_pub::outbox::Outbox; use activity_pub::outbox::Outbox;
use db_conn::DbConn; use db_conn::DbConn;
use models::follows::*; use models::follows::*;

View file

@ -21,7 +21,13 @@
<p> <p>
{{ n_likes }} like{{ n_likes | pluralize }} {{ n_likes }} like{{ n_likes | pluralize }}
</p> </p>
<a class="button" href="like">Add yours</a> <a class="button" href="like">
{% if has_liked %}
I don't like this anymore
{% else %}
Add yours
{% endif %}
</a>
</div> </div>
<h2>Comments</h2> <h2>Comments</h2>