Skip to content

Commit

Permalink
Allow changes of post owner on unpublished local topics (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod authored Jul 5, 2024
1 parent 3416e4b commit 3a6512d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
6 changes: 3 additions & 3 deletions extensions/discourse_activity_pub_guardian_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ def can_edit_post?(post)
end

def can_change_post_owner?
return false if activity_pub_enabled_topic?
return false if activity_pub_change_owner_restricted?
super
end

def activity_pub_enabled_topic?
def activity_pub_change_owner_restricted?
return false unless DiscourseActivityPub.enabled && request&.params&.[]("topic_id")
topic = Topic.find_by(id: request.params["topic_id"].to_i)
topic&.activity_pub_enabled
topic && (topic.activity_pub_remote? || topic.activity_pub_published?)
end

def can_admin?(actor)
Expand Down
5 changes: 4 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@
add_to_class(:topic, :activity_pub_objects_collection) { activity_pub_object.objects_collection }
add_to_class(:topic, :activity_pub_actor) { activity_pub_taxonomy&.activity_pub_actor }
add_to_class(:topic, :activity_pub_name) { title }

add_to_class(:topic, :activity_pub_local?) do
!first_post&.activity_pub_object || first_post.activity_pub_object.local
end
add_to_class(:topic, :activity_pub_remote?) { !activity_pub_local? }
Post.has_one :activity_pub_object, class_name: "DiscourseActivityPubObject", as: :model

Post.include DiscourseActivityPub::AP::ModelCallbacks
Expand Down
45 changes: 36 additions & 9 deletions spec/lib/guardian_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@
end

describe "can_change_post_owner?" do
shared_examples "returns true for admins" do
it "returns true for admins" do
expect(Guardian.new(admin, request).can_change_post_owner?).to be_truthy
expect(Guardian.new(user, request).can_change_post_owner?).to be_falsey
expect(Guardian.new(another_user, request).can_change_post_owner?).to be_falsey
end
end

shared_examples "returns false for all users" do
it "returns false for all users" do
expect(Guardian.new(admin, request).can_change_post_owner?).to be_falsey
expect(Guardian.new(user, request).can_change_post_owner?).to be_falsey
expect(Guardian.new(another_user, request).can_change_post_owner?).to be_falsey
end
end

describe "a Post" do
context "with a change owner request" do
let!(:request) do
Expand All @@ -74,19 +90,30 @@
context "with activity pub enabled" do
before { toggle_activity_pub(category) }

it "returns false for all users" do
expect(Guardian.new(admin, request).can_change_post_owner?).to be_falsey
expect(Guardian.new(user, request).can_change_post_owner?).to be_falsey
expect(Guardian.new(another_user, request).can_change_post_owner?).to be_falsey
context "when the topic is not published" do
include_examples "returns true for admins"
end

context "when the topic is published" do
before do
post.custom_fields["activity_pub_published_at"] = Time.now
post.save_custom_fields(true)
end

include_examples "returns false for all users"
end

context "when the topic is remote" do
fab!(:note) do
Fabricate(:discourse_activity_pub_object_note, model: post, local: false)
end

include_examples "returns false for all users"
end
end

context "with activity pub disabled" do
it "returns true for the right users" do
expect(Guardian.new(admin, request).can_change_post_owner?).to be_truthy
expect(Guardian.new(user, request).can_change_post_owner?).to be_falsey
expect(Guardian.new(another_user, request).can_change_post_owner?).to be_falsey
end
include_examples "returns true for admins"
end
end
end
Expand Down

0 comments on commit 3a6512d

Please sign in to comment.