Skip to content

Commit

Permalink
FIX: Don't error out on deleted users (#149)
Browse files Browse the repository at this point in the history
The GuardianExtension#poster_group_allow_translate? method restricts and determines whether a post can be translated based on the poster's group membership. There are reports that this errors out when deleting spammers.

The problem is that when a spammer is deleted, their post user_id is nullified, and because the above method doesn't account for this case, we try to call #in_any_groups? on nil, which errors out.

This PR fixes that by explicitly accounting for the case of a post with a nil user ID.
  • Loading branch information
Drenmi authored Jun 18, 2024
1 parent 2b10ec1 commit 17bd836
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/discourse_translator/guardian_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def user_group_allow_translate?
def poster_group_allow_translate?(post)
return false if !current_user
return true if SiteSetting.restrict_translation_by_poster_group_map.empty?
return false if post.user.nil?
post.user.in_any_groups?(SiteSetting.restrict_translation_by_poster_group_map)
end
end
20 changes: 19 additions & 1 deletion spec/lib/guardian_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
end
end

describe "deleted poster" do
fab!(:group)
fab!(:user)
fab!(:poster) { Fabricate(:user, groups: [group]) }
fab!(:post) { Fabricate(:post, user: poster) }
let!(:guardian) { Guardian.new(user) }

describe "#poster_group_allow_translate?" do
it "returns false when the post user has been deleted" do
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"

post.update(user: nil)

expect(guardian.poster_group_allow_translate?(post)).to eq(false)
end
end
end

describe "logged in user" do
fab!(:group)
fab!(:user) { Fabricate(:user, groups: [group]) }
Expand All @@ -45,7 +63,7 @@
end
end

describe "#poster_group_allow_translate??" do
describe "#poster_group_allow_translate?" do
it "returns true when the post user is in restrict_translation_by_poster_group" do
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"

Expand Down

0 comments on commit 17bd836

Please sign in to comment.