From 071f64d317408b556c4f85e2f2abd80fe815de8b Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 19 Mar 2016 00:16:04 +0100 Subject: [PATCH] Fix #13, Atom feeds now contain formatted post content --- app/helpers/application_helper.rb | 11 +++++++++++ app/helpers/atom_builder_helper.rb | 20 ++++++++++++++++++-- app/helpers/stream_entries_helper.rb | 11 ----------- app/services/base_service.rb | 1 + spec/helpers/application_helper_spec.rb | 4 ++++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 18aec6dbe..56207c4bb 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -12,6 +12,17 @@ module ApplicationHelper id.start_with?("tag:#{Rails.configuration.x.local_domain}") end + def linkify(status) + mention_hash = {} + status.mentions.each { |m| mention_hash[m.acct] = m } + coder = HTMLEntities.new + + auto_link(coder.encode(status.text), link: :urls, html: { rel: 'nofollow noopener' }).gsub(Account::MENTION_RE) do |m| + account = mention_hash[Account::MENTION_RE.match(m)[1]] + return "#{m.split('@').first}@#{account.acct}" + end.html_safe + end + def active_nav_class(path) current_page?(path) ? 'active' : '' end diff --git a/app/helpers/atom_builder_helper.rb b/app/helpers/atom_builder_helper.rb index 95d7c96ec..0cfc438ac 100644 --- a/app/helpers/atom_builder_helper.rb +++ b/app/helpers/atom_builder_helper.rb @@ -135,6 +135,22 @@ module AtomBuilderHelper xml.logo url end + def conditionally_formatted(activity) + if activity.is_a?(Status) + if activity.reblog? && activity.reblog.local? + linkify(activity.reblog) + elsif activity.local? + linkify(activity) + else + activity.content + end + elsif activity.nil? + '' + else + activity.content + end + end + def include_author(xml, account) object_type xml, :person uri xml, url_for_target(account) @@ -150,7 +166,7 @@ module AtomBuilderHelper published_at xml, stream_entry.created_at updated_at xml, stream_entry.updated_at title xml, stream_entry.title - content xml, stream_entry.content + content xml, conditionally_formatted(stream_entry.activity) verb xml, stream_entry.verb link_self xml, account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom') object_type xml, stream_entry.object_type @@ -176,7 +192,7 @@ module AtomBuilderHelper # Statuses have content if [:note, :comment].include? stream_entry.target.object_type - content xml, stream_entry.target.content + content xml, conditionally_formatted(stream_entry.target) end end end diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb index 5adb09f77..b044e8a61 100644 --- a/app/helpers/stream_entries_helper.rb +++ b/app/helpers/stream_entries_helper.rb @@ -20,17 +20,6 @@ module StreamEntriesHelper date < 5.days.ago ? date.strftime("%d.%m.%Y") : "#{time_ago_in_words(date)} ago" end - def linkify(status) - mention_hash = {} - status.mentions.each { |m| mention_hash[m.acct] = m } - coder = HTMLEntities.new - - auto_link(coder.encode(status.text), link: :urls, html: { rel: 'nofollow noopener' }).gsub(Account::MENTION_RE) do |m| - account = mention_hash[Account::MENTION_RE.match(m)[1]] - "#{m.split('@').first}@#{account.acct}" - end.html_safe - end - def reblogged_by_me_class(status) user_signed_in? && current_user.account.reblogged?(status) ? 'reblogged' : '' end diff --git a/app/services/base_service.rb b/app/services/base_service.rb index 5a92f7f29..ed86f2230 100644 --- a/app/services/base_service.rb +++ b/app/services/base_service.rb @@ -1,5 +1,6 @@ class BaseService include RoutingHelper + include ActionView::Helpers::TextHelper include ApplicationHelper include AtomBuilderHelper end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index c19f636ca..ca38c792c 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -28,4 +28,8 @@ RSpec.describe ApplicationHelper, type: :helper do expect(helper.local_id?('tag:foreign.tld;objectId=12:objectType=Status')).to be false end end + + describe '#linkify' do + pending + end end