Add an endpoint to like posts
This commit is contained in:
parent
ac7b4441e8
commit
ee7dfee3ef
@ -142,3 +142,39 @@ impl Activity for Follow {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Like {
|
||||
id: String,
|
||||
actor: serde_json::Value,
|
||||
object: serde_json::Value
|
||||
}
|
||||
|
||||
impl Like {
|
||||
pub fn new<A: Actor, B: Object>(actor: &A, obj: &B, conn: &PgConnection) -> Like {
|
||||
Like {
|
||||
id: format!("{}/like/{}", actor.compute_id(conn), obj.compute_id(conn)),
|
||||
actor: serde_json::Value::String(actor.compute_id(conn)),
|
||||
object: serde_json::Value::String(obj.compute_id(conn))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Activity for Like {
|
||||
fn get_id(&self) -> String {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
fn get_type(&self) -> String {
|
||||
"Follow".to_string()
|
||||
}
|
||||
|
||||
fn serialize(&self) -> serde_json::Value {
|
||||
json!({
|
||||
"type": "Follow",
|
||||
"id": self.id,
|
||||
"actor": self.actor,
|
||||
"object": self.object
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +92,9 @@ fn main() {
|
||||
routes::posts::create,
|
||||
|
||||
routes::comments::new,
|
||||
routes::comments::create
|
||||
routes::comments::create,
|
||||
|
||||
routes::likes::create
|
||||
])
|
||||
.manage(init_pool())
|
||||
.attach(Template::fairing())
|
||||
|
@ -8,6 +8,7 @@ use activity_pub::{PUBLIC_VISIBILTY, ap_url};
|
||||
use activity_pub::actor::Actor;
|
||||
use activity_pub::object::Object;
|
||||
use models::blogs::Blog;
|
||||
use models::likes::Like;
|
||||
use models::users::User;
|
||||
use models::post_authors::PostAuthor;
|
||||
use schema::posts;
|
||||
@ -85,6 +86,13 @@ impl Post {
|
||||
.into_iter().nth(0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_likes(&self, conn: &PgConnection) -> Vec<Like> {
|
||||
use schema::likes;
|
||||
likes::table.filter(likes::post_id.eq(self.id))
|
||||
.load::<Like>(conn)
|
||||
.expect("Couldn't load likes associted to post")
|
||||
}
|
||||
|
||||
pub fn update_ap_url(&self, conn: &PgConnection) {
|
||||
if self.ap_url.len() == 0 {
|
||||
diesel::update(self)
|
||||
|
21
src/routes/likes.rs
Normal file
21
src/routes/likes.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use rocket::response::Redirect;
|
||||
|
||||
use activity_pub::activity::Like;
|
||||
use activity_pub::outbox::broadcast;
|
||||
use db_conn::DbConn;
|
||||
use models::likes;
|
||||
use models::posts::Post;
|
||||
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));
|
||||
|
||||
Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref())
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
pub mod blogs;
|
||||
pub mod comments;
|
||||
pub mod instance;
|
||||
pub mod likes;
|
||||
pub mod posts;
|
||||
pub mod session;
|
||||
pub mod user;
|
||||
|
@ -31,7 +31,8 @@ fn details(blog: String, slug: String, conn: DbConn) -> Template {
|
||||
"content": c.content,
|
||||
"author": c.get_author(&*conn)
|
||||
})
|
||||
}).collect::<Vec<serde_json::Value>>()
|
||||
}).collect::<Vec<serde_json::Value>>(),
|
||||
"n_likes": post.get_likes(&*conn).len()
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,12 @@
|
||||
</p>
|
||||
<p>License: {{ post.license }}</p>
|
||||
<hr>
|
||||
|
||||
<p>
|
||||
{{ n_likes }} like{{ n_likes | pluralize }}
|
||||
</p>
|
||||
<a href="like">Add yours</a>
|
||||
|
||||
<h2>Comments</h2>
|
||||
{% for comment in comments %}
|
||||
<div id="comment-{{ comment.id }}">
|
||||
|
Loading…
Reference in New Issue
Block a user