From d7e0b5aa9031887d5031627ef3a476662565bc8f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 20 Feb 2020 17:42:32 -0600 Subject: [PATCH] posts: add metatags for approving and banning posts. * Allow approvers to approve a post by tagging it with status:active. * Allow approvers to ban a post by tagging it with status:banned. * Allow approvers to unban a post by tagging it with -status:banned. --- app/models/post.rb | 14 +++++++++- test/unit/post_test.rb | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/app/models/post.rb b/app/models/post.rb index d06078a76..d36564be5 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -753,7 +753,7 @@ class Post < ApplicationRecord def filter_metatags(tags) @pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|-?locked):/i} tags = apply_categorization_metatags(tags) - @post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-child|-favgroup|favgroup|upvote|downvote):/i} + @post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-child|-favgroup|favgroup|upvote|downvote|status|-status):/i} apply_pre_metatags return tags end @@ -803,6 +803,18 @@ class Post < ApplicationRecord when /^(up|down)vote:(.+)$/i vote!($1) + when /^status:active$/i + raise User::PrivilegeError unless CurrentUser.is_approver? + approvals.create!(user: CurrentUser.user) + + when /^status:banned$/i + raise User::PrivilegeError unless CurrentUser.is_approver? + ban! + + when /^-status:banned$/i + raise User::PrivilegeError unless CurrentUser.is_approver? + unban! + when /^child:none$/i children.each do |post| post.update!(parent_id: nil) diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index d7e774906..dc74510f9 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -998,6 +998,64 @@ class PostTest < ActiveSupport::TestCase end end + context "for status:active" do + should "approve the post if the user has permission" do + as(create(:approver)) do + @post.update!(is_pending: true) + @post.update(tag_string: "aaa status:active") + end + + assert_equal(false, @post.reload.is_pending?) + end + + should "not approve the post if the user is doesn't have permission" do + assert_raises(User::PrivilegeError) do + @post.update!(is_pending: true) + @post.update(tag_string: "aaa status:active") + end + + assert_equal(true, @post.reload.is_pending?) + end + end + + context "for status:banned" do + should "ban the post if the user has permission" do + as(create(:approver)) do + @post.update(tag_string: "aaa status:banned") + end + + assert_equal(true, @post.reload.is_banned?) + end + + should "not ban the post if the user doesn't have permission" do + assert_raises(User::PrivilegeError) do + @post.update(tag_string: "aaa status:banned") + end + + assert_equal(false, @post.reload.is_banned?) + end + end + + context "for -status:banned" do + should "unban the post if the user has permission" do + as(create(:approver)) do + @post.update!(is_banned: true) + @post.update(tag_string: "aaa -status:banned") + end + + assert_equal(false, @post.reload.is_banned?) + end + + should "not unban the post if the user doesn't have permission" do + assert_raises(User::PrivilegeError) do + @post.update!(is_banned: true) + @post.update(tag_string: "aaa status:banned") + end + + assert_equal(true, @post.reload.is_banned?) + end + end + context "for a source" do should "set the source with source:foo_bar_baz" do @post.update(:tag_string => "source:foo_bar_baz")