FIX: Hide post replies content for ignored users (#7320)

This commit is contained in:
Tarek Khalil 2019-04-05 15:13:36 +01:00 committed by Régis Hanol
parent 9c2e6b9a54
commit 131eba0366
8 changed files with 51 additions and 17 deletions

View File

@ -80,7 +80,6 @@ export function transformBasicPost(post) {
expandablePost: false,
replyCount: post.reply_count,
locked: post.locked,
ignored: post.ignored,
userCustomFields: post.user_custom_fields
};

View File

@ -619,6 +619,15 @@ const User = RestModel.extend({
return ajax(`${userPath(this.get("username"))}/notification_level.json`, {
type: "PUT",
data: { notification_level: level, expiring_at: expiringAt }
}).then(() => {
const currentUser = Discourse.User.current();
if (currentUser) {
if (level === "normal" || level === "mute") {
currentUser.ignored_users.removeObject(this.get("username"));
} else if (level === "ignore") {
currentUser.ignored_users.addObject(this.get("username"));
}
}
});
},

View File

@ -17,6 +17,8 @@ export default class PostCooked {
this.expanding = false;
this._highlighted = false;
this.decoratorHelper = decoratorHelper;
this.currentUser = decoratorHelper.widget.currentUser;
this.ignoredUsers = this.currentUser ? this.currentUser.ignored_users : null;
}
update(prev) {
@ -29,7 +31,7 @@ export default class PostCooked {
}
init() {
const $html = $(`<div class='cooked'>${this.attrs.cooked}</div>`);
const $html = this._computeCooked();
this._insertQuoteControls($html);
this._showLinkCounts($html);
this._fixImageSizes($html);
@ -212,6 +214,16 @@ export default class PostCooked {
expandContract = iconHTML(desc, { title: "post.expand_collapse" });
$(".title", $aside).css("cursor", "pointer");
}
if (this.ignoredUsers && this.ignoredUsers.length > 0) {
const username = $aside
.find(".title")
.text()
.trim()
.slice(0, -1);
if (username.length > 0 && this.ignoredUsers.includes(username)) {
$aside.find("p").replaceWith(`<i>${I18n.t("post.ignored")}</i>`);
}
}
$(".quote-controls", $aside).html(expandContract + navLink);
}
@ -241,6 +253,20 @@ export default class PostCooked {
}
});
}
_computeCooked() {
if (
this.ignoredUsers &&
this.ignoredUsers.length > 0 &&
this.ignoredUsers.includes(this.attrs.username)
) {
return $(
`<div class='cooked post-ignored'>${I18n.t("post.ignored")}</div>`
);
}
return $(`<div class='cooked'>${this.attrs.cooked}</div>`);
}
}
PostCooked.prototype.type = "Widget";

View File

@ -657,7 +657,12 @@ export default createWidget("post", {
} else {
classNames.push("regular");
}
if (attrs.ignored) {
if (
this.currentUser &&
this.currentUser.ignored_users &&
this.currentUser.ignored_users.length > 0 &&
this.currentUser.ignored_users.includes(attrs.username)
) {
classNames.push("post-ignored");
}
if (addPostClassesCallbacks) {

View File

@ -6,8 +6,7 @@ class BasicPostSerializer < ApplicationSerializer
:avatar_template,
:created_at,
:cooked,
:cooked_hidden,
:ignored
:cooked_hidden
def name
object.user && object.user.name
@ -36,21 +35,11 @@ class BasicPostSerializer < ApplicationSerializer
else
I18n.t('flagging.user_must_edit')
end
elsif ignored
I18n.t('ignored.hidden_content')
else
object.filter_quotes(@parent_post)
end
end
def ignored
return false unless SiteSetting.ignore_user_enabled?
object.is_first_post? &&
scope.current_user&.id != object.user_id &&
IgnoredUser.where(user_id: scope.current_user&.id,
ignored_user_id: object.user_id).exists?
end
def include_name?
SiteSetting.enable_names?
end

View File

@ -42,7 +42,8 @@ class CurrentUserSerializer < BasicUserSerializer
:top_category_ids,
:hide_profile_and_presence,
:groups,
:second_factor_enabled
:second_factor_enabled,
:ignored_users
def groups
object.visible_groups.pluck(:id, :name).map { |id, name| { id: id, name: name.downcase } }
@ -157,6 +158,10 @@ class CurrentUserSerializer < BasicUserSerializer
CategoryUser.lookup(object, :muted).pluck(:category_id)
end
def ignored_users
IgnoredUser.where(user: object.id).joins(:ignored_user).pluck(:username)
end
def top_category_ids
omitted_notification_levels = [CategoryUser.notification_levels[:muted], CategoryUser.notification_levels[:regular]]
CategoryUser.where(user_id: object.id)

View File

@ -2247,6 +2247,7 @@ en:
quote_reply: "Quote"
edit_reason: "Reason: "
post_number: "post {{number}}"
ignored: "Hidden content"
wiki_last_edited_on: "wiki last edited on"
last_edited_on: "post last edited on"
reply_as_new_topic: "Reply as linked Topic"

View File

@ -10,7 +10,7 @@ RSpec.describe WebHookPostSerializer do
it 'should only include the required keys' do
count = serialized_for_user(admin).keys.count
difference = count - 35
difference = count - 34
expect(difference).to eq(0), lambda {
message = ""