diff options
Diffstat (limited to 'app/lib/ostatus')
-rw-r--r-- | app/lib/ostatus/activity/base.rb | 38 | ||||
-rw-r--r-- | app/lib/ostatus/activity/creation.rb | 123 | ||||
-rw-r--r-- | app/lib/ostatus/activity/deletion.rb | 4 | ||||
-rw-r--r-- | app/lib/ostatus/activity/remote.rb | 6 | ||||
-rw-r--r-- | app/lib/ostatus/activity/share.rb | 2 | ||||
-rw-r--r-- | app/lib/ostatus/atom_serializer.rb | 117 | ||||
-rw-r--r-- | app/lib/ostatus/tag_manager.rb | 73 |
7 files changed, 259 insertions, 104 deletions
diff --git a/app/lib/ostatus/activity/base.rb b/app/lib/ostatus/activity/base.rb index e1477f0eb..039381397 100644 --- a/app/lib/ostatus/activity/base.rb +++ b/app/lib/ostatus/activity/base.rb @@ -11,39 +11,61 @@ class OStatus::Activity::Base end def verb - raw = @xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content - TagManager::VERBS.key(raw) + raw = @xml.at_xpath('./activity:verb', activity: OStatus::TagManager::AS_XMLNS).content + OStatus::TagManager::VERBS.key(raw) rescue :post end def type - raw = @xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content - TagManager::TYPES.key(raw) + raw = @xml.at_xpath('./activity:object-type', activity: OStatus::TagManager::AS_XMLNS).content + OStatus::TagManager::TYPES.key(raw) rescue :activity end def id - @xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content + @xml.at_xpath('./xmlns:id', xmlns: OStatus::TagManager::XMLNS).content end def url - link = @xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) + link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: OStatus::TagManager::XMLNS).find { |link_candidate| link_candidate['type'] == 'text/html' } link.nil? ? nil : link['href'] end + def activitypub_uri + link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: OStatus::TagManager::XMLNS).find { |link_candidate| ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(link_candidate['type']) } + link.nil? ? nil : link['href'] + end + + def activitypub_uri? + activitypub_uri.present? + end + private def find_status(uri) - if TagManager.instance.local_id?(uri) - local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status') + if OStatus::TagManager.instance.local_id?(uri) + local_id = OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Status') + return Status.find_by(id: local_id) + elsif ActivityPub::TagManager.instance.local_uri?(uri) + local_id = ActivityPub::TagManager.instance.uri_to_local_id(uri) return Status.find_by(id: local_id) end Status.find_by(uri: uri) end + def find_activitypub_status(uri, href) + tag_matches = /tag:([^,:]+)[^:]*:objectId=([\d]+)/.match(uri) + href_matches = %r{/users/([^/]+)}.match(href) + + unless tag_matches.nil? || href_matches.nil? + uri = "https://#{tag_matches[1]}/users/#{href_matches[1]}/statuses/#{tag_matches[2]}" + Status.find_by(uri: uri) + end + end + def redis Redis.current end diff --git a/app/lib/ostatus/activity/creation.rb b/app/lib/ostatus/activity/creation.rb index e22f746f2..2687776f9 100644 --- a/app/lib/ostatus/activity/creation.rb +++ b/app/lib/ostatus/activity/creation.rb @@ -9,31 +9,49 @@ class OStatus::Activity::Creation < OStatus::Activity::Base return [nil, false] if @account.suspended? - Rails.logger.debug "Creating remote status #{id}" + if activitypub_uri? && [:public, :unlisted].include?(visibility_scope) + result = perform_via_activitypub + return result if result.first.present? + end + + RedisLock.acquire(lock_options) do |lock| + if lock.acquired? + # Return early if status already exists in db + @status = find_status(id) + return [@status, false] unless @status.nil? + @status = process_status + end + end - # Return early if status already exists in db - status = find_status(id) - - return [status, false] unless status.nil? - - status = Status.create!( - uri: id, - url: url, - account: @account, - reblog: reblog, - text: content, - spoiler_text: content_warning, - created_at: published, - reply: thread?, - language: content_language, - visibility: visibility_scope, - conversation: find_or_create_conversation, - thread: thread? ? find_status(thread.first) : nil - ) - - save_mentions(status) - save_hashtags(status) - save_media(status) + [@status, true] + end + + def process_status + Rails.logger.debug "Creating remote status #{id}" + cached_reblog = reblog + status = nil + + ApplicationRecord.transaction do + status = Status.create!( + uri: id, + url: url, + account: @account, + reblog: cached_reblog, + text: content, + spoiler_text: content_warning, + created_at: published, + reply: thread?, + language: content_language, + visibility: visibility_scope, + conversation: find_or_create_conversation, + thread: thread? ? find_status(thread.first) || find_activitypub_status(thread.first, thread.second) : nil + ) + + save_mentions(status) + save_hashtags(status) + save_media(status) + save_emojis(status) + end if thread? && status.thread.nil? Rails.logger.debug "Trying to attach #{status.id} (#{id}) to #{thread.first}" @@ -45,46 +63,50 @@ class OStatus::Activity::Creation < OStatus::Activity::Base LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text? DistributionWorker.perform_async(status.id) - [status, true] + status + end + + def perform_via_activitypub + [find_status(activitypub_uri) || ActivityPub::FetchRemoteStatusService.new.call(activitypub_uri), false] end def content - @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content + @xml.at_xpath('./xmlns:content', xmlns: OStatus::TagManager::XMLNS).content end def content_language - @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' + @xml.at_xpath('./xmlns:content', xmlns: OStatus::TagManager::XMLNS)['xml:lang']&.presence || 'en' end def content_warning - @xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || '' + @xml.at_xpath('./xmlns:summary', xmlns: OStatus::TagManager::XMLNS)&.content || '' end def visibility_scope - @xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public + @xml.at_xpath('./mastodon:scope', mastodon: OStatus::TagManager::MTDN_XMLNS)&.content&.to_sym || :public end def published - @xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content + @xml.at_xpath('./xmlns:published', xmlns: OStatus::TagManager::XMLNS).content end def thread? - !@xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil? + !@xml.at_xpath('./thr:in-reply-to', thr: OStatus::TagManager::THR_XMLNS).nil? end def thread - thr = @xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS) + thr = @xml.at_xpath('./thr:in-reply-to', thr: OStatus::TagManager::THR_XMLNS) [thr['ref'], thr['href']] end private def find_or_create_conversation - uri = @xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content + uri = @xml.at_xpath('./ostatus:conversation', ostatus: OStatus::TagManager::OS_XMLNS)&.attribute('ref')&.content return if uri.nil? - if TagManager.instance.local_id?(uri) - local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Conversation') + if OStatus::TagManager.instance.local_id?(uri) + local_id = OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation') return Conversation.find_by(id: local_id) end @@ -94,8 +116,8 @@ class OStatus::Activity::Creation < OStatus::Activity::Base def save_mentions(parent) processed_account_ids = [] - @xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link| - next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type'] + @xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: OStatus::TagManager::XMLNS).each do |link| + next if [OStatus::TagManager::TYPES[:group], OStatus::TagManager::TYPES[:collection]].include? link['ostatus:object-type'] mentioned_account = account_from_href(link['href']) @@ -109,14 +131,14 @@ class OStatus::Activity::Creation < OStatus::Activity::Base end def save_hashtags(parent) - tags = @xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?) + tags = @xml.xpath('./xmlns:category', xmlns: OStatus::TagManager::XMLNS).map { |category| category['term'] }.select(&:present?) ProcessHashtagsService.new.call(parent, tags) end def save_media(parent) do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? - @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link| + @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: OStatus::TagManager::XMLNS).each do |link| next unless link['href'] media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href']) @@ -137,6 +159,25 @@ class OStatus::Activity::Creation < OStatus::Activity::Base end end + def save_emojis(parent) + do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? + + return if do_not_download + + @xml.xpath('./xmlns:link[@rel="emoji"]', xmlns: OStatus::TagManager::XMLNS).each do |link| + next unless link['href'] && link['name'] + + shortcode = link['name'].delete(':') + emoji = CustomEmoji.find_by(shortcode: shortcode, domain: parent.account.domain) + + next unless emoji.nil? + + emoji = CustomEmoji.new(shortcode: shortcode, domain: parent.account.domain) + emoji.image_remote_url = link['href'] + emoji.save + end + end + def account_from_href(href) url = Addressable::URI.parse(href).normalize @@ -146,4 +187,8 @@ class OStatus::Activity::Creation < OStatus::Activity::Base Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href) end end + + def lock_options + { redis: Redis.current, key: "create:#{id}" } + end end diff --git a/app/lib/ostatus/activity/deletion.rb b/app/lib/ostatus/activity/deletion.rb index 860faf501..c98f5ee0a 100644 --- a/app/lib/ostatus/activity/deletion.rb +++ b/app/lib/ostatus/activity/deletion.rb @@ -3,7 +3,9 @@ class OStatus::Activity::Deletion < OStatus::Activity::Base def perform Rails.logger.debug "Deleting remote status #{id}" - status = Status.find_by(uri: id, account: @account) + + status = Status.find_by(uri: id, account: @account) + status ||= Status.find_by(uri: activitypub_uri, account: @account) if activitypub_uri? if status.nil? redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) diff --git a/app/lib/ostatus/activity/remote.rb b/app/lib/ostatus/activity/remote.rb index ecec6886c..5b204b6d8 100644 --- a/app/lib/ostatus/activity/remote.rb +++ b/app/lib/ostatus/activity/remote.rb @@ -2,6 +2,10 @@ class OStatus::Activity::Remote < OStatus::Activity::Base def perform - find_status(id) || FetchRemoteStatusService.new.call(url) + if activitypub_uri? + find_status(activitypub_uri) || FetchRemoteStatusService.new.call(url) + else + find_status(id) || FetchRemoteStatusService.new.call(url) + end end end diff --git a/app/lib/ostatus/activity/share.rb b/app/lib/ostatus/activity/share.rb index 290008021..5ca601415 100644 --- a/app/lib/ostatus/activity/share.rb +++ b/app/lib/ostatus/activity/share.rb @@ -10,7 +10,7 @@ class OStatus::Activity::Share < OStatus::Activity::Creation end def object - @xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS) + @xml.at_xpath('.//activity:object', activity: OStatus::TagManager::AS_XMLNS) end private diff --git a/app/lib/ostatus/atom_serializer.rb b/app/lib/ostatus/atom_serializer.rb index 0d62361be..a1ac11a51 100644 --- a/app/lib/ostatus/atom_serializer.rb +++ b/app/lib/ostatus/atom_serializer.rb @@ -15,10 +15,10 @@ class OStatus::AtomSerializer def author(account) author = Ox::Element.new('author') - uri = TagManager.instance.uri_for(account) + uri = OStatus::TagManager.instance.uri_for(account) append_element(author, 'id', uri) - append_element(author, 'activity:object-type', TagManager::TYPES[:person]) + append_element(author, 'activity:object-type', OStatus::TagManager::TYPES[:person]) append_element(author, 'uri', uri) append_element(author, 'name', account.username) append_element(author, 'email', account.local? ? account.local_username_and_domain : account.acct) @@ -65,27 +65,30 @@ class OStatus::AtomSerializer add_namespaces(entry) if root - append_element(entry, 'id', TagManager.instance.unique_tag(stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type)) + append_element(entry, 'id', OStatus::TagManager.instance.uri_for(stream_entry.status)) append_element(entry, 'published', stream_entry.created_at.iso8601) append_element(entry, 'updated', stream_entry.updated_at.iso8601) append_element(entry, 'title', stream_entry&.status&.title || "#{stream_entry.account.acct} deleted status") entry << author(stream_entry.account) if root - append_element(entry, 'activity:object-type', TagManager::TYPES[stream_entry.object_type]) - append_element(entry, 'activity:verb', TagManager::VERBS[stream_entry.verb]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[stream_entry.object_type]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[stream_entry.verb]) entry << object(stream_entry.target) if stream_entry.targeted? if stream_entry.status.nil? append_element(entry, 'content', 'Deleted status') + elsif stream_entry.status.destroyed? + append_element(entry, 'content', 'Deleted status') + append_element(entry, 'link', nil, rel: :alternate, type: 'application/activity+json', href: ActivityPub::TagManager.instance.uri_for(stream_entry.status)) if stream_entry.account.local? else serialize_status_attributes(entry, stream_entry.status) end - append_element(entry, 'link', nil, rel: :alternate, type: 'text/html', href: account_stream_entry_url(stream_entry.account, stream_entry)) + append_element(entry, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(stream_entry.status)) append_element(entry, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')) - append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(stream_entry.thread), href: TagManager.instance.url_for(stream_entry.thread)) if stream_entry.threaded? + append_element(entry, 'thr:in-reply-to', nil, ref: OStatus::TagManager.instance.uri_for(stream_entry.thread), href: TagManager.instance.url_for(stream_entry.thread)) if stream_entry.threaded? append_element(entry, 'ostatus:conversation', nil, ref: conversation_uri(stream_entry.status.conversation)) unless stream_entry&.status&.conversation_id.nil? entry @@ -94,20 +97,20 @@ class OStatus::AtomSerializer def object(status) object = Ox::Element.new('activity:object') - append_element(object, 'id', TagManager.instance.uri_for(status)) + append_element(object, 'id', OStatus::TagManager.instance.uri_for(status)) append_element(object, 'published', status.created_at.iso8601) append_element(object, 'updated', status.updated_at.iso8601) append_element(object, 'title', status.title) object << author(status.account) - append_element(object, 'activity:object-type', TagManager::TYPES[status.object_type]) - append_element(object, 'activity:verb', TagManager::VERBS[status.verb]) + append_element(object, 'activity:object-type', OStatus::TagManager::TYPES[status.object_type]) + append_element(object, 'activity:verb', OStatus::TagManager::VERBS[status.verb]) serialize_status_attributes(object, status) append_element(object, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(status)) - append_element(object, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(status.thread), href: TagManager.instance.url_for(status.thread)) unless status.thread.nil? + append_element(object, 'thr:in-reply-to', nil, ref: OStatus::TagManager.instance.uri_for(status.thread), href: TagManager.instance.url_for(status.thread)) unless status.thread.nil? append_element(object, 'ostatus:conversation', nil, ref: conversation_uri(status.conversation)) unless status.conversation_id.nil? object @@ -119,14 +122,14 @@ class OStatus::AtomSerializer description = "#{follow.account.acct} started following #{follow.target_account.acct}" - append_element(entry, 'id', TagManager.instance.unique_tag(follow.created_at, follow.id, 'Follow')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(follow.created_at, follow.id, 'Follow')) append_element(entry, 'title', description) append_element(entry, 'content', description, type: :html) entry << author(follow.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:follow]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:follow]) object = author(follow.target_account) object.value = 'activity:object' @@ -139,13 +142,13 @@ class OStatus::AtomSerializer entry = Ox::Element.new('entry') add_namespaces(entry) - append_element(entry, 'id', TagManager.instance.unique_tag(follow_request.created_at, follow_request.id, 'FollowRequest')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(follow_request.created_at, follow_request.id, 'FollowRequest')) append_element(entry, 'title', "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}") entry << author(follow_request.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:request_friend]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:request_friend]) object = author(follow_request.target_account) object.value = 'activity:object' @@ -158,19 +161,19 @@ class OStatus::AtomSerializer entry = Ox::Element.new('entry') add_namespaces(entry) - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) append_element(entry, 'title', "#{follow_request.target_account.acct} authorizes follow request by #{follow_request.account.acct}") entry << author(follow_request.target_account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:authorize]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:authorize]) object = Ox::Element.new('activity:object') object << author(follow_request.account) - append_element(object, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(object, 'activity:verb', TagManager::VERBS[:request_friend]) + append_element(object, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(object, 'activity:verb', OStatus::TagManager::VERBS[:request_friend]) inner_object = author(follow_request.target_account) inner_object.value = 'activity:object' @@ -184,19 +187,19 @@ class OStatus::AtomSerializer entry = Ox::Element.new('entry') add_namespaces(entry) - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) append_element(entry, 'title', "#{follow_request.target_account.acct} rejects follow request by #{follow_request.account.acct}") entry << author(follow_request.target_account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:reject]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:reject]) object = Ox::Element.new('activity:object') object << author(follow_request.account) - append_element(object, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(object, 'activity:verb', TagManager::VERBS[:request_friend]) + append_element(object, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(object, 'activity:verb', OStatus::TagManager::VERBS[:request_friend]) inner_object = author(follow_request.target_account) inner_object.value = 'activity:object' @@ -212,14 +215,14 @@ class OStatus::AtomSerializer description = "#{follow.account.acct} is no longer following #{follow.target_account.acct}" - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow.id, 'Follow')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(Time.now.utc, follow.id, 'Follow')) append_element(entry, 'title', description) append_element(entry, 'content', description, type: :html) entry << author(follow.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:unfollow]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:unfollow]) object = author(follow.target_account) object.value = 'activity:object' @@ -234,13 +237,13 @@ class OStatus::AtomSerializer description = "#{block.account.acct} no longer wishes to interact with #{block.target_account.acct}" - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) append_element(entry, 'title', description) entry << author(block.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:block]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:block]) object = author(block.target_account) object.value = 'activity:object' @@ -255,13 +258,13 @@ class OStatus::AtomSerializer description = "#{block.account.acct} no longer blocks #{block.target_account.acct}" - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) append_element(entry, 'title', description) entry << author(block.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:unblock]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:unblock]) object = author(block.target_account) object.value = 'activity:object' @@ -276,18 +279,18 @@ class OStatus::AtomSerializer description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}" - append_element(entry, 'id', TagManager.instance.unique_tag(favourite.created_at, favourite.id, 'Favourite')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(favourite.created_at, favourite.id, 'Favourite')) append_element(entry, 'title', description) append_element(entry, 'content', description, type: :html) entry << author(favourite.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:favorite]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:favorite]) entry << object(favourite.status) - append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) + append_element(entry, 'thr:in-reply-to', nil, ref: OStatus::TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) entry end @@ -298,18 +301,18 @@ class OStatus::AtomSerializer description = "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}" - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, favourite.id, 'Favourite')) + append_element(entry, 'id', OStatus::TagManager.instance.unique_tag(Time.now.utc, favourite.id, 'Favourite')) append_element(entry, 'title', description) append_element(entry, 'content', description, type: :html) entry << author(favourite.account) - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:unfavorite]) + append_element(entry, 'activity:object-type', OStatus::TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', OStatus::TagManager::VERBS[:unfavorite]) entry << object(favourite.status) - append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) + append_element(entry, 'thr:in-reply-to', nil, ref: OStatus::TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) entry end @@ -329,28 +332,30 @@ class OStatus::AtomSerializer def conversation_uri(conversation) return conversation.uri if conversation.uri? - TagManager.instance.unique_tag(conversation.created_at, conversation.id, 'Conversation') + OStatus::TagManager.instance.unique_tag(conversation.created_at, conversation.id, 'Conversation') end def add_namespaces(parent) - parent['xmlns'] = TagManager::XMLNS - parent['xmlns:thr'] = TagManager::THR_XMLNS - parent['xmlns:activity'] = TagManager::AS_XMLNS - parent['xmlns:poco'] = TagManager::POCO_XMLNS - parent['xmlns:media'] = TagManager::MEDIA_XMLNS - parent['xmlns:ostatus'] = TagManager::OS_XMLNS - parent['xmlns:mastodon'] = TagManager::MTDN_XMLNS + parent['xmlns'] = OStatus::TagManager::XMLNS + parent['xmlns:thr'] = OStatus::TagManager::THR_XMLNS + parent['xmlns:activity'] = OStatus::TagManager::AS_XMLNS + parent['xmlns:poco'] = OStatus::TagManager::POCO_XMLNS + parent['xmlns:media'] = OStatus::TagManager::MEDIA_XMLNS + parent['xmlns:ostatus'] = OStatus::TagManager::OS_XMLNS + parent['xmlns:mastodon'] = OStatus::TagManager::MTDN_XMLNS end def serialize_status_attributes(entry, status) + append_element(entry, 'link', nil, rel: :alternate, type: 'application/activity+json', href: ActivityPub::TagManager.instance.uri_for(status)) if status.account.local? + append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text? append_element(entry, 'content', Formatter.instance.format(status).to_str, type: 'html', 'xml:lang': status.language) status.mentions.each do |mentioned| - append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:person], href: TagManager.instance.uri_for(mentioned.account)) + append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': OStatus::TagManager::TYPES[:person], href: OStatus::TagManager.instance.uri_for(mentioned.account)) end - append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:collection], href: TagManager::COLLECTIONS[:public]) if status.public_visibility? + append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': OStatus::TagManager::TYPES[:collection], href: OStatus::TagManager::COLLECTIONS[:public]) if status.public_visibility? status.tags.each do |tag| append_element(entry, 'category', nil, term: tag.name) @@ -363,5 +368,9 @@ class OStatus::AtomSerializer end append_element(entry, 'mastodon:scope', status.visibility) + + status.emojis.each do |emoji| + append_element(entry, 'link', nil, rel: :emoji, href: full_asset_url(emoji.image.url), name: emoji.shortcode) + end end end diff --git a/app/lib/ostatus/tag_manager.rb b/app/lib/ostatus/tag_manager.rb new file mode 100644 index 000000000..4f4501312 --- /dev/null +++ b/app/lib/ostatus/tag_manager.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +class OStatus::TagManager + include Singleton + include RoutingHelper + + VERBS = { + post: 'http://activitystrea.ms/schema/1.0/post', + share: 'http://activitystrea.ms/schema/1.0/share', + favorite: 'http://activitystrea.ms/schema/1.0/favorite', + unfavorite: 'http://activitystrea.ms/schema/1.0/unfavorite', + delete: 'http://activitystrea.ms/schema/1.0/delete', + follow: 'http://activitystrea.ms/schema/1.0/follow', + request_friend: 'http://activitystrea.ms/schema/1.0/request-friend', + authorize: 'http://activitystrea.ms/schema/1.0/authorize', + reject: 'http://activitystrea.ms/schema/1.0/reject', + unfollow: 'http://ostatus.org/schema/1.0/unfollow', + block: 'http://mastodon.social/schema/1.0/block', + unblock: 'http://mastodon.social/schema/1.0/unblock', + }.freeze + + TYPES = { + activity: 'http://activitystrea.ms/schema/1.0/activity', + note: 'http://activitystrea.ms/schema/1.0/note', + comment: 'http://activitystrea.ms/schema/1.0/comment', + person: 'http://activitystrea.ms/schema/1.0/person', + collection: 'http://activitystrea.ms/schema/1.0/collection', + group: 'http://activitystrea.ms/schema/1.0/group', + }.freeze + + COLLECTIONS = { + public: 'http://activityschema.org/collection/public', + }.freeze + + XMLNS = 'http://www.w3.org/2005/Atom' + MEDIA_XMLNS = 'http://purl.org/syndication/atommedia' + AS_XMLNS = 'http://activitystrea.ms/spec/1.0/' + THR_XMLNS = 'http://purl.org/syndication/thread/1.0' + POCO_XMLNS = 'http://portablecontacts.net/spec/1.0' + DFRN_XMLNS = 'http://purl.org/macgirvin/dfrn/1.0' + OS_XMLNS = 'http://ostatus.org/schema/1.0' + MTDN_XMLNS = 'http://mastodon.social/schema/1.0' + + def unique_tag(date, id, type) + "tag:#{Rails.configuration.x.local_domain},#{date.strftime('%Y-%m-%d')}:objectId=#{id}:objectType=#{type}" + end + + def unique_tag_to_local_id(tag, expected_type) + return nil unless local_id?(tag) + + if ActivityPub::TagManager.instance.local_uri?(tag) + ActivityPub::TagManager.instance.uri_to_local_id(tag) + else + matches = Regexp.new("objectId=([\\d]+):objectType=#{expected_type}").match(tag) + return matches[1] unless matches.nil? + end + end + + def local_id?(id) + id.start_with?("tag:#{Rails.configuration.x.local_domain}") || ActivityPub::TagManager.instance.local_uri?(id) + end + + def uri_for(target) + return target.uri if target.respond_to?(:local?) && !target.local? + + case target.object_type + when :person + account_url(target) + when :note, :comment, :activity + target.uri || unique_tag(target.created_at, target.id, 'Status') + end + end +end |