-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3497 from manyfold3d/activitypub-presenters
Refactor ActivityPub rendering code into presenters
- Loading branch information
Showing
5 changed files
with
150 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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,49 @@ | ||
module ActivityPub | ||
class BasePresenter | ||
PUBLIC_COLLECTION = "https://www.w3.org/ns/activitystreams#Public" | ||
|
||
def initialize(object) | ||
@object = object | ||
end | ||
|
||
def present! | ||
raise NotImplementedError | ||
end | ||
|
||
def federate? | ||
true | ||
end | ||
|
||
def to | ||
nil | ||
end | ||
|
||
def bto | ||
nil | ||
end | ||
|
||
def cc | ||
nil | ||
end | ||
|
||
def bcc | ||
nil | ||
end | ||
|
||
def audience | ||
nil | ||
end | ||
|
||
protected | ||
|
||
def address_fields | ||
{ | ||
"to" => to, | ||
"bto" => bto, | ||
"cc" => cc, | ||
"bcc" => bcc, | ||
"audience" => audience | ||
}.compact | ||
end | ||
end | ||
end |
This file contains 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,48 @@ | ||
module ActivityPub | ||
class CommentPresenter < BasePresenter | ||
def present! | ||
Federails::DataTransformer::Note.to_federation( | ||
@object, | ||
content: to_html, | ||
custom: { | ||
"context" => Rails.application.routes.url_helpers.url_for([@object.commentable, {only_path: false}]), | ||
"sensitive" => @object.sensitive, | ||
"tag" => hashtags | ||
}.merge(address_fields) | ||
) | ||
end | ||
|
||
def federate? | ||
@object.public? | ||
end | ||
|
||
def to | ||
PUBLIC_COLLECTION if @object.public? | ||
end | ||
|
||
def cc | ||
@object.commenter.federails_actor.followers_url | ||
end | ||
|
||
private | ||
|
||
def hashtags | ||
return nil unless @object.commentable.respond_to?(:tags) | ||
|
||
@object.commentable.tags.pluck(:name).map do |tag| | ||
{ | ||
type: "Hashtag", | ||
name: "##{tag.tr(" ", "_").camelize}", | ||
href: Rails.application.routes.url_helpers.url_for([@object.commentable.class, {tag: tag}]) | ||
} | ||
end | ||
end | ||
|
||
def to_html | ||
[ | ||
Kramdown::Document.new(@object.comment).to_html, | ||
hashtags&.map { |t| %(<a href="#{t[:href]}" class="mention hashtag" rel="tag">#{t[:name]}</a>) }&.join(" ") | ||
].compact.join("\n\n") | ||
end | ||
end | ||
end |
This file contains 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,46 @@ | ||
module ActivityPub | ||
class CreatorPresenter < BasePresenter | ||
def present! | ||
{ | ||
"@context": { | ||
f3di: "http://purl.org/f3di/ns#", | ||
toot: "http://joinmastodon.org/ns#", | ||
attributionDomains: { | ||
"@id": "toot:attributionDomains", | ||
"@type": "@id" | ||
} | ||
}, | ||
summary: summary_html, | ||
attributionDomains: [ | ||
[Rails.application.default_url_options[:host], Rails.application.default_url_options[:port]].compact.join(":") | ||
], | ||
"f3di:concreteType": "Creator", | ||
attachment: @object.links.map { |it| {type: "Link", href: it.url} } | ||
}.merge(address_fields) | ||
end | ||
|
||
def federate? | ||
# Currently unused | ||
public? | ||
end | ||
|
||
def to | ||
PUBLIC_COLLECTION if public? | ||
end | ||
|
||
def cc | ||
@object.federails_actor.followers_url | ||
end | ||
|
||
private | ||
|
||
def public? | ||
CreatorPolicy.new(nil, @object).show? | ||
end | ||
|
||
def summary_html | ||
return unless @object.caption || @object.notes | ||
"<section>#{"<header>#{@object.caption}</header>" if @object.caption}#{Kramdown::Document.new(@object.notes).to_html.rstrip if @object.notes}</section>" | ||
end | ||
end | ||
end |