From: Dessalines Date: Mon, 27 Jul 2020 15:42:15 +0000 (-0400) Subject: Federate sticky flag dessalines (#1021) X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/readmes/%22https:/hacktivis.me/%7BlocalRss%7D?a=commitdiff_plain;h=c066915b8e5c2a998327332b6907ed5ea75ed579;p=lemmy.git Federate sticky flag dessalines (#1021) * 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 --- diff --git a/server/src/apub/extensions/page_extension.rs b/server/src/apub/extensions/page_extension.rs index 9851a013..3b0b89b5 100644 --- a/server/src/apub/extensions/page_extension.rs +++ b/server/src/apub/extensions/page_extension.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; pub struct PageExtension { pub comments_enabled: bool, pub sensitive: bool, + pub stickied: bool, } impl UnparsedExtension 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(()) } } diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index dc4ae249..eab841ca 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -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, diff --git a/ui/src/api_tests/api.spec.ts b/ui/src/api_tests/api.spec.ts index 9ab9fc2a..52ef4fda 100644 --- a/ui/src/api_tests/api.spec.ts +++ b/ui/src/api_tests/api.spec.ts @@ -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); }); });