tag_manager.rb 3.71 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8
require 'singleton'

class TagManager
  include Singleton
  include RoutingHelper

9
  VERBS = {
10 11 12 13 14 15 16 17 18 19 20 21
    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',
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }.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'
44
  MTDN_XMLNS  = 'http://mastodon.social/schema/1.0'
45

46 47 48 49 50 51 52 53 54 55 56 57 58
  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)
    matches = Regexp.new("objectId=([\\d]+):objectType=#{expected_type}").match(tag)
    return matches[1] unless matches.nil?
  end

  def local_id?(id)
    id.start_with?("tag:#{Rails.configuration.x.local_domain}")
  end

59 60 61 62
  def web_domain?(domain)
    domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.web_domain).zero?
  end

63
  def local_domain?(domain)
64
    domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.local_domain).zero?
65 66
  end

67
  def normalize_domain(domain)
68 69
    return if domain.nil?

70
    uri = Addressable::URI.new
71
    uri.host = domain.gsub(/[\/]/, '')
72 73 74
    uri.normalize.host
  end

75 76 77 78 79 80
  def same_acct?(canonical, needle)
    return true if canonical.casecmp(needle).zero?
    username, domain = needle.split('@')
    local_domain?(domain) && canonical.casecmp(username).zero?
  end

81
  def local_url?(url)
82
    uri    = Addressable::URI.parse(url).normalize
83 84 85 86
    domain = uri.host + (uri.port ? ":#{uri.port}" : '')
    TagManager.instance.local_domain?(domain)
  end

87 88 89 90 91 92
  def uri_for(target)
    return target.uri if target.respond_to?(:local?) && !target.local?

    case target.object_type
    when :person
      account_url(target)
93 94
    when :note, :comment, :activity
      unique_tag(target.created_at, target.id, 'Status')
95 96 97 98 99 100 101 102 103 104
    else
      unique_tag(target.stream_entry.created_at, target.stream_entry.activity_id, target.stream_entry.activity_type)
    end
  end

  def url_for(target)
    return target.url if target.respond_to?(:local?) && !target.local?

    case target.object_type
    when :person
105 106 107
      short_account_url(target)
    when :note, :comment, :activity
      short_account_status_url(target.account, target)
108 109 110 111 112
    else
      account_stream_entry_url(target.account, target.stream_entry)
    end
  end
end