Skip to content

Commit 5948a18

Browse files
committed
Create object attachments for posts with image uploads
1 parent 9e0b66f commit 5948a18

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

app/models/concerns/discourse_activity_pub/ap/model_callbacks.rb

+14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ def update_activity_pub_activity_object
144144
if performing_activity.create? || performing_activity.update?
145145
performing_activity_object.name = self.activity_pub_name if self.activity_pub_name
146146
performing_activity_object.content = self.activity_pub_content
147+
148+
if self.activity_pub_attachments.present?
149+
self.activity_pub_attachments.each do |attachment|
150+
performing_activity_object.attachments.build(
151+
object_id: performing_activity_object.id,
152+
object_type: performing_activity_object.class.name,
153+
ap_type: attachment.type,
154+
url: attachment.url.href,
155+
name: attachment.name,
156+
media_type: attachment.media_type,
157+
)
158+
end
159+
end
160+
147161
performing_activity_object.save!
148162
end
149163
end

plugin.rb

+13
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,19 @@
555555
@activity_pub_topic_trashed ||= Topic.with_deleted.find_by(id: self.topic_id)
556556
end
557557
add_to_class(:post, :activity_pub_object_id) { activity_pub_object&.ap_id }
558+
add_to_class(:post, :activity_pub_attachments) do
559+
uploads
560+
.where(extension: FileHelper.supported_images)
561+
.map do |upload|
562+
DiscourseActivityPub::AP::Object::Image.new(
563+
json: {
564+
name: upload.original_filename,
565+
url: UrlHelper.absolute(upload.url),
566+
mediaType: MiniMime.lookup_by_extension(upload.extension).content_type,
567+
},
568+
)
569+
end
570+
end
558571

559572
add_model_callback(:post, :after_destroy) do
560573
# We need these to create a Delete activity when the post is actually destroyed

spec/models/post_spec.rb

+51
Original file line numberDiff line numberDiff line change
@@ -2817,5 +2817,56 @@ def perform_delete
28172817
end
28182818
end
28192819
end
2820+
2821+
context "with uploads" do
2822+
let!(:topic) { Fabricate(:topic, category: category) }
2823+
let!(:user) { Fabricate(:user) }
2824+
2825+
before do
2826+
toggle_activity_pub(category, publication_type: "full_topic")
2827+
topic.create_activity_pub_collection!
2828+
end
2829+
2830+
def perform_create
2831+
post.perform_activity_pub_activity(:create)
2832+
post.reload
2833+
end
2834+
2835+
context "with a supported media type" do
2836+
let!(:post) { Fabricate(:post_with_uploaded_image, topic: topic) }
2837+
2838+
before do
2839+
DiscourseActivityPub::ActorHandler.update_or_create_actor(post.user)
2840+
post.link_post_uploads
2841+
end
2842+
2843+
it "creates attachments" do
2844+
perform_create
2845+
expect(post.activity_pub_object.attachments.size).to eq(1)
2846+
expect(post.activity_pub_object.attachments.first.ap_type).to eq("Image")
2847+
expect(post.activity_pub_object.attachments.first.name).to eq(
2848+
post.uploads.first.original_filename,
2849+
)
2850+
expect(post.activity_pub_object.attachments.first.media_type).to eq("image/png")
2851+
expect(post.activity_pub_object.attachments.first.url).to eq(
2852+
UrlHelper.absolute(post.uploads.first.url),
2853+
)
2854+
end
2855+
end
2856+
2857+
context "with an unsupported media type" do
2858+
let!(:post) { Fabricate(:post_with_an_attachment, topic: topic) }
2859+
2860+
before do
2861+
DiscourseActivityPub::ActorHandler.update_or_create_actor(post.user)
2862+
post.link_post_uploads
2863+
end
2864+
2865+
it "does not create attachments" do
2866+
perform_create
2867+
expect(post.activity_pub_object.attachments.size).to eq(0)
2868+
end
2869+
end
2870+
end
28202871
end
28212872
end

0 commit comments

Comments
 (0)