FEATURE: allow users to wikify their own posts based on trust level

This commit is contained in:
Arpit Jalan 2016-01-11 20:56:00 +05:30
parent 19c630e7c2
commit 06bac23e5f
10 changed files with 65 additions and 21 deletions

View File

@ -349,6 +349,21 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
this.sendAction('toggleBookmark', post);
},
// Wiki button
buttonForWiki(post) {
if (!post.get('can_wiki')) return;
if (post.get('wiki')) {
return new Button('wiki', 'post.controls.unwiki', 'pencil-square-o', {className: 'wiki wikied'});
} else {
return new Button('wiki', 'post.controls.wiki', 'pencil-square-o', {className: 'wiki'});
}
},
clickWiki(post) {
this.sendAction('toggleWiki', post);
},
buttonForAdmin() {
if (!Discourse.User.currentProp('canManageTopic')) { return; }
return new Button('admin', 'post.controls.admin', 'wrench');
@ -357,10 +372,7 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
renderAdminPopup(post, buffer) {
if (!Discourse.User.currentProp('canManageTopic')) { return; }
const isWiki = post.get('wiki'),
wikiIcon = iconHTML('pencil-square-o'),
wikiText = isWiki ? I18n.t('post.controls.unwiki') : I18n.t('post.controls.wiki'),
isModerator = post.get('post_type') === this.site.get('post_types.moderator_action'),
const isModerator = post.get('post_type') === this.site.get('post_types.moderator_action'),
postTypeIcon = iconHTML('shield'),
postTypeText = isModerator ? I18n.t('post.controls.revert_to_regular') : I18n.t('post.controls.convert_to_moderator'),
rebakePostIcon = iconHTML('cog'),
@ -373,7 +385,6 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
const html = '<div class="post-admin-menu popup-menu">' +
'<h3>' + I18n.t('admin_title') + '</h3>' +
'<ul>' +
'<li class="btn" data-action="toggleWiki">' + wikiIcon + wikiText + '</li>' +
(Discourse.User.currentProp('staff') ? '<li class="btn" data-action="togglePostType">' + postTypeIcon + postTypeText + '</li>' : '') +
'<li class="btn" data-action="rebakePost">' + rebakePostIcon + rebakePostText + '</li>' +
(post.hidden ? '<li class="btn" data-action="unhidePost">' + unhidePostIcon + unhidePostText + '</li>' : '') +
@ -393,10 +404,6 @@ const PostMenuComponent = Ember.Component.extend(StringBuffer, {
});
},
clickToggleWiki() {
this.sendAction('toggleWiki', this.get('post'));
},
clickTogglePostType() {
this.sendAction("togglePostType", this.get("post"));
},

View File

@ -174,6 +174,10 @@ nav.post-controls {
box-shadow: none;
}
&.wikied {
color: green;
}
&.bookmark {padding: 8px 11px; }
.read-icon {

View File

@ -58,6 +58,7 @@ button {
margin:10px 0 10px 0;
}
&.has-like {color: $love;}
&.wikied { color: green; }
.read-icon {
&:before {
font-family: "FontAwesome";

View File

@ -305,9 +305,9 @@ class PostsController < ApplicationController
end
def wiki
guardian.ensure_can_wiki!
post = find_post_from_params
guardian.ensure_can_wiki!(post)
post.revise(current_user, { wiki: params[:wiki] })
render nothing: true

View File

@ -38,6 +38,7 @@ class PostSerializer < BasicPostSerializer
:can_edit,
:can_delete,
:can_recover,
:can_wiki,
:link_counts,
:read,
:user_title,
@ -130,6 +131,10 @@ class PostSerializer < BasicPostSerializer
scope.can_recover_post?(object)
end
def can_wiki
scope.can_wiki?(object)
end
def display_username
object.user.try(:name)
end

View File

@ -1017,6 +1017,8 @@ en:
min_trust_to_edit_wiki_post: "The minimum trust level required to edit post marked as wiki."
min_trust_to_allow_self_wiki: "The minimum trust level required to make user's own post wiki."
min_trust_to_send_messages: "The minimum trust level required to create new private messages."
newuser_max_links: "How many links a new user can add to a post."

View File

@ -105,7 +105,7 @@ basic:
post_menu:
client: true
type: list
default: 'like-count|like|share|flag|edit|bookmark|delete|admin|reply'
default: 'like-count|like|share|flag|edit|bookmark|wiki|delete|admin|reply'
choices:
- like-count
- like
@ -116,10 +116,11 @@ basic:
- bookmark
- admin
- reply
- wiki
post_menu_hidden_items:
client: true
type: list
default: 'bookmark|edit|delete|admin'
default: 'bookmark|edit|wiki|delete|admin'
choices:
- like
- edit
@ -129,6 +130,7 @@ basic:
- bookmark
- admin
- reply
- wiki
share_links:
client: true
type: list
@ -623,6 +625,9 @@ trust:
min_trust_to_edit_wiki_post:
default: 1
enum: 'TrustLevelSetting'
min_trust_to_allow_self_wiki:
default: 3
enum: 'TrustLevelSetting'
min_trust_to_send_messages:
default: 1
enum: 'TrustLevelSetting'

View File

@ -173,8 +173,9 @@ module PostGuardian
is_admin?
end
def can_wiki?
is_staff? || @user.has_trust_level?(TrustLevel[4])
def can_wiki?(post)
return false unless authenticated?
is_staff? || @user.has_trust_level?(TrustLevel[4]) || (@user.has_trust_level?(SiteSetting.min_trust_to_allow_self_wiki) && is_my_own?(post))
end
def can_change_post_type?

View File

@ -7,6 +7,7 @@ describe Guardian do
let(:user) { build(:user) }
let(:moderator) { build(:moderator) }
let(:admin) { build(:admin) }
let(:trust_level_2) { build(:user, trust_level: 2) }
let(:trust_level_3) { build(:user, trust_level: 3) }
let(:trust_level_4) { build(:user, trust_level: 4) }
let(:another_admin) { build(:admin) }
@ -1998,16 +1999,34 @@ describe Guardian do
end
describe 'can_wiki?' do
let(:post) { build(:post) }
it 'returns false for regular user' do
expect(Guardian.new(coding_horror).can_wiki?).to be_falsey
expect(Guardian.new(coding_horror).can_wiki?(post)).to be_falsey
end
it "returns false when user does not satisfy trust level but owns the post" do
own_post = Fabricate(:post, user: trust_level_2)
expect(Guardian.new(trust_level_2).can_wiki?(own_post)).to be_falsey
end
it "returns false when user satisfies trust level but tries to wiki someone else's post" do
SiteSetting.min_trust_to_allow_self_wiki = 2
expect(Guardian.new(trust_level_2).can_wiki?(post)).to be_falsey
end
it 'returns true when user satisfies trust level and owns the post' do
SiteSetting.min_trust_to_allow_self_wiki = 2
own_post = Fabricate(:post, user: trust_level_2)
expect(Guardian.new(trust_level_2).can_wiki?(own_post)).to be_truthy
end
it 'returns true for admin user' do
expect(Guardian.new(admin).can_wiki?).to be_truthy
expect(Guardian.new(admin).can_wiki?(post)).to be_truthy
end
it 'returns true for trust_level_4 user' do
expect(Guardian.new(trust_level_4).can_wiki?).to be_truthy
expect(Guardian.new(trust_level_4).can_wiki?(post)).to be_truthy
end
end
end

View File

@ -408,7 +408,7 @@ describe PostsController do
let(:post) {Fabricate(:post, user: user)}
it "raises an error if the user doesn't have permission to wiki the post" do
Guardian.any_instance.expects(:can_wiki?).returns(false)
Guardian.any_instance.expects(:can_wiki?).with(post).returns(false)
xhr :put, :wiki, post_id: post.id, wiki: 'true'
@ -416,7 +416,7 @@ describe PostsController do
end
it "can wiki a post" do
Guardian.any_instance.expects(:can_wiki?).returns(true)
Guardian.any_instance.expects(:can_wiki?).with(post).returns(true)
xhr :put, :wiki, post_id: post.id, wiki: 'true'
@ -426,7 +426,7 @@ describe PostsController do
it "can unwiki a post" do
wikied_post = Fabricate(:post, user: user, wiki: true)
Guardian.any_instance.expects(:can_wiki?).returns(true)
Guardian.any_instance.expects(:can_wiki?).with(wikied_post).returns(true)
xhr :put, :wiki, post_id: wikied_post.id, wiki: 'false'