Add an endpoint to like posts
This commit is contained in:
parent
ac7b4441e8
commit
ee7dfee3ef
@ -46,7 +46,7 @@ impl Activity for Accept {
|
|||||||
fn serialize(&self) -> serde_json::Value {
|
fn serialize(&self) -> serde_json::Value {
|
||||||
json!({
|
json!({
|
||||||
"type": "Accept",
|
"type": "Accept",
|
||||||
"id": self.id,
|
"id": self.id,
|
||||||
"actor": self.actor,
|
"actor": self.actor,
|
||||||
"object": self.object,
|
"object": self.object,
|
||||||
"published": self.date.to_rfc3339()
|
"published": self.date.to_rfc3339()
|
||||||
@ -136,7 +136,43 @@ impl Activity for Follow {
|
|||||||
fn serialize(&self) -> serde_json::Value {
|
fn serialize(&self) -> serde_json::Value {
|
||||||
json!({
|
json!({
|
||||||
"type": "Follow",
|
"type": "Follow",
|
||||||
"id": self.id,
|
"id": self.id,
|
||||||
|
"actor": self.actor,
|
||||||
|
"object": self.object
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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,
|
"actor": self.actor,
|
||||||
"object": self.object
|
"object": self.object
|
||||||
})
|
})
|
||||||
|
@ -92,7 +92,9 @@ fn main() {
|
|||||||
routes::posts::create,
|
routes::posts::create,
|
||||||
|
|
||||||
routes::comments::new,
|
routes::comments::new,
|
||||||
routes::comments::create
|
routes::comments::create,
|
||||||
|
|
||||||
|
routes::likes::create
|
||||||
])
|
])
|
||||||
.manage(init_pool())
|
.manage(init_pool())
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
|
@ -8,6 +8,7 @@ use activity_pub::{PUBLIC_VISIBILTY, ap_url};
|
|||||||
use activity_pub::actor::Actor;
|
use activity_pub::actor::Actor;
|
||||||
use activity_pub::object::Object;
|
use activity_pub::object::Object;
|
||||||
use models::blogs::Blog;
|
use models::blogs::Blog;
|
||||||
|
use models::likes::Like;
|
||||||
use models::users::User;
|
use models::users::User;
|
||||||
use models::post_authors::PostAuthor;
|
use models::post_authors::PostAuthor;
|
||||||
use schema::posts;
|
use schema::posts;
|
||||||
@ -85,6 +86,13 @@ impl Post {
|
|||||||
.into_iter().nth(0).unwrap()
|
.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) {
|
pub fn update_ap_url(&self, conn: &PgConnection) {
|
||||||
if self.ap_url.len() == 0 {
|
if self.ap_url.len() == 0 {
|
||||||
diesel::update(self)
|
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 blogs;
|
||||||
pub mod comments;
|
pub mod comments;
|
||||||
pub mod instance;
|
pub mod instance;
|
||||||
|
pub mod likes;
|
||||||
pub mod posts;
|
pub mod posts;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
@ -31,7 +31,8 @@ fn details(blog: String, slug: String, conn: DbConn) -> Template {
|
|||||||
"content": c.content,
|
"content": c.content,
|
||||||
"author": c.get_author(&*conn)
|
"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>
|
||||||
<p>License: {{ post.license }}</p>
|
<p>License: {{ post.license }}</p>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
{{ n_likes }} like{{ n_likes | pluralize }}
|
||||||
|
</p>
|
||||||
|
<a href="like">Add yours</a>
|
||||||
|
|
||||||
<h2>Comments</h2>
|
<h2>Comments</h2>
|
||||||
{% for comment in comments %}
|
{% for comment in comments %}
|
||||||
<div id="comment-{{ comment.id }}">
|
<div id="comment-{{ comment.id }}">
|
||||||
|
Loading…
Reference in New Issue
Block a user