Change RSS feeds (#18356)
* Change RSS feeds - Use date and time for titles instead of ellipsized text - Use full content in body, even when there is a content warning - Use media extensions * Change feed icons and add width and height attributes to custom emojis * Fix custom emoji animate on hover breaking * Fix testslocal
parent
f17e73da09
commit
2b8dc58b7f
19 changed files with 311 additions and 307 deletions
@ -0,0 +1,33 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::Builder |
||||
attr_reader :dsl |
||||
|
||||
def self.build |
||||
new.tap do |builder| |
||||
yield builder.dsl |
||||
end.to_xml |
||||
end |
||||
|
||||
def initialize |
||||
@dsl = RSS::Channel.new |
||||
end |
||||
|
||||
def to_xml |
||||
('<?xml version="1.0" encoding="UTF-8"?>'.dup << Ox.dump(wrap_in_document, effort: :tolerant)).force_encoding('UTF-8') |
||||
end |
||||
|
||||
private |
||||
|
||||
def wrap_in_document |
||||
Ox::Document.new(version: '1.0').tap do |document| |
||||
document << Ox::Element.new('rss').tap do |rss| |
||||
rss['version'] = '2.0' |
||||
rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0' |
||||
rss['xmlns:media'] = 'http://search.yahoo.com/mrss/' |
||||
|
||||
rss << @dsl.to_element |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,49 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::Channel < RSS::Element |
||||
def initialize |
||||
super() |
||||
|
||||
@root = create_element('channel') |
||||
end |
||||
|
||||
def title(str) |
||||
append_element('title', str) |
||||
end |
||||
|
||||
def link(str) |
||||
append_element('link', str) |
||||
end |
||||
|
||||
def last_build_date(date) |
||||
append_element('lastBuildDate', date.to_formatted_s(:rfc822)) |
||||
end |
||||
|
||||
def image(url, title, link) |
||||
append_element('image') do |image| |
||||
image << create_element('url', url) |
||||
image << create_element('title', title) |
||||
image << create_element('link', link) |
||||
end |
||||
end |
||||
|
||||
def description(str) |
||||
append_element('description', str) |
||||
end |
||||
|
||||
def generator(str) |
||||
append_element('generator', str) |
||||
end |
||||
|
||||
def icon(str) |
||||
append_element('webfeeds:icon', str) |
||||
end |
||||
|
||||
def logo(str) |
||||
append_element('webfeeds:logo', str) |
||||
end |
||||
|
||||
def item(&block) |
||||
@root << RSS::Item.with(&block) |
||||
end |
||||
end |
@ -0,0 +1,24 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::Element |
||||
def self.with(*args, &block) |
||||
new(*args).tap(&block).to_element |
||||
end |
||||
|
||||
def create_element(name, content = nil) |
||||
Ox::Element.new(name).tap do |element| |
||||
yield element if block_given? |
||||
element << content if content.present? |
||||
end |
||||
end |
||||
|
||||
def append_element(name, content = nil) |
||||
@root << create_element(name, content).tap do |element| |
||||
yield element if block_given? |
||||
end |
||||
end |
||||
|
||||
def to_element |
||||
@root |
||||
end |
||||
end |
@ -0,0 +1,45 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::Item < RSS::Element |
||||
def initialize |
||||
super() |
||||
|
||||
@root = create_element('item') |
||||
end |
||||
|
||||
def title(str) |
||||
append_element('title', str) |
||||
end |
||||
|
||||
def link(str) |
||||
append_element('guid', str) do |guid| |
||||
guid['isPermaLink'] = 'true' |
||||
end |
||||
|
||||
append_element('link', str) |
||||
end |
||||
|
||||
def pub_date(date) |
||||
append_element('pubDate', date.to_formatted_s(:rfc822)) |
||||
end |
||||
|
||||
def description(str) |
||||
append_element('description', str) |
||||
end |
||||
|
||||
def category(str) |
||||
append_element('category', str) |
||||
end |
||||
|
||||
def enclosure(url, type, size) |
||||
append_element('enclosure') do |enclosure| |
||||
enclosure['url'] = url |
||||
enclosure['length'] = size |
||||
enclosure['type'] = type |
||||
end |
||||
end |
||||
|
||||
def media_content(url, type, size, &block) |
||||
@root << RSS::MediaContent.with(url, type, size, &block) |
||||
end |
||||
end |
@ -0,0 +1,29 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::MediaContent < RSS::Element |
||||
def initialize(url, type, size) |
||||
super() |
||||
|
||||
@root = create_element('media:content') do |content| |
||||
content['url'] = url |
||||
content['type'] = type |
||||
content['fileSize'] = size |
||||
end |
||||
end |
||||
|
||||
def medium(str) |
||||
@root['medium'] = str |
||||
end |
||||
|
||||
def rating(str) |
||||
append_element('media:rating', str) do |rating| |
||||
rating['scheme'] = 'urn:simple' |
||||
end |
||||
end |
||||
|
||||
def description(str) |
||||
append_element('media:description', str) do |description| |
||||
description['type'] = 'plain' |
||||
end |
||||
end |
||||
end |
@ -1,55 +0,0 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::Serializer |
||||
include FormattingHelper |
||||
|
||||
private |
||||
|
||||
def render_statuses(builder, statuses) |
||||
statuses.each do |status| |
||||
builder.item do |item| |
||||
item.title(status_title(status)) |
||||
.link(ActivityPub::TagManager.instance.url_for(status)) |
||||
.pub_date(status.created_at) |
||||
.description(status_description(status)) |
||||
|
||||
status.ordered_media_attachments.each do |media| |
||||
item.enclosure(full_asset_url(media.file.url(:original, false)), media.file.content_type, media.file.size) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
def status_title(status) |
||||
preview = status.proper.spoiler_text.presence || status.proper.text |
||||
|
||||
if preview.length > 30 || preview[0, 30].include?("\n") |
||||
preview = preview[0, 30] |
||||
preview = preview[0, preview.index("\n").presence || 30] + '…' |
||||
end |
||||
|
||||
preview = "#{status.proper.spoiler_text.present? ? 'CW ' : ''}“#{preview}”#{status.proper.sensitive? ? ' (sensitive)' : ''}" |
||||
|
||||
if status.reblog? |
||||
"#{status.account.acct} boosted #{status.reblog.account.acct}: #{preview}" |
||||
else |
||||
"#{status.account.acct}: #{preview}" |
||||
end |
||||
end |
||||
|
||||
def status_description(status) |
||||
if status.proper.spoiler_text? |
||||
status.proper.spoiler_text |
||||
else |
||||
html = status_content_format(status.proper).to_str |
||||
after_html = '' |
||||
|
||||
if status.proper.preloadable_poll |
||||
poll_options_html = status.proper.preloadable_poll.options.map { |o| "[ ] #{o}" }.join('<br />') |
||||
after_html = "<p>#{poll_options_html}</p>" |
||||
end |
||||
|
||||
"#{html}#{after_html}" |
||||
end |
||||
end |
||||
end |
@ -1,130 +0,0 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSSBuilder |
||||
class ItemBuilder |
||||
def initialize |
||||
@item = Ox::Element.new('item') |
||||
end |
||||
|
||||
def title(str) |
||||
@item << (Ox::Element.new('title') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def link(str) |
||||
@item << Ox::Element.new('guid').tap do |guid| |
||||
guid['isPermalink'] = 'true' |
||||
guid << str |
||||
end |
||||
|
||||
@item << (Ox::Element.new('link') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def pub_date(date) |
||||
@item << (Ox::Element.new('pubDate') << date.to_formatted_s(:rfc822)) |
||||
|
||||
self |
||||
end |
||||
|
||||
def description(str) |
||||
@item << (Ox::Element.new('description') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def enclosure(url, type, size) |
||||
@item << Ox::Element.new('enclosure').tap do |enclosure| |
||||
enclosure['url'] = url |
||||
enclosure['length'] = size |
||||
enclosure['type'] = type |
||||
end |
||||
|
||||
self |
||||
end |
||||
|
||||
def to_element |
||||
@item |
||||
end |
||||
end |
||||
|
||||
def initialize |
||||
@document = Ox::Document.new(version: '1.0') |
||||
@channel = Ox::Element.new('channel') |
||||
|
||||
@document << (rss << @channel) |
||||
end |
||||
|
||||
def title(str) |
||||
@channel << (Ox::Element.new('title') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def link(str) |
||||
@channel << (Ox::Element.new('link') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def image(str) |
||||
@channel << Ox::Element.new('image').tap do |image| |
||||
image << (Ox::Element.new('url') << str) |
||||
image << (Ox::Element.new('title') << '') |
||||
image << (Ox::Element.new('link') << '') |
||||
end |
||||
|
||||
@channel << (Ox::Element.new('webfeeds:icon') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def cover(str) |
||||
@channel << Ox::Element.new('webfeeds:cover').tap do |cover| |
||||
cover['image'] = str |
||||
end |
||||
|
||||
self |
||||
end |
||||
|
||||
def logo(str) |
||||
@channel << (Ox::Element.new('webfeeds:logo') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def accent_color(str) |
||||
@channel << (Ox::Element.new('webfeeds:accentColor') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def description(str) |
||||
@channel << (Ox::Element.new('description') << str) |
||||
|
||||
self |
||||
end |
||||
|
||||
def item |
||||
@channel << ItemBuilder.new.tap do |item| |
||||
yield item |
||||
end.to_element |
||||
|
||||
self |
||||
end |
||||
|
||||
def to_xml |
||||
('<?xml version="1.0" encoding="UTF-8"?>' + Ox.dump(@document, effort: :tolerant)).force_encoding('UTF-8') |
||||
end |
||||
|
||||
private |
||||
|
||||
def rss |
||||
Ox::Element.new('rss').tap do |rss| |
||||
rss['version'] = '2.0' |
||||
rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0' |
||||
end |
||||
end |
||||
end |
@ -1,28 +0,0 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::AccountSerializer < RSS::Serializer |
||||
include ActionView::Helpers::NumberHelper |
||||
include AccountsHelper |
||||
include RoutingHelper |
||||
|
||||
def render(account, statuses, tag) |
||||
builder = RSSBuilder.new |
||||
|
||||
builder.title("#{display_name(account)} (@#{account.local_username_and_domain})") |
||||
.description(account_description(account)) |
||||
.link(tag.present? ? short_account_tag_url(account, tag) : short_account_url(account)) |
||||
.logo(full_pack_url('media/images/logo.svg')) |
||||
.accent_color('2b90d9') |
||||
|
||||
builder.image(full_asset_url(account.avatar.url(:original))) if account.avatar? |
||||
builder.cover(full_asset_url(account.header.url(:original))) if account.header? |
||||
|
||||
render_statuses(builder, statuses) |
||||
|
||||
builder.to_xml |
||||
end |
||||
|
||||
def self.render(account, statuses, tag) |
||||
new.render(account, statuses, tag) |
||||
end |
||||
end |
@ -1,25 +0,0 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class RSS::TagSerializer < RSS::Serializer |
||||
include ActionView::Helpers::NumberHelper |
||||
include ActionView::Helpers::SanitizeHelper |
||||
include RoutingHelper |
||||
|
||||
def render(tag, statuses) |
||||
builder = RSSBuilder.new |
||||
|
||||
builder.title("##{tag.name}") |
||||
.description(strip_tags(I18n.t('about.about_hashtag_html', hashtag: tag.name))) |
||||
.link(tag_url(tag)) |
||||
.logo(full_pack_url('media/images/logo.svg')) |
||||
.accent_color('2b90d9') |
||||
|
||||
render_statuses(builder, statuses) |
||||
|
||||
builder.to_xml |
||||
end |
||||
|
||||
def self.render(tag, statuses) |
||||
new.render(tag, statuses) |
||||
end |
||||
end |
@ -0,0 +1,37 @@ |
||||
RSS::Builder.build do |doc| |
||||
doc.title(display_name(@account)) |
||||
doc.description(I18n.t('rss.descriptions.account', acct: @account.local_username_and_domain)) |
||||
doc.link(params[:tag].present? ? short_account_tag_url(@account, params[:tag]) : short_account_url(@account)) |
||||
doc.image(full_asset_url(@account.avatar.url(:original)), display_name(@account), params[:tag].present? ? short_account_tag_url(@account, params[:tag]) : short_account_url(@account)) |
||||
doc.last_build_date(@statuses.first.created_at) if @statuses.any? |
||||
doc.icon(full_asset_url(@account.avatar.url(:original))) |
||||
doc.logo(full_pack_url('media/images/logo_transparent_white.svg')) |
||||
doc.generator("Mastodon v#{Mastodon::Version.to_s}") |
||||
|
||||
@statuses.each do |status| |
||||
doc.item do |item| |
||||
item.title(l(status.created_at)) |
||||
item.link(ActivityPub::TagManager.instance.url_for(status)) |
||||
item.pub_date(status.created_at) |
||||
item.description(rss_status_content_format(status)) |
||||
|
||||
if status.ordered_media_attachments.first&.audio? |
||||
media = status.ordered_media_attachments.first |
||||
item.enclosure(full_asset_url(media.file.url(:original, false)), media.file.content_type, media.file.size) |
||||
end |
||||
|
||||
status.ordered_media_attachments.each do |media| |
||||
item.media_content(full_asset_url(media.file.url(:original, false)), media.file.content_type, media.file.size) do |media_content| |
||||
media_content.medium(media.gifv? ? 'image' : media.type.to_s) |
||||
media_content.rating(status.sensitive? ? 'adult' : 'nonadult') |
||||
media_content.description(media.description) if media.description.present? |
||||
media_content.thumbnail(media.thumbnail.url(:original, false)) if media.thumbnail? |
||||
end |
||||
end |
||||
|
||||
status.tags.each do |tag| |
||||
item.category(tag.name) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,36 @@ |
||||
RSS::Builder.build do |doc| |
||||
doc.title("##{@tag.name}") |
||||
doc.description(I18n.t('rss.descriptions.tag', hashtag: @tag.name)) |
||||
doc.link(tag_url(@tag)) |
||||
doc.last_build_date(@statuses.first.created_at) if @statuses.any? |
||||
doc.icon(full_asset_url(@account.avatar.url(:original))) |
||||
doc.logo(full_pack_url('media/images/logo_transparent_white.svg')) |
||||
doc.generator("Mastodon v#{Mastodon::Version.to_s}") |
||||
|
||||
@statuses.each do |status| |
||||
doc.item do |item| |
||||
item.title(l(status.created_at)) |
||||
item.link(ActivityPub::TagManager.instance.url_for(status)) |
||||
item.pub_date(status.created_at) |
||||
item.description(rss_status_content_format(status)) |
||||
|
||||
if status.ordered_media_attachments.first&.audio? |
||||
media = status.ordered_media_attachments.first |
||||
item.enclosure(full_asset_url(media.file.url(:original, false)), media.file.content_type, media.file.size) |
||||
end |
||||
|
||||
status.ordered_media_attachments.each do |media| |
||||
item.media_content(full_asset_url(media.file.url(:original, false)), media.file.content_type, media.file.size) do |media_content| |
||||
media_content.medium(media.gifv? ? 'image' : media.type.to_s) |
||||
media_content.rating(status.sensitive? ? 'adult' : 'nonadult') |
||||
media_content.description(media.description) if media.description.present? |
||||
media_content.thumbnail(media.thumbnail.url(:original, false)) if media.thumbnail? |
||||
end |
||||
end |
||||
|
||||
status.tags.each do |tag| |
||||
item.category(tag.name) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -1,56 +0,0 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
require 'rails_helper' |
||||
|
||||
describe RSS::Serializer do |
||||
describe '#status_title' do |
||||
let(:text) { 'This is a toot' } |
||||
let(:spoiler) { '' } |
||||
let(:sensitive) { false } |
||||
let(:reblog) { nil } |
||||
let(:account) { Fabricate(:account) } |
||||
let(:status) { Fabricate(:status, account: account, text: text, spoiler_text: spoiler, sensitive: sensitive, reblog: reblog) } |
||||
|
||||
subject { RSS::Serializer.new.send(:status_title, status) } |
||||
|
||||
context 'on a toot with long text' do |
||||
let(:text) { "This toot's text is longer than the allowed number of characters" } |
||||
|
||||
it 'truncates toot text appropriately' do |
||||
expect(subject).to eq "#{account.acct}: “This toot's text is longer tha…”" |
||||
end |
||||
end |
||||
|
||||
context 'on a toot with long text with a newline' do |
||||
let(:text) { "This toot's text is longer\nthan the allowed number of characters" } |
||||
|
||||
it 'truncates toot text appropriately' do |
||||
expect(subject).to eq "#{account.acct}: “This toot's text is longer…”" |
||||
end |
||||
end |
||||
|
||||
context 'on a toot with a content warning' do |
||||
let(:spoiler) { 'long toot' } |
||||
|
||||
it 'displays spoiler text instead of toot content' do |
||||
expect(subject).to eq "#{account.acct}: CW “long toot”" |
||||
end |
||||
end |
||||
|
||||
context 'on a toot with sensitive media' do |
||||
let(:sensitive) { true } |
||||
|
||||
it 'displays that the media is sensitive' do |
||||
expect(subject).to eq "#{account.acct}: “This is a toot” (sensitive)" |
||||
end |
||||
end |
||||
|
||||
context 'on a reblog' do |
||||
let(:reblog) { Fabricate(:status, text: 'This is a toot') } |
||||
|
||||
it 'display that the toot is a reblog' do |
||||
expect(subject).to eq "#{account.acct} boosted #{reblog.account.acct}: “This is a toot”" |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue