From 2bc48e9064c56101d1761a10d8c5badba60d2bbe Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 22 Feb 2016 19:11:07 +0100 Subject: [PATCH] Individual atom entries --- app/controllers/atom_controller.rb | 4 +++ app/models/account.rb | 8 +++++ app/services/process_interaction_service.rb | 5 ++- app/views/atom/entry.xml.ruby | 39 +++++++++++++++++++++ app/views/atom/user_stream.xml.ruby | 13 +++++-- config/routes.rb | 5 +-- 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 app/views/atom/entry.xml.ruby diff --git a/app/controllers/atom_controller.rb b/app/controllers/atom_controller.rb index e0b45c5800..f9d8a4582b 100644 --- a/app/controllers/atom_controller.rb +++ b/app/controllers/atom_controller.rb @@ -5,6 +5,10 @@ class AtomController < ApplicationController @account = Account.find_by!(id: params[:id], domain: nil) end + def entry + @entry = StreamEntry.find(params[:id]) + end + private def set_format diff --git a/app/models/account.rb b/app/models/account.rb index fac835168d..42d92eddf4 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -37,6 +37,14 @@ class Account < ActiveRecord::Base :person end + def title + self.username + end + + def summary + self.note + end + def subscribed? !(self.secret.blank? || self.verify_token.blank?) end diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb index 8262ead8f5..e95a488d18 100644 --- a/app/services/process_interaction_service.rb +++ b/app/services/process_interaction_service.rb @@ -3,7 +3,7 @@ class ProcessInteractionService body = salmon.unpack(envelope) xml = Nokogiri::XML(body) - return if xml.at_xpath('//author/name').nil? || xml.at_xpath('//author/uri').nil? + return if !involves_target_account(xml, target_account) || xml.at_xpath('//author/name').nil? || xml.at_xpath('//author/uri').nil? username = xml.at_xpath('//author/name').content url = xml.at_xpath('//author/uri').content @@ -28,6 +28,9 @@ class ProcessInteractionService private + def involves_target_account(target_account) + end + def salmon OStatus2::Salmon.new end diff --git a/app/views/atom/entry.xml.ruby b/app/views/atom/entry.xml.ruby new file mode 100644 index 0000000000..b66a720b36 --- /dev/null +++ b/app/views/atom/entry.xml.ruby @@ -0,0 +1,39 @@ +Nokogiri::XML::Builder.new do |xml| + xml.entry(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') do + xml.id_ unique_tag(@entry.created_at, @entry.activity_id, @entry.activity_type) + + xml.published @entry.activity.created_at.iso8601 + xml.updated @entry.activity.updated_at.iso8601 + + xml.title @entry.title + xml.content({ type: 'html' }, @entry.content) + xml['activity'].send('verb', "http://activitystrea.ms/schema/1.0/#{@entry.verb}") + + xml.author do + xml['activity'].send('object-type', 'http://activitystrea.ms/schema/1.0/person') + xml.uri profile_url(name: @entry.account.username) + xml.name @entry.account.username + xml.summary @entry.account.note + + xml.link(rel: 'alternate', type: 'text/html', href: profile_url(name: @entry.account.username)) + + xml['poco'].preferredUsername @entry.account.username + xml['poco'].displayName @entry.account.display_name + xml['poco'].note @entry.account.note + end + + if @entry.targeted? + xml['activity'].send('object') do + xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{@entry.target.object_type}") + xml.id_ @entry.target.uri + xml.title @entry.target.title + xml.summary @entry.target.summary + xml.link(rel: 'alternate', type: 'text/html', href: @entry.target.uri) + end + else + xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{@entry.object_type}") + end + + xml.link(rel: 'self', type: 'application/atom+xml', href: atom_entry_url(id: @entry.id)) + end +end.to_xml diff --git a/app/views/atom/user_stream.xml.ruby b/app/views/atom/user_stream.xml.ruby index d058d5cb47..398464a9cf 100644 --- a/app/views/atom/user_stream.xml.ruby +++ b/app/views/atom/user_stream.xml.ruby @@ -1,5 +1,5 @@ Nokogiri::XML::Builder.new do |xml| - xml.feed(xmlns: 'http://www.w3.org/2005/Atom', 'xmlns:thr': 'http://purl.org/syndication/thread/1.0', 'xmlns:activity': 'http://activitystrea.ms/spec/1.0/') do + xml.feed(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') do xml.id_ atom_user_stream_url(id: @account.id) xml.title @account.display_name xml.subtitle @account.note @@ -12,6 +12,10 @@ Nokogiri::XML::Builder.new do |xml| xml.summary @account.note xml.link(rel: 'alternate', type: 'text/html', href: profile_url(name: @account.username)) + + xml['poco'].preferredUsername @account.username + xml['poco'].displayName @account.display_name + xml['poco'].note @account.note end xml.link(rel: 'alternate', type: 'text/html', href: profile_url(name: @account.username)) @@ -19,7 +23,7 @@ Nokogiri::XML::Builder.new do |xml| xml.link(rel: 'salmon', href: salmon_url(@account)) xml.link(rel: 'self', type: 'application/atom+xml', href: atom_user_stream_url(id: @account.id)) - @account.stream_entries.each do |stream_entry| + @account.stream_entries.order('id desc').each do |stream_entry| xml.entry do xml.id_ unique_tag(stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type) @@ -34,10 +38,15 @@ Nokogiri::XML::Builder.new do |xml| xml['activity'].send('object') do xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{stream_entry.target.object_type}") xml.id_ stream_entry.target.uri + xml.title stream_entry.target.title + xml.summary stream_entry.target.summary + xml.link(rel: 'alternate', type: 'text/html', href: stream_entry.target.uri) end else xml['activity'].send('object-type', "http://activitystrea.ms/schema/1.0/#{stream_entry.object_type}") end + + xml.link(rel: 'self', type: 'application/atom+xml', href: atom_entry_url(id: stream_entry.id)) end end end diff --git a/config/routes.rb b/config/routes.rb index 9ddc00c8f5..16f602e4ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,8 +2,9 @@ Rails.application.routes.draw do get '.well-known/host-meta', to: 'xrd#host_meta', as: :host_meta get '.well-known/webfinger', to: 'xrd#webfinger', as: :webfinger - get 'atom/:id', to: 'atom#user_stream', as: :atom_user_stream - get 'user/:name', to: 'profile#show', as: :profile + get 'atom/entry/:id', to: 'atom#entry', as: :atom_entry + get 'atom/user/:id', to: 'atom#user_stream', as: :atom_user_stream + get 'user/:name', to: 'profile#show', as: :profile mount Mastodon::API => '/api/'