Sanitize remote html in atom feeds, API (not just UI), use cached mention

relations on Status#mentions
This commit is contained in:
Eugen Rochko 2016-03-24 12:40:55 +01:00
parent 7cd3de3494
commit c8999a116e
6 changed files with 18 additions and 31 deletions

View file

@ -12,6 +12,14 @@ module ApplicationHelper
id.start_with?("tag:#{Rails.configuration.x.local_domain}") id.start_with?("tag:#{Rails.configuration.x.local_domain}")
end 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 linkify(status) def linkify(status)
mention_hash = {} mention_hash = {}
status.mentions.each { |m| mention_hash[m.acct] = m } status.mentions.each { |m| mention_hash[m.acct] = m }

View file

@ -137,13 +137,7 @@ module AtomBuilderHelper
def conditionally_formatted(activity) def conditionally_formatted(activity)
if activity.is_a?(Status) if activity.is_a?(Status)
if activity.reblog? && activity.reblog.local? content_for_status(activity.reblog? ? activity.reblog : activity)
linkify(activity.reblog)
elsif !activity.reblog? && activity.local?
linkify(activity)
else
activity.content
end
elsif activity.nil? elsif activity.nil?
nil nil
else else

View file

@ -27,12 +27,4 @@ module StreamEntriesHelper
def favourited_by_me_class(status) def favourited_by_me_class(status)
user_signed_in? && current_user.account.favourited?(status) ? 'favourited' : '' user_signed_in? && current_user.account.favourited?(status) ? 'favourited' : ''
end 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
end end

View file

@ -60,22 +60,15 @@ class Status < ActiveRecord::Base
end end
def mentions def mentions
m = [] if @mentions.nil?
@mentions = []
m << thread.account if reply? @mentions << thread.account if reply?
m << reblog.account if reblog? @mentions << reblog.account if reblog?
self.mentioned_accounts.each { |mention| @mentions << mention.account } unless reblog?
unless reblog? @mentions = @mentions.uniq
self.text.scan(Account::MENTION_RE).each do |match|
uri = match.first
username, domain = uri.split('@')
account = Account.find_by(username: username, domain: domain)
m << account unless account.nil?
end
end end
m.uniq @mentions
end end
def ancestors def ancestors

View file

@ -2,7 +2,7 @@ object @status
attributes :id, :created_at, :in_reply_to_id attributes :id, :created_at, :in_reply_to_id
node(:uri) { |status| uri_for_target(status) } node(:uri) { |status| uri_for_target(status) }
node(:content) { |status| status.local? ? linkify(status) : status.content } node(:content) { |status| content_for_status(status) }
node(:url) { |status| url_for_target(status) } node(:url) { |status| url_for_target(status) }
node(:reblogs_count) { |status| status.reblogs_count } node(:reblogs_count) { |status| status.reblogs_count }
node(:favourites_count) { |status| status.favourites_count } node(:favourites_count) { |status| status.favourites_count }

View file

@ -50,7 +50,7 @@ RSpec.describe Status, type: :model do
end end
it 'returns mentioned accounts' do it 'returns mentioned accounts' do
subject.text = 'Hello @bob!' subject.mentioned_accounts.create!(account: bob)
expect(subject.mentions).to include bob expect(subject.mentions).to include bob
end end