Skip to content

Commit

Permalink
Implement SiteSetting to allow anonymous liking
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingmettle committed Jun 15, 2023
1 parent 7958f57 commit 6573bc0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/site_settings.yml
Expand Up @@ -664,6 +664,9 @@ users:
allow_anonymous_posting:
default: false
client: true
allow_anonymous_likes:
default: false
client: true
anonymous_posting_min_trust_level:
default: 1
enum: "TrustLevelSetting"
Expand Down
5 changes: 4 additions & 1 deletion lib/guardian/post_guardian.rb
Expand Up @@ -43,7 +43,10 @@ def post_can_act?(post, action_key, opts: {}, can_see_post: nil)
already_did_flagging = taken.any? && (taken & PostActionType.notify_flag_types.values).any?

result =
if authenticated? && post && !@user.anonymous?
if authenticated? && post
# Allow anonymous users to like if feature is enabled and short-circuit otherwise
return SiteSetting.allow_anonymous_likes? && (action_key == :like) if @user.anonymous?

# Silenced users can't flag
return false if is_flag && @user.silenced?

Expand Down
32 changes: 32 additions & 0 deletions spec/lib/guardian_spec.rb
Expand Up @@ -98,6 +98,38 @@
fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(:post) }

describe "an anonymous user" do
before { SiteSetting.allow_anonymous_posting = true }

context "when allow_anonymous_likes is enabled" do
before { SiteSetting.allow_anonymous_likes = true }

it "returns true when liking" do
expect(Guardian.new(anonymous_user).post_can_act?(post, :like)).to be_truthy
end

it "cannot perform any other action" do
expect(Guardian.new(anonymous_user).post_can_act?(post, :flag)).to be_falsey
expect(Guardian.new(anonymous_user).post_can_act?(post, :bookmark)).to be_falsey
expect(Guardian.new(anonymous_user).post_can_act?(post, :notify_user)).to be_falsey
end
end

context "when allow_anonymous_likes is disabled" do
before { SiteSetting.allow_anonymous_likes = false }

it "returns false when liking" do
expect(Guardian.new(anonymous_user).post_can_act?(post, :like)).to be_falsey
end

it "cannot perform any other action" do
expect(Guardian.new(anonymous_user).post_can_act?(post, :flag)).to be_falsey
expect(Guardian.new(anonymous_user).post_can_act?(post, :bookmark)).to be_falsey
expect(Guardian.new(anonymous_user).post_can_act?(post, :notify_user)).to be_falsey
end
end
end

it "returns false when the user is nil" do
expect(Guardian.new(nil).post_can_act?(post, :like)).to be_falsey
end
Expand Down

0 comments on commit 6573bc0

Please sign in to comment.