Skip to content
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

Extract ActivityPub::Identifier model concern #30072

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/lib/activitypub/tag_manager.rb
Expand Up @@ -58,7 +58,7 @@ def uri_for_username(username)
account_url(username: username)
end

def generate_uri_for(_target)
def generate_activity_uri
URI.join(root_url, 'payloads', SecureRandom.uuid)
end

Expand Down
6 changes: 1 addition & 5 deletions app/models/block.rb
Expand Up @@ -13,6 +13,7 @@
#

class Block < ApplicationRecord
include ActivityPub::Identifier
include Paginable
include RelationshipCacheable

Expand All @@ -25,7 +26,6 @@ def local?
false # Force uri_for to use uri attribute
end

before_validation :set_uri, only: :create
after_commit :invalidate_blocking_cache
after_commit :invalidate_follow_recommendations_cache

Expand All @@ -39,8 +39,4 @@ def invalidate_blocking_cache
def invalidate_follow_recommendations_cache
Rails.cache.delete("follow_recommendations/#{account_id}")
end

def set_uri
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
end
end
21 changes: 21 additions & 0 deletions app/models/concerns/activity_pub/identifier.rb
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module ActivityPub
module Identifier
extend ActiveSupport::Concern

included do
before_validation :generate_global_identifier,
only: :create,
unless: :uri?
end

private

def generate_global_identifier
self.uri = ActivityPub::TagManager
.instance
.generate_activity_uri
end
end
end
6 changes: 1 addition & 5 deletions app/models/follow.rb
Expand Up @@ -16,6 +16,7 @@
#

class Follow < ApplicationRecord
include ActivityPub::Identifier
include Paginable
include RelationshipCacheable
include RateLimitable
Expand All @@ -42,7 +43,6 @@ def revoke_request!
destroy!
end

before_validation :set_uri, only: :create
after_create :increment_cache_counters
after_destroy :remove_endorsements
after_destroy :decrement_cache_counters
Expand All @@ -51,10 +51,6 @@ def revoke_request!

private

def set_uri
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
end

def remove_endorsements
AccountPin.where(target_account_id: target_account_id, account_id: account_id).delete_all
end
Expand Down
6 changes: 1 addition & 5 deletions app/models/follow_request.rb
Expand Up @@ -16,6 +16,7 @@
#

class FollowRequest < ApplicationRecord
include ActivityPub::Identifier
include Paginable
include RelationshipCacheable
include RateLimitable
Expand Down Expand Up @@ -44,15 +45,10 @@ def local?
false # Force uri_for to use uri attribute
end

before_validation :set_uri, only: :create
after_commit :invalidate_follow_recommendations_cache

private

def set_uri
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil?
end

def invalidate_follow_recommendations_cache
Rails.cache.delete("follow_recommendations/#{account_id}")
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/relay.rb
Expand Up @@ -26,7 +26,7 @@ class Relay < ApplicationRecord
alias enabled? accepted?

def enable!
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
activity_id = ActivityPub::TagManager.instance.generate_activity_uri
payload = Oj.dump(follow_activity(activity_id))

update!(state: :pending, follow_activity_id: activity_id)
Expand All @@ -35,7 +35,7 @@ def enable!
end

def disable!
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
activity_id = ActivityPub::TagManager.instance.generate_activity_uri
payload = Oj.dump(unfollow_activity(activity_id))

update!(state: :idle, follow_activity_id: nil)
Expand Down
2 changes: 1 addition & 1 deletion app/models/report.rb
Expand Up @@ -160,7 +160,7 @@ def history
private

def set_uri
self.uri = ActivityPub::TagManager.instance.generate_uri_for(self) if uri.nil? && account.local?
self.uri = ActivityPub::TagManager.instance.generate_activity_uri if uri.nil? && account.local?
end

def validate_rule_ids
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/activitypub/activity_presenter.rb
Expand Up @@ -29,7 +29,7 @@ def from_status(status, allow_inlining: true)

def from_encrypted_message(encrypted_message)
new.tap do |presenter|
presenter.id = ActivityPub::TagManager.instance.generate_uri_for(nil)
presenter.id = ActivityPub::TagManager.instance.generate_activity_uri
presenter.type = 'Create'
presenter.actor = ActivityPub::TagManager.instance.uri_for(encrypted_message.source_account)
presenter.published = Time.now.utc
Expand Down