-
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.
change Comment to use new data entity concern
- Loading branch information
Showing
2 changed files
with
47 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,55 @@ | ||
require "federails/data_transformer/note" | ||
|
||
class Comment < ApplicationRecord | ||
include PublicIDable | ||
|
||
belongs_to :commenter, polymorphic: true | ||
belongs_to :commenter, polymorphic: true, optional: true | ||
belongs_to :commentable, polymorphic: true | ||
|
||
after_create :post_create_activity | ||
after_update :post_update_activity | ||
|
||
def federated_url | ||
return nil unless public? | ||
Rails.application.routes.url_helpers.url_for([commentable, self, {only_path: false}]) | ||
end | ||
include Federails::DataEntity | ||
acts_as_federails_data handles: "Note", actor_entity_method: :commenter | ||
|
||
def to_activitypub_object | ||
# Build tag structure | ||
tags = commentable.respond_to?(:tags) ? | ||
commentable.tags.pluck(:name).map do |tag| | ||
{ | ||
type: "Hashtag", | ||
name: "##{tag.tr(" ", "_").camelize}", | ||
href: Rails.application.routes.url_helpers.url_for([commentable.class, tag: tag]) | ||
} | ||
end | ||
: nil | ||
tag_html = tags&.map { |t| %(<a href="#{t[:href]}" class="mention hashtag" rel="tag">#{t[:name]}</a>) }&.join(" ") | ||
# Comments become Notes in ActvityPub world | ||
{ | ||
id: federated_url, | ||
type: "Note", | ||
content: [ | ||
Kramdown::Document.new(comment).to_html, | ||
tag_html | ||
].compact.join("\n\n"), | ||
context: Rails.application.routes.url_helpers.url_for([commentable, only_path: false]), | ||
published: created_at&.iso8601, | ||
attributedTo: (commenter&.federails_actor&.respond_to?(:federated_url) ? commenter.federails_actor.federated_url : nil), | ||
sensitive: sensitive, | ||
to: ["https://www.w3.org/ns/activitystreams#Public"], | ||
cc: [commenter.federails_actor.followers_url], | ||
tag: tags | ||
}.compact | ||
# Comments become Notes in ActvityPub | ||
Federails::DataTransformer::Note.to_federation self, content: to_html, | ||
custom: { | ||
"context" => Rails.application.routes.url_helpers.url_for([commentable, only_path: false]), | ||
"sensitive" => sensitive, | ||
"tag" => activitypub_tags | ||
}.merge(address_options) | ||
end | ||
|
||
private | ||
|
||
def public? | ||
Pundit::PolicyFinder.new(commenter.class).policy.new(nil, commenter).show? && | ||
Pundit::PolicyFinder.new(commentable.class).policy.new(nil, commentable).show? | ||
end | ||
|
||
private | ||
|
||
def post_create_activity | ||
post_activity "Create" | ||
def activitypub_tags | ||
return nil unless commentable.respond_to?(:tags) | ||
commentable.tags.pluck(:name).map do |tag| | ||
{ | ||
type: "Hashtag", | ||
name: "##{tag.tr(" ", "_").camelize}", | ||
href: Rails.application.routes.url_helpers.url_for([commentable.class, tag: tag]) | ||
} | ||
end | ||
end | ||
|
||
def post_update_activity | ||
post_activity "Update" | ||
def to_html | ||
[ | ||
Kramdown::Document.new(comment).to_html, | ||
activitypub_tags&.map { |t| %(<a href="#{t[:href]}" class="mention hashtag" rel="tag">#{t[:name]}</a>) }&.join(" ") | ||
].compact.join("\n\n") | ||
end | ||
|
||
def post_activity(action) | ||
if public? | ||
Federails::Activity.create!( | ||
actor: commenter.federails_actor, | ||
action: action, | ||
entity: self | ||
) | ||
end | ||
def address_options | ||
public? ? | ||
{ | ||
"to" => ["https://www.w3.org/ns/activitystreams#Public"], | ||
"cc" => [commenter.federails_actor.followers_url] | ||
} | ||
: {} | ||
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