Delete likes
This commit is contained in:
parent
b68e481b3f
commit
b8aade1e12
@ -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<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();
|
||||
}
|
||||
}
|
||||
|
@ -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::<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> {
|
||||
PKey::from_rsa(Rsa::private_key_from_pem(self.private_key.clone().unwrap().as_ref()).unwrap()).unwrap()
|
||||
}
|
||||
|
@ -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::*;
|
||||
|
@ -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;
|
||||
|
@ -10,12 +10,19 @@ use models::users::User;
|
||||
#[get("/~/<blog>/<slug>/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())
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Temp
|
||||
})
|
||||
}).collect::<Vec<serde_json::Value>>(),
|
||||
"n_likes": post.get_likes(&*conn).len(),
|
||||
"has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
|
||||
"account": user
|
||||
}))
|
||||
}
|
||||
|
@ -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::*;
|
||||
|
@ -21,7 +21,13 @@
|
||||
<p>
|
||||
{{ n_likes }} like{{ n_likes | pluralize }}
|
||||
</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>
|
||||
|
||||
<h2>Comments</h2>
|
||||
|
Loading…
Reference in New Issue
Block a user