From 0e8f59c16fcb21301c736ecbc4424cb4c5388c42 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 29 Feb 2016 19:42:08 +0100 Subject: Refactoring Grape API methods into normal controllers & other things --- app/helpers/accounts_helper.rb | 3 + app/helpers/api/salmon_helper.rb | 2 + app/helpers/api/subscriptions_helper.rb | 2 + app/helpers/application_helper.rb | 22 ---- app/helpers/atom_builder_helper.rb | 194 ++++++++++++++++++++++++++++++++ app/helpers/atom_helper.rb | 194 -------------------------------- app/helpers/profile_helper.rb | 22 ---- app/helpers/stream_entries_helper.rb | 22 ++++ 8 files changed, 223 insertions(+), 238 deletions(-) create mode 100644 app/helpers/accounts_helper.rb create mode 100644 app/helpers/api/salmon_helper.rb create mode 100644 app/helpers/api/subscriptions_helper.rb create mode 100644 app/helpers/atom_builder_helper.rb delete mode 100644 app/helpers/atom_helper.rb delete mode 100644 app/helpers/profile_helper.rb create mode 100644 app/helpers/stream_entries_helper.rb (limited to 'app/helpers') diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb new file mode 100644 index 000000000..51fe6bc4e --- /dev/null +++ b/app/helpers/accounts_helper.rb @@ -0,0 +1,3 @@ +module AccountsHelper + +end diff --git a/app/helpers/api/salmon_helper.rb b/app/helpers/api/salmon_helper.rb new file mode 100644 index 000000000..513c6fb7d --- /dev/null +++ b/app/helpers/api/salmon_helper.rb @@ -0,0 +1,2 @@ +module Api::SalmonHelper +end diff --git a/app/helpers/api/subscriptions_helper.rb b/app/helpers/api/subscriptions_helper.rb new file mode 100644 index 000000000..3796aee42 --- /dev/null +++ b/app/helpers/api/subscriptions_helper.rb @@ -0,0 +1,2 @@ +module Api::SubscriptionsHelper +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 90b45a025..5d696316d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,4 @@ module ApplicationHelper - include RoutingHelper - def unique_tag(date, id, type) "tag:#{LOCAL_DOMAIN},#{date.strftime('%Y-%m-%d')}:objectId=#{id}:objectType=#{type}" end @@ -13,24 +11,4 @@ module ApplicationHelper def local_id?(id) id.start_with?("tag:#{LOCAL_DOMAIN}") end - - def subscription_url(account) - add_base_url_prefix subscriptions_path(id: account.id, format: '') - end - - def salmon_url(account) - add_base_url_prefix salmon_path(id: account.id, format: '') - end - - def profile_url(account) - account.local? ? super(name: account.username) : account.url - end - - def status_url(status) - status.local? ? super(name: status.account.username, id: status.stream_entry.id) : status.url - end - - def add_base_url_prefix(suffix) - File.join(root_url, "api", suffix) - end end diff --git a/app/helpers/atom_builder_helper.rb b/app/helpers/atom_builder_helper.rb new file mode 100644 index 000000000..8e3652c70 --- /dev/null +++ b/app/helpers/atom_builder_helper.rb @@ -0,0 +1,194 @@ +module AtomBuilderHelper + def stream_updated_at + @account.stream_entries.last ? (@account.updated_at > @account.stream_entries.last.created_at ? @account.updated_at : @account.stream_entries.last.created_at) : @account.updated_at + end + + def entry(xml, is_root, &block) + if is_root + root_tag(xml, :entry, &block) + else + xml.entry(&block) + end + end + + def feed(xml, &block) + root_tag(xml, :feed, &block) + end + + def unique_id(xml, date, id, type) + xml.id_ unique_tag(date, id, type) + end + + def simple_id(xml, id) + xml.id_ id + end + + def published_at(xml, date) + xml.published date.iso8601 + end + + def updated_at(xml, date) + xml.updated date.iso8601 + end + + def verb(xml, verb) + xml['activity'].send('verb', "http://activitystrea.ms/schema/1.0/#{verb}") + end + + def content(xml, content) + xml.content({ type: 'html' }, content) + end + + def title(xml, title) + xml.title title + end + + def author(xml, &block) + xml.author(&block) + end + + def target(xml, &block) + xml['activity'].object(&block) + end + + def object_type(xml, type) + xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{type}") + end + + def uri(xml, uri) + xml.uri uri + end + + def name(xml, name) + xml.name name + end + + def summary(xml, summary) + xml.summary summary + end + + def subtitle(xml, subtitle) + xml.subtitle subtitle + end + + def link_alternate(xml, url) + xml.link(rel: 'alternate', type: 'text/html', href: url) + end + + def link_self(xml, url) + xml.link(rel: 'self', type: 'application/atom+xml', href: url) + end + + def link_hub(xml, url) + xml.link(rel: 'hub', href: url) + end + + def link_salmon(xml, url) + xml.link(rel: 'salmon', href: url) + end + + def portable_contact(xml, account) + xml['poco'].preferredUsername account.username + xml['poco'].displayName account.display_name + xml['poco'].note account.note + end + + def in_reply_to(xml, uri, url) + 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)) + end + + def link_avatar(xml, account) + xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => '300', 'media:height' =>'300', 'href' => asset_url(account.avatar.url(:large, false))) + xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => '96', 'media:height' =>'96', 'href' => asset_url(account.avatar.url(:medium, false))) + xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => '48', 'media:height' =>'48', 'href' => asset_url(account.avatar.url(:small, false))) + end + + def logo(xml, url) + xml.logo url + end + + def include_author(xml, account) + object_type xml, :person + uri xml, url_for_target(account) + name xml, account.username + summary xml, account.note + link_alternate xml, url_for_target(account) + link_avatar xml, account + portable_contact xml, account + end + + def include_entry(xml, stream_entry) + unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type + published_at xml, stream_entry.activity.created_at + updated_at xml, stream_entry.activity.updated_at + title xml, stream_entry.title + content xml, stream_entry.content + 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 + + # Comments need thread element + if stream_entry.threaded? + in_reply_to xml, uri_for_target(stream_entry.thread), url_for_target(stream_entry.thread) + end + + if stream_entry.targeted? + target(xml) do + object_type xml, stream_entry.target.object_type + simple_id xml, uri_for_target(stream_entry.target) + title xml, stream_entry.target.title + link_alternate xml, url_for_target(stream_entry.target) + + # People have summary and portable contacts information + if stream_entry.target.object_type == :person + summary xml, stream_entry.target.content + portable_contact xml, stream_entry.target + link_avatar xml, stream_entry.target + end + + # Statuses have content + if [:note, :comment].include? stream_entry.target.object_type + content xml, stream_entry.target.content + end + end + end + + stream_entry.mentions.each do |mentioned| + link_mention xml, mentioned + end + end + + private + + def root_tag(xml, tag, &block) + xml.send(tag, { :xmlns => 'http://www.w3.org/2005/Atom', 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0', 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/', 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0', 'xmlns:media' => 'http://purl.org/syndication/atommedia' }, &block) + end +end diff --git a/app/helpers/atom_helper.rb b/app/helpers/atom_helper.rb deleted file mode 100644 index bd7aee9a0..000000000 --- a/app/helpers/atom_helper.rb +++ /dev/null @@ -1,194 +0,0 @@ -module AtomHelper - def stream_updated_at - @account.stream_entries.last ? (@account.updated_at > @account.stream_entries.last.created_at ? @account.updated_at : @account.stream_entries.last.created_at) : @account.updated_at - end - - def entry(xml, is_root, &block) - if is_root - root_tag(xml, :entry, &block) - else - xml.entry(&block) - end - end - - def feed(xml, &block) - root_tag(xml, :feed, &block) - end - - def unique_id(xml, date, id, type) - xml.id_ unique_tag(date, id, type) - end - - def simple_id(xml, id) - xml.id_ id - end - - def published_at(xml, date) - xml.published date.iso8601 - end - - def updated_at(xml, date) - xml.updated date.iso8601 - end - - def verb(xml, verb) - xml['activity'].send('verb', "http://activitystrea.ms/schema/1.0/#{verb}") - end - - def content(xml, content) - xml.content({ type: 'html' }, content) - end - - def title(xml, title) - xml.title title - end - - def author(xml, &block) - xml.author(&block) - end - - def target(xml, &block) - xml['activity'].object(&block) - end - - def object_type(xml, type) - xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{type}") - end - - def uri(xml, uri) - xml.uri uri - end - - def name(xml, name) - xml.name name - end - - def summary(xml, summary) - xml.summary summary - end - - def subtitle(xml, subtitle) - xml.subtitle subtitle - end - - def link_alternate(xml, url) - xml.link(rel: 'alternate', type: 'text/html', href: url) - end - - def link_self(xml, url) - xml.link(rel: 'self', type: 'application/atom+xml', href: url) - end - - def link_hub(xml, url) - xml.link(rel: 'hub', href: url) - end - - def link_salmon(xml, url) - xml.link(rel: 'salmon', href: url) - end - - def portable_contact(xml, account) - xml['poco'].preferredUsername account.username - xml['poco'].displayName account.display_name - xml['poco'].note account.note - end - - def in_reply_to(xml, uri, url) - xml['thr'].send('in-reply-to', { ref: uri, href: url, type: 'text/html' }) - end - - def disambiguate_uri(target) - if target.local? - if target.object_type == :person - profile_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 disambiguate_url(target) - if target.local? - if target.object_type == :person - profile_url(target) - else - status_url(target) - end - else - target.url - end - end - - def link_mention(xml, account) - xml.link(rel: 'mentioned', href: disambiguate_uri(account)) - end - - def link_avatar(xml, account) - xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => '300', 'media:height' =>'300', 'href' => asset_url(account.avatar.url(:large))) - xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => '96', 'media:height' =>'96', 'href' => asset_url(account.avatar.url(:medium))) - xml.link('rel' => 'avatar', 'type' => account.avatar_content_type, 'media:width' => '48', 'media:height' =>'48', 'href' => asset_url(account.avatar.url(:small))) - end - - def logo(xml, url) - xml.logo url - end - - def include_author(xml, account) - object_type xml, :person - uri xml, profile_url(account) - name xml, account.username - summary xml, account.note - link_alternate xml, profile_url(account) - link_avatar xml, account - portable_contact xml, account - end - - def include_entry(xml, stream_entry) - unique_id xml, stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type - published_at xml, stream_entry.activity.created_at - updated_at xml, stream_entry.activity.updated_at - title xml, stream_entry.title - content xml, stream_entry.content - verb xml, stream_entry.verb - link_self xml, atom_entry_url(id: stream_entry.id) - object_type xml, stream_entry.object_type - - # Comments need thread element - if stream_entry.threaded? - in_reply_to xml, disambiguate_uri(stream_entry.thread), disambiguate_url(stream_entry.thread) - end - - if stream_entry.targeted? - target(xml) do - object_type xml, stream_entry.target.object_type - simple_id xml, disambiguate_uri(stream_entry.target) - title xml, stream_entry.target.title - link_alternate xml, disambiguate_url(stream_entry.target) - - # People have summary and portable contacts information - if stream_entry.target.object_type == :person - summary xml, stream_entry.target.content - portable_contact xml, stream_entry.target - link_avatar xml, stream_entry.target - end - - # Statuses have content - if [:note, :comment].include? stream_entry.target.object_type - content xml, stream_entry.target.content - end - end - end - - stream_entry.mentions.each do |mentioned| - link_mention xml, mentioned - end - end - - private - - def root_tag(xml, tag, &block) - xml.send(tag, { :xmlns => 'http://www.w3.org/2005/Atom', 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0', 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/', 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0', 'xmlns:media' => 'http://purl.org/syndication/atommedia' }, &block) - end -end diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb deleted file mode 100644 index 3a34dfbd8..000000000 --- a/app/helpers/profile_helper.rb +++ /dev/null @@ -1,22 +0,0 @@ -module ProfileHelper - def display_name(account) - account.display_name.blank? ? account.username : account.display_name - end - - def avatar_for_status_url(status) - status.reblog? ? status.reblog.account.avatar.url(:small) : status.account.avatar.url(:small) - end - - def entry_classes(status, is_predecessor, is_successor, include_threads) - classes = ['entry'] - classes << 'entry-reblog' if status.reblog? - classes << 'entry-predecessor' if is_predecessor - classes << 'entry-successor' if is_successor - classes << 'entry-center' if include_threads - classes.join(' ') - end - - def relative_time(date) - date < 5.days.ago ? date.strftime("%d.%m.%Y") : "#{time_ago_in_words(date)} ago" - end -end diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb new file mode 100644 index 000000000..c588f5ce7 --- /dev/null +++ b/app/helpers/stream_entries_helper.rb @@ -0,0 +1,22 @@ +module StreamEntriesHelper + def display_name(account) + account.display_name.blank? ? account.username : account.display_name + end + + def avatar_for_status_url(status) + status.reblog? ? status.reblog.account.avatar.url(:small) : status.account.avatar.url(:small) + end + + def entry_classes(status, is_predecessor, is_successor, include_threads) + classes = ['entry'] + classes << 'entry-reblog' if status.reblog? + classes << 'entry-predecessor' if is_predecessor + classes << 'entry-successor' if is_successor + classes << 'entry-center' if include_threads + classes.join(' ') + end + + def relative_time(date) + date < 5.days.ago ? date.strftime("%d.%m.%Y") : "#{time_ago_in_words(date)} ago" + end +end -- cgit