From 4d42a389540690b32886f2a38af1f86aee617d27 Mon Sep 17 00:00:00 2001 From: abcang Date: Tue, 18 Jul 2017 23:38:22 +0900 Subject: Improve admin page (#4121) * Improve admin page * Fix test * Add spec * Improve select style --- .../admin/reported_statuses_controller.rb | 15 ++++- app/controllers/admin/reports_controller.rb | 4 +- app/controllers/admin/statuses_controller.rb | 69 ++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/statuses_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/admin/reported_statuses_controller.rb b/app/controllers/admin/reported_statuses_controller.rb index 32434d30f..5a31adecf 100644 --- a/app/controllers/admin/reported_statuses_controller.rb +++ b/app/controllers/admin/reported_statuses_controller.rb @@ -5,7 +5,14 @@ module Admin include Authorization before_action :set_report - before_action :set_status + before_action :set_status, only: [:update, :destroy] + + def create + @form = Form::StatusBatch.new(form_status_batch_params) + flash[:alert] = t('admin.statuses.failed_to_execute') unless @form.save + + redirect_to admin_report_path(@report) + end def update @status.update(status_params) @@ -15,7 +22,7 @@ module Admin def destroy authorize @status, :destroy? RemovalWorker.perform_async(@status.id) - redirect_to admin_report_path(@report) + render json: @status end private @@ -24,6 +31,10 @@ module Admin params.require(:status).permit(:sensitive) end + def form_status_batch_params + params.require(:form_status_batch).permit(:action, status_ids: []) + end + def set_report @report = Report.find(params[:report_id]) end diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb index 2d8c3c820..226467739 100644 --- a/app/controllers/admin/reports_controller.rb +++ b/app/controllers/admin/reports_controller.rb @@ -8,7 +8,9 @@ module Admin @reports = filtered_reports.page(params[:page]) end - def show; end + def show + @form = Form::StatusBatch.new + end def update process_report diff --git a/app/controllers/admin/statuses_controller.rb b/app/controllers/admin/statuses_controller.rb new file mode 100644 index 000000000..50712f0dd --- /dev/null +++ b/app/controllers/admin/statuses_controller.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +module Admin + class StatusesController < BaseController + include Authorization + + helper_method :current_params + + before_action :set_account + before_action :set_status, only: [:update, :destroy] + + PAR_PAGE = 20 + + def index + @statuses = @account.statuses + if params[:media] + account_media_status_ids = @account.media_attachments.attached.reorder(nil).select(:status_id).distinct + @statuses.merge!(Status.where(id: account_media_status_ids)) + end + @statuses = @statuses.preload(:media_attachments, :mentions).page(params[:page]).per(PAR_PAGE) + + @form = Form::StatusBatch.new + end + + def create + @form = Form::StatusBatch.new(form_status_batch_params) + flash[:alert] = t('admin.statuses.failed_to_execute') unless @form.save + + redirect_to admin_account_statuses_path(@account.id, current_params) + end + + def update + @status.update(status_params) + redirect_to admin_account_statuses_path(@account.id, current_params) + end + + def destroy + authorize @status, :destroy? + RemovalWorker.perform_async(@status.id) + render json: @status + end + + private + + def status_params + params.require(:status).permit(:sensitive) + end + + def form_status_batch_params + params.require(:form_status_batch).permit(:action, status_ids: []) + end + + def set_status + @status = @account.statuses.find(params[:id]) + end + + def set_account + @account = Account.find(params[:account_id]) + end + + def current_params + page = (params[:page] || 1).to_i + { + media: params[:media], + page: page > 1 && page, + }.select { |_, value| value.present? } + end + end +end -- cgit From 89b988cab5e4729bd80400a2b25ec2b790ebd18d Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Tue, 18 Jul 2017 23:39:47 +0900 Subject: Introduce Ostatus name space (#4164) * Wrap methods of ProcessFeedService::ProcessEntry in classes This is a change same with 425acecfdb15093a265b191120fb2d4e4c4135c4, except that it has the following changes: * Revert irrelevant change in find_or_create_conversation * Fix error handling for RemoteActivity * Introduce Ostatus name space --- app/controllers/accounts_controller.rb | 2 +- app/controllers/stream_entries_controller.rb | 2 +- app/lib/atom_serializer.rb | 367 ------ app/lib/ostatus/activity/base.rb | 50 + app/lib/ostatus/activity/creation.rb | 149 +++ app/lib/ostatus/activity/deletion.rb | 14 + app/lib/ostatus/activity/general.rb | 20 + app/lib/ostatus/activity/post.rb | 23 + app/lib/ostatus/activity/remote.rb | 7 + app/lib/ostatus/activity/share.rb | 26 + app/lib/ostatus/atom_serializer.rb | 367 ++++++ app/services/authorize_follow_service.rb | 2 +- app/services/block_service.rb | 2 +- app/services/concerns/stream_entry_renderer.rb | 2 +- app/services/favourite_service.rb | 2 +- app/services/follow_service.rb | 4 +- app/services/process_feed_service.rb | 274 +--- app/services/reject_follow_service.rb | 2 +- app/services/unblock_service.rb | 2 +- app/services/unfavourite_service.rb | 2 +- app/services/unfollow_service.rb | 2 +- app/workers/pubsubhubbub/distribution_worker.rb | 4 +- spec/lib/atom_serializer_spec.rb | 1554 ----------------------- spec/lib/ostatus/atom_serializer_spec.rb | 1554 +++++++++++++++++++++++ spec/services/process_feed_service_spec.rb | 40 + 25 files changed, 2271 insertions(+), 2202 deletions(-) delete mode 100644 app/lib/atom_serializer.rb create mode 100644 app/lib/ostatus/activity/base.rb create mode 100644 app/lib/ostatus/activity/creation.rb create mode 100644 app/lib/ostatus/activity/deletion.rb create mode 100644 app/lib/ostatus/activity/general.rb create mode 100644 app/lib/ostatus/activity/post.rb create mode 100644 app/lib/ostatus/activity/remote.rb create mode 100644 app/lib/ostatus/activity/share.rb create mode 100644 app/lib/ostatus/atom_serializer.rb delete mode 100644 spec/lib/atom_serializer_spec.rb create mode 100644 spec/lib/ostatus/atom_serializer_spec.rb (limited to 'app/controllers') diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index a95aabf1d..37a1e540f 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -13,7 +13,7 @@ class AccountsController < ApplicationController format.atom do @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(20, params[:max_id], params[:since_id]) - render xml: AtomSerializer.render(AtomSerializer.new.feed(@account, @entries.to_a)) + render xml: Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.feed(@account, @entries.to_a)) end format.json do diff --git a/app/controllers/stream_entries_controller.rb b/app/controllers/stream_entries_controller.rb index 54a435238..e3db77caa 100644 --- a/app/controllers/stream_entries_controller.rb +++ b/app/controllers/stream_entries_controller.rb @@ -19,7 +19,7 @@ class StreamEntriesController < ApplicationController end format.atom do - render xml: AtomSerializer.render(AtomSerializer.new.entry(@stream_entry, true)) + render xml: Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.entry(@stream_entry, true)) end end end diff --git a/app/lib/atom_serializer.rb b/app/lib/atom_serializer.rb deleted file mode 100644 index b14d596dd..000000000 --- a/app/lib/atom_serializer.rb +++ /dev/null @@ -1,367 +0,0 @@ -# frozen_string_literal: true - -class AtomSerializer - include RoutingHelper - include ActionView::Helpers::SanitizeHelper - - class << self - def render(element) - document = Ox::Document.new(version: '1.0') - document << element - ('' + Ox.dump(element, effort: :tolerant)).force_encoding('UTF-8') - end - end - - def author(account) - author = Ox::Element.new('author') - - uri = TagManager.instance.uri_for(account) - - append_element(author, 'id', uri) - append_element(author, 'activity:object-type', TagManager::TYPES[:person]) - append_element(author, 'uri', uri) - append_element(author, 'name', account.username) - append_element(author, 'email', account.local? ? account.local_username_and_domain : account.acct) - append_element(author, 'summary', Formatter.instance.simplified_format(account).to_str, type: :html) if account.note? - append_element(author, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(account)) - append_element(author, 'link', nil, rel: :avatar, type: account.avatar_content_type, 'media:width': 120, 'media:height': 120, href: full_asset_url(account.avatar.url(:original))) if account.avatar? - append_element(author, 'link', nil, rel: :header, type: account.header_content_type, 'media:width': 700, 'media:height': 335, href: full_asset_url(account.header.url(:original))) if account.header? - append_element(author, 'poco:preferredUsername', account.username) - append_element(author, 'poco:displayName', account.display_name) if account.display_name? - append_element(author, 'poco:note', account.local? ? account.note : strip_tags(account.note)) if account.note? - append_element(author, 'mastodon:scope', account.locked? ? :private : :public) - - author - end - - def feed(account, stream_entries) - feed = Ox::Element.new('feed') - - add_namespaces(feed) - - append_element(feed, 'id', account_url(account, format: 'atom')) - append_element(feed, 'title', account.display_name.presence || account.username) - append_element(feed, 'subtitle', account.note) - append_element(feed, 'updated', account.updated_at.iso8601) - append_element(feed, 'logo', full_asset_url(account.avatar.url(:original))) - - feed << author(account) - - append_element(feed, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(account)) - append_element(feed, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_url(account, format: 'atom')) - append_element(feed, 'link', nil, rel: :next, type: 'application/atom+xml', href: account_url(account, format: 'atom', max_id: stream_entries.last.id)) if stream_entries.size == 20 - append_element(feed, 'link', nil, rel: :hub, href: api_push_url) - append_element(feed, 'link', nil, rel: :salmon, href: api_salmon_url(account.id)) - - stream_entries.each do |stream_entry| - feed << entry(stream_entry) - end - - feed - end - - def entry(stream_entry, root = false) - entry = Ox::Element.new('entry') - - add_namespaces(entry) if root - - append_element(entry, 'id', TagManager.instance.unique_tag(stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type)) - append_element(entry, 'published', stream_entry.created_at.iso8601) - append_element(entry, 'updated', stream_entry.updated_at.iso8601) - append_element(entry, 'title', stream_entry&.status&.title || "#{stream_entry.account.acct} deleted status") - - entry << author(stream_entry.account) if root - - append_element(entry, 'activity:object-type', TagManager::TYPES[stream_entry.object_type]) - append_element(entry, 'activity:verb', TagManager::VERBS[stream_entry.verb]) - - entry << object(stream_entry.target) if stream_entry.targeted? - - if stream_entry.status.nil? - append_element(entry, 'content', 'Deleted status') - else - serialize_status_attributes(entry, stream_entry.status) - end - - append_element(entry, 'link', nil, rel: :alternate, type: 'text/html', href: account_stream_entry_url(stream_entry.account, stream_entry)) - append_element(entry, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')) - append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(stream_entry.thread), href: TagManager.instance.url_for(stream_entry.thread)) if stream_entry.threaded? - append_element(entry, 'ostatus:conversation', nil, ref: conversation_uri(stream_entry.status.conversation)) unless stream_entry&.status&.conversation_id.nil? - - entry - end - - def object(status) - object = Ox::Element.new('activity:object') - - append_element(object, 'id', TagManager.instance.uri_for(status)) - append_element(object, 'published', status.created_at.iso8601) - append_element(object, 'updated', status.updated_at.iso8601) - append_element(object, 'title', status.title) - - object << author(status.account) - - append_element(object, 'activity:object-type', TagManager::TYPES[status.object_type]) - append_element(object, 'activity:verb', TagManager::VERBS[status.verb]) - - serialize_status_attributes(object, status) - - append_element(object, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(status)) - append_element(object, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(status.thread), href: TagManager.instance.url_for(status.thread)) unless status.thread.nil? - append_element(object, 'ostatus:conversation', nil, ref: conversation_uri(status.conversation)) unless status.conversation_id.nil? - - object - end - - def follow_salmon(follow) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - description = "#{follow.account.acct} started following #{follow.target_account.acct}" - - append_element(entry, 'id', TagManager.instance.unique_tag(follow.created_at, follow.id, 'Follow')) - append_element(entry, 'title', description) - append_element(entry, 'content', description, type: :html) - - entry << author(follow.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:follow]) - - object = author(follow.target_account) - object.value = 'activity:object' - - entry << object - entry - end - - def follow_request_salmon(follow_request) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - append_element(entry, 'id', TagManager.instance.unique_tag(follow_request.created_at, follow_request.id, 'FollowRequest')) - append_element(entry, 'title', "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}") - - entry << author(follow_request.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:request_friend]) - - object = author(follow_request.target_account) - object.value = 'activity:object' - - entry << object - entry - end - - def authorize_follow_request_salmon(follow_request) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) - append_element(entry, 'title', "#{follow_request.target_account.acct} authorizes follow request by #{follow_request.account.acct}") - - entry << author(follow_request.target_account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:authorize]) - - object = Ox::Element.new('activity:object') - object << author(follow_request.account) - - append_element(object, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(object, 'activity:verb', TagManager::VERBS[:request_friend]) - - inner_object = author(follow_request.target_account) - inner_object.value = 'activity:object' - - object << inner_object - entry << object - entry - end - - def reject_follow_request_salmon(follow_request) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) - append_element(entry, 'title', "#{follow_request.target_account.acct} rejects follow request by #{follow_request.account.acct}") - - entry << author(follow_request.target_account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:reject]) - - object = Ox::Element.new('activity:object') - object << author(follow_request.account) - - append_element(object, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(object, 'activity:verb', TagManager::VERBS[:request_friend]) - - inner_object = author(follow_request.target_account) - inner_object.value = 'activity:object' - - object << inner_object - entry << object - entry - end - - def unfollow_salmon(follow) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - description = "#{follow.account.acct} is no longer following #{follow.target_account.acct}" - - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow.id, 'Follow')) - append_element(entry, 'title', description) - append_element(entry, 'content', description, type: :html) - - entry << author(follow.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:unfollow]) - - object = author(follow.target_account) - object.value = 'activity:object' - - entry << object - entry - end - - def block_salmon(block) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - description = "#{block.account.acct} no longer wishes to interact with #{block.target_account.acct}" - - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) - append_element(entry, 'title', description) - - entry << author(block.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:block]) - - object = author(block.target_account) - object.value = 'activity:object' - - entry << object - entry - end - - def unblock_salmon(block) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - description = "#{block.account.acct} no longer blocks #{block.target_account.acct}" - - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) - append_element(entry, 'title', description) - - entry << author(block.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:unblock]) - - object = author(block.target_account) - object.value = 'activity:object' - - entry << object - entry - end - - def favourite_salmon(favourite) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}" - - append_element(entry, 'id', TagManager.instance.unique_tag(favourite.created_at, favourite.id, 'Favourite')) - append_element(entry, 'title', description) - append_element(entry, 'content', description, type: :html) - - entry << author(favourite.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:favorite]) - - entry << object(favourite.status) - - append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) - - entry - end - - def unfavourite_salmon(favourite) - entry = Ox::Element.new('entry') - add_namespaces(entry) - - description = "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}" - - append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, favourite.id, 'Favourite')) - append_element(entry, 'title', description) - append_element(entry, 'content', description, type: :html) - - entry << author(favourite.account) - - append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) - append_element(entry, 'activity:verb', TagManager::VERBS[:unfavorite]) - - entry << object(favourite.status) - - append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) - - entry - end - - private - - def append_element(parent, name, content = nil, attributes = {}) - element = Ox::Element.new(name) - attributes.each { |k, v| element[k] = sanitize_str(v) } - element << sanitize_str(content) unless content.nil? - parent << element - end - - def sanitize_str(raw_str) - raw_str.to_s - end - - def conversation_uri(conversation) - return conversation.uri if conversation.uri? - TagManager.instance.unique_tag(conversation.created_at, conversation.id, 'Conversation') - end - - def add_namespaces(parent) - parent['xmlns'] = TagManager::XMLNS - parent['xmlns:thr'] = TagManager::THR_XMLNS - parent['xmlns:activity'] = TagManager::AS_XMLNS - parent['xmlns:poco'] = TagManager::POCO_XMLNS - parent['xmlns:media'] = TagManager::MEDIA_XMLNS - parent['xmlns:ostatus'] = TagManager::OS_XMLNS - parent['xmlns:mastodon'] = TagManager::MTDN_XMLNS - end - - def serialize_status_attributes(entry, status) - append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text? - append_element(entry, 'content', Formatter.instance.format(status).to_str, type: 'html', 'xml:lang': status.language) - - status.mentions.each do |mentioned| - append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:person], href: TagManager.instance.uri_for(mentioned.account)) - end - - append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:collection], href: TagManager::COLLECTIONS[:public]) if status.public_visibility? - - status.tags.each do |tag| - append_element(entry, 'category', nil, term: tag.name) - end - - append_element(entry, 'category', nil, term: 'nsfw') if status.sensitive? - - status.media_attachments.each do |media| - append_element(entry, 'link', nil, rel: :enclosure, type: media.file_content_type, length: media.file_file_size, href: full_asset_url(media.file.url(:original, false))) - end - - append_element(entry, 'mastodon:scope', status.visibility) - end -end diff --git a/app/lib/ostatus/activity/base.rb b/app/lib/ostatus/activity/base.rb new file mode 100644 index 000000000..f528815b3 --- /dev/null +++ b/app/lib/ostatus/activity/base.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +class Ostatus::Activity::Base + def initialize(xml, account = nil) + @xml = xml + @account = account + end + + def status? + [:activity, :note, :comment].include?(type) + end + + def verb + raw = @xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content + TagManager::VERBS.key(raw) + rescue + :post + end + + def type + raw = @xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content + TagManager::TYPES.key(raw) + rescue + :activity + end + + def id + @xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content + end + + def url + link = @xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) + link.nil? ? nil : link['href'] + end + + private + + def find_status(uri) + if TagManager.instance.local_id?(uri) + local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status') + return Status.find_by(id: local_id) + end + + Status.find_by(uri: uri) + end + + def redis + Redis.current + end +end diff --git a/app/lib/ostatus/activity/creation.rb b/app/lib/ostatus/activity/creation.rb new file mode 100644 index 000000000..c54d64fd7 --- /dev/null +++ b/app/lib/ostatus/activity/creation.rb @@ -0,0 +1,149 @@ +# frozen_string_literal: true + +class Ostatus::Activity::Creation < Ostatus::Activity::Base + def perform + if redis.exists("delete_upon_arrival:#{@account.id}:#{id}") + Rails.logger.debug "Delete for status #{id} was queued, ignoring" + return [nil, false] + end + + return [nil, false] if @account.suspended? + + Rails.logger.debug "Creating remote status #{id}" + + # Return early if status already exists in db + status = find_status(id) + + return [status, false] unless status.nil? + + status = Status.create!( + uri: id, + url: url, + account: @account, + reblog: reblog, + text: content, + spoiler_text: content_warning, + created_at: published, + reply: thread?, + language: content_language, + visibility: visibility_scope, + conversation: find_or_create_conversation, + thread: thread? ? find_status(thread.first) : nil + ) + + save_mentions(status) + save_hashtags(status) + save_media(status) + + if thread? && status.thread.nil? + Rails.logger.debug "Trying to attach #{status.id} (#{id}) to #{thread.first}" + ThreadResolveWorker.perform_async(status.id, thread.second) + end + + Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution" + + LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text? + DistributionWorker.perform_async(status.id) + + [status, true] + end + + def content + @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content + end + + def content_language + @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' + end + + def content_warning + @xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || '' + end + + def visibility_scope + @xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public + end + + def published + @xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content + end + + def thread? + !@xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil? + end + + def thread + thr = @xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS) + [thr['ref'], thr['href']] + end + + private + + def find_or_create_conversation + uri = @xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content + return if uri.nil? + + if TagManager.instance.local_id?(uri) + local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Conversation') + return Conversation.find_by(id: local_id) + end + + Conversation.find_by(uri: uri) || Conversation.create!(uri: uri) + end + + def save_mentions(parent) + processed_account_ids = [] + + @xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link| + next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type'] + + mentioned_account = account_from_href(link['href']) + + next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id) + + mentioned_account.mentions.where(status: parent).first_or_create(status: parent) + + # So we can skip duplicate mentions + processed_account_ids << mentioned_account.id + end + end + + def save_hashtags(parent) + tags = @xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?) + ProcessHashtagsService.new.call(parent, tags) + end + + def save_media(parent) + do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? + + @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link| + next unless link['href'] + + media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href']) + parsed_url = Addressable::URI.parse(link['href']).normalize + + next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? + + media.save + + next if do_not_download + + begin + media.file_remote_url = link['href'] + media.save! + rescue ActiveRecord::RecordInvalid + next + end + end + end + + def account_from_href(href) + url = Addressable::URI.parse(href).normalize + + if TagManager.instance.web_domain?(url.host) + Account.find_local(url.path.gsub('/users/', '')) + else + Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href) + end + end +end diff --git a/app/lib/ostatus/activity/deletion.rb b/app/lib/ostatus/activity/deletion.rb new file mode 100644 index 000000000..c4d05a467 --- /dev/null +++ b/app/lib/ostatus/activity/deletion.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class Ostatus::Activity::Deletion < Ostatus::Activity::Base + def perform + Rails.logger.debug "Deleting remote status #{id}" + status = Status.find_by(uri: id, account: @account) + + if status.nil? + redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) + else + RemoveStatusService.new.call(status) + end + end +end diff --git a/app/lib/ostatus/activity/general.rb b/app/lib/ostatus/activity/general.rb new file mode 100644 index 000000000..3ff7a039a --- /dev/null +++ b/app/lib/ostatus/activity/general.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class Ostatus::Activity::General < Ostatus::Activity::Base + def specialize + special_class&.new(@xml, @account) + end + + private + + def special_class + case verb + when :post + Ostatus::Activity::Post + when :share + Ostatus::Activity::Share + when :delete + Ostatus::Activity::Deletion + end + end +end diff --git a/app/lib/ostatus/activity/post.rb b/app/lib/ostatus/activity/post.rb new file mode 100644 index 000000000..8028db2f8 --- /dev/null +++ b/app/lib/ostatus/activity/post.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class Ostatus::Activity::Post < Ostatus::Activity::Creation + def perform + status, just_created = super + + if just_created + status.mentions.includes(:account).each do |mention| + mentioned_account = mention.account + next unless mentioned_account.local? + NotifyService.new.call(mentioned_account, mention) + end + end + + status + end + + private + + def reblog + nil + end +end diff --git a/app/lib/ostatus/activity/remote.rb b/app/lib/ostatus/activity/remote.rb new file mode 100644 index 000000000..755f885e6 --- /dev/null +++ b/app/lib/ostatus/activity/remote.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Ostatus::Activity::Remote < Ostatus::Activity::Base + def perform + find_status(id) || FetchRemoteStatusService.new.call(url) + end +end diff --git a/app/lib/ostatus/activity/share.rb b/app/lib/ostatus/activity/share.rb new file mode 100644 index 000000000..73aac58ed --- /dev/null +++ b/app/lib/ostatus/activity/share.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class Ostatus::Activity::Share < Ostatus::Activity::Creation + def perform + return if reblog.nil? + + status, just_created = super + NotifyService.new.call(reblog.account, status) if reblog.account.local? && just_created + status + end + + def object + @xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS) + end + + private + + def reblog + return @reblog if defined? @reblog + + original_status = Ostatus::Activity::Remote.new(object).perform + return if original_status.nil? + + @reblog = original_status.reblog? ? original_status.reblog : original_status + end +end diff --git a/app/lib/ostatus/atom_serializer.rb b/app/lib/ostatus/atom_serializer.rb new file mode 100644 index 000000000..909d84df3 --- /dev/null +++ b/app/lib/ostatus/atom_serializer.rb @@ -0,0 +1,367 @@ +# frozen_string_literal: true + +class Ostatus::AtomSerializer + include RoutingHelper + include ActionView::Helpers::SanitizeHelper + + class << self + def render(element) + document = Ox::Document.new(version: '1.0') + document << element + ('' + Ox.dump(element, effort: :tolerant)).force_encoding('UTF-8') + end + end + + def author(account) + author = Ox::Element.new('author') + + uri = TagManager.instance.uri_for(account) + + append_element(author, 'id', uri) + append_element(author, 'activity:object-type', TagManager::TYPES[:person]) + append_element(author, 'uri', uri) + append_element(author, 'name', account.username) + append_element(author, 'email', account.local? ? account.local_username_and_domain : account.acct) + append_element(author, 'summary', Formatter.instance.simplified_format(account).to_str, type: :html) if account.note? + append_element(author, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(account)) + append_element(author, 'link', nil, rel: :avatar, type: account.avatar_content_type, 'media:width': 120, 'media:height': 120, href: full_asset_url(account.avatar.url(:original))) if account.avatar? + append_element(author, 'link', nil, rel: :header, type: account.header_content_type, 'media:width': 700, 'media:height': 335, href: full_asset_url(account.header.url(:original))) if account.header? + append_element(author, 'poco:preferredUsername', account.username) + append_element(author, 'poco:displayName', account.display_name) if account.display_name? + append_element(author, 'poco:note', account.local? ? account.note : strip_tags(account.note)) if account.note? + append_element(author, 'mastodon:scope', account.locked? ? :private : :public) + + author + end + + def feed(account, stream_entries) + feed = Ox::Element.new('feed') + + add_namespaces(feed) + + append_element(feed, 'id', account_url(account, format: 'atom')) + append_element(feed, 'title', account.display_name.presence || account.username) + append_element(feed, 'subtitle', account.note) + append_element(feed, 'updated', account.updated_at.iso8601) + append_element(feed, 'logo', full_asset_url(account.avatar.url(:original))) + + feed << author(account) + + append_element(feed, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(account)) + append_element(feed, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_url(account, format: 'atom')) + append_element(feed, 'link', nil, rel: :next, type: 'application/atom+xml', href: account_url(account, format: 'atom', max_id: stream_entries.last.id)) if stream_entries.size == 20 + append_element(feed, 'link', nil, rel: :hub, href: api_push_url) + append_element(feed, 'link', nil, rel: :salmon, href: api_salmon_url(account.id)) + + stream_entries.each do |stream_entry| + feed << entry(stream_entry) + end + + feed + end + + def entry(stream_entry, root = false) + entry = Ox::Element.new('entry') + + add_namespaces(entry) if root + + append_element(entry, 'id', TagManager.instance.unique_tag(stream_entry.created_at, stream_entry.activity_id, stream_entry.activity_type)) + append_element(entry, 'published', stream_entry.created_at.iso8601) + append_element(entry, 'updated', stream_entry.updated_at.iso8601) + append_element(entry, 'title', stream_entry&.status&.title || "#{stream_entry.account.acct} deleted status") + + entry << author(stream_entry.account) if root + + append_element(entry, 'activity:object-type', TagManager::TYPES[stream_entry.object_type]) + append_element(entry, 'activity:verb', TagManager::VERBS[stream_entry.verb]) + + entry << object(stream_entry.target) if stream_entry.targeted? + + if stream_entry.status.nil? + append_element(entry, 'content', 'Deleted status') + else + serialize_status_attributes(entry, stream_entry.status) + end + + append_element(entry, 'link', nil, rel: :alternate, type: 'text/html', href: account_stream_entry_url(stream_entry.account, stream_entry)) + append_element(entry, 'link', nil, rel: :self, type: 'application/atom+xml', href: account_stream_entry_url(stream_entry.account, stream_entry, format: 'atom')) + append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(stream_entry.thread), href: TagManager.instance.url_for(stream_entry.thread)) if stream_entry.threaded? + append_element(entry, 'ostatus:conversation', nil, ref: conversation_uri(stream_entry.status.conversation)) unless stream_entry&.status&.conversation_id.nil? + + entry + end + + def object(status) + object = Ox::Element.new('activity:object') + + append_element(object, 'id', TagManager.instance.uri_for(status)) + append_element(object, 'published', status.created_at.iso8601) + append_element(object, 'updated', status.updated_at.iso8601) + append_element(object, 'title', status.title) + + object << author(status.account) + + append_element(object, 'activity:object-type', TagManager::TYPES[status.object_type]) + append_element(object, 'activity:verb', TagManager::VERBS[status.verb]) + + serialize_status_attributes(object, status) + + append_element(object, 'link', nil, rel: :alternate, type: 'text/html', href: TagManager.instance.url_for(status)) + append_element(object, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(status.thread), href: TagManager.instance.url_for(status.thread)) unless status.thread.nil? + append_element(object, 'ostatus:conversation', nil, ref: conversation_uri(status.conversation)) unless status.conversation_id.nil? + + object + end + + def follow_salmon(follow) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + description = "#{follow.account.acct} started following #{follow.target_account.acct}" + + append_element(entry, 'id', TagManager.instance.unique_tag(follow.created_at, follow.id, 'Follow')) + append_element(entry, 'title', description) + append_element(entry, 'content', description, type: :html) + + entry << author(follow.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:follow]) + + object = author(follow.target_account) + object.value = 'activity:object' + + entry << object + entry + end + + def follow_request_salmon(follow_request) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + append_element(entry, 'id', TagManager.instance.unique_tag(follow_request.created_at, follow_request.id, 'FollowRequest')) + append_element(entry, 'title', "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}") + + entry << author(follow_request.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:request_friend]) + + object = author(follow_request.target_account) + object.value = 'activity:object' + + entry << object + entry + end + + def authorize_follow_request_salmon(follow_request) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) + append_element(entry, 'title', "#{follow_request.target_account.acct} authorizes follow request by #{follow_request.account.acct}") + + entry << author(follow_request.target_account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:authorize]) + + object = Ox::Element.new('activity:object') + object << author(follow_request.account) + + append_element(object, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(object, 'activity:verb', TagManager::VERBS[:request_friend]) + + inner_object = author(follow_request.target_account) + inner_object.value = 'activity:object' + + object << inner_object + entry << object + entry + end + + def reject_follow_request_salmon(follow_request) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow_request.id, 'FollowRequest')) + append_element(entry, 'title', "#{follow_request.target_account.acct} rejects follow request by #{follow_request.account.acct}") + + entry << author(follow_request.target_account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:reject]) + + object = Ox::Element.new('activity:object') + object << author(follow_request.account) + + append_element(object, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(object, 'activity:verb', TagManager::VERBS[:request_friend]) + + inner_object = author(follow_request.target_account) + inner_object.value = 'activity:object' + + object << inner_object + entry << object + entry + end + + def unfollow_salmon(follow) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + description = "#{follow.account.acct} is no longer following #{follow.target_account.acct}" + + append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, follow.id, 'Follow')) + append_element(entry, 'title', description) + append_element(entry, 'content', description, type: :html) + + entry << author(follow.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:unfollow]) + + object = author(follow.target_account) + object.value = 'activity:object' + + entry << object + entry + end + + def block_salmon(block) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + description = "#{block.account.acct} no longer wishes to interact with #{block.target_account.acct}" + + append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) + append_element(entry, 'title', description) + + entry << author(block.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:block]) + + object = author(block.target_account) + object.value = 'activity:object' + + entry << object + entry + end + + def unblock_salmon(block) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + description = "#{block.account.acct} no longer blocks #{block.target_account.acct}" + + append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, block.id, 'Block')) + append_element(entry, 'title', description) + + entry << author(block.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:unblock]) + + object = author(block.target_account) + object.value = 'activity:object' + + entry << object + entry + end + + def favourite_salmon(favourite) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}" + + append_element(entry, 'id', TagManager.instance.unique_tag(favourite.created_at, favourite.id, 'Favourite')) + append_element(entry, 'title', description) + append_element(entry, 'content', description, type: :html) + + entry << author(favourite.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:favorite]) + + entry << object(favourite.status) + + append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) + + entry + end + + def unfavourite_salmon(favourite) + entry = Ox::Element.new('entry') + add_namespaces(entry) + + description = "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}" + + append_element(entry, 'id', TagManager.instance.unique_tag(Time.now.utc, favourite.id, 'Favourite')) + append_element(entry, 'title', description) + append_element(entry, 'content', description, type: :html) + + entry << author(favourite.account) + + append_element(entry, 'activity:object-type', TagManager::TYPES[:activity]) + append_element(entry, 'activity:verb', TagManager::VERBS[:unfavorite]) + + entry << object(favourite.status) + + append_element(entry, 'thr:in-reply-to', nil, ref: TagManager.instance.uri_for(favourite.status), href: TagManager.instance.url_for(favourite.status)) + + entry + end + + private + + def append_element(parent, name, content = nil, attributes = {}) + element = Ox::Element.new(name) + attributes.each { |k, v| element[k] = sanitize_str(v) } + element << sanitize_str(content) unless content.nil? + parent << element + end + + def sanitize_str(raw_str) + raw_str.to_s + end + + def conversation_uri(conversation) + return conversation.uri if conversation.uri? + TagManager.instance.unique_tag(conversation.created_at, conversation.id, 'Conversation') + end + + def add_namespaces(parent) + parent['xmlns'] = TagManager::XMLNS + parent['xmlns:thr'] = TagManager::THR_XMLNS + parent['xmlns:activity'] = TagManager::AS_XMLNS + parent['xmlns:poco'] = TagManager::POCO_XMLNS + parent['xmlns:media'] = TagManager::MEDIA_XMLNS + parent['xmlns:ostatus'] = TagManager::OS_XMLNS + parent['xmlns:mastodon'] = TagManager::MTDN_XMLNS + end + + def serialize_status_attributes(entry, status) + append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text? + append_element(entry, 'content', Formatter.instance.format(status).to_str, type: 'html', 'xml:lang': status.language) + + status.mentions.each do |mentioned| + append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:person], href: TagManager.instance.uri_for(mentioned.account)) + end + + append_element(entry, 'link', nil, rel: :mentioned, 'ostatus:object-type': TagManager::TYPES[:collection], href: TagManager::COLLECTIONS[:public]) if status.public_visibility? + + status.tags.each do |tag| + append_element(entry, 'category', nil, term: tag.name) + end + + append_element(entry, 'category', nil, term: 'nsfw') if status.sensitive? + + status.media_attachments.each do |media| + append_element(entry, 'link', nil, rel: :enclosure, type: media.file_content_type, length: media.file_file_size, href: full_asset_url(media.file.url(:original, false))) + end + + append_element(entry, 'mastodon:scope', status.visibility) + end +end diff --git a/app/services/authorize_follow_service.rb b/app/services/authorize_follow_service.rb index 97c76bee1..a25d11dbd 100644 --- a/app/services/authorize_follow_service.rb +++ b/app/services/authorize_follow_service.rb @@ -10,6 +10,6 @@ class AuthorizeFollowService < BaseService private def build_xml(follow_request) - AtomSerializer.render(AtomSerializer.new.authorize_follow_request_salmon(follow_request)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request)) end end diff --git a/app/services/block_service.rb b/app/services/block_service.rb index d59b47afb..15420e192 100644 --- a/app/services/block_service.rb +++ b/app/services/block_service.rb @@ -18,6 +18,6 @@ class BlockService < BaseService private def build_xml(block) - AtomSerializer.render(AtomSerializer.new.block_salmon(block)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.block_salmon(block)) end end diff --git a/app/services/concerns/stream_entry_renderer.rb b/app/services/concerns/stream_entry_renderer.rb index ef176d8a6..d9c30c53c 100644 --- a/app/services/concerns/stream_entry_renderer.rb +++ b/app/services/concerns/stream_entry_renderer.rb @@ -2,6 +2,6 @@ module StreamEntryRenderer def stream_entry_to_xml(stream_entry) - AtomSerializer.render(AtomSerializer.new.entry(stream_entry, true)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.entry(stream_entry, true)) end end diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb index 90267af33..a08aba638 100644 --- a/app/services/favourite_service.rb +++ b/app/services/favourite_service.rb @@ -28,6 +28,6 @@ class FavouriteService < BaseService private def build_xml(favourite) - AtomSerializer.render(AtomSerializer.new.favourite_salmon(favourite)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.favourite_salmon(favourite)) end end diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index e54ff7d0f..7a7275b6e 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -57,10 +57,10 @@ class FollowService < BaseService end def build_follow_request_xml(follow_request) - AtomSerializer.render(AtomSerializer.new.follow_request_salmon(follow_request)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.follow_request_salmon(follow_request)) end def build_follow_xml(follow) - AtomSerializer.render(AtomSerializer.new.follow_salmon(follow)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.follow_salmon(follow)) end end diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb index c335d2159..b99048a06 100644 --- a/app/services/process_feed_service.rb +++ b/app/services/process_feed_service.rb @@ -16,274 +16,14 @@ class ProcessFeedService < BaseService end def process_entries(xml, account) - xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact + xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).reverse_each.map { |entry| process_entry(entry, account) }.compact end - class ProcessEntry - def call(xml, account) - @account = account - @xml = xml - - return if skip_unsupported_type? - - case verb - when :post, :share - return create_status - when :delete - return delete_status - end - rescue ActiveRecord::RecordInvalid => e - Rails.logger.debug "Nothing was saved for #{id} because: #{e}" - nil - end - - private - - def create_status - if redis.exists("delete_upon_arrival:#{@account.id}:#{id}") - Rails.logger.debug "Delete for status #{id} was queued, ignoring" - return - end - - status, just_created = nil - - Rails.logger.debug "Creating remote status #{id}" - - if verb == :share - original_status = shared_status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS)) - return nil if original_status.nil? - end - - ApplicationRecord.transaction do - status, just_created = status_from_xml(@xml) - - return if status.nil? - return status unless just_created - - if verb == :share - status.reblog = original_status.reblog? ? original_status.reblog : original_status - end - - status.save! - end - - if thread?(@xml) && status.thread.nil? - Rails.logger.debug "Trying to attach #{status.id} (#{id(@xml)}) to #{thread(@xml).first}" - ThreadResolveWorker.perform_async(status.id, thread(@xml).second) - end - - notify_about_mentions!(status) unless status.reblog? - notify_about_reblog!(status) if status.reblog? && status.reblog.account.local? - - Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution" - - LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text? - DistributionWorker.perform_async(status.id) - - status - end - - def notify_about_mentions!(status) - status.mentions.includes(:account).each do |mention| - mentioned_account = mention.account - next unless mentioned_account.local? - NotifyService.new.call(mentioned_account, mention) - end - end - - def notify_about_reblog!(status) - NotifyService.new.call(status.reblog.account, status) - end - - def delete_status - Rails.logger.debug "Deleting remote status #{id}" - status = Status.find_by(uri: id, account: @account) - - if status.nil? - redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) - else - RemoveStatusService.new.call(status) - end - end - - def skip_unsupported_type? - !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type)) - end - - def shared_status_from_xml(entry) - status = find_status(id(entry)) - - return status unless status.nil? - - FetchRemoteStatusService.new.call(url(entry)) - end - - def status_from_xml(entry) - # Return early if status already exists in db - status = find_status(id(entry)) - - return [status, false] unless status.nil? - - account = @account - - return [nil, false] if account.suspended? - - status = Status.create!( - uri: id(entry), - url: url(entry), - account: account, - text: content(entry), - spoiler_text: content_warning(entry), - created_at: published(entry), - reply: thread?(entry), - language: content_language(entry), - visibility: visibility_scope(entry), - conversation: find_or_create_conversation(entry), - thread: thread?(entry) ? find_status(thread(entry).first) : nil - ) - - mentions_from_xml(status, entry) - hashtags_from_xml(status, entry) - media_from_xml(status, entry) - - [status, true] - end - - def find_or_create_conversation(xml) - uri = xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content - return if uri.nil? - - if TagManager.instance.local_id?(uri) - local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Conversation') - return Conversation.find_by(id: local_id) - end - - Conversation.find_by(uri: uri) || Conversation.create!(uri: uri) - end - - def find_status(uri) - if TagManager.instance.local_id?(uri) - local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status') - return Status.find_by(id: local_id) - end - - Status.find_by(uri: uri) - end - - def mentions_from_xml(parent, xml) - processed_account_ids = [] - - xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link| - next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type'] - - mentioned_account = account_from_href(link['href']) - - next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id) - - mentioned_account.mentions.where(status: parent).first_or_create(status: parent) - - # So we can skip duplicate mentions - processed_account_ids << mentioned_account.id - end - end - - def account_from_href(href) - url = Addressable::URI.parse(href).normalize - - if TagManager.instance.web_domain?(url.host) - Account.find_local(url.path.gsub('/users/', '')) - else - Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href) - end - end - - def hashtags_from_xml(parent, xml) - tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?) - ProcessHashtagsService.new.call(parent, tags) - end - - def media_from_xml(parent, xml) - do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? - - xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link| - next unless link['href'] - - media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href']) - parsed_url = Addressable::URI.parse(link['href']).normalize - - next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? - - media.save - - next if do_not_download - - begin - media.file_remote_url = link['href'] - media.save! - rescue ActiveRecord::RecordInvalid - next - end - end - end - - def id(xml = @xml) - xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content - end - - def verb(xml = @xml) - raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content - TagManager::VERBS.key(raw) - rescue - :post - end - - def type(xml = @xml) - raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content - TagManager::TYPES.key(raw) - rescue - :activity - end - - def url(xml = @xml) - link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) - link.nil? ? nil : link['href'] - end - - def content(xml = @xml) - xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content - end - - def content_language(xml = @xml) - xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' - end - - def content_warning(xml = @xml) - xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || '' - end - - def visibility_scope(xml = @xml) - xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public - end - - def published(xml = @xml) - xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content - end - - def thread?(xml = @xml) - !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil? - end - - def thread(xml = @xml) - thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS) - [thr['ref'], thr['href']] - end - - def account?(xml = @xml) - !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil? - end - - def redis - Redis.current - end + def process_entry(xml, account) + activity = Ostatus::Activity::General.new(xml, account) + activity.specialize&.perform if activity.status? + rescue ActiveRecord::RecordInvalid => e + Rails.logger.debug "Nothing was saved for #{id} because: #{e}" + nil end end diff --git a/app/services/reject_follow_service.rb b/app/services/reject_follow_service.rb index 675007938..87fc49b34 100644 --- a/app/services/reject_follow_service.rb +++ b/app/services/reject_follow_service.rb @@ -10,6 +10,6 @@ class RejectFollowService < BaseService private def build_xml(follow_request) - AtomSerializer.render(AtomSerializer.new.reject_follow_request_salmon(follow_request)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request)) end end diff --git a/app/services/unblock_service.rb b/app/services/unblock_service.rb index 3a3fd2d8c..50c2dc2f0 100644 --- a/app/services/unblock_service.rb +++ b/app/services/unblock_service.rb @@ -11,6 +11,6 @@ class UnblockService < BaseService private def build_xml(block) - AtomSerializer.render(AtomSerializer.new.unblock_salmon(block)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.unblock_salmon(block)) end end diff --git a/app/services/unfavourite_service.rb b/app/services/unfavourite_service.rb index a32e87bff..ede3caad1 100644 --- a/app/services/unfavourite_service.rb +++ b/app/services/unfavourite_service.rb @@ -13,6 +13,6 @@ class UnfavouriteService < BaseService private def build_xml(favourite) - AtomSerializer.render(AtomSerializer.new.unfavourite_salmon(favourite)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.unfavourite_salmon(favourite)) end end diff --git a/app/services/unfollow_service.rb b/app/services/unfollow_service.rb index 9b39f4945..0c9a5f657 100644 --- a/app/services/unfollow_service.rb +++ b/app/services/unfollow_service.rb @@ -14,6 +14,6 @@ class UnfollowService < BaseService private def build_xml(follow) - AtomSerializer.render(AtomSerializer.new.unfollow_salmon(follow)) + Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.unfollow_salmon(follow)) end end diff --git a/app/workers/pubsubhubbub/distribution_worker.rb b/app/workers/pubsubhubbub/distribution_worker.rb index 7592354cc..9c1fa76cb 100644 --- a/app/workers/pubsubhubbub/distribution_worker.rb +++ b/app/workers/pubsubhubbub/distribution_worker.rb @@ -22,7 +22,7 @@ class Pubsubhubbub::DistributionWorker def distribute_public!(stream_entries) return if stream_entries.empty? - @payload = AtomSerializer.render(AtomSerializer.new.feed(@account, stream_entries)) + @payload = Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.feed(@account, stream_entries)) Pubsubhubbub::DeliveryWorker.push_bulk(@subscriptions) do |subscription| [subscription.id, @payload] @@ -32,7 +32,7 @@ class Pubsubhubbub::DistributionWorker def distribute_hidden!(stream_entries) return if stream_entries.empty? - @payload = AtomSerializer.render(AtomSerializer.new.feed(@account, stream_entries)) + @payload = Ostatus::AtomSerializer.render(Ostatus::AtomSerializer.new.feed(@account, stream_entries)) @domains = @account.followers.domains Pubsubhubbub::DeliveryWorker.push_bulk(@subscriptions.reject { |s| !allowed_to_receive?(s.callback_url, s.domain) }) do |subscription| diff --git a/spec/lib/atom_serializer_spec.rb b/spec/lib/atom_serializer_spec.rb deleted file mode 100644 index d14fc5b40..000000000 --- a/spec/lib/atom_serializer_spec.rb +++ /dev/null @@ -1,1554 +0,0 @@ -require 'rails_helper' - -RSpec.describe AtomSerializer do - shared_examples 'follow request salmon' do - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'username') - follow_request = Fabricate(:follow_request, account: account) - - follow_request_salmon = serialize(follow_request) - - expect(follow_request_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends activity:object-type element with activity type' do - follow_request = Fabricate(:follow_request) - - follow_request_salmon = serialize(follow_request) - - object_type = follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with request_friend type' do - follow_request = Fabricate(:follow_request) - - follow_request_salmon = serialize(follow_request) - - verb = follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:request_friend] - end - - it 'appends activity:object with target account' do - target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') - follow_request = Fabricate(:follow_request, target_account: target_account) - - follow_request_salmon = serialize(follow_request) - - object = follow_request_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq 'https://domain/id' - end - end - - shared_examples 'namespaces' do - it 'adds namespaces' do - element = serialize - - expect(element['xmlns']).to eq TagManager::XMLNS - expect(element['xmlns:thr']).to eq TagManager::THR_XMLNS - expect(element['xmlns:activity']).to eq TagManager::AS_XMLNS - expect(element['xmlns:poco']).to eq TagManager::POCO_XMLNS - expect(element['xmlns:media']).to eq TagManager::MEDIA_XMLNS - expect(element['xmlns:ostatus']).to eq TagManager::OS_XMLNS - expect(element['xmlns:mastodon']).to eq TagManager::MTDN_XMLNS - end - end - - shared_examples 'no namespaces' do - it 'does not add namespaces' do - expect(serialize['xmlns']).to eq nil - end - end - - shared_examples 'status attributes' do - it 'appends summary element with spoiler text if present' do - status = Fabricate(:status, language: :ca, spoiler_text: 'spoiler text') - - element = serialize(status) - - summary = element.summary - expect(summary['xml:lang']).to eq 'ca' - expect(summary.text).to eq 'spoiler text' - end - - it 'does not append summary element with spoiler text if not present' do - status = Fabricate(:status, spoiler_text: '') - element = serialize(status) - element.nodes.each { |node| expect(node.name).not_to eq 'summary' } - end - - it 'appends content element with formatted status' do - status = Fabricate(:status, language: :ca, text: 'text') - - element = serialize(status) - - content = element.content - expect(content[:type]).to eq 'html' - expect(content['xml:lang']).to eq 'ca' - expect(content.text).to eq '

text

' - end - - it 'appends link elements for mentioned accounts' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status) - Fabricate(:mention, account: account, status: status) - - element = serialize(status) - - mentioned = element.nodes.find do |node| - node.name == 'link' && - node[:rel] == 'mentioned' && - node['ostatus:object-type'] == TagManager::TYPES[:person] - end - expect(mentioned[:href]).to eq 'https://cb6e6126.ngrok.io/users/username' - end - end - - describe 'render' do - it 'returns XML with emojis' do - element = Ox::Element.new('tag') - element << '💩' - xml = AtomSerializer.render(element) - - expect(xml).to eq "\n💩\n" - end - - it 'returns XML, stripping invalid characters like \b and \v' do - element = Ox::Element.new('tag') - element << "im l33t\b haxo\b\vr" - xml = AtomSerializer.render(element) - - expect(xml).to eq "\nim l33t haxor\n" - end - end - - describe '#author' do - context 'when note is present' do - it 'appends poco:note element with note for local account' do - account = Fabricate(:account, domain: nil, note: '

note

') - - author = AtomSerializer.new.author(account) - - note = author.nodes.find { |node| node.name == 'poco:note' } - expect(note.text).to eq '

note

' - end - - it 'appends poco:note element with tags-stripped note for remote account' do - account = Fabricate(:account, domain: 'remote', note: '

note

') - - author = AtomSerializer.new.author(account) - - note = author.nodes.find { |node| node.name == 'poco:note' } - expect(note.text).to eq 'note' - end - - it 'appends summary element with type attribute and simplified note if present' do - account = Fabricate(:account, note: 'note') - author = AtomSerializer.new.author(account) - expect(author.summary.text).to eq '

note

' - expect(author.summary[:type]).to eq 'html' - end - end - - context 'when note is not present' do - it 'does not append poco:note element' do - account = Fabricate(:account, note: '') - author = AtomSerializer.new.author(account) - author.nodes.each { |node| expect(node.name).not_to eq 'poco:note' } - end - - it 'does not append summary element' do - account = Fabricate(:account, note: '') - author = AtomSerializer.new.author(account) - author.nodes.each { |node| expect(node.name).not_to eq 'summary' } - end - end - - it 'returns author element' do - account = Fabricate(:account) - author = AtomSerializer.new.author(account) - expect(author.name).to eq 'author' - end - - it 'appends activity:object-type element with person type' do - account = Fabricate(:account, domain: nil, username: 'username') - - author = AtomSerializer.new.author(account) - - object_type = author.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:person] - end - - it 'appends email element with username and domain for local account' do - account = Fabricate(:account, username: 'username') - author = AtomSerializer.new.author(account) - expect(author.email.text).to eq 'username@cb6e6126.ngrok.io' - end - - it 'appends email element with username and domain for remote user' do - account = Fabricate(:account, domain: 'domain', username: 'username') - author = AtomSerializer.new.author(account) - expect(author.email.text).to eq 'username@domain' - end - - it 'appends link element for an alternative' do - account = Fabricate(:account, domain: nil, username: 'username') - - author = AtomSerializer.new.author(account) - - link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } - expect(link[:type]).to eq 'text/html' - expect(link[:rel]).to eq 'alternate' - expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username' - end - - it 'has link element for avatar if present' do - account = Fabricate(:account, avatar: attachment_fixture('avatar.gif')) - - author = AtomSerializer.new.author(account) - - link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'avatar' } - expect(link[:type]).to eq 'image/gif' - expect(link['media:width']).to eq '120' - expect(link['media:height']).to eq '120' - expect(link[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/ - end - - it 'does not have link element for avatar if not present' do - account = Fabricate(:account, avatar: nil) - - author = AtomSerializer.new.author(account) - - author.nodes.each do |node| - expect(node[:rel]).not_to eq 'avatar' if node.name == 'link' - end - end - - it 'appends link element for header if present' do - account = Fabricate(:account, header: attachment_fixture('avatar.gif')) - - author = AtomSerializer.new.author(account) - - link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'header' } - expect(link[:type]).to eq 'image/gif' - expect(link['media:width']).to eq '700' - expect(link['media:height']).to eq '335' - expect(link[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/headers\/.+\/original\/avatar.gif/ - end - - it 'does not append link element for header if not present' do - account = Fabricate(:account, header: nil) - - author = AtomSerializer.new.author(account) - - author.nodes.each do |node| - expect(node[:rel]).not_to eq 'header' if node.name == 'link' - end - end - - it 'appends poco:displayName element with display name if present' do - account = Fabricate(:account, display_name: 'display name') - - author = AtomSerializer.new.author(account) - - display_name = author.nodes.find { |node| node.name == 'poco:displayName' } - expect(display_name.text).to eq 'display name' - end - - it 'does not append poco:displayName element with display name if not present' do - account = Fabricate(:account, display_name: '') - author = AtomSerializer.new.author(account) - author.nodes.each { |node| expect(node.name).not_to eq 'poco:displayName' } - end - - it "appends mastodon:scope element with 'private' if locked" do - account = Fabricate(:account, locked: true) - - author = AtomSerializer.new.author(account) - - scope = author.nodes.find { |node| node.name == 'mastodon:scope' } - expect(scope.text).to eq 'private' - end - - it "appends mastodon:scope element with 'public' if unlocked" do - account = Fabricate(:account, locked: false) - - author = AtomSerializer.new.author(account) - - scope = author.nodes.find { |node| node.name == 'mastodon:scope' } - expect(scope.text).to eq 'public' - end - - it 'includes URI' do - account = Fabricate(:account, domain: nil, username: 'username') - - author = AtomSerializer.new.author(account) - - expect(author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - expect(author.uri.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'includes username' do - account = Fabricate(:account, username: 'username') - - author = AtomSerializer.new.author(account) - - name = author.nodes.find { |node| node.name == 'name' } - username = author.nodes.find { |node| node.name == 'poco:preferredUsername' } - expect(name.text).to eq 'username' - expect(username.text).to eq 'username' - end - end - - describe '#entry' do - shared_examples 'not root' do - include_examples 'no namespaces' do - def serialize - subject - end - end - - it 'does not append author element' do - subject.nodes.each { |node| expect(node.name).not_to eq 'author' } - end - end - - context 'it is root' do - include_examples 'namespaces' do - def serialize - stream_entry = Fabricate(:stream_entry) - AtomSerializer.new.entry(stream_entry, true) - end - end - - it 'appends author element' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - entry = AtomSerializer.new.entry(status.stream_entry, true) - - expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - end - - context 'if status is present' do - include_examples 'status attributes' do - def serialize(status) - AtomSerializer.new.entry(status.stream_entry, true) - end - end - - it 'appends link element for the public collection if status is publicly visible' do - status = Fabricate(:status, visibility: :public) - - entry = AtomSerializer.new.entry(status.stream_entry) - - mentioned_person = entry.nodes.find do |node| - node.name == 'link' && - node[:rel] == 'mentioned' && - node['ostatus:object-type'] == TagManager::TYPES[:collection] - end - expect(mentioned_person[:href]).to eq TagManager::COLLECTIONS[:public] - end - - it 'does not append link element for the public collection if status is not publicly visible' do - status = Fabricate(:status, visibility: :private) - - entry = AtomSerializer.new.entry(status.stream_entry) - - entry.nodes.each do |node| - if node.name == 'link' && - node[:rel] == 'mentioned' && - node['ostatus:object-type'] == TagManager::TYPES[:collection] - expect(mentioned_collection[:href]).not_to eq TagManager::COLLECTIONS[:public] - end - end - end - - it 'appends category elements for tags' do - tag = Fabricate(:tag, name: 'tag') - status = Fabricate(:status, tags: [ tag ]) - - entry = AtomSerializer.new.entry(status.stream_entry) - - expect(entry.category[:term]).to eq 'tag' - end - - it 'appends category element for NSFW if status is sensitive' do - status = Fabricate(:status, sensitive: true) - entry = AtomSerializer.new.entry(status.stream_entry) - expect(entry.category[:term]).to eq 'nsfw' - end - - it 'appends link elements for media attachments' do - file = attachment_fixture('attachment.jpg') - media_attachment = Fabricate(:media_attachment, file: file) - status = Fabricate(:status, media_attachments: [ media_attachment ]) - - entry = AtomSerializer.new.entry(status.stream_entry) - - enclosure = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'enclosure' } - expect(enclosure[:type]).to eq 'image/jpeg' - expect(enclosure[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/media_attachments\/files\/.+\/original\/attachment.jpg$/ - end - - it 'appends mastodon:scope element with visibility' do - status = Fabricate(:status, visibility: :public) - - entry = AtomSerializer.new.entry(status.stream_entry) - - scope = entry.nodes.find { |node| node.name == 'mastodon:scope' } - expect(scope.text).to eq 'public' - end - - it 'returns element whose rendered view triggers creation when processed' do - remote_account = Account.create!(username: 'username') - remote_status = Fabricate(:status, account: remote_account) - remote_status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z') - - entry = AtomSerializer.new.entry(remote_status.stream_entry, true) - xml = AtomSerializer.render(entry).gsub('cb6e6126.ngrok.io', 'remote') - - remote_status.destroy! - remote_account.destroy! - - account = Account.create!( - domain: 'remote', - username: 'username', - last_webfingered_at: Time.now.utc, - ) - - ProcessFeedService.new.call(xml, account) - - expect(Status.find_by(uri: "tag:remote,2000-01-01:objectId=#{remote_status.id}:objectType=Status")).to be_instance_of Status - end - end - - context 'if status is not present' do - it 'appends content element saying status is deleted' do - status = Fabricate(:status) - status.destroy! - - entry = AtomSerializer.new.entry(status.stream_entry) - - expect(entry.content.text).to eq 'Deleted status' - end - - it 'appends title element saying the status is deleted' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - status.destroy! - - entry = AtomSerializer.new.entry(status.stream_entry) - - expect(entry.title.text).to eq 'username deleted status' - end - end - - context 'it is not root' do - let(:stream_entry) { Fabricate(:stream_entry) } - subject { AtomSerializer.new.entry(stream_entry, false) } - include_examples 'not root' - end - - context 'without root parameter' do - let(:stream_entry) { Fabricate(:stream_entry) } - subject { AtomSerializer.new.entry(stream_entry) } - include_examples 'not root' - end - - it 'returns entry element' do - stream_entry = Fabricate(:stream_entry) - entry = AtomSerializer.new.entry(stream_entry) - expect(entry.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - status = Fabricate(:status, reblog_of_id: nil) - status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z') - - entry = AtomSerializer.new.entry(status.stream_entry) - - expect(entry.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" - end - - it 'appends published element with created date' do - stream_entry = Fabricate(:stream_entry, created_at: '2000-01-01T00:00:00Z') - entry = AtomSerializer.new.entry(stream_entry) - expect(entry.published.text).to eq '2000-01-01T00:00:00Z' - end - - it 'appends updated element with updated date' do - stream_entry = Fabricate(:stream_entry, updated_at: '2000-01-01T00:00:00Z') - entry = AtomSerializer.new.entry(stream_entry) - expect(entry.updated.text).to eq '2000-01-01T00:00:00Z' - end - - it 'appends title element with status title' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account, reblog_of_id: nil) - entry = AtomSerializer.new.entry(status.stream_entry) - expect(entry.title.text).to eq 'New status by username' - end - - it 'appends activity:object-type element with object type' do - status = Fabricate(:status) - entry = AtomSerializer.new.entry(status.stream_entry) - object_type = entry.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:note] - end - - it 'appends activity:verb element with object type' do - status = Fabricate(:status) - - entry = AtomSerializer.new.entry(status.stream_entry) - - object_type = entry.nodes.find { |node| node.name == 'activity:verb' } - expect(object_type.text).to eq TagManager::VERBS[:post] - end - - it 'appends activity:object element with target if present' do - reblogged = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') - reblog = Fabricate(:status, reblog: reblogged) - - entry = AtomSerializer.new.entry(reblog.stream_entry) - - object = entry.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{reblogged.id}:objectType=Status" - end - - it 'does not append activity:object element if target is not present' do - status = Fabricate(:status, reblog_of_id: nil) - entry = AtomSerializer.new.entry(status.stream_entry) - entry.nodes.each { |node| expect(node.name).not_to eq 'activity:object' } - end - - it 'appends link element for an alternative' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - entry = AtomSerializer.new.entry(status.stream_entry) - - link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } - expect(link[:type]).to eq 'text/html' - expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}" - end - - it 'appends link element for itself' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - entry = AtomSerializer.new.entry(status.stream_entry) - - link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' } - expect(link[:type]).to eq 'application/atom+xml' - expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}.atom" - end - - it 'appends thr:in-reply-to element if threaded' do - in_reply_to_status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z', reblog_of_id: nil) - reply_status = Fabricate(:status, in_reply_to_id: in_reply_to_status.id) - - entry = AtomSerializer.new.entry(reply_status.stream_entry) - - in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' } - expect(in_reply_to[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{in_reply_to_status.id}:objectType=Status" - end - - it 'does not append thr:in-reply-to element if not threaded' do - status = Fabricate(:status) - entry = AtomSerializer.new.entry(status.stream_entry) - entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' } - end - - it 'appends ostatus:conversation if conversation id is present' do - status = Fabricate(:status) - status.conversation.update!(created_at: '2000-01-01T00:00:00Z') - - entry = AtomSerializer.new.entry(status.stream_entry) - - conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' } - expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation_id}:objectType=Conversation" - end - - it 'does not append ostatus:conversation if conversation id is not present' do - status = Fabricate.build(:status, conversation_id: nil) - status.save!(validate: false) - - entry = AtomSerializer.new.entry(status.stream_entry) - - entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' } - end - end - - describe '#feed' do - include_examples 'namespaces' do - def serialize - account = Fabricate(:account) - AtomSerializer.new.feed(account, []) - end - end - - it 'returns feed element' do - account = Fabricate(:account) - feed = AtomSerializer.new.feed(account, []) - expect(feed.name).to eq 'feed' - end - - it 'appends id element with account Atom URL' do - account = Fabricate(:account, username: 'username') - feed = AtomSerializer.new.feed(account, []) - expect(feed.id.text).to eq 'https://cb6e6126.ngrok.io/users/username.atom' - end - - it 'appends title element with account display name if present' do - account = Fabricate(:account, display_name: 'display name') - feed = AtomSerializer.new.feed(account, []) - expect(feed.title.text).to eq 'display name' - end - - it 'does not append title element with account username if account display name is not present' do - account = Fabricate(:account, display_name: '', username: 'username') - feed = AtomSerializer.new.feed(account, []) - expect(feed.title.text).to eq 'username' - end - - it 'appends subtitle element with account note' do - account = Fabricate(:account, note: 'note') - feed = AtomSerializer.new.feed(account, []) - expect(feed.subtitle.text).to eq 'note' - end - - it 'appends updated element with date account got updated' do - account = Fabricate(:account, updated_at: '2000-01-01T00:00:00Z') - feed = AtomSerializer.new.feed(account, []) - expect(feed.updated.text).to eq '2000-01-01T00:00:00Z' - end - - it 'appends logo element with full asset URL for original account avatar' do - account = Fabricate(:account, avatar: attachment_fixture('avatar.gif')) - feed = AtomSerializer.new.feed(account, []) - expect(feed.logo.text).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/ - end - - it 'appends author element' do - account = Fabricate(:account, username: 'username') - feed = AtomSerializer.new.feed(account, []) - expect(feed.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends link element for an alternative' do - account = Fabricate(:account, username: 'username') - - feed = AtomSerializer.new.feed(account, []) - - link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } - expect(link[:type]).to eq 'text/html' - expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username' - end - - it 'appends link element for itself' do - account = Fabricate(:account, username: 'username') - - feed = AtomSerializer.new.feed(account, []) - - link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' } - expect(link[:type]).to eq 'application/atom+xml' - expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/users/username.atom' - end - - it 'appends link element for the next if it has 20 stream entries' do - account = Fabricate(:account, username: 'username') - stream_entry = Fabricate(:stream_entry) - - feed = AtomSerializer.new.feed(account, Array.new(20, stream_entry)) - - link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'next' } - expect(link[:type]).to eq 'application/atom+xml' - expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username.atom?max_id=#{stream_entry.id}" - end - - it 'does not append link element for the next if it does not have 20 stream entries' do - account = Fabricate(:account, username: 'username') - - feed = AtomSerializer.new.feed(account, []) - - feed.nodes.each do |node| - expect(node[:rel]).not_to eq 'next' if node.name == 'link' - end - end - - it 'appends link element for hub' do - account = Fabricate(:account, username: 'username') - - feed = AtomSerializer.new.feed(account, []) - - link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'hub' } - expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/api/push' - end - - it 'appends link element for Salmon' do - account = Fabricate(:account, username: 'username') - - feed = AtomSerializer.new.feed(account, []) - - link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'salmon' } - expect(link[:href]).to start_with 'https://cb6e6126.ngrok.io/api/salmon/' - end - - it 'appends stream entries' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - feed = AtomSerializer.new.feed(account, [status.stream_entry]) - - expect(feed.entry.title.text).to eq 'New status by username' - end - end - - describe '#block_salmon' do - include_examples 'namespaces' do - def serialize - block = Fabricate(:block) - AtomSerializer.new.block_salmon(block) - end - end - - it 'returns entry element' do - block = Fabricate(:block) - block_salmon = AtomSerializer.new.block_salmon(block) - expect(block_salmon.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - block = Fabricate(:block) - - time_before = Time.now - block_salmon = AtomSerializer.new.block_salmon(block) - time_after = Time.now - - expect(block_salmon.id.text).to( - eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block')) - .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block'))) - ) - end - - it 'appends title element with description' do - account = Fabricate(:account, domain: nil, username: 'account') - target_account = Fabricate(:account, domain: 'remote', username: 'target_account') - block = Fabricate(:block, account: account, target_account: target_account) - - block_salmon = AtomSerializer.new.block_salmon(block) - - expect(block_salmon.title.text).to eq 'account no longer wishes to interact with target_account@remote' - end - - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'account') - block = Fabricate(:block, account: account) - - block_salmon = AtomSerializer.new.block_salmon(block) - - expect(block_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account' - end - - it 'appends activity:object-type element with activity type' do - block = Fabricate(:block) - - block_salmon = AtomSerializer.new.block_salmon(block) - - object_type = block_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with block' do - block = Fabricate(:block) - - block_salmon = AtomSerializer.new.block_salmon(block) - - verb = block_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:block] - end - - it 'appends activity:object element with target account' do - target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') - block = Fabricate(:block, target_account: target_account) - - block_salmon = AtomSerializer.new.block_salmon(block) - - object = block_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq 'https://domain/id' - end - - it 'returns element whose rendered view triggers block when processed' do - block = Fabricate(:block) - block_salmon = AtomSerializer.new.block_salmon(block) - xml = AtomSerializer.render(block_salmon) - envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair) - block.destroy! - - ProcessInteractionService.new.call(envelope, block.target_account) - - expect(block.account.blocking?(block.target_account)).to be true - end - end - - describe '#unblock_salmon' do - include_examples 'namespaces' do - def serialize - block = Fabricate(:block) - AtomSerializer.new.unblock_salmon(block) - end - end - - it 'returns entry element' do - block = Fabricate(:block) - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - expect(unblock_salmon.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - block = Fabricate(:block) - - time_before = Time.now - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - time_after = Time.now - - expect(unblock_salmon.id.text).to( - eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block')) - .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block'))) - ) - end - - it 'appends title element with description' do - account = Fabricate(:account, domain: nil, username: 'account') - target_account = Fabricate(:account, domain: 'remote', username: 'target_account') - block = Fabricate(:block, account: account, target_account: target_account) - - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - - expect(unblock_salmon.title.text).to eq 'account no longer blocks target_account@remote' - end - - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'account') - block = Fabricate(:block, account: account) - - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - - expect(unblock_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account' - end - - it 'appends activity:object-type element with activity type' do - block = Fabricate(:block) - - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - - object_type = unblock_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with block' do - block = Fabricate(:block) - - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - - verb = unblock_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:unblock] - end - - it 'appends activity:object element with target account' do - target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') - block = Fabricate(:block, target_account: target_account) - - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - - object = unblock_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq 'https://domain/id' - end - - it 'returns element whose rendered view triggers block when processed' do - block = Fabricate(:block) - unblock_salmon = AtomSerializer.new.unblock_salmon(block) - xml = AtomSerializer.render(unblock_salmon) - envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair) - - ProcessInteractionService.new.call(envelope, block.target_account) - - expect{ block.reload }.to raise_error ActiveRecord::RecordNotFound - end - end - - describe '#favourite_salmon' do - include_examples 'namespaces' do - def serialize - favourite = Fabricate(:favourite) - AtomSerializer.new.favourite_salmon(favourite) - end - end - - it 'returns entry element' do - favourite = Fabricate(:favourite) - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - expect(favourite_salmon.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - favourite = Fabricate(:favourite, created_at: '2000-01-01T00:00:00Z') - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - expect(favourite_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{favourite.id}:objectType=Favourite" - end - - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'username') - favourite = Fabricate(:favourite, account: account) - - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - - expect(favourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends activity:object-type element with activity type' do - favourite = Fabricate(:favourite) - - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - - object_type = favourite_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity' - end - - it 'appends activity:verb element with favorite' do - favourite = Fabricate(:favourite) - - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - - verb = favourite_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:favorite] - end - - it 'appends activity:object element with status' do - status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') - favourite = Fabricate(:favourite, status: status) - - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - - object = favourite_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" - end - - it 'appends thr:in-reply-to element for status' do - status_account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z') - favourite = Fabricate(:favourite, status: status) - - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - - in_reply_to = favourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' } - expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" - expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" - end - - it 'includes description' do - account = Fabricate(:account, domain: nil, username: 'account') - status_account = Fabricate(:account, domain: 'remote', username: 'status_account') - status = Fabricate(:status, account: status_account) - favourite = Fabricate(:favourite, account: account, status: status) - - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - - expect(favourite_salmon.title.text).to eq 'account favourited a status by status_account@remote' - expect(favourite_salmon.content.text).to eq 'account favourited a status by status_account@remote' - end - - it 'returns element whose rendered view triggers favourite when processed' do - favourite = Fabricate(:favourite) - favourite_salmon = AtomSerializer.new.favourite_salmon(favourite) - xml = AtomSerializer.render(favourite_salmon) - envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair) - favourite.destroy! - - ProcessInteractionService.new.call(envelope, favourite.status.account) - expect(favourite.account.favourited?(favourite.status)).to be true - end - end - - describe '#unfavourite_salmon' do - include_examples 'namespaces' do - def serialize - favourite = Fabricate(:favourite) - AtomSerializer.new.favourite_salmon(favourite) - end - end - - it 'returns entry element' do - favourite = Fabricate(:favourite) - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - expect(unfavourite_salmon.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - favourite = Fabricate(:favourite) - - time_before = Time.now - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - time_after = Time.now - - expect(unfavourite_salmon.id.text).to( - eq(TagManager.instance.unique_tag(time_before.utc, favourite.id, 'Favourite')) - .or(eq(TagManager.instance.unique_tag(time_after.utc, favourite.id, 'Favourite'))) - ) - end - - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'username') - favourite = Fabricate(:favourite, account: account) - - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - - expect(unfavourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends activity:object-type element with activity type' do - favourite = Fabricate(:favourite) - - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - - object_type = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity' - end - - it 'appends activity:verb element with favorite' do - favourite = Fabricate(:favourite) - - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - - verb = unfavourite_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:unfavorite] - end - - it 'appends activity:object element with status' do - status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') - favourite = Fabricate(:favourite, status: status) - - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - - object = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" - end - - it 'appends thr:in-reply-to element for status' do - status_account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z') - favourite = Fabricate(:favourite, status: status) - - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - - in_reply_to = unfavourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' } - expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" - expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" - end - - it 'includes description' do - account = Fabricate(:account, domain: nil, username: 'account') - status_account = Fabricate(:account, domain: 'remote', username: 'status_account') - status = Fabricate(:status, account: status_account) - favourite = Fabricate(:favourite, account: account, status: status) - - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - - expect(unfavourite_salmon.title.text).to eq 'account no longer favourites a status by status_account@remote' - expect(unfavourite_salmon.content.text).to eq 'account no longer favourites a status by status_account@remote' - end - - it 'returns element whose rendered view triggers unfavourite when processed' do - favourite = Fabricate(:favourite) - unfavourite_salmon = AtomSerializer.new.unfavourite_salmon(favourite) - xml = AtomSerializer.render(unfavourite_salmon) - envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair) - - ProcessInteractionService.new.call(envelope, favourite.status.account) - expect { favourite.reload }.to raise_error ActiveRecord::RecordNotFound - end - end - - describe '#follow_salmon' do - include_examples 'namespaces' do - def serialize - follow = Fabricate(:follow) - AtomSerializer.new.follow_salmon(follow) - end - end - - it 'returns entry element' do - follow = Fabricate(:follow) - follow_salmon = AtomSerializer.new.follow_salmon(follow) - expect(follow_salmon.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - follow = Fabricate(:follow, created_at: '2000-01-01T00:00:00Z') - follow_salmon = AtomSerializer.new.follow_salmon(follow) - expect(follow_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow.id}:objectType=Follow" - end - - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'username') - follow = Fabricate(:follow, account: account) - - follow_salmon = AtomSerializer.new.follow_salmon(follow) - - expect(follow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends activity:object-type element with activity type' do - follow = Fabricate(:follow) - - follow_salmon = AtomSerializer.new.follow_salmon(follow) - - object_type = follow_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with follow' do - follow = Fabricate(:follow) - - follow_salmon = AtomSerializer.new.follow_salmon(follow) - - verb = follow_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:follow] - end - - it 'appends activity:object element with target account' do - target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') - follow = Fabricate(:follow, target_account: target_account) - - follow_salmon = AtomSerializer.new.follow_salmon(follow) - - object = follow_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq 'https://domain/id' - end - - it 'includes description' do - account = Fabricate(:account, domain: nil, username: 'account') - target_account = Fabricate(:account, domain: 'remote', username: 'target_account') - follow = Fabricate(:follow, account: account, target_account: target_account) - - follow_salmon = AtomSerializer.new.follow_salmon(follow) - - expect(follow_salmon.title.text).to eq 'account started following target_account@remote' - expect(follow_salmon.content.text).to eq 'account started following target_account@remote' - end - - it 'returns element whose rendered view triggers follow when processed' do - follow = Fabricate(:follow) - follow_salmon = AtomSerializer.new.follow_salmon(follow) - xml = AtomSerializer.render(follow_salmon) - follow.destroy! - envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair) - - ProcessInteractionService.new.call(envelope, follow.target_account) - - expect(follow.account.following?(follow.target_account)).to be true - end - end - - describe '#unfollow_salmon' do - include_examples 'namespaces' do - def serialize - follow = Fabricate(:follow) - follow.destroy! - AtomSerializer.new.unfollow_salmon(follow) - end - end - - it 'returns entry element' do - follow = Fabricate(:follow) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - expect(unfollow_salmon.name).to eq 'entry' - end - - it 'appends id element with unique tag' do - follow = Fabricate(:follow) - follow.destroy! - - time_before = Time.now - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - time_after = Time.now - - expect(unfollow_salmon.id.text).to( - eq(TagManager.instance.unique_tag(time_before.utc, follow.id, 'Follow')) - .or(eq(TagManager.instance.unique_tag(time_after.utc, follow.id, 'Follow'))) - ) - end - - it 'appends title element with description' do - account = Fabricate(:account, domain: nil, username: 'account') - target_account = Fabricate(:account, domain: 'remote', username: 'target_account') - follow = Fabricate(:follow, account: account, target_account: target_account) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - expect(unfollow_salmon.title.text).to eq 'account is no longer following target_account@remote' - end - - it 'appends content element with description' do - account = Fabricate(:account, domain: nil, username: 'account') - target_account = Fabricate(:account, domain: 'remote', username: 'target_account') - follow = Fabricate(:follow, account: account, target_account: target_account) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - expect(unfollow_salmon.content.text).to eq 'account is no longer following target_account@remote' - end - - it 'appends author element with account' do - account = Fabricate(:account, domain: nil, username: 'username') - follow = Fabricate(:follow, account: account) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - expect(unfollow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends activity:object-type element with activity type' do - follow = Fabricate(:follow) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - object_type = unfollow_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with follow' do - follow = Fabricate(:follow) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - verb = unfollow_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:unfollow] - end - - it 'appends activity:object element with target account' do - target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') - follow = Fabricate(:follow, target_account: target_account) - follow.destroy! - - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - - object = unfollow_salmon.nodes.find { |node| node.name == 'activity:object' } - expect(object.id.text).to eq 'https://domain/id' - end - - it 'returns element whose rendered view triggers unfollow when processed' do - follow = Fabricate(:follow) - follow.destroy! - unfollow_salmon = AtomSerializer.new.unfollow_salmon(follow) - xml = AtomSerializer.render(unfollow_salmon) - follow.account.follow!(follow.target_account) - envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair) - - ProcessInteractionService.new.call(envelope, follow.target_account) - - expect(follow.account.following?(follow.target_account)).to be false - end - end - - describe '#follow_request_salmon' do - include_examples 'namespaces' do - def serialize - follow_request = Fabricate(:follow_request) - AtomSerializer.new.follow_request_salmon(follow_request) - end - end - - context do - def serialize(follow_request) - AtomSerializer.new.follow_request_salmon(follow_request) - end - - it_behaves_like 'follow request salmon' - - it 'appends id element with unique tag' do - follow_request = Fabricate(:follow_request, created_at: '2000-01-01T00:00:00Z') - follow_request_salmon = serialize(follow_request) - expect(follow_request_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow_request.id}:objectType=FollowRequest" - end - - it 'appends title element with description' do - account = Fabricate(:account, domain: nil, username: 'account') - target_account = Fabricate(:account, domain: 'remote', username: 'target_account') - follow_request = Fabricate(:follow_request, account: account, target_account: target_account) - follow_request_salmon = serialize(follow_request) - expect(follow_request_salmon.title.text).to eq 'account requested to follow target_account@remote' - end - - it 'returns element whose rendered view triggers follow request when processed' do - follow_request = Fabricate(:follow_request) - follow_request_salmon = serialize(follow_request) - xml = AtomSerializer.render(follow_request_salmon) - envelope = OStatus2::Salmon.new.pack(xml, follow_request.account.keypair) - follow_request.destroy! - - ProcessInteractionService.new.call(envelope, follow_request.target_account) - - expect(follow_request.account.requested?(follow_request.target_account)).to eq true - end - end - end - - describe '#authorize_follow_request_salmon' do - include_examples 'namespaces' do - def serialize - follow_request = Fabricate(:follow_request) - AtomSerializer.new.authorize_follow_request_salmon(follow_request) - end - end - - it_behaves_like 'follow request salmon' do - def serialize(follow_request) - authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request) - authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' } - end - end - - it 'appends id element with unique tag' do - follow_request = Fabricate(:follow_request) - - time_before = Time.now - authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request) - time_after = Time.now - - expect(authorize_follow_request_salmon.id.text).to( - eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest')) - .or(eq(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest'))) - ) - end - - it 'appends title element with description' do - account = Fabricate(:account, domain: 'remote', username: 'account') - target_account = Fabricate(:account, domain: nil, username: 'target_account') - follow_request = Fabricate(:follow_request, account: account, target_account: target_account) - - authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request) - - expect(authorize_follow_request_salmon.title.text).to eq 'target_account authorizes follow request by account@remote' - end - - it 'appends activity:object-type element with activity type' do - follow_request = Fabricate(:follow_request) - - authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request) - - object_type = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with authorize' do - follow_request = Fabricate(:follow_request) - - authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request) - - verb = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:authorize] - end - - it 'returns element whose rendered view creates follow from follow request when processed' do - follow_request = Fabricate(:follow_request) - authorize_follow_request_salmon = AtomSerializer.new.authorize_follow_request_salmon(follow_request) - xml = AtomSerializer.render(authorize_follow_request_salmon) - envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair) - - ProcessInteractionService.new.call(envelope, follow_request.account) - - expect(follow_request.account.following?(follow_request.target_account)).to eq true - expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound - end - end - - describe '#reject_follow_request_salmon' do - include_examples 'namespaces' do - def serialize - follow_request = Fabricate(:follow_request) - AtomSerializer.new.reject_follow_request_salmon(follow_request) - end - end - - it_behaves_like 'follow request salmon' do - def serialize(follow_request) - reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request) - reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' } - end - end - - it 'appends id element with unique tag' do - follow_request = Fabricate(:follow_request) - - time_before = Time.now - reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request) - time_after = Time.now - - expect(reject_follow_request_salmon.id.text).to( - eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest')) - .or(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest')) - ) - end - - it 'appends title element with description' do - account = Fabricate(:account, domain: 'remote', username: 'account') - target_account = Fabricate(:account, domain: nil, username: 'target_account') - follow_request = Fabricate(:follow_request, account: account, target_account: target_account) - reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request) - expect(reject_follow_request_salmon.title.text).to eq 'target_account rejects follow request by account@remote' - end - - it 'appends activity:object-type element with activity type' do - follow_request = Fabricate(:follow_request) - reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request) - object_type = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:activity] - end - - it 'appends activity:verb element with authorize' do - follow_request = Fabricate(:follow_request) - reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request) - verb = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' } - expect(verb.text).to eq TagManager::VERBS[:reject] - end - - it 'returns element whose rendered view deletes follow request when processed' do - follow_request = Fabricate(:follow_request) - reject_follow_request_salmon = AtomSerializer.new.reject_follow_request_salmon(follow_request) - xml = AtomSerializer.render(reject_follow_request_salmon) - envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair) - - ProcessInteractionService.new.call(envelope, follow_request.account) - - expect(follow_request.account.following?(follow_request.target_account)).to eq false - expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound - end - end - - describe '#object' do - include_examples 'status attributes' do - def serialize(status) - AtomSerializer.new.object(status) - end - end - - it 'returns activity:object element' do - status = Fabricate(:status) - object = AtomSerializer.new.object(status) - expect(object.name).to eq 'activity:object' - end - - it 'appends id element with URL for status' do - status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') - object = AtomSerializer.new.object(status) - expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" - end - - it 'appends published element with created date' do - status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') - object = AtomSerializer.new.object(status) - expect(object.published.text).to eq '2000-01-01T00:00:00Z' - end - - it 'appends updated element with updated date' do - status = Fabricate(:status, updated_at: '2000-01-01T00:00:00Z') - object = AtomSerializer.new.object(status) - expect(object.updated.text).to eq '2000-01-01T00:00:00Z' - end - - it 'appends title element with title' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - object = AtomSerializer.new.object(status) - - expect(object.title.text).to eq 'New status by username' - end - - it 'appends author element with account' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - entry = AtomSerializer.new.object(status) - - expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' - end - - it 'appends activity:object-type element with object type' do - status = Fabricate(:status) - - entry = AtomSerializer.new.object(status) - - object_type = entry.nodes.find { |node| node.name == 'activity:object-type' } - expect(object_type.text).to eq TagManager::TYPES[:note] - end - - it 'appends activity:verb element with verb' do - status = Fabricate(:status) - - entry = AtomSerializer.new.object(status) - - object_type = entry.nodes.find { |node| node.name == 'activity:verb' } - expect(object_type.text).to eq TagManager::VERBS[:post] - end - - it 'appends link element for an alternative' do - account = Fabricate(:account, username: 'username') - status = Fabricate(:status, account: account) - - entry = AtomSerializer.new.object(status) - - link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } - expect(link[:type]).to eq 'text/html' - expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" - end - - it 'appends thr:in-reply-to element if it is a reply and thread is not nil' do - account = Fabricate(:account, username: 'username') - thread = Fabricate(:status, account: account, created_at: '2000-01-01T00:00:00Z') - reply = Fabricate(:status, thread: thread) - - entry = AtomSerializer.new.object(reply) - - in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' } - expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{thread.id}:objectType=Status" - expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{thread.id}" - end - - it 'does not append thr:in-reply-to element if thread is nil' do - status = Fabricate(:status, thread: nil) - entry = AtomSerializer.new.object(status) - entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' } - end - - it 'does not append ostatus:conversation element if conversation_id is nil' do - status = Fabricate.build(:status, conversation_id: nil) - status.save!(validate: false) - - entry = AtomSerializer.new.object(status) - - entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' } - end - - it 'appends ostatus:conversation element if conversation_id is not nil' do - status = Fabricate(:status) - status.conversation.update!(created_at: '2000-01-01T00:00:00Z') - - entry = AtomSerializer.new.object(status) - - conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' } - expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation.id}:objectType=Conversation" - end - end -end diff --git a/spec/lib/ostatus/atom_serializer_spec.rb b/spec/lib/ostatus/atom_serializer_spec.rb new file mode 100644 index 000000000..8caef9355 --- /dev/null +++ b/spec/lib/ostatus/atom_serializer_spec.rb @@ -0,0 +1,1554 @@ +require 'rails_helper' + +RSpec.describe Ostatus::AtomSerializer do + shared_examples 'follow request salmon' do + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'username') + follow_request = Fabricate(:follow_request, account: account) + + follow_request_salmon = serialize(follow_request) + + expect(follow_request_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends activity:object-type element with activity type' do + follow_request = Fabricate(:follow_request) + + follow_request_salmon = serialize(follow_request) + + object_type = follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with request_friend type' do + follow_request = Fabricate(:follow_request) + + follow_request_salmon = serialize(follow_request) + + verb = follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:request_friend] + end + + it 'appends activity:object with target account' do + target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') + follow_request = Fabricate(:follow_request, target_account: target_account) + + follow_request_salmon = serialize(follow_request) + + object = follow_request_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq 'https://domain/id' + end + end + + shared_examples 'namespaces' do + it 'adds namespaces' do + element = serialize + + expect(element['xmlns']).to eq TagManager::XMLNS + expect(element['xmlns:thr']).to eq TagManager::THR_XMLNS + expect(element['xmlns:activity']).to eq TagManager::AS_XMLNS + expect(element['xmlns:poco']).to eq TagManager::POCO_XMLNS + expect(element['xmlns:media']).to eq TagManager::MEDIA_XMLNS + expect(element['xmlns:ostatus']).to eq TagManager::OS_XMLNS + expect(element['xmlns:mastodon']).to eq TagManager::MTDN_XMLNS + end + end + + shared_examples 'no namespaces' do + it 'does not add namespaces' do + expect(serialize['xmlns']).to eq nil + end + end + + shared_examples 'status attributes' do + it 'appends summary element with spoiler text if present' do + status = Fabricate(:status, language: :ca, spoiler_text: 'spoiler text') + + element = serialize(status) + + summary = element.summary + expect(summary['xml:lang']).to eq 'ca' + expect(summary.text).to eq 'spoiler text' + end + + it 'does not append summary element with spoiler text if not present' do + status = Fabricate(:status, spoiler_text: '') + element = serialize(status) + element.nodes.each { |node| expect(node.name).not_to eq 'summary' } + end + + it 'appends content element with formatted status' do + status = Fabricate(:status, language: :ca, text: 'text') + + element = serialize(status) + + content = element.content + expect(content[:type]).to eq 'html' + expect(content['xml:lang']).to eq 'ca' + expect(content.text).to eq '

text

' + end + + it 'appends link elements for mentioned accounts' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status) + Fabricate(:mention, account: account, status: status) + + element = serialize(status) + + mentioned = element.nodes.find do |node| + node.name == 'link' && + node[:rel] == 'mentioned' && + node['ostatus:object-type'] == TagManager::TYPES[:person] + end + expect(mentioned[:href]).to eq 'https://cb6e6126.ngrok.io/users/username' + end + end + + describe 'render' do + it 'returns XML with emojis' do + element = Ox::Element.new('tag') + element << '💩' + xml = Ostatus::AtomSerializer.render(element) + + expect(xml).to eq "\n💩\n" + end + + it 'returns XML, stripping invalid characters like \b and \v' do + element = Ox::Element.new('tag') + element << "im l33t\b haxo\b\vr" + xml = Ostatus::AtomSerializer.render(element) + + expect(xml).to eq "\nim l33t haxor\n" + end + end + + describe '#author' do + context 'when note is present' do + it 'appends poco:note element with note for local account' do + account = Fabricate(:account, domain: nil, note: '

note

') + + author = Ostatus::AtomSerializer.new.author(account) + + note = author.nodes.find { |node| node.name == 'poco:note' } + expect(note.text).to eq '

note

' + end + + it 'appends poco:note element with tags-stripped note for remote account' do + account = Fabricate(:account, domain: 'remote', note: '

note

') + + author = Ostatus::AtomSerializer.new.author(account) + + note = author.nodes.find { |node| node.name == 'poco:note' } + expect(note.text).to eq 'note' + end + + it 'appends summary element with type attribute and simplified note if present' do + account = Fabricate(:account, note: 'note') + author = Ostatus::AtomSerializer.new.author(account) + expect(author.summary.text).to eq '

note

' + expect(author.summary[:type]).to eq 'html' + end + end + + context 'when note is not present' do + it 'does not append poco:note element' do + account = Fabricate(:account, note: '') + author = Ostatus::AtomSerializer.new.author(account) + author.nodes.each { |node| expect(node.name).not_to eq 'poco:note' } + end + + it 'does not append summary element' do + account = Fabricate(:account, note: '') + author = Ostatus::AtomSerializer.new.author(account) + author.nodes.each { |node| expect(node.name).not_to eq 'summary' } + end + end + + it 'returns author element' do + account = Fabricate(:account) + author = Ostatus::AtomSerializer.new.author(account) + expect(author.name).to eq 'author' + end + + it 'appends activity:object-type element with person type' do + account = Fabricate(:account, domain: nil, username: 'username') + + author = Ostatus::AtomSerializer.new.author(account) + + object_type = author.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:person] + end + + it 'appends email element with username and domain for local account' do + account = Fabricate(:account, username: 'username') + author = Ostatus::AtomSerializer.new.author(account) + expect(author.email.text).to eq 'username@cb6e6126.ngrok.io' + end + + it 'appends email element with username and domain for remote user' do + account = Fabricate(:account, domain: 'domain', username: 'username') + author = Ostatus::AtomSerializer.new.author(account) + expect(author.email.text).to eq 'username@domain' + end + + it 'appends link element for an alternative' do + account = Fabricate(:account, domain: nil, username: 'username') + + author = Ostatus::AtomSerializer.new.author(account) + + link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } + expect(link[:type]).to eq 'text/html' + expect(link[:rel]).to eq 'alternate' + expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username' + end + + it 'has link element for avatar if present' do + account = Fabricate(:account, avatar: attachment_fixture('avatar.gif')) + + author = Ostatus::AtomSerializer.new.author(account) + + link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'avatar' } + expect(link[:type]).to eq 'image/gif' + expect(link['media:width']).to eq '120' + expect(link['media:height']).to eq '120' + expect(link[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/ + end + + it 'does not have link element for avatar if not present' do + account = Fabricate(:account, avatar: nil) + + author = Ostatus::AtomSerializer.new.author(account) + + author.nodes.each do |node| + expect(node[:rel]).not_to eq 'avatar' if node.name == 'link' + end + end + + it 'appends link element for header if present' do + account = Fabricate(:account, header: attachment_fixture('avatar.gif')) + + author = Ostatus::AtomSerializer.new.author(account) + + link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'header' } + expect(link[:type]).to eq 'image/gif' + expect(link['media:width']).to eq '700' + expect(link['media:height']).to eq '335' + expect(link[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/headers\/.+\/original\/avatar.gif/ + end + + it 'does not append link element for header if not present' do + account = Fabricate(:account, header: nil) + + author = Ostatus::AtomSerializer.new.author(account) + + author.nodes.each do |node| + expect(node[:rel]).not_to eq 'header' if node.name == 'link' + end + end + + it 'appends poco:displayName element with display name if present' do + account = Fabricate(:account, display_name: 'display name') + + author = Ostatus::AtomSerializer.new.author(account) + + display_name = author.nodes.find { |node| node.name == 'poco:displayName' } + expect(display_name.text).to eq 'display name' + end + + it 'does not append poco:displayName element with display name if not present' do + account = Fabricate(:account, display_name: '') + author = Ostatus::AtomSerializer.new.author(account) + author.nodes.each { |node| expect(node.name).not_to eq 'poco:displayName' } + end + + it "appends mastodon:scope element with 'private' if locked" do + account = Fabricate(:account, locked: true) + + author = Ostatus::AtomSerializer.new.author(account) + + scope = author.nodes.find { |node| node.name == 'mastodon:scope' } + expect(scope.text).to eq 'private' + end + + it "appends mastodon:scope element with 'public' if unlocked" do + account = Fabricate(:account, locked: false) + + author = Ostatus::AtomSerializer.new.author(account) + + scope = author.nodes.find { |node| node.name == 'mastodon:scope' } + expect(scope.text).to eq 'public' + end + + it 'includes URI' do + account = Fabricate(:account, domain: nil, username: 'username') + + author = Ostatus::AtomSerializer.new.author(account) + + expect(author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + expect(author.uri.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'includes username' do + account = Fabricate(:account, username: 'username') + + author = Ostatus::AtomSerializer.new.author(account) + + name = author.nodes.find { |node| node.name == 'name' } + username = author.nodes.find { |node| node.name == 'poco:preferredUsername' } + expect(name.text).to eq 'username' + expect(username.text).to eq 'username' + end + end + + describe '#entry' do + shared_examples 'not root' do + include_examples 'no namespaces' do + def serialize + subject + end + end + + it 'does not append author element' do + subject.nodes.each { |node| expect(node.name).not_to eq 'author' } + end + end + + context 'it is root' do + include_examples 'namespaces' do + def serialize + stream_entry = Fabricate(:stream_entry) + Ostatus::AtomSerializer.new.entry(stream_entry, true) + end + end + + it 'appends author element' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry, true) + + expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + end + + context 'if status is present' do + include_examples 'status attributes' do + def serialize(status) + Ostatus::AtomSerializer.new.entry(status.stream_entry, true) + end + end + + it 'appends link element for the public collection if status is publicly visible' do + status = Fabricate(:status, visibility: :public) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + mentioned_person = entry.nodes.find do |node| + node.name == 'link' && + node[:rel] == 'mentioned' && + node['ostatus:object-type'] == TagManager::TYPES[:collection] + end + expect(mentioned_person[:href]).to eq TagManager::COLLECTIONS[:public] + end + + it 'does not append link element for the public collection if status is not publicly visible' do + status = Fabricate(:status, visibility: :private) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + entry.nodes.each do |node| + if node.name == 'link' && + node[:rel] == 'mentioned' && + node['ostatus:object-type'] == TagManager::TYPES[:collection] + expect(mentioned_collection[:href]).not_to eq TagManager::COLLECTIONS[:public] + end + end + end + + it 'appends category elements for tags' do + tag = Fabricate(:tag, name: 'tag') + status = Fabricate(:status, tags: [ tag ]) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + expect(entry.category[:term]).to eq 'tag' + end + + it 'appends category element for NSFW if status is sensitive' do + status = Fabricate(:status, sensitive: true) + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + expect(entry.category[:term]).to eq 'nsfw' + end + + it 'appends link elements for media attachments' do + file = attachment_fixture('attachment.jpg') + media_attachment = Fabricate(:media_attachment, file: file) + status = Fabricate(:status, media_attachments: [ media_attachment ]) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + enclosure = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'enclosure' } + expect(enclosure[:type]).to eq 'image/jpeg' + expect(enclosure[:href]).to match /^https:\/\/cb6e6126.ngrok.io\/system\/media_attachments\/files\/.+\/original\/attachment.jpg$/ + end + + it 'appends mastodon:scope element with visibility' do + status = Fabricate(:status, visibility: :public) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + scope = entry.nodes.find { |node| node.name == 'mastodon:scope' } + expect(scope.text).to eq 'public' + end + + it 'returns element whose rendered view triggers creation when processed' do + remote_account = Account.create!(username: 'username') + remote_status = Fabricate(:status, account: remote_account) + remote_status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z') + + entry = Ostatus::AtomSerializer.new.entry(remote_status.stream_entry, true) + xml = Ostatus::AtomSerializer.render(entry).gsub('cb6e6126.ngrok.io', 'remote') + + remote_status.destroy! + remote_account.destroy! + + account = Account.create!( + domain: 'remote', + username: 'username', + last_webfingered_at: Time.now.utc, + ) + + ProcessFeedService.new.call(xml, account) + + expect(Status.find_by(uri: "tag:remote,2000-01-01:objectId=#{remote_status.id}:objectType=Status")).to be_instance_of Status + end + end + + context 'if status is not present' do + it 'appends content element saying status is deleted' do + status = Fabricate(:status) + status.destroy! + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + expect(entry.content.text).to eq 'Deleted status' + end + + it 'appends title element saying the status is deleted' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + status.destroy! + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + expect(entry.title.text).to eq 'username deleted status' + end + end + + context 'it is not root' do + let(:stream_entry) { Fabricate(:stream_entry) } + subject { Ostatus::AtomSerializer.new.entry(stream_entry, false) } + include_examples 'not root' + end + + context 'without root parameter' do + let(:stream_entry) { Fabricate(:stream_entry) } + subject { Ostatus::AtomSerializer.new.entry(stream_entry) } + include_examples 'not root' + end + + it 'returns entry element' do + stream_entry = Fabricate(:stream_entry) + entry = Ostatus::AtomSerializer.new.entry(stream_entry) + expect(entry.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + status = Fabricate(:status, reblog_of_id: nil) + status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z') + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + expect(entry.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" + end + + it 'appends published element with created date' do + stream_entry = Fabricate(:stream_entry, created_at: '2000-01-01T00:00:00Z') + entry = Ostatus::AtomSerializer.new.entry(stream_entry) + expect(entry.published.text).to eq '2000-01-01T00:00:00Z' + end + + it 'appends updated element with updated date' do + stream_entry = Fabricate(:stream_entry, updated_at: '2000-01-01T00:00:00Z') + entry = Ostatus::AtomSerializer.new.entry(stream_entry) + expect(entry.updated.text).to eq '2000-01-01T00:00:00Z' + end + + it 'appends title element with status title' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account, reblog_of_id: nil) + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + expect(entry.title.text).to eq 'New status by username' + end + + it 'appends activity:object-type element with object type' do + status = Fabricate(:status) + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + object_type = entry.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:note] + end + + it 'appends activity:verb element with object type' do + status = Fabricate(:status) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + object_type = entry.nodes.find { |node| node.name == 'activity:verb' } + expect(object_type.text).to eq TagManager::VERBS[:post] + end + + it 'appends activity:object element with target if present' do + reblogged = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') + reblog = Fabricate(:status, reblog: reblogged) + + entry = Ostatus::AtomSerializer.new.entry(reblog.stream_entry) + + object = entry.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{reblogged.id}:objectType=Status" + end + + it 'does not append activity:object element if target is not present' do + status = Fabricate(:status, reblog_of_id: nil) + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + entry.nodes.each { |node| expect(node.name).not_to eq 'activity:object' } + end + + it 'appends link element for an alternative' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } + expect(link[:type]).to eq 'text/html' + expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}" + end + + it 'appends link element for itself' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' } + expect(link[:type]).to eq 'application/atom+xml' + expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}.atom" + end + + it 'appends thr:in-reply-to element if threaded' do + in_reply_to_status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z', reblog_of_id: nil) + reply_status = Fabricate(:status, in_reply_to_id: in_reply_to_status.id) + + entry = Ostatus::AtomSerializer.new.entry(reply_status.stream_entry) + + in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' } + expect(in_reply_to[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{in_reply_to_status.id}:objectType=Status" + end + + it 'does not append thr:in-reply-to element if not threaded' do + status = Fabricate(:status) + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' } + end + + it 'appends ostatus:conversation if conversation id is present' do + status = Fabricate(:status) + status.conversation.update!(created_at: '2000-01-01T00:00:00Z') + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' } + expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation_id}:objectType=Conversation" + end + + it 'does not append ostatus:conversation if conversation id is not present' do + status = Fabricate.build(:status, conversation_id: nil) + status.save!(validate: false) + + entry = Ostatus::AtomSerializer.new.entry(status.stream_entry) + + entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' } + end + end + + describe '#feed' do + include_examples 'namespaces' do + def serialize + account = Fabricate(:account) + Ostatus::AtomSerializer.new.feed(account, []) + end + end + + it 'returns feed element' do + account = Fabricate(:account) + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.name).to eq 'feed' + end + + it 'appends id element with account Atom URL' do + account = Fabricate(:account, username: 'username') + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.id.text).to eq 'https://cb6e6126.ngrok.io/users/username.atom' + end + + it 'appends title element with account display name if present' do + account = Fabricate(:account, display_name: 'display name') + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.title.text).to eq 'display name' + end + + it 'does not append title element with account username if account display name is not present' do + account = Fabricate(:account, display_name: '', username: 'username') + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.title.text).to eq 'username' + end + + it 'appends subtitle element with account note' do + account = Fabricate(:account, note: 'note') + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.subtitle.text).to eq 'note' + end + + it 'appends updated element with date account got updated' do + account = Fabricate(:account, updated_at: '2000-01-01T00:00:00Z') + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.updated.text).to eq '2000-01-01T00:00:00Z' + end + + it 'appends logo element with full asset URL for original account avatar' do + account = Fabricate(:account, avatar: attachment_fixture('avatar.gif')) + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.logo.text).to match /^https:\/\/cb6e6126.ngrok.io\/system\/accounts\/avatars\/.+\/original\/avatar.gif/ + end + + it 'appends author element' do + account = Fabricate(:account, username: 'username') + feed = Ostatus::AtomSerializer.new.feed(account, []) + expect(feed.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends link element for an alternative' do + account = Fabricate(:account, username: 'username') + + feed = Ostatus::AtomSerializer.new.feed(account, []) + + link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } + expect(link[:type]).to eq 'text/html' + expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username' + end + + it 'appends link element for itself' do + account = Fabricate(:account, username: 'username') + + feed = Ostatus::AtomSerializer.new.feed(account, []) + + link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'self' } + expect(link[:type]).to eq 'application/atom+xml' + expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/users/username.atom' + end + + it 'appends link element for the next if it has 20 stream entries' do + account = Fabricate(:account, username: 'username') + stream_entry = Fabricate(:stream_entry) + + feed = Ostatus::AtomSerializer.new.feed(account, Array.new(20, stream_entry)) + + link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'next' } + expect(link[:type]).to eq 'application/atom+xml' + expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username.atom?max_id=#{stream_entry.id}" + end + + it 'does not append link element for the next if it does not have 20 stream entries' do + account = Fabricate(:account, username: 'username') + + feed = Ostatus::AtomSerializer.new.feed(account, []) + + feed.nodes.each do |node| + expect(node[:rel]).not_to eq 'next' if node.name == 'link' + end + end + + it 'appends link element for hub' do + account = Fabricate(:account, username: 'username') + + feed = Ostatus::AtomSerializer.new.feed(account, []) + + link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'hub' } + expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/api/push' + end + + it 'appends link element for Salmon' do + account = Fabricate(:account, username: 'username') + + feed = Ostatus::AtomSerializer.new.feed(account, []) + + link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'salmon' } + expect(link[:href]).to start_with 'https://cb6e6126.ngrok.io/api/salmon/' + end + + it 'appends stream entries' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + feed = Ostatus::AtomSerializer.new.feed(account, [status.stream_entry]) + + expect(feed.entry.title.text).to eq 'New status by username' + end + end + + describe '#block_salmon' do + include_examples 'namespaces' do + def serialize + block = Fabricate(:block) + Ostatus::AtomSerializer.new.block_salmon(block) + end + end + + it 'returns entry element' do + block = Fabricate(:block) + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + expect(block_salmon.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + block = Fabricate(:block) + + time_before = Time.now + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + time_after = Time.now + + expect(block_salmon.id.text).to( + eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block')) + .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block'))) + ) + end + + it 'appends title element with description' do + account = Fabricate(:account, domain: nil, username: 'account') + target_account = Fabricate(:account, domain: 'remote', username: 'target_account') + block = Fabricate(:block, account: account, target_account: target_account) + + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + + expect(block_salmon.title.text).to eq 'account no longer wishes to interact with target_account@remote' + end + + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'account') + block = Fabricate(:block, account: account) + + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + + expect(block_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account' + end + + it 'appends activity:object-type element with activity type' do + block = Fabricate(:block) + + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + + object_type = block_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with block' do + block = Fabricate(:block) + + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + + verb = block_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:block] + end + + it 'appends activity:object element with target account' do + target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') + block = Fabricate(:block, target_account: target_account) + + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + + object = block_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq 'https://domain/id' + end + + it 'returns element whose rendered view triggers block when processed' do + block = Fabricate(:block) + block_salmon = Ostatus::AtomSerializer.new.block_salmon(block) + xml = Ostatus::AtomSerializer.render(block_salmon) + envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair) + block.destroy! + + ProcessInteractionService.new.call(envelope, block.target_account) + + expect(block.account.blocking?(block.target_account)).to be true + end + end + + describe '#unblock_salmon' do + include_examples 'namespaces' do + def serialize + block = Fabricate(:block) + Ostatus::AtomSerializer.new.unblock_salmon(block) + end + end + + it 'returns entry element' do + block = Fabricate(:block) + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + expect(unblock_salmon.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + block = Fabricate(:block) + + time_before = Time.now + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + time_after = Time.now + + expect(unblock_salmon.id.text).to( + eq(TagManager.instance.unique_tag(time_before.utc, block.id, 'Block')) + .or(eq(TagManager.instance.unique_tag(time_after.utc, block.id, 'Block'))) + ) + end + + it 'appends title element with description' do + account = Fabricate(:account, domain: nil, username: 'account') + target_account = Fabricate(:account, domain: 'remote', username: 'target_account') + block = Fabricate(:block, account: account, target_account: target_account) + + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + + expect(unblock_salmon.title.text).to eq 'account no longer blocks target_account@remote' + end + + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'account') + block = Fabricate(:block, account: account) + + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + + expect(unblock_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/account' + end + + it 'appends activity:object-type element with activity type' do + block = Fabricate(:block) + + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + + object_type = unblock_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with block' do + block = Fabricate(:block) + + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + + verb = unblock_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:unblock] + end + + it 'appends activity:object element with target account' do + target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') + block = Fabricate(:block, target_account: target_account) + + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + + object = unblock_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq 'https://domain/id' + end + + it 'returns element whose rendered view triggers block when processed' do + block = Fabricate(:block) + unblock_salmon = Ostatus::AtomSerializer.new.unblock_salmon(block) + xml = Ostatus::AtomSerializer.render(unblock_salmon) + envelope = OStatus2::Salmon.new.pack(xml, block.account.keypair) + + ProcessInteractionService.new.call(envelope, block.target_account) + + expect{ block.reload }.to raise_error ActiveRecord::RecordNotFound + end + end + + describe '#favourite_salmon' do + include_examples 'namespaces' do + def serialize + favourite = Fabricate(:favourite) + Ostatus::AtomSerializer.new.favourite_salmon(favourite) + end + end + + it 'returns entry element' do + favourite = Fabricate(:favourite) + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + expect(favourite_salmon.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + favourite = Fabricate(:favourite, created_at: '2000-01-01T00:00:00Z') + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + expect(favourite_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{favourite.id}:objectType=Favourite" + end + + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'username') + favourite = Fabricate(:favourite, account: account) + + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + + expect(favourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends activity:object-type element with activity type' do + favourite = Fabricate(:favourite) + + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + + object_type = favourite_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity' + end + + it 'appends activity:verb element with favorite' do + favourite = Fabricate(:favourite) + + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + + verb = favourite_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:favorite] + end + + it 'appends activity:object element with status' do + status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') + favourite = Fabricate(:favourite, status: status) + + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + + object = favourite_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" + end + + it 'appends thr:in-reply-to element for status' do + status_account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z') + favourite = Fabricate(:favourite, status: status) + + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + + in_reply_to = favourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' } + expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" + expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" + end + + it 'includes description' do + account = Fabricate(:account, domain: nil, username: 'account') + status_account = Fabricate(:account, domain: 'remote', username: 'status_account') + status = Fabricate(:status, account: status_account) + favourite = Fabricate(:favourite, account: account, status: status) + + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + + expect(favourite_salmon.title.text).to eq 'account favourited a status by status_account@remote' + expect(favourite_salmon.content.text).to eq 'account favourited a status by status_account@remote' + end + + it 'returns element whose rendered view triggers favourite when processed' do + favourite = Fabricate(:favourite) + favourite_salmon = Ostatus::AtomSerializer.new.favourite_salmon(favourite) + xml = Ostatus::AtomSerializer.render(favourite_salmon) + envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair) + favourite.destroy! + + ProcessInteractionService.new.call(envelope, favourite.status.account) + expect(favourite.account.favourited?(favourite.status)).to be true + end + end + + describe '#unfavourite_salmon' do + include_examples 'namespaces' do + def serialize + favourite = Fabricate(:favourite) + Ostatus::AtomSerializer.new.favourite_salmon(favourite) + end + end + + it 'returns entry element' do + favourite = Fabricate(:favourite) + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + expect(unfavourite_salmon.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + favourite = Fabricate(:favourite) + + time_before = Time.now + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + time_after = Time.now + + expect(unfavourite_salmon.id.text).to( + eq(TagManager.instance.unique_tag(time_before.utc, favourite.id, 'Favourite')) + .or(eq(TagManager.instance.unique_tag(time_after.utc, favourite.id, 'Favourite'))) + ) + end + + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'username') + favourite = Fabricate(:favourite, account: account) + + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + + expect(unfavourite_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends activity:object-type element with activity type' do + favourite = Fabricate(:favourite) + + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + + object_type = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq 'http://activitystrea.ms/schema/1.0/activity' + end + + it 'appends activity:verb element with favorite' do + favourite = Fabricate(:favourite) + + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + + verb = unfavourite_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:unfavorite] + end + + it 'appends activity:object element with status' do + status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') + favourite = Fabricate(:favourite, status: status) + + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + + object = unfavourite_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" + end + + it 'appends thr:in-reply-to element for status' do + status_account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: status_account, created_at: '2000-01-01T00:00:00Z') + favourite = Fabricate(:favourite, status: status) + + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + + in_reply_to = unfavourite_salmon.nodes.find { |node| node.name == 'thr:in-reply-to' } + expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" + expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" + end + + it 'includes description' do + account = Fabricate(:account, domain: nil, username: 'account') + status_account = Fabricate(:account, domain: 'remote', username: 'status_account') + status = Fabricate(:status, account: status_account) + favourite = Fabricate(:favourite, account: account, status: status) + + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + + expect(unfavourite_salmon.title.text).to eq 'account no longer favourites a status by status_account@remote' + expect(unfavourite_salmon.content.text).to eq 'account no longer favourites a status by status_account@remote' + end + + it 'returns element whose rendered view triggers unfavourite when processed' do + favourite = Fabricate(:favourite) + unfavourite_salmon = Ostatus::AtomSerializer.new.unfavourite_salmon(favourite) + xml = Ostatus::AtomSerializer.render(unfavourite_salmon) + envelope = OStatus2::Salmon.new.pack(xml, favourite.account.keypair) + + ProcessInteractionService.new.call(envelope, favourite.status.account) + expect { favourite.reload }.to raise_error ActiveRecord::RecordNotFound + end + end + + describe '#follow_salmon' do + include_examples 'namespaces' do + def serialize + follow = Fabricate(:follow) + Ostatus::AtomSerializer.new.follow_salmon(follow) + end + end + + it 'returns entry element' do + follow = Fabricate(:follow) + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + expect(follow_salmon.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + follow = Fabricate(:follow, created_at: '2000-01-01T00:00:00Z') + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + expect(follow_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow.id}:objectType=Follow" + end + + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'username') + follow = Fabricate(:follow, account: account) + + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + + expect(follow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends activity:object-type element with activity type' do + follow = Fabricate(:follow) + + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + + object_type = follow_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with follow' do + follow = Fabricate(:follow) + + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + + verb = follow_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:follow] + end + + it 'appends activity:object element with target account' do + target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') + follow = Fabricate(:follow, target_account: target_account) + + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + + object = follow_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq 'https://domain/id' + end + + it 'includes description' do + account = Fabricate(:account, domain: nil, username: 'account') + target_account = Fabricate(:account, domain: 'remote', username: 'target_account') + follow = Fabricate(:follow, account: account, target_account: target_account) + + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + + expect(follow_salmon.title.text).to eq 'account started following target_account@remote' + expect(follow_salmon.content.text).to eq 'account started following target_account@remote' + end + + it 'returns element whose rendered view triggers follow when processed' do + follow = Fabricate(:follow) + follow_salmon = Ostatus::AtomSerializer.new.follow_salmon(follow) + xml = Ostatus::AtomSerializer.render(follow_salmon) + follow.destroy! + envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair) + + ProcessInteractionService.new.call(envelope, follow.target_account) + + expect(follow.account.following?(follow.target_account)).to be true + end + end + + describe '#unfollow_salmon' do + include_examples 'namespaces' do + def serialize + follow = Fabricate(:follow) + follow.destroy! + Ostatus::AtomSerializer.new.unfollow_salmon(follow) + end + end + + it 'returns entry element' do + follow = Fabricate(:follow) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + expect(unfollow_salmon.name).to eq 'entry' + end + + it 'appends id element with unique tag' do + follow = Fabricate(:follow) + follow.destroy! + + time_before = Time.now + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + time_after = Time.now + + expect(unfollow_salmon.id.text).to( + eq(TagManager.instance.unique_tag(time_before.utc, follow.id, 'Follow')) + .or(eq(TagManager.instance.unique_tag(time_after.utc, follow.id, 'Follow'))) + ) + end + + it 'appends title element with description' do + account = Fabricate(:account, domain: nil, username: 'account') + target_account = Fabricate(:account, domain: 'remote', username: 'target_account') + follow = Fabricate(:follow, account: account, target_account: target_account) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + expect(unfollow_salmon.title.text).to eq 'account is no longer following target_account@remote' + end + + it 'appends content element with description' do + account = Fabricate(:account, domain: nil, username: 'account') + target_account = Fabricate(:account, domain: 'remote', username: 'target_account') + follow = Fabricate(:follow, account: account, target_account: target_account) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + expect(unfollow_salmon.content.text).to eq 'account is no longer following target_account@remote' + end + + it 'appends author element with account' do + account = Fabricate(:account, domain: nil, username: 'username') + follow = Fabricate(:follow, account: account) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + expect(unfollow_salmon.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends activity:object-type element with activity type' do + follow = Fabricate(:follow) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + object_type = unfollow_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with follow' do + follow = Fabricate(:follow) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + verb = unfollow_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:unfollow] + end + + it 'appends activity:object element with target account' do + target_account = Fabricate(:account, domain: 'domain', uri: 'https://domain/id') + follow = Fabricate(:follow, target_account: target_account) + follow.destroy! + + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + + object = unfollow_salmon.nodes.find { |node| node.name == 'activity:object' } + expect(object.id.text).to eq 'https://domain/id' + end + + it 'returns element whose rendered view triggers unfollow when processed' do + follow = Fabricate(:follow) + follow.destroy! + unfollow_salmon = Ostatus::AtomSerializer.new.unfollow_salmon(follow) + xml = Ostatus::AtomSerializer.render(unfollow_salmon) + follow.account.follow!(follow.target_account) + envelope = OStatus2::Salmon.new.pack(xml, follow.account.keypair) + + ProcessInteractionService.new.call(envelope, follow.target_account) + + expect(follow.account.following?(follow.target_account)).to be false + end + end + + describe '#follow_request_salmon' do + include_examples 'namespaces' do + def serialize + follow_request = Fabricate(:follow_request) + Ostatus::AtomSerializer.new.follow_request_salmon(follow_request) + end + end + + context do + def serialize(follow_request) + Ostatus::AtomSerializer.new.follow_request_salmon(follow_request) + end + + it_behaves_like 'follow request salmon' + + it 'appends id element with unique tag' do + follow_request = Fabricate(:follow_request, created_at: '2000-01-01T00:00:00Z') + follow_request_salmon = serialize(follow_request) + expect(follow_request_salmon.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{follow_request.id}:objectType=FollowRequest" + end + + it 'appends title element with description' do + account = Fabricate(:account, domain: nil, username: 'account') + target_account = Fabricate(:account, domain: 'remote', username: 'target_account') + follow_request = Fabricate(:follow_request, account: account, target_account: target_account) + follow_request_salmon = serialize(follow_request) + expect(follow_request_salmon.title.text).to eq 'account requested to follow target_account@remote' + end + + it 'returns element whose rendered view triggers follow request when processed' do + follow_request = Fabricate(:follow_request) + follow_request_salmon = serialize(follow_request) + xml = Ostatus::AtomSerializer.render(follow_request_salmon) + envelope = OStatus2::Salmon.new.pack(xml, follow_request.account.keypair) + follow_request.destroy! + + ProcessInteractionService.new.call(envelope, follow_request.target_account) + + expect(follow_request.account.requested?(follow_request.target_account)).to eq true + end + end + end + + describe '#authorize_follow_request_salmon' do + include_examples 'namespaces' do + def serialize + follow_request = Fabricate(:follow_request) + Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + end + end + + it_behaves_like 'follow request salmon' do + def serialize(follow_request) + authorize_follow_request_salmon = Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' } + end + end + + it 'appends id element with unique tag' do + follow_request = Fabricate(:follow_request) + + time_before = Time.now + authorize_follow_request_salmon = Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + time_after = Time.now + + expect(authorize_follow_request_salmon.id.text).to( + eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest')) + .or(eq(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest'))) + ) + end + + it 'appends title element with description' do + account = Fabricate(:account, domain: 'remote', username: 'account') + target_account = Fabricate(:account, domain: nil, username: 'target_account') + follow_request = Fabricate(:follow_request, account: account, target_account: target_account) + + authorize_follow_request_salmon = Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + + expect(authorize_follow_request_salmon.title.text).to eq 'target_account authorizes follow request by account@remote' + end + + it 'appends activity:object-type element with activity type' do + follow_request = Fabricate(:follow_request) + + authorize_follow_request_salmon = Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + + object_type = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with authorize' do + follow_request = Fabricate(:follow_request) + + authorize_follow_request_salmon = Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + + verb = authorize_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:authorize] + end + + it 'returns element whose rendered view creates follow from follow request when processed' do + follow_request = Fabricate(:follow_request) + authorize_follow_request_salmon = Ostatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request) + xml = Ostatus::AtomSerializer.render(authorize_follow_request_salmon) + envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair) + + ProcessInteractionService.new.call(envelope, follow_request.account) + + expect(follow_request.account.following?(follow_request.target_account)).to eq true + expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound + end + end + + describe '#reject_follow_request_salmon' do + include_examples 'namespaces' do + def serialize + follow_request = Fabricate(:follow_request) + Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + end + end + + it_behaves_like 'follow request salmon' do + def serialize(follow_request) + reject_follow_request_salmon = Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object' } + end + end + + it 'appends id element with unique tag' do + follow_request = Fabricate(:follow_request) + + time_before = Time.now + reject_follow_request_salmon = Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + time_after = Time.now + + expect(reject_follow_request_salmon.id.text).to( + eq(TagManager.instance.unique_tag(time_before.utc, follow_request.id, 'FollowRequest')) + .or(TagManager.instance.unique_tag(time_after.utc, follow_request.id, 'FollowRequest')) + ) + end + + it 'appends title element with description' do + account = Fabricate(:account, domain: 'remote', username: 'account') + target_account = Fabricate(:account, domain: nil, username: 'target_account') + follow_request = Fabricate(:follow_request, account: account, target_account: target_account) + reject_follow_request_salmon = Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + expect(reject_follow_request_salmon.title.text).to eq 'target_account rejects follow request by account@remote' + end + + it 'appends activity:object-type element with activity type' do + follow_request = Fabricate(:follow_request) + reject_follow_request_salmon = Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + object_type = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:activity] + end + + it 'appends activity:verb element with authorize' do + follow_request = Fabricate(:follow_request) + reject_follow_request_salmon = Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + verb = reject_follow_request_salmon.nodes.find { |node| node.name == 'activity:verb' } + expect(verb.text).to eq TagManager::VERBS[:reject] + end + + it 'returns element whose rendered view deletes follow request when processed' do + follow_request = Fabricate(:follow_request) + reject_follow_request_salmon = Ostatus::AtomSerializer.new.reject_follow_request_salmon(follow_request) + xml = Ostatus::AtomSerializer.render(reject_follow_request_salmon) + envelope = OStatus2::Salmon.new.pack(xml, follow_request.target_account.keypair) + + ProcessInteractionService.new.call(envelope, follow_request.account) + + expect(follow_request.account.following?(follow_request.target_account)).to eq false + expect { follow_request.reload }.to raise_error ActiveRecord::RecordNotFound + end + end + + describe '#object' do + include_examples 'status attributes' do + def serialize(status) + Ostatus::AtomSerializer.new.object(status) + end + end + + it 'returns activity:object element' do + status = Fabricate(:status) + object = Ostatus::AtomSerializer.new.object(status) + expect(object.name).to eq 'activity:object' + end + + it 'appends id element with URL for status' do + status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') + object = Ostatus::AtomSerializer.new.object(status) + expect(object.id.text).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.id}:objectType=Status" + end + + it 'appends published element with created date' do + status = Fabricate(:status, created_at: '2000-01-01T00:00:00Z') + object = Ostatus::AtomSerializer.new.object(status) + expect(object.published.text).to eq '2000-01-01T00:00:00Z' + end + + it 'appends updated element with updated date' do + status = Fabricate(:status, updated_at: '2000-01-01T00:00:00Z') + object = Ostatus::AtomSerializer.new.object(status) + expect(object.updated.text).to eq '2000-01-01T00:00:00Z' + end + + it 'appends title element with title' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + object = Ostatus::AtomSerializer.new.object(status) + + expect(object.title.text).to eq 'New status by username' + end + + it 'appends author element with account' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + entry = Ostatus::AtomSerializer.new.object(status) + + expect(entry.author.id.text).to eq 'https://cb6e6126.ngrok.io/users/username' + end + + it 'appends activity:object-type element with object type' do + status = Fabricate(:status) + + entry = Ostatus::AtomSerializer.new.object(status) + + object_type = entry.nodes.find { |node| node.name == 'activity:object-type' } + expect(object_type.text).to eq TagManager::TYPES[:note] + end + + it 'appends activity:verb element with verb' do + status = Fabricate(:status) + + entry = Ostatus::AtomSerializer.new.object(status) + + object_type = entry.nodes.find { |node| node.name == 'activity:verb' } + expect(object_type.text).to eq TagManager::VERBS[:post] + end + + it 'appends link element for an alternative' do + account = Fabricate(:account, username: 'username') + status = Fabricate(:status, account: account) + + entry = Ostatus::AtomSerializer.new.object(status) + + link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } + expect(link[:type]).to eq 'text/html' + expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" + end + + it 'appends thr:in-reply-to element if it is a reply and thread is not nil' do + account = Fabricate(:account, username: 'username') + thread = Fabricate(:status, account: account, created_at: '2000-01-01T00:00:00Z') + reply = Fabricate(:status, thread: thread) + + entry = Ostatus::AtomSerializer.new.object(reply) + + in_reply_to = entry.nodes.find { |node| node.name == 'thr:in-reply-to' } + expect(in_reply_to.ref).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{thread.id}:objectType=Status" + expect(in_reply_to.href).to eq "https://cb6e6126.ngrok.io/@username/#{thread.id}" + end + + it 'does not append thr:in-reply-to element if thread is nil' do + status = Fabricate(:status, thread: nil) + entry = Ostatus::AtomSerializer.new.object(status) + entry.nodes.each { |node| expect(node.name).not_to eq 'thr:in-reply-to' } + end + + it 'does not append ostatus:conversation element if conversation_id is nil' do + status = Fabricate.build(:status, conversation_id: nil) + status.save!(validate: false) + + entry = Ostatus::AtomSerializer.new.object(status) + + entry.nodes.each { |node| expect(node.name).not_to eq 'ostatus:conversation' } + end + + it 'appends ostatus:conversation element if conversation_id is not nil' do + status = Fabricate(:status) + status.conversation.update!(created_at: '2000-01-01T00:00:00Z') + + entry = Ostatus::AtomSerializer.new.object(status) + + conversation = entry.nodes.find { |node| node.name == 'ostatus:conversation' } + expect(conversation[:ref]).to eq "tag:cb6e6126.ngrok.io,2000-01-01:objectId=#{status.conversation.id}:objectType=Conversation" + end + end +end diff --git a/spec/services/process_feed_service_spec.rb b/spec/services/process_feed_service_spec.rb index d85ee3b56..5e34370ee 100644 --- a/spec/services/process_feed_service_spec.rb +++ b/spec/services/process_feed_service_spec.rb @@ -167,6 +167,46 @@ XML expect(created_statuses.first.reblog.text).to eq 'Overwatch rocks' end + it 'ignores reblogs if it failed to retreive reblogged statuses' do + stub_request(:head, 'https://overwatch.com/users/tracer/updates/1').to_return(status: 404) + + actor = Fabricate(:account, username: 'tracer', domain: 'overwatch.com') + + body = < + + tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status + 2017-04-27T13:49:25Z + 2017-04-27T13:49:25Z + + https://overwatch.com/users/tracer + http://activitystrea.ms/schema/1.0/person + https://overwatch.com/users/tracer + tracer + + http://activitystrea.ms/schema/1.0/activity + http://activitystrea.ms/schema/1.0/share + Overwatch rocks + + tag:overwatch.com,2017-04-27:objectId=4467137:objectType=Status + http://activitystrea.ms/schema/1.0/note + http://activitystrea.ms/schema/1.0/post + + https://overwatch.com/users/tracer + http://activitystrea.ms/schema/1.0/person + https://overwatch.com/users/tracer + tracer + + Overwatch rocks + + +XML + + created_statuses = subject.call(body, actor) + + expect(created_statuses).to eq [] + end + it 'ignores statuses with an out-of-order delete' do sender = Fabricate(:account, username: 'tracer', domain: 'overwatch.com') -- cgit From 3267e4a7851b57bef7d16da4b7c66764f63d4416 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Wed, 19 Jul 2017 00:14:43 +0900 Subject: Add unfollow modal (optional) (#4246) * Add unfollow modal * unfollowing someone * remove unnecessary prop --- app/controllers/settings/preferences_controller.rb | 1 + .../mastodon/containers/account_container.js | 24 +++++++++++++++++++--- .../containers/header_container.js | 14 ++++++++++++- .../mastodon/features/ui/components/modal_root.js | 2 +- app/javascript/mastodon/locales/ar.json | 2 ++ app/javascript/mastodon/locales/bg.json | 2 ++ app/javascript/mastodon/locales/ca.json | 2 ++ app/javascript/mastodon/locales/de.json | 2 ++ .../mastodon/locales/defaultMessages.json | 21 +++++++++++++++++++ app/javascript/mastodon/locales/en.json | 2 ++ app/javascript/mastodon/locales/eo.json | 2 ++ app/javascript/mastodon/locales/es.json | 2 ++ app/javascript/mastodon/locales/fa.json | 2 ++ app/javascript/mastodon/locales/fi.json | 2 ++ app/javascript/mastodon/locales/fr.json | 2 ++ app/javascript/mastodon/locales/he.json | 2 ++ app/javascript/mastodon/locales/hr.json | 2 ++ app/javascript/mastodon/locales/hu.json | 2 ++ app/javascript/mastodon/locales/id.json | 2 ++ app/javascript/mastodon/locales/io.json | 2 ++ app/javascript/mastodon/locales/it.json | 2 ++ app/javascript/mastodon/locales/ja.json | 2 ++ app/javascript/mastodon/locales/ko.json | 2 ++ app/javascript/mastodon/locales/nl.json | 2 ++ app/javascript/mastodon/locales/no.json | 2 ++ app/javascript/mastodon/locales/oc.json | 2 ++ app/javascript/mastodon/locales/pl.json | 2 ++ app/javascript/mastodon/locales/pt-BR.json | 2 ++ app/javascript/mastodon/locales/pt.json | 2 ++ app/javascript/mastodon/locales/ru.json | 2 ++ app/javascript/mastodon/locales/th.json | 2 ++ app/javascript/mastodon/locales/tr.json | 2 ++ app/javascript/mastodon/locales/uk.json | 2 ++ app/javascript/mastodon/locales/zh-CN.json | 2 ++ app/javascript/mastodon/locales/zh-HK.json | 2 ++ app/javascript/mastodon/locales/zh-TW.json | 2 ++ app/lib/user_settings_decorator.rb | 5 +++++ app/models/user.rb | 4 ++++ app/serializers/initial_state_serializer.rb | 1 + app/views/settings/preferences/show.html.haml | 1 + config/locales/simple_form.en.yml | 1 + spec/lib/user_settings_decorator_spec.rb | 7 +++++++ spec/models/user_spec.rb | 8 ++++++++ 43 files changed, 146 insertions(+), 5 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index a3f5a008b..f107f2b16 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -35,6 +35,7 @@ class Settings::PreferencesController < ApplicationController params.require(:user).permit( :setting_default_privacy, :setting_default_sensitive, + :setting_unfollow_modal, :setting_boost_modal, :setting_delete_modal, :setting_auto_play_gif, diff --git a/app/javascript/mastodon/containers/account_container.js b/app/javascript/mastodon/containers/account_container.js index 1426bcaa4..ca1efd0e5 100644 --- a/app/javascript/mastodon/containers/account_container.js +++ b/app/javascript/mastodon/containers/account_container.js @@ -1,4 +1,6 @@ +import React from 'react'; import { connect } from 'react-redux'; +import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; import { makeGetAccount } from '../selectors'; import Account from '../components/account'; import { @@ -9,6 +11,11 @@ import { muteAccount, unmuteAccount, } from '../actions/accounts'; +import { openModal } from '../actions/modal'; + +const messages = defineMessages({ + unfollowConfirm: { id: 'confirmations.unfollow.confirm', defaultMessage: 'Unfollow' }, +}); const makeMapStateToProps = () => { const getAccount = makeGetAccount(); @@ -16,15 +23,25 @@ const makeMapStateToProps = () => { const mapStateToProps = (state, props) => ({ account: getAccount(state, props.id), me: state.getIn(['meta', 'me']), + unfollowModal: state.getIn(['meta', 'unfollow_modal']), }); return mapStateToProps; }; -const mapDispatchToProps = (dispatch) => ({ +const mapDispatchToProps = (dispatch, { intl }) => ({ + onFollow (account) { if (account.getIn(['relationship', 'following'])) { - dispatch(unfollowAccount(account.get('id'))); + if (this.unfollowModal) { + dispatch(openModal('CONFIRM', { + message: @{account.get('acct')} }} />, + confirm: intl.formatMessage(messages.unfollowConfirm), + onConfirm: () => dispatch(unfollowAccount(account.get('id'))), + })); + } else { + dispatch(unfollowAccount(account.get('id'))); + } } else { dispatch(followAccount(account.get('id'))); } @@ -45,6 +62,7 @@ const mapDispatchToProps = (dispatch) => ({ dispatch(muteAccount(account.get('id'))); } }, + }); -export default connect(makeMapStateToProps, mapDispatchToProps)(Account); +export default injectIntl(connect(makeMapStateToProps, mapDispatchToProps)(Account)); diff --git a/app/javascript/mastodon/features/account_timeline/containers/header_container.js b/app/javascript/mastodon/features/account_timeline/containers/header_container.js index 19dd64699..baa81bbc2 100644 --- a/app/javascript/mastodon/features/account_timeline/containers/header_container.js +++ b/app/javascript/mastodon/features/account_timeline/containers/header_container.js @@ -17,6 +17,7 @@ import { blockDomain, unblockDomain } from '../../../actions/domain_blocks'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; const messages = defineMessages({ + unfollowConfirm: { id: 'confirmations.unfollow.confirm', defaultMessage: 'Unfollow' }, blockConfirm: { id: 'confirmations.block.confirm', defaultMessage: 'Block' }, muteConfirm: { id: 'confirmations.mute.confirm', defaultMessage: 'Mute' }, blockDomainConfirm: { id: 'confirmations.domain_block.confirm', defaultMessage: 'Hide entire domain' }, @@ -28,15 +29,25 @@ const makeMapStateToProps = () => { const mapStateToProps = (state, { accountId }) => ({ account: getAccount(state, Number(accountId)), me: state.getIn(['meta', 'me']), + unfollowModal: state.getIn(['meta', 'unfollow_modal']), }); return mapStateToProps; }; const mapDispatchToProps = (dispatch, { intl }) => ({ + onFollow (account) { if (account.getIn(['relationship', 'following'])) { - dispatch(unfollowAccount(account.get('id'))); + if (this.unfollowModal) { + dispatch(openModal('CONFIRM', { + message: @{account.get('acct')} }} />, + confirm: intl.formatMessage(messages.unfollowConfirm), + onConfirm: () => dispatch(unfollowAccount(account.get('id'))), + })); + } else { + dispatch(unfollowAccount(account.get('id'))); + } } else { dispatch(followAccount(account.get('id'))); } @@ -85,6 +96,7 @@ const mapDispatchToProps = (dispatch, { intl }) => ({ onUnblockDomain (domain, accountId) { dispatch(unblockDomain(domain, accountId)); }, + }); export default injectIntl(connect(makeMapStateToProps, mapDispatchToProps)(Header)); diff --git a/app/javascript/mastodon/features/ui/components/modal_root.js b/app/javascript/mastodon/features/ui/components/modal_root.js index 4240871a7..f303088d7 100644 --- a/app/javascript/mastodon/features/ui/components/modal_root.js +++ b/app/javascript/mastodon/features/ui/components/modal_root.js @@ -85,7 +85,7 @@ export default class ModalRoot extends React.PureComponent { > {interpolatedStyles =>
- {interpolatedStyles.map(({ key, data: { type }, style }) => ( + {interpolatedStyles.map(({ key, data: { type, props }, style }) => (
diff --git a/app/javascript/mastodon/locales/ar.json b/app/javascript/mastodon/locales/ar.json index 7b890ce64..89ddb2d15 100644 --- a/app/javascript/mastodon/locales/ar.json +++ b/app/javascript/mastodon/locales/ar.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "أكتم", "confirmations.mute.message": "هل أنت متأكد أنك تريد كتم {name} ؟", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "الأنشطة", "emoji_button.flags": "الأعلام", "emoji_button.food": "الطعام والشراب", diff --git a/app/javascript/mastodon/locales/bg.json b/app/javascript/mastodon/locales/bg.json index 0cf6bf3ac..3dba91b82 100644 --- a/app/javascript/mastodon/locales/bg.json +++ b/app/javascript/mastodon/locales/bg.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/ca.json b/app/javascript/mastodon/locales/ca.json index 1e44d6fa5..54f2e5e22 100644 --- a/app/javascript/mastodon/locales/ca.json +++ b/app/javascript/mastodon/locales/ca.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Estàs realment, realment segur que vols bloquejar totalment {domain}? En la majoria dels casos bloquejar o silenciar és suficient i preferible.", "confirmations.mute.confirm": "Silenciar", "confirmations.mute.message": "Estàs segur que vols silenciar {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activitat", "emoji_button.flags": "Flags", "emoji_button.food": "Menjar i Beure", diff --git a/app/javascript/mastodon/locales/de.json b/app/javascript/mastodon/locales/de.json index f73011e73..a041e6655 100644 --- a/app/javascript/mastodon/locales/de.json +++ b/app/javascript/mastodon/locales/de.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/defaultMessages.json b/app/javascript/mastodon/locales/defaultMessages.json index aaa558c0e..bf462a537 100644 --- a/app/javascript/mastodon/locales/defaultMessages.json +++ b/app/javascript/mastodon/locales/defaultMessages.json @@ -228,6 +228,19 @@ ], "path": "app/javascript/mastodon/components/video_player.json" }, + { + "descriptors": [ + { + "defaultMessage": "Unfollow", + "id": "confirmations.unfollow.confirm" + }, + { + "defaultMessage": "Are you sure you want to unfollow {name}?", + "id": "confirmations.unfollow.message" + } + ], + "path": "app/javascript/mastodon/containers/account_container.json" + }, { "descriptors": [ { @@ -268,6 +281,10 @@ }, { "descriptors": [ + { + "defaultMessage": "Unfollow", + "id": "confirmations.unfollow.confirm" + }, { "defaultMessage": "Block", "id": "confirmations.block.confirm" @@ -280,6 +297,10 @@ "defaultMessage": "Hide entire domain", "id": "confirmations.domain_block.confirm" }, + { + "defaultMessage": "Are you sure you want to unfollow {name}?", + "id": "confirmations.unfollow.message" + }, { "defaultMessage": "Are you sure you want to block {name}?", "id": "confirmations.block.message" diff --git a/app/javascript/mastodon/locales/en.json b/app/javascript/mastodon/locales/en.json index 15afe2309..fe2bd4cb4 100644 --- a/app/javascript/mastodon/locales/en.json +++ b/app/javascript/mastodon/locales/en.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/eo.json b/app/javascript/mastodon/locales/eo.json index 4f9e26c25..029cef883 100644 --- a/app/javascript/mastodon/locales/eo.json +++ b/app/javascript/mastodon/locales/eo.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/es.json b/app/javascript/mastodon/locales/es.json index 64ba78716..36ad66ace 100644 --- a/app/javascript/mastodon/locales/es.json +++ b/app/javascript/mastodon/locales/es.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/fa.json b/app/javascript/mastodon/locales/fa.json index 306937cc2..113daef77 100644 --- a/app/javascript/mastodon/locales/fa.json +++ b/app/javascript/mastodon/locales/fa.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "آیا جدی جدی می‌خواهید کل دامین {domain} را مسدود کنید؟ بیشتر وقت‌ها مسدودکردن یا بی‌صداکردن چند حساب کاربری خاص کافی است و توصیه می‌شود.", "confirmations.mute.confirm": "بی‌صدا کن", "confirmations.mute.message": "آیا واقعاً می‌خواهید {name} را بی‌صدا کنید؟", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "فعالیت", "emoji_button.flags": "پرچم‌ها", "emoji_button.food": "غذا و نوشیدنی", diff --git a/app/javascript/mastodon/locales/fi.json b/app/javascript/mastodon/locales/fi.json index 1b17fb155..da9e5d0f2 100644 --- a/app/javascript/mastodon/locales/fi.json +++ b/app/javascript/mastodon/locales/fi.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/fr.json b/app/javascript/mastodon/locales/fr.json index b6605295b..c3e743259 100644 --- a/app/javascript/mastodon/locales/fr.json +++ b/app/javascript/mastodon/locales/fr.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Êtes-vous vraiment, vraiment sûr⋅e de vouloir bloquer {domain} en entier ? Dans la plupart des cas, quelques blocages ou masquages ciblés sont suffisants et préférables.", "confirmations.mute.confirm": "Masquer", "confirmations.mute.message": "Confirmez vous le masquage de {name} ?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activités", "emoji_button.flags": "Drapeaux", "emoji_button.food": "Boire et manger", diff --git a/app/javascript/mastodon/locales/he.json b/app/javascript/mastodon/locales/he.json index 8b63bd26b..c8dc4fe8d 100644 --- a/app/javascript/mastodon/locales/he.json +++ b/app/javascript/mastodon/locales/he.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "באמת באמת לחסום את כל קהילת {domain}? ברב המקרים השתקות נבחרות של מספר משתמשים מסויימים צריכה להספיק.", "confirmations.mute.confirm": "להשתיק", "confirmations.mute.message": "להשתיק את {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "פעילות", "emoji_button.flags": "דגלים", "emoji_button.food": "אוכל ושתיה", diff --git a/app/javascript/mastodon/locales/hr.json b/app/javascript/mastodon/locales/hr.json index 165e3088f..fd669b2b2 100644 --- a/app/javascript/mastodon/locales/hr.json +++ b/app/javascript/mastodon/locales/hr.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Jesi li zaista, zaista siguran da želiš blokirati sve sa {domain}? U većini slučajeva nekoliko ciljanih blokiranja ili utišavanja je dostatno i poželjnije.", "confirmations.mute.confirm": "Utišaj", "confirmations.mute.message": "Jesi li siguran da želiš utišati {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Aktivnost", "emoji_button.flags": "Zastave", "emoji_button.food": "Hrana & Piće", diff --git a/app/javascript/mastodon/locales/hu.json b/app/javascript/mastodon/locales/hu.json index 71dcce505..b3672cb7a 100644 --- a/app/javascript/mastodon/locales/hu.json +++ b/app/javascript/mastodon/locales/hu.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/id.json b/app/javascript/mastodon/locales/id.json index 0c21877d8..8a17262fe 100644 --- a/app/javascript/mastodon/locales/id.json +++ b/app/javascript/mastodon/locales/id.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Bisukan", "confirmations.mute.message": "Apa anda yakin ingin membisukan {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Aktivitas", "emoji_button.flags": "Bendera", "emoji_button.food": "Makanan & Minuman", diff --git a/app/javascript/mastodon/locales/io.json b/app/javascript/mastodon/locales/io.json index 788d09f34..154ca66ce 100644 --- a/app/javascript/mastodon/locales/io.json +++ b/app/javascript/mastodon/locales/io.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/it.json b/app/javascript/mastodon/locales/it.json index 9176bfaaf..6cb274bae 100644 --- a/app/javascript/mastodon/locales/it.json +++ b/app/javascript/mastodon/locales/it.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/ja.json b/app/javascript/mastodon/locales/ja.json index a686cdc03..c62e36482 100644 --- a/app/javascript/mastodon/locales/ja.json +++ b/app/javascript/mastodon/locales/ja.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "本当に{domain}全体を非表示にしますか? 多くの場合は個別にブロックやミュートするだけで充分であり、また好ましいです。", "confirmations.mute.confirm": "ミュート", "confirmations.mute.message": "本当に{name}をミュートしますか?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "活動", "emoji_button.flags": "国旗", "emoji_button.food": "食べ物", diff --git a/app/javascript/mastodon/locales/ko.json b/app/javascript/mastodon/locales/ko.json index 0b47cc990..fbd0098d7 100644 --- a/app/javascript/mastodon/locales/ko.json +++ b/app/javascript/mastodon/locales/ko.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "정말로 {domain} 전체를 숨기시겠습니까? 대부분의 경우 개별 차단이나 뮤트로 충분합니다.", "confirmations.mute.confirm": "뮤트", "confirmations.mute.message": "정말로 {name}를 뮤트하시겠습니까?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "활동", "emoji_button.flags": "국기", "emoji_button.food": "음식", diff --git a/app/javascript/mastodon/locales/nl.json b/app/javascript/mastodon/locales/nl.json index cf6a8bd31..f7b0bbf68 100644 --- a/app/javascript/mastodon/locales/nl.json +++ b/app/javascript/mastodon/locales/nl.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Weet je het echt, echt zeker dat je alles van {domain} wil negeren? In de meeste gevallen is het blokkeren of negeren van een paar specifieke personen voldoende en gewenst.", "confirmations.mute.confirm": "Negeren", "confirmations.mute.message": "Weet je zeker dat je {name} wilt negeren?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activiteiten", "emoji_button.flags": "Vlaggen", "emoji_button.food": "Eten en drinken", diff --git a/app/javascript/mastodon/locales/no.json b/app/javascript/mastodon/locales/no.json index 1f4082d7b..98f59f774 100644 --- a/app/javascript/mastodon/locales/no.json +++ b/app/javascript/mastodon/locales/no.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Er du sikker på at du vil skjule hele domenet {domain}? I de fleste tilfeller er det bedre med målrettet blokkering eller demping.", "confirmations.mute.confirm": "Demp", "confirmations.mute.message": "Er du sikker på at du vil dempe {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Aktivitet", "emoji_button.flags": "Flagg", "emoji_button.food": "Mat og drikke", diff --git a/app/javascript/mastodon/locales/oc.json b/app/javascript/mastodon/locales/oc.json index dc6dd5e32..ca094c18a 100644 --- a/app/javascript/mastodon/locales/oc.json +++ b/app/javascript/mastodon/locales/oc.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Sètz segur segur de voler blocar complètament {domain} ? De còps cal pas que blocar o rescondre unas personas solament.", "confirmations.mute.confirm": "Metre en silenci", "confirmations.mute.message": "Sètz segur de voler metre en silenci {name} ?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activitat", "emoji_button.flags": "Drapèus", "emoji_button.food": "Beure e manjar", diff --git a/app/javascript/mastodon/locales/pl.json b/app/javascript/mastodon/locales/pl.json index 233d61995..683f589b1 100644 --- a/app/javascript/mastodon/locales/pl.json +++ b/app/javascript/mastodon/locales/pl.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Czy na pewno chcesz zablokować całą domenę {domain}? Zwykle lepszym rozwiązaniem jest blokada lub wyciszenie kilku użytkowników.", "confirmations.mute.confirm": "Wycisz", "confirmations.mute.message": "Czy na pewno chcesz wyciszyć {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Aktywność", "emoji_button.flags": "Flagi", "emoji_button.food": "Żywność i napoje", diff --git a/app/javascript/mastodon/locales/pt-BR.json b/app/javascript/mastodon/locales/pt-BR.json index cf2b911f2..3944e33e9 100644 --- a/app/javascript/mastodon/locales/pt-BR.json +++ b/app/javascript/mastodon/locales/pt-BR.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/pt.json b/app/javascript/mastodon/locales/pt.json index cf2b911f2..3944e33e9 100644 --- a/app/javascript/mastodon/locales/pt.json +++ b/app/javascript/mastodon/locales/pt.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/ru.json b/app/javascript/mastodon/locales/ru.json index 942a13ede..cffc285f4 100644 --- a/app/javascript/mastodon/locales/ru.json +++ b/app/javascript/mastodon/locales/ru.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Вы на самом деле уверены, что хотите блокировать весь {domain}? В большинстве случаев нескольких отдельных блокировок или глушений достаточно.", "confirmations.mute.confirm": "Заглушить", "confirmations.mute.message": "Вы уверены, что хотите заглушить {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Занятия", "emoji_button.flags": "Флаги", "emoji_button.food": "Еда и напитки", diff --git a/app/javascript/mastodon/locales/th.json b/app/javascript/mastodon/locales/th.json index e9e96c14f..63bed6d8c 100644 --- a/app/javascript/mastodon/locales/th.json +++ b/app/javascript/mastodon/locales/th.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Mute", "confirmations.mute.message": "Are you sure you want to mute {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Activity", "emoji_button.flags": "Flags", "emoji_button.food": "Food & Drink", diff --git a/app/javascript/mastodon/locales/tr.json b/app/javascript/mastodon/locales/tr.json index adfa79cd9..5bd308e95 100644 --- a/app/javascript/mastodon/locales/tr.json +++ b/app/javascript/mastodon/locales/tr.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "Sessize al", "confirmations.mute.message": "{name} kullanıcısını sessize almak istiyor musunuz?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Aktivite", "emoji_button.flags": "Bayraklar", "emoji_button.food": "Yiyecek ve İçecek", diff --git a/app/javascript/mastodon/locales/uk.json b/app/javascript/mastodon/locales/uk.json index 435067281..e1611505d 100644 --- a/app/javascript/mastodon/locales/uk.json +++ b/app/javascript/mastodon/locales/uk.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Ви точно, точно впевнені, що хочете заблокувати весь домен {domain}? У більшості випадків для нормальної роботи краще заблокувати/заглушити лише деяких користувачів.", "confirmations.mute.confirm": "Заглушити", "confirmations.mute.message": "Ви впевнені, що хочете заглушити {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "Заняття", "emoji_button.flags": "Прапори", "emoji_button.food": "Їжа та напої", diff --git a/app/javascript/mastodon/locales/zh-CN.json b/app/javascript/mastodon/locales/zh-CN.json index 0f2c1fcec..18bf872e5 100644 --- a/app/javascript/mastodon/locales/zh-CN.json +++ b/app/javascript/mastodon/locales/zh-CN.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "静音", "confirmations.mute.message": "想好了,真的要静音 {name}?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "活动", "emoji_button.flags": "旗帜", "emoji_button.food": "食物和饮料", diff --git a/app/javascript/mastodon/locales/zh-HK.json b/app/javascript/mastodon/locales/zh-HK.json index c0b4cfce9..a461085c7 100644 --- a/app/javascript/mastodon/locales/zh-HK.json +++ b/app/javascript/mastodon/locales/zh-HK.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "Are you really, really sure you want to block the entire {domain}? In most cases a few targeted blocks or mutes are sufficient and preferable.", "confirmations.mute.confirm": "靜音", "confirmations.mute.message": "你確定要將{name}靜音嗎?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "活動", "emoji_button.flags": "旗幟", "emoji_button.food": "飲飲食食", diff --git a/app/javascript/mastodon/locales/zh-TW.json b/app/javascript/mastodon/locales/zh-TW.json index 772cc691c..d766fb394 100644 --- a/app/javascript/mastodon/locales/zh-TW.json +++ b/app/javascript/mastodon/locales/zh-TW.json @@ -55,6 +55,8 @@ "confirmations.domain_block.message": "你真的真的確定要封鎖整個 {domain} ?多數情況下,比較推薦封鎖或消音幾個特定目標就好。", "confirmations.mute.confirm": "消音", "confirmations.mute.message": "你確定要消音 {name} ?", + "confirmations.unfollow.confirm": "Unfollow", + "confirmations.unfollow.message": "Are you sure you want to unfollow {name}?", "emoji_button.activity": "活動", "emoji_button.flags": "旗幟", "emoji_button.food": "食物與飲料", diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb index c5da18029..62046ed72 100644 --- a/app/lib/user_settings_decorator.rb +++ b/app/lib/user_settings_decorator.rb @@ -19,6 +19,7 @@ class UserSettingsDecorator user.settings['interactions'] = merged_interactions user.settings['default_privacy'] = default_privacy_preference user.settings['default_sensitive'] = default_sensitive_preference + user.settings['unfollow_modal'] = unfollow_modal_preference user.settings['boost_modal'] = boost_modal_preference user.settings['delete_modal'] = delete_modal_preference user.settings['auto_play_gif'] = auto_play_gif_preference @@ -42,6 +43,10 @@ class UserSettingsDecorator boolean_cast_setting 'setting_default_sensitive' end + def unfollow_modal_preference + boolean_cast_setting 'setting_unfollow_modal' + end + def boost_modal_preference boolean_cast_setting 'setting_boost_modal' end diff --git a/app/models/user.rb b/app/models/user.rb index becf0018f..25dc25864 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -83,6 +83,10 @@ class User < ApplicationRecord settings.default_sensitive end + def setting_unfollow_modal + settings.unfollow_modal + end + def setting_boost_modal settings.boost_modal end diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb index 704d29a57..0191948b1 100644 --- a/app/serializers/initial_state_serializer.rb +++ b/app/serializers/initial_state_serializer.rb @@ -15,6 +15,7 @@ class InitialStateSerializer < ActiveModel::Serializer if object.current_account store[:me] = object.current_account.id + store[:unfollow_modal] = object.current_account.user.setting_unfollow_modal store[:boost_modal] = object.current_account.user.setting_boost_modal store[:delete_modal] = object.current_account.user.setting_delete_modal store[:auto_play_gif] = object.current_account.user.setting_auto_play_gif diff --git a/app/views/settings/preferences/show.html.haml b/app/views/settings/preferences/show.html.haml index 3b5d90942..fae6090c8 100644 --- a/app/views/settings/preferences/show.html.haml +++ b/app/views/settings/preferences/show.html.haml @@ -44,6 +44,7 @@ = f.input :setting_noindex, as: :boolean, wrapper: :with_label .fields-group + = f.input :setting_unfollow_modal, as: :boolean, wrapper: :with_label = f.input :setting_boost_modal, as: :boolean, wrapper: :with_label = f.input :setting_delete_modal, as: :boolean, wrapper: :with_label diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index 476ccc773..536bb06e1 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -42,6 +42,7 @@ en: setting_default_sensitive: Always mark media as sensitive setting_delete_modal: Show confirmation dialog before deleting a toot setting_system_font_ui: Use system's default font + setting_unfollow_modal: Show confirmation dialog before unfollowing someone setting_noindex: Opt-out of search engine indexing severity: Severity type: Import type diff --git a/spec/lib/user_settings_decorator_spec.rb b/spec/lib/user_settings_decorator_spec.rb index a67487779..6fbf6536b 100644 --- a/spec/lib/user_settings_decorator_spec.rb +++ b/spec/lib/user_settings_decorator_spec.rb @@ -35,6 +35,13 @@ describe UserSettingsDecorator do expect(user.settings['default_sensitive']).to eq true end + it 'updates the user settings value for unfollow modal' do + values = { 'setting_unfollow_modal' => '0' } + + settings.update(values) + expect(user.settings['unfollow_modal']).to eq false + end + it 'updates the user settings value for boost modal' do values = { 'setting_boost_modal' => '1' } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2019ec0f6..ef45818b9 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -219,6 +219,14 @@ RSpec.describe User, type: :model do end end + describe '#setting_unfollow_modal' do + it 'returns unfollow modal setting' do + user = Fabricate(:user) + user.settings[:unfollow_modal] = true + expect(user.setting_unfollow_modal).to eq true + end + end + describe '#setting_delete_modal' do it 'returns delete modal setting' do user = Fabricate(:user) -- cgit