-
Notifications
You must be signed in to change notification settings - Fork 4
FEATURE: Add support for image attachments #197
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
Open
angusmcleod
wants to merge
10
commits into
discourse:main
Choose a base branch
from
angusmcleod:add_media_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
26fa5a5
Add processing of note attachments
angusmcleod 26dc372
Fix specs
angusmcleod 2343fbc
Update attached image html
angusmcleod db16ff9
Update attachment annotations
angusmcleod 4580098
Add ActivityPub Link object type handling
angusmcleod 67f9192
Fix linting
angusmcleod d5a22e7
Create object attachments for posts with image uploads
angusmcleod fccc533
Add attachment publication support
angusmcleod eb18a1e
Linting
angusmcleod a397067
Fix multisite spec
angusmcleod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/models/concerns/discourse_activity_pub/ap/type_validations.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
module DiscourseActivityPub | ||
module AP | ||
module TypeValidations | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_validation :ensure_ap_type | ||
validates :ap_type, presence: true | ||
end | ||
|
||
def ap | ||
@ap ||= DiscourseActivityPub::AP::Object.get_klass(ap_type)&.new(stored: self) | ||
end | ||
|
||
def _model | ||
self.respond_to?(:model) ? self.model : self.actor.model | ||
end | ||
|
||
def ensure_ap_type | ||
self.ap_type = _model.activity_pub_default_object_type if !self.ap_type && _model.present? | ||
|
||
unless ap | ||
self.errors.add( | ||
:ap_type, | ||
I18n.t( | ||
"activerecord.errors.models.discourse_activity_pub_activity.attributes.ap_type.invalid", | ||
), | ||
) | ||
|
||
raise ActiveRecord::RecordInvalid | ||
end | ||
|
||
self.ap_type = ap.type | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class DiscourseActivityPubAttachment < ActiveRecord::Base | ||
include DiscourseActivityPub::AP::TypeValidations | ||
include DiscourseActivityPub::AP::ObjectValidations | ||
|
||
belongs_to :object, class_name: "DiscourseActivityPubObject", polymorphic: true | ||
|
||
validate :validate_media_type | ||
|
||
protected | ||
|
||
def validate_media_type | ||
unless MiniMime.lookup_by_content_type(self.media_type) | ||
self.errors.add( | ||
:media_type, | ||
I18n.t( | ||
"activerecord.errors.models.discourse_activity_pub_attachment.attributes.media_type.invalid", | ||
), | ||
) | ||
raise ActiveRecord::RecordInvalid | ||
end | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: discourse_activity_pub_attachments | ||
# | ||
# id :bigint not null, primary key | ||
# ap_type :string not null | ||
# object_id :bigint not null | ||
# object_type :string not null | ||
# url :string | ||
# name :string | ||
# media_type :string(200) | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/serializers/discourse_activity_pub/ap/object/document_serializer.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class DiscourseActivityPub::AP::Object::DocumentSerializer < DiscourseActivityPub::AP::ObjectSerializer | ||
attributes :media_type | ||
end |
5 changes: 5 additions & 0 deletions
5
app/serializers/discourse_activity_pub/ap/object/image_serializer.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class DiscourseActivityPub::AP::Object::ImageSerializer < DiscourseActivityPub::AP::ObjectSerializer | ||
attributes :media_type | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
db/migrate/20250319134150_create_discourse_activity_pub_attachments.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
class CreateDiscourseActivityPubAttachments < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :discourse_activity_pub_attachments do |t| | ||
t.string :ap_type, null: false | ||
t.bigint :object_id, null: false | ||
t.string :object_type, null: false | ||
t.string :url | ||
t.string :name | ||
t.string :media_type, limit: 200 | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
module DiscourseActivityPub | ||
module AP | ||
class Link | ||
attr_accessor :value | ||
|
||
def initialize(value = nil) | ||
@value = value | ||
end | ||
|
||
def type | ||
"Link" | ||
end | ||
|
||
def href | ||
return value if value.is_a?(String) | ||
value[:href] if value.is_a?(Hash) | ||
end | ||
|
||
def media_type | ||
value[:mediaType] if value.is_a?(Hash) | ||
end | ||
|
||
def name | ||
value[:name] if value.is_a?(Hash) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the spec only support one attachment per note/article?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It supports multiple. The spec (and implementers like mastodon) use the singular term
attachment
, which actually an array of attachments. Yeah, I found it confusing too.