about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/feed_manager.rb8
-rw-r--r--app/lib/formatter.rb47
-rw-r--r--app/lib/tag_manager.rb41
3 files changed, 94 insertions, 2 deletions
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
index a19d06a85..a0c480b94 100644
--- a/app/lib/feed_manager.rb
+++ b/app/lib/feed_manager.rb
@@ -1,11 +1,15 @@
+require 'singleton'
+
 class FeedManager
+  include Singleton
+
   MAX_ITEMS = 800
 
-  def self.key(type, id)
+  def key(type, id)
     "feed:#{type}:#{id}"
   end
 
-  def self.filter_status?(status, follower)
+  def filter_status?(status, follower)
     replied_to_user = status.reply? ? status.thread.account : nil
     (status.reply? && !(follower.id = replied_to_user.id || follower.following?(replied_to_user)))
   end
diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb
new file mode 100644
index 000000000..52e229520
--- /dev/null
+++ b/app/lib/formatter.rb
@@ -0,0 +1,47 @@
+require 'singleton'
+
+class Formatter
+  include Singleton
+
+  include ActionView::Helpers::TextHelper
+  include ActionView::Helpers::SanitizeHelper
+
+  def format(status)
+    return reformat(status) unless status.local?
+
+    html = status.text
+    html = encode(html)
+    html = link_urls(html)
+    html = link_mentions(html, status.mentions)
+
+    html.html_safe
+  end
+
+  def reformat(status)
+    sanitize(status.content, tags: %w(a br p), attributes: %w(href rel))
+  end
+
+  private
+
+  def encode(html)
+    HTMLEntities.new.encode(html)
+  end
+
+  def link_urls(html)
+    auto_link(html, link: :urls, html: { rel: 'nofollow noopener' })
+  end
+
+  def link_mentions(html, mentions)
+    html.gsub(Account::MENTION_RE) do |match|
+      acct    = Account::MENTION_RE.match(match)[1]
+      mention = mentions.find { |mention| mention.account.acct.eql?(acct) }
+
+      return match if mention.nil?
+      mention_html(match, mention.account)
+    end
+  end
+
+  def mention_html(match, account)
+    "#{match.split('@').first}<a href=\"#{TagManager.instance.url_for(account)}\" class=\"mention\">@<span>#{account.acct}</span></a>"
+  end
+end
diff --git a/app/lib/tag_manager.rb b/app/lib/tag_manager.rb
new file mode 100644
index 000000000..4d29ca1f8
--- /dev/null
+++ b/app/lib/tag_manager.rb
@@ -0,0 +1,41 @@
+require 'singleton'
+
+class TagManager
+  include Singleton
+  include RoutingHelper
+
+  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
+
+  def uri_for(target)
+    return target.uri if target.respond_to?(:local?) && !target.local?
+
+    case target.object_type
+    when :person
+      account_url(target)
+    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
+      account_url(target)
+    else
+      account_stream_entry_url(target.account, target.stream_entry)
+    end
+  end
+end