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 <me@nutomic.com>
This commit is contained in:
Dessalines 2020-07-27 11:42:15 -04:00 committed by GitHub
parent d1342afe93
commit c066915b8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 13 deletions

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

View 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,

View 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);
});
});