Skip to content

Commit

Permalink
FEATURE: Implement only allow post voting topics in this category
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmartinez1062 committed Dec 5, 2023
1 parent 3f70825 commit e1e9afe
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 18 deletions.
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ en:
category:
post_voting_settings_heading: Post Voting
create_as_post_voting_default: New topics default to Post Voting topics in this category.
only_post_voting_in_this_category: New topics can only be Post Voting topics in this category.

composer:
create_post_voting:
Expand Down
9 changes: 9 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ plugins:
post_voting_enable_likes_on_answers:
client: true
default: false
only_allow_post_voting_topics_in_this_caregory:
client: true
type: category
default: ""
min_trust_to_flag_posts_voting_comments:
default: 1
enum: "TrustLevelSetting"
client: true

1 change: 1 addition & 0 deletions lib/post_voting/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module ::PostVoting
CREATE_AS_POST_VOTING_DEFAULT = "create_as_post_voting_default"
ONLY_POST_VOTING_IN_THIS_CATEGORY = "only_post_voting_in_this_category"

class Engine < Rails::Engine
engine_name "post_voting"
Expand Down
16 changes: 0 additions & 16 deletions lib/post_voting/guardian.rb

This file was deleted.

48 changes: 48 additions & 0 deletions lib/post_voting/guardian_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module PostVoting
module GuardianExtension
def can_edit_comment?(comment)
return false if !self.user
return true if comment.user_id == self.user.id
return true if self.is_admin?
return true if self.is_moderator?
false
end

def can_delete_comment?(comment)
can_edit_comment?(comment)
end

def can_flag_post_voting_comments?
return false if self.user.silenced?
return true if self.user.staff?

self.user.trust_level >= (SiteSetting.min_trust_to_flag_posts_voting_comments)
true
end

def can_flag_post_voting_comment?(comment)
if !authenticated? || !comment || comment.trashed? || !comment.user
return false
end
return false if comment.user.staff? && !SiteSetting.allow_flagging_staff
return false if comment.user_id == @user.id

can_flag_post_voting_comments?
end

def can_flag_post_voting_comment_as?(comment, flag_type_id, opts)
return false if !is_staff? && (opts[:take_action] || opts[:queue_for_review])

if flag_type_id == ReviewableScore.types[:notify_user]
is_warning = ActiveRecord::Type::Boolean.new.deserialize(opts[:is_warning])

return false if is_warning && !is_staff?
end

true
end

end
end
19 changes: 17 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
%w[
../lib/post_voting/engine.rb
../lib/post_voting/vote_manager.rb
../lib/post_voting/guardian.rb
../lib/post_voting/guardian_extension.rb
../lib/post_voting/comment_creator.rb
../extensions/post_extension.rb
../extensions/post_serializer_extension.rb
Expand Down Expand Up @@ -54,7 +54,8 @@
TopicViewSerializer.include(PostVoting::TopicViewSerializerExtension)
TopicListItemSerializer.include(PostVoting::TopicListItemSerializerExtension)
User.include(PostVoting::UserExtension)
Guardian.include(PostVoting::Guardian)
Guardian.prepend(PostVoting::GuardianExtension)

ComposerMessagesFinder.prepend(PostVoting::ComposerMessagesFinderExtension)
end

Expand Down Expand Up @@ -196,6 +197,20 @@
object.create_as_post_voting_default
end

register_category_custom_field_type(PostVoting::ONLY_POST_VOTING_IN_THIS_CATEGORY, :boolean)
if Site.respond_to? :preloaded_category_custom_fields
Site.preloaded_category_custom_fields << PostVoting::ONLY_POST_VOTING_IN_THIS_CATEGORY
end
add_to_class(:category, :only_post_voting_in_this_category) do
ActiveModel::Type::Boolean.new.cast(
self.custom_fields[PostVoting::ONLY_POST_VOTING_IN_THIS_CATEGORY],
)
end
add_to_serializer(:basic_category, :only_post_voting_in_this_category) do
object.only_post_voting_in_this_category
end


add_model_callback(:post, :before_create) do
if SiteSetting.post_voting_enabled && self.is_post_voting_topic? && self.via_email &&
self.reply_to_post_number == 1
Expand Down
32 changes: 32 additions & 0 deletions spec/requests/post_voting/comments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
RSpec.describe PostVoting::CommentsController do
fab!(:user) { Fabricate(:user) }
fab!(:admin) { Fabricate(:admin) }
fab!(:moderator) { Fabricate(:moderator) }
fab!(:group) { Fabricate(:group) }
fab!(:category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: category, subtype: Topic::POST_VOTING_SUBTYPE) }
Expand Down Expand Up @@ -231,6 +232,23 @@
expect(body["cooked"]).to eq("<p>this is some new raw</p>")
end

it "should allow a moderator to update the comment" do
sign_in(moderator)

put "/post_voting/comments.json",
params: {
comment_id: comment.id,
raw: "this is some new raw",
}

expect(response.status).to eq(200)

body = response.parsed_body

expect(body["raw"]).to eq("this is some new raw")
expect(body["cooked"]).to eq("<p>this is some new raw</p>")
end

it "should allow users to update their own comment and publishes a MessageBus message" do
sign_in(comment.user)

Expand Down Expand Up @@ -307,6 +325,20 @@
).to eq(true)
end

it "should allow a moderator to delete a comment of another user" do
sign_in(moderator)

delete "/post_voting/comments.json", params: { comment_id: comment.id }

expect(response.status).to eq(200)
expect(
PostVotingComment
.with_deleted
.where("deleted_at IS NOT NULL AND id = ?", comment.id)
.exists?,
).to eq(true)
end

it "should allow users to delete their own comment and publishes a message bus message" do
sign_in(comment.user)

Expand Down

0 comments on commit e1e9afe

Please sign in to comment.