]> Untitled Git - lemmy.git/commitdiff
1/3rd done with post likes
authorDessalines <tyhou13@gmx.com>
Tue, 28 Apr 2020 02:46:09 +0000 (22:46 -0400)
committerDessalines <tyhou13@gmx.com>
Tue, 28 Apr 2020 02:47:26 +0000 (22:47 -0400)
server/Cargo.lock
server/src/api/mod.rs
server/src/api/post.rs
server/src/apub/mod.rs
server/src/apub/post.rs

index e6bac6374402e0a453f4e6d194dc254393d046ae..56e81b4763ea22c2e18e916c230612cf21483242 100644 (file)
@@ -2814,18 +2814,18 @@ dependencies = [
 
 [[package]]
 name = "thiserror"
-version = "1.0.15"
+version = "1.0.16"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "54b3d3d2ff68104100ab257bb6bb0cb26c901abe4bd4ba15961f3bf867924012"
+checksum = "d12a1dae4add0f0d568eebc7bf142f145ba1aa2544cafb195c76f0f409091b60"
 dependencies = [
  "thiserror-impl",
 ]
 
 [[package]]
 name = "thiserror-impl"
-version = "1.0.15"
+version = "1.0.16"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ca972988113b7715266f91250ddb98070d033c62a011fa0fcc57434a649310dd"
+checksum = "3f34e0c1caaa462fd840ec6b768946ea1e7842620d94fe29d5b847138f521269"
 dependencies = [
  "proc-macro2",
  "quote",
index 70ff2bfec195910744720e897d6f62e6b38e2c15..8bc03feea5e250d824cae64a5a1e69f59776bce7 100644 (file)
@@ -25,7 +25,7 @@ use crate::{
 use crate::apub::{
   fetcher::search_by_apub_id,
   signatures::generate_actor_keypair,
-  {make_apub_endpoint, ActorType, ApubObjectType, EndpointType},
+  {make_apub_endpoint, ActorType, ApubLikeableType, ApubObjectType, EndpointType},
 };
 use crate::settings::Settings;
 use crate::websocket::{
index 5be227d8d364991915c83e6dc892314d519ddf57..306365facfa6b9dd8b6209db9f8069bb7fc2cdcc 100644 (file)
@@ -169,12 +169,13 @@ impl Perform for Oper<CreatePost> {
       score: 1,
     };
 
-    // Only add the like if the score isnt 0
     let _inserted_like = match PostLike::like(&conn, &like_form) {
       Ok(like) => like,
       Err(_e) => return Err(APIError::err("couldnt_like_post").into()),
     };
 
+    updated_post.send_like(&user, &conn)?;
+
     // Refetch the view
     let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) {
       Ok(post) => post,
@@ -368,7 +369,8 @@ impl Perform for Oper<CreatePostLike> {
     }
 
     // Check for a site ban
-    if UserView::read(&conn, user_id)?.banned {
+    let user = User_::read(&conn, user_id)?;
+    if user.banned {
       return Err(APIError::err("site_ban").into());
     }
 
@@ -388,6 +390,14 @@ impl Perform for Oper<CreatePostLike> {
         Ok(like) => like,
         Err(_e) => return Err(APIError::err("couldnt_like_post").into()),
       };
+
+      if like_form.score == 1 {
+        post.send_like(&user, &conn)?;
+      } else if like_form.score == -1 {
+        post.send_dislike(&user, &conn)?;
+      }
+    } else {
+      // TODO tombstone the post like
     }
 
     let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) {
index 77d42b82e7fb9d4cf235070592832056e6229c3c..f56a8344afea8134c87706cb7aca83695020f156 100644 (file)
@@ -10,7 +10,7 @@ pub mod user;
 pub mod user_inbox;
 
 use activitystreams::{
-  activity::{Accept, Create, Follow, Update},
+  activity::{Accept, Create, Dislike, Follow, Like, Update},
   actor::{properties::ApActorProperties, Actor, Group, Person},
   collection::UnorderedCollection,
   context,
@@ -161,6 +161,11 @@ pub trait ApubObjectType {
   fn send_update(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>;
 }
 
+pub trait ApubLikeableType {
+  fn send_like(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>;
+  fn send_dislike(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>;
+}
+
 pub fn get_shared_inbox(actor_id: &str) -> String {
   let url = Url::parse(actor_id).unwrap();
   format!(
index 4164eca712fd83796ea77e9043b2e20c2ce3c0e8..505ab98a017f580aca08056f08be43c6d945748a 100644 (file)
@@ -160,3 +160,69 @@ impl ApubObjectType for Post {
     Ok(())
   }
 }
+
+impl ApubLikeableType for Post {
+  fn send_like(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
+    let page = self.to_apub(conn)?;
+    let community = Community::read(conn, self.community_id)?;
+    let mut like = Like::new();
+    populate_object_props(
+      &mut like.object_props,
+      &community.get_followers_url(),
+      &self.ap_id,
+    )?;
+    like
+      .like_props
+      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
+      .set_object_base_box(page)?;
+
+    // Insert the sent activity into the activity table
+    let activity_form = activity::ActivityForm {
+      user_id: creator.id,
+      data: serde_json::to_value(&like)?,
+      local: true,
+      updated: None,
+    };
+    activity::Activity::create(&conn, &activity_form)?;
+
+    send_activity(
+      &like,
+      &creator.private_key.as_ref().unwrap(),
+      &creator.actor_id,
+      community.get_follower_inboxes(&conn)?,
+    )?;
+    Ok(())
+  }
+
+  fn send_dislike(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
+    let page = self.to_apub(conn)?;
+    let community = Community::read(conn, self.community_id)?;
+    let mut dislike = Dislike::new();
+    populate_object_props(
+      &mut dislike.object_props,
+      &community.get_followers_url(),
+      &self.ap_id,
+    )?;
+    dislike
+      .dislike_props
+      .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
+      .set_object_base_box(page)?;
+
+    // Insert the sent activity into the activity table
+    let activity_form = activity::ActivityForm {
+      user_id: creator.id,
+      data: serde_json::to_value(&dislike)?,
+      local: true,
+      updated: None,
+    };
+    activity::Activity::create(&conn, &activity_form)?;
+
+    send_activity(
+      &dislike,
+      &creator.private_key.as_ref().unwrap(),
+      &creator.actor_id,
+      community.get_follower_inboxes(&conn)?,
+    )?;
+    Ok(())
+  }
+}