Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Implement SiteSetting to Allow Anonymous Likes #22131

Merged
merged 5 commits into from Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
meltingmettle marked this conversation as resolved.
Show resolved Hide resolved
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 }
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved

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