FEATURE: add user_suspended attribute in post serialize. (#16413)

This PR will include `suspended` attribute in post serializer to check it in post widget and add a CSS class name.

Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
Vinoth Kannan 2022-04-13 19:58:09 +05:30 committed by GitHub
parent 01107e418e
commit c863244382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 1 deletions

View File

@ -84,6 +84,7 @@ export function transformBasicPost(post) {
readCount: post.readers_count,
canPublishPage: false,
trustLevel: post.trust_level,
userSuspended: post.user_suspended,
};
_additionalAttributes.forEach((a) => (postAtts[a] = post[a]));

View File

@ -849,6 +849,9 @@ export default createWidget("post", {
} else {
classNames.push("regular");
}
if (attrs.userSuspended) {
classNames.push("user-suspended");
}
if (addPostClassesCallbacks) {
for (let i = 0; i < addPostClassesCallbacks.length; i++) {
let pluginClasses = addPostClassesCallbacks[i].call(this, attrs);

View File

@ -246,6 +246,15 @@ acceptance("Topic", function (needs) {
assert.ok(exists(".category-moderator"), "it has a class applied");
assert.ok(exists(".d-icon-shield-alt"), "it shows an icon");
});
test("Suspended user posts", async function (assert) {
await visit("/t/topic-from-suspended-user/54077");
assert.ok(
exists(".topic-post.user-suspended > #post_1"),
"it has a class applied"
);
});
});
acceptance("Topic featured links", function (needs) {

View File

@ -6251,6 +6251,7 @@ export default {
edit_reason: null,
can_view_edit_history: true,
wiki: false,
user_suspended: true,
},
{
id: 419,

View File

@ -86,7 +86,8 @@ class PostSerializer < BasicPostSerializer
:excerpt,
:reviewable_id,
:reviewable_score_count,
:reviewable_score_pending_count
:reviewable_score_pending_count,
:user_suspended
def initialize(object, opts)
super(object, opts)
@ -549,6 +550,14 @@ class PostSerializer < BasicPostSerializer
can_review_topic?
end
def user_suspended
true
end
def include_user_suspended?
object.user&.suspended?
end
private
def can_review_topic?

View File

@ -88,6 +88,26 @@ describe PostSerializer do
end
end
context "a post by a suspended user" do
def subject
PostSerializer.new(post, scope: Guardian.new(Fabricate(:admin)), root: false).as_json
end
it "serializes correctly" do
expect(subject[:user_suspended]).to be_nil
post.user.update!(
suspended_till: 1.month.from_now,
)
expect(subject[:user_suspended]).to eq(true)
freeze_time (2.months.from_now)
expect(subject[:user_suspended]).to be_nil
end
end
context "display_username" do
let(:user) { post.user }
let(:serializer) { PostSerializer.new(post, scope: Guardian.new, root: false) }