0
0
Fork 0

Refactored generation of unique tags, URIs and object URLs into own classes,

as well as formatting of content
This commit is contained in:
Eugen Rochko 2016-09-09 20:04:34 +02:00
parent 735b4cc62e
commit 3cc47beb6e
28 changed files with 316 additions and 180 deletions

View file

@ -1,54 +1,4 @@
module ApplicationHelper
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 content_for_status(actual_status)
if actual_status.local?
linkify(actual_status)
else
sanitize(actual_status.content, tags: %w(a br p), attributes: %w(href rel))
end
end
def account_from_mentions(search_string, mentions)
mentions.each { |x| return x.account if x.account.acct.eql?(search_string) }
nil
# If that was unsuccessful, try fetching user from db separately
# But this shouldn't ever happen if the mentions were created correctly!
# username, domain = search_string.split('@')
# if domain == Rails.configuration.x.local_domain
# account = Account.find_local(username)
# else
# account = Account.find_remote(username, domain)
# end
# account
end
def linkify(status)
auto_link(HTMLEntities.new.encode(status.text), link: :urls, html: { rel: 'nofollow noopener' }).gsub(Account::MENTION_RE) do |m|
account = account_from_mentions(Account::MENTION_RE.match(m)[1], status.mentions)
unless account.nil?
"#{m.split('@').first}<a href=\"#{url_for_target(account)}\" class=\"mention\">@<span>#{account.acct}</span></a>"
else
m
end
end.html_safe
end
def active_nav_class(path)
current_page?(path) ? 'active' : ''
end

View file

@ -16,7 +16,7 @@ module AtomBuilderHelper
end
def unique_id(xml, date, id, type)
xml.id_ unique_tag(date, id, type)
xml.id_ TagManager.instance.unique_tag(date, id, type)
end
def simple_id(xml, id)
@ -97,32 +97,8 @@ module AtomBuilderHelper
xml['thr'].send('in-reply-to', { ref: uri, href: url, type: 'text/html' })
end
def uri_for_target(target)
if target.local?
if target.object_type == :person
account_url(target)
else
unique_tag(target.stream_entry.created_at, target.stream_entry.activity_id, target.stream_entry.activity_type)
end
else
target.uri
end
end
def url_for_target(target)
if target.local?
if target.object_type == :person
account_url(target)
else
account_stream_entry_url(target.account, target.stream_entry)
end
else
target.url
end
end
def link_mention(xml, account)
xml.link(rel: 'mentioned', href: uri_for_target(account))
xml.link(rel: 'mentioned', href: TagManager.instance.uri_for(account))
end
def link_enclosure(xml, media)
@ -145,7 +121,7 @@ module AtomBuilderHelper
def conditionally_formatted(activity)
if activity.is_a?(Status)
content_for_status(activity.reblog? ? activity.reblog : activity)
Formatter.instance.format(activity.reblog? ? activity.reblog : activity)
elsif activity.nil?
nil
else
@ -155,11 +131,11 @@ module AtomBuilderHelper
def include_author(xml, account)
object_type xml, :person
uri xml, uri_for_target(account)
uri xml, TagManager.instance.uri_for(account)
name xml, account.username
email xml, account.local? ? "#{account.acct}@#{Rails.configuration.x.local_domain}" : account.acct
summary xml, account.note
link_alternate xml, url_for_target(account)
link_alternate xml, TagManager.instance.url_for(account)
link_avatar xml, account
portable_contact xml, account
end
@ -176,7 +152,7 @@ module AtomBuilderHelper
# Comments need thread element
if stream_entry.threaded?
in_reply_to xml, uri_for_target(stream_entry.thread), url_for_target(stream_entry.thread)
in_reply_to xml, TagManager.instance.uri_for(stream_entry.thread), TagManager.instance.url_for(stream_entry.thread)
end
if stream_entry.targeted?
@ -185,9 +161,9 @@ module AtomBuilderHelper
include_author xml, stream_entry.target
else
object_type xml, stream_entry.target.object_type
simple_id xml, uri_for_target(stream_entry.target)
simple_id xml, TagManager.instance.uri_for(stream_entry.target)
title xml, stream_entry.target.title
link_alternate xml, url_for_target(stream_entry.target)
link_alternate xml, TagManager.instance.url_for(stream_entry.target)
end
# Statuses have content and author