diff options
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/emoji_formatter.rb | 28 | ||||
-rw-r--r-- | app/lib/rss/builder.rb | 33 | ||||
-rw-r--r-- | app/lib/rss/channel.rb | 49 | ||||
-rw-r--r-- | app/lib/rss/element.rb | 24 | ||||
-rw-r--r-- | app/lib/rss/item.rb | 45 | ||||
-rw-r--r-- | app/lib/rss/media_content.rb | 29 | ||||
-rw-r--r-- | app/lib/rss/serializer.rb | 55 | ||||
-rw-r--r-- | app/lib/rss_builder.rb | 130 |
8 files changed, 202 insertions, 191 deletions
diff --git a/app/lib/emoji_formatter.rb b/app/lib/emoji_formatter.rb index f808f3a22..194849c23 100644 --- a/app/lib/emoji_formatter.rb +++ b/app/lib/emoji_formatter.rb @@ -11,6 +11,7 @@ class EmojiFormatter # @param [Array<CustomEmoji>] custom_emojis # @param [Hash] options # @option options [Boolean] :animate + # @option options [String] :style def initialize(html, custom_emojis, options = {}) raise ArgumentError unless html.html_safe? @@ -85,14 +86,29 @@ class EmojiFormatter def image_for_emoji(shortcode, emoji) original_url, static_url = emoji - if animate? - image_tag(original_url, draggable: false, class: 'emojione', alt: ":#{shortcode}:", title: ":#{shortcode}:") - else - image_tag(original_url, draggable: false, class: 'emojione custom-emoji', alt: ":#{shortcode}:", title: ":#{shortcode}:", data: { original: original_url, static: static_url }) - end + image_tag( + animate? ? original_url : static_url, + image_attributes.merge(alt: ":#{shortcode}:", title: ":#{shortcode}:", data: image_data_attributes(original_url, static_url)) + ) + end + + def image_attributes + { rel: 'emoji', draggable: false, width: 16, height: 16, class: image_class_names, style: image_style } + end + + def image_data_attributes(original_url, static_url) + { original: original_url, static: static_url } unless animate? + end + + def image_class_names + animate? ? 'emojione' : 'emojione custom-emoji' + end + + def image_style + @options[:style] end def animate? - @options[:animate] + @options[:animate] || @options.key?(:style) end end diff --git a/app/lib/rss/builder.rb b/app/lib/rss/builder.rb new file mode 100644 index 000000000..a9b3f08c5 --- /dev/null +++ b/app/lib/rss/builder.rb @@ -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 diff --git a/app/lib/rss/channel.rb b/app/lib/rss/channel.rb new file mode 100644 index 000000000..1dba94e47 --- /dev/null +++ b/app/lib/rss/channel.rb @@ -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 diff --git a/app/lib/rss/element.rb b/app/lib/rss/element.rb new file mode 100644 index 000000000..7142fa039 --- /dev/null +++ b/app/lib/rss/element.rb @@ -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 diff --git a/app/lib/rss/item.rb b/app/lib/rss/item.rb new file mode 100644 index 000000000..c02991ace --- /dev/null +++ b/app/lib/rss/item.rb @@ -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 diff --git a/app/lib/rss/media_content.rb b/app/lib/rss/media_content.rb new file mode 100644 index 000000000..7aefd8b40 --- /dev/null +++ b/app/lib/rss/media_content.rb @@ -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 diff --git a/app/lib/rss/serializer.rb b/app/lib/rss/serializer.rb deleted file mode 100644 index d44e94221..000000000 --- a/app/lib/rss/serializer.rb +++ /dev/null @@ -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 diff --git a/app/lib/rss_builder.rb b/app/lib/rss_builder.rb deleted file mode 100644 index 63ddba2e8..000000000 --- a/app/lib/rss_builder.rb +++ /dev/null @@ -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 |