Add an endpoint to like posts

This commit is contained in:
Bat 2018-05-10 17:38:03 +01:00
parent ac7b4441e8
commit ee7dfee3ef
7 changed files with 79 additions and 4 deletions

View File

@ -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
})
}
}

View File

@ -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())

View File

@ -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
View 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())
}

View File

@ -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;

View File

@ -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()
})) }))
} }

View File

@ -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 }}">