]> Untitled Git - lemmy.git/commitdiff
Federate sticky flag dessalines (#1021)
authorDessalines <dessalines@users.noreply.github.com>
Mon, 27 Jul 2020 15:42:15 +0000 (11:42 -0400)
committerGitHub <noreply@github.com>
Mon, 27 Jul 2020 15:42:15 +0000 (11:42 -0400)
* Federate sticky flag (ref #1018)

* Adding tests for federated sticky and lock.

* Changing test to make sure it returns the correct locked error.

Co-authored-by: Felix Ableitner <me@nutomic.com>
server/src/apub/extensions/page_extension.rs
server/src/apub/post.rs
ui/src/api_tests/api.spec.ts

index 9851a0137d0470be711842b33558965c4a020826..3b0b89b5ad56133a7345bb158787fb44f637f633 100644 (file)
@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
 pub struct PageExtension {
   pub comments_enabled: bool,
   pub sensitive: bool,
+  pub stickied: bool,
 }
 
 impl<U> UnparsedExtension<U> for PageExtension
@@ -19,12 +20,14 @@ where
     Ok(PageExtension {
       comments_enabled: unparsed_mut.remove("commentsEnabled")?,
       sensitive: unparsed_mut.remove("sensitive")?,
+      stickied: unparsed_mut.remove("stickied")?,
     })
   }
 
   fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
     unparsed_mut.insert("commentsEnabled", self.comments_enabled)?;
     unparsed_mut.insert("sensitive", self.sensitive)?;
+    unparsed_mut.insert("stickied", self.stickied)?;
     Ok(())
   }
 }
index dc4ae24990b2b7fed3b272aefab9f4bca91fc4b1..eab841ca5cfb92491f45bb350b2be72b9da7d3a8 100644 (file)
@@ -1,22 +1,14 @@
 use crate::{
   apub::{
     activities::send_activity_to_community,
-    create_apub_response,
-    create_apub_tombstone_response,
-    create_tombstone,
+    create_apub_response, create_apub_tombstone_response, create_tombstone,
     extensions::page_extension::PageExtension,
     fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user},
-    ActorType,
-    ApubLikeableType,
-    ApubObjectType,
-    FromApub,
-    PageExt,
-    ToApub,
+    ActorType, ApubLikeableType, ApubObjectType, FromApub, PageExt, ToApub,
   },
   blocking,
   routes::DbPoolParam,
-  DbPool,
-  LemmyError,
+  DbPool, LemmyError,
 };
 use activitystreams_ext::Ext1;
 use activitystreams_new::{
@@ -133,6 +125,7 @@ impl ToApub for Post {
     let ext = PageExtension {
       comments_enabled: !self.locked,
       sensitive: self.nsfw,
+      stickied: self.stickied,
     };
     Ok(Ext1::new(page, ext))
   }
@@ -244,7 +237,7 @@ impl FromApub for PostForm {
         .map(|u| u.to_owned().naive_local()),
       deleted: None,
       nsfw: ext.sensitive,
-      stickied: None, // -> put it in "featured" collection of the community
+      stickied: Some(ext.stickied),
       embed_title,
       embed_description,
       embed_html,
index 9ab9fc2ae89eddeec53237a1b6cf4ca872ac16a1..52ef4fda917c13ee694dde25c28a776a4454ebb3 100644 (file)
@@ -6,7 +6,8 @@ import {
   PostForm,
   DeletePostForm,
   RemovePostForm,
-  // TODO need to test LockPost and StickyPost federated
+  StickyPostForm,
+  LockPostForm,
   PostResponse,
   SearchResponse,
   FollowCommunityForm,
@@ -345,6 +346,27 @@ describe('main', () => {
       expect(updateResponse.post.community_local).toBe(false);
       expect(updateResponse.post.creator_local).toBe(true);
 
+      let stickyPostForm: StickyPostForm = {
+        edit_id: 2,
+        stickied: true,
+        auth: lemmyAlphaAuth,
+      };
+
+      let stickyRes: PostResponse = await fetch(
+        `${lemmyAlphaApiUrl}/post/sticky`,
+        {
+          method: 'POST',
+          headers: {
+            'Content-Type': 'application/json',
+          },
+          body: wrapper(stickyPostForm),
+        }
+      ).then(d => d.json());
+
+      expect(stickyRes.post.name).toBe(name);
+      expect(stickyRes.post.stickied).toBe(true);
+
+      // Fetch from B
       let getPostUrl = `${lemmyBetaApiUrl}/post?id=2`;
       let getPostRes: GetPostResponse = await fetch(getPostUrl, {
         method: 'GET',
@@ -353,6 +375,76 @@ describe('main', () => {
       expect(getPostRes.post.name).toBe(name);
       expect(getPostRes.post.community_local).toBe(true);
       expect(getPostRes.post.creator_local).toBe(false);
+      expect(getPostRes.post.stickied).toBe(true);
+
+      let lockPostForm: LockPostForm = {
+        edit_id: 2,
+        locked: true,
+        auth: lemmyAlphaAuth,
+      };
+
+      let lockedRes: PostResponse = await fetch(
+        `${lemmyAlphaApiUrl}/post/lock`,
+        {
+          method: 'POST',
+          headers: {
+            'Content-Type': 'application/json',
+          },
+          body: wrapper(lockPostForm),
+        }
+      ).then(d => d.json());
+
+      expect(lockedRes.post.name).toBe(name);
+      expect(lockedRes.post.locked).toBe(true);
+
+      // Fetch from B to make sure its locked
+      getPostRes = await fetch(getPostUrl, {
+        method: 'GET',
+      }).then(d => d.json());
+      expect(getPostRes.post.locked).toBe(true);
+
+      // Create a test comment on a locked post, it should be undefined
+      // since it shouldn't get created.
+      let content = 'A rejected comment on a locked post';
+      let commentForm: CommentForm = {
+        content,
+        post_id: 2,
+        auth: lemmyAlphaAuth,
+      };
+
+      let createResponse: CommentResponse = await fetch(
+        `${lemmyAlphaApiUrl}/comment`,
+        {
+          method: 'POST',
+          headers: {
+            'Content-Type': 'application/json',
+          },
+          body: wrapper(commentForm),
+        }
+      ).then(d => d.json());
+
+      expect(createResponse['error']).toBe('locked');
+
+      // Unlock the post for later actions
+      let unlockPostForm: LockPostForm = {
+        edit_id: 2,
+        locked: false,
+        auth: lemmyAlphaAuth,
+      };
+
+      let unlockedRes: PostResponse = await fetch(
+        `${lemmyAlphaApiUrl}/post/lock`,
+        {
+          method: 'POST',
+          headers: {
+            'Content-Type': 'application/json',
+          },
+          body: wrapper(unlockPostForm),
+        }
+      ).then(d => d.json());
+
+      expect(unlockedRes.post.name).toBe(name);
+      expect(unlockedRes.post.locked).toBe(false);
     });
   });