From 7d8e3721aea71315b0ef8e66cdc2ede0fe6ffc2a Mon Sep 17 00:00:00 2001 From: "Akihiko Odaki (@fn_aki@pawoo.net)" Date: Wed, 28 Jun 2017 21:50:23 +0900 Subject: Overwrite old statuses with reblogs in PrecomputeFeedService (#3984) --- app/services/precompute_feed_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/services') diff --git a/app/services/precompute_feed_service.rb b/app/services/precompute_feed_service.rb index 83765bb05..4c24567c8 100644 --- a/app/services/precompute_feed_service.rb +++ b/app/services/precompute_feed_service.rb @@ -14,7 +14,7 @@ class PrecomputeFeedService < BaseService def populate_feed redis.pipelined do - statuses.each do |status| + statuses.reverse_each do |status| process_status(status) end -- cgit From f79c10162e51689f6759dfa39251c8ba8e7e11e8 Mon Sep 17 00:00:00 2001 From: "Akihiko Odaki (@fn_aki@pawoo.net)" Date: Thu, 29 Jun 2017 08:25:31 +0900 Subject: Use multiple pairs for zadd in PrecomputeFeedService (#3990) --- app/services/precompute_feed_service.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'app/services') diff --git a/app/services/precompute_feed_service.rb b/app/services/precompute_feed_service.rb index 4c24567c8..e2f6ff0cb 100644 --- a/app/services/precompute_feed_service.rb +++ b/app/services/precompute_feed_service.rb @@ -13,21 +13,16 @@ class PrecomputeFeedService < BaseService attr_reader :account def populate_feed - redis.pipelined do - statuses.reverse_each do |status| - process_status(status) - end + pairs = statuses.reverse_each.map(&method(:process_status)) + redis.pipelined do + redis.zadd(account_home_key, pairs) redis.del("account:#{@account.id}:regeneration") end end def process_status(status) - add_status_to_feed(status) unless status_filtered?(status) - end - - def add_status_to_feed(status) - redis.zadd(account_home_key, status.id, status.reblog? ? status.reblog_of_id : status.id) + [status.id, status.reblog? ? status.reblog_of_id : status.id] unless status_filtered?(status) end def status_filtered?(status) -- cgit From b342c81c17cc8cf4af2ac3b1c57f4f250e0fefc1 Mon Sep 17 00:00:00 2001 From: abcang Date: Thu, 29 Jun 2017 20:04:07 +0900 Subject: rescue HTTP::ConnectionError (#3992) --- app/services/fetch_atom_service.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app/services') diff --git a/app/services/fetch_atom_service.rb b/app/services/fetch_atom_service.rb index 8f42db0aa..d430b22e9 100644 --- a/app/services/fetch_atom_service.rb +++ b/app/services/fetch_atom_service.rb @@ -20,6 +20,10 @@ class FetchAtomService < BaseService process_html(fetch(url)) rescue OpenSSL::SSL::SSLError => e Rails.logger.debug "SSL error: #{e}" + nil + rescue HTTP::ConnectionError => e + Rails.logger.debug "HTTP ConnectionError: #{e}" + nil end private -- cgit From 7362469d8956d5f972283aadd4157631aa66b085 Mon Sep 17 00:00:00 2001 From: "Akihiko Odaki (@fn_aki@pawoo.net)" Date: Fri, 30 Jun 2017 20:39:42 +0900 Subject: Do not raise an error if PrecomputeFeed could not find any status (#4015) --- app/services/precompute_feed_service.rb | 2 +- spec/services/precompute_feed_service_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'app/services') diff --git a/app/services/precompute_feed_service.rb b/app/services/precompute_feed_service.rb index e2f6ff0cb..a32ba1dae 100644 --- a/app/services/precompute_feed_service.rb +++ b/app/services/precompute_feed_service.rb @@ -16,7 +16,7 @@ class PrecomputeFeedService < BaseService pairs = statuses.reverse_each.map(&method(:process_status)) redis.pipelined do - redis.zadd(account_home_key, pairs) + redis.zadd(account_home_key, pairs) if pairs.any? redis.del("account:#{@account.id}:regeneration") end end diff --git a/spec/services/precompute_feed_service_spec.rb b/spec/services/precompute_feed_service_spec.rb index 72235a966..e2294469c 100644 --- a/spec/services/precompute_feed_service_spec.rb +++ b/spec/services/precompute_feed_service_spec.rb @@ -18,5 +18,10 @@ RSpec.describe PrecomputeFeedService do expect(Redis.current.zscore(FeedManager.instance.key(:home, account.id), reblog.id)).to eq status.id end + + it 'does not raise an error even if it could not find any status' do + account = Fabricate(:account) + subject.call(account) + end end end -- cgit From a6d02cff368d96178b0843ef021232d2187abbcd Mon Sep 17 00:00:00 2001 From: abcang Date: Mon, 3 Jul 2017 18:03:34 +0900 Subject: Rescue exceptions caused by FetchLinkCardService (#4045) --- app/models/concerns/remotable.rb | 8 ++++++-- app/services/fetch_link_card_service.rb | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'app/services') diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb index b0077ce96..08d4fc59c 100644 --- a/app/models/concerns/remotable.rb +++ b/app/models/concerns/remotable.rb @@ -10,7 +10,11 @@ module Remotable method_name = "#{attribute_name}=".to_sym define_method method_name do |url| - parsed_url = Addressable::URI.parse(url).normalize + begin + parsed_url = Addressable::URI.parse(url).normalize + rescue Addressable::URI::InvalidURIError + return + end return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[attribute_name] == url @@ -26,7 +30,7 @@ module Remotable send("#{attachment_name}_file_name=", filename) self[attribute_name] = url if has_attribute?(attribute_name) - rescue HTTP::TimeoutError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError => e + rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError => e Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}" nil end diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index c2df7b2f0..4ce221267 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -18,6 +18,8 @@ class FetchLinkCardService < BaseService return if res.code != 200 || res.mime_type != 'text/html' attempt_opengraph(card, url) unless attempt_oembed(card, url) + rescue HTTP::ConnectionError, OpenSSL::SSL::SSLError + nil end private -- cgit From 5e6acf960183aea9440ce0d9e28c86f043e88c54 Mon Sep 17 00:00:00 2001 From: abcang Date: Wed, 5 Jul 2017 21:54:21 +0900 Subject: Fix Nokogiri::HTML at FetchLinkCardService (#4072) --- app/services/fetch_link_card_service.rb | 4 +++- spec/fixtures/requests/sjis.txt | 20 ++++++++++++++++++++ spec/services/fetch_link_card_service_spec.rb | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/requests/sjis.txt (limited to 'app/services') diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 4ce221267..8ddaa2bf4 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'nkf' class FetchLinkCardService < BaseService include HttpHelper @@ -84,7 +85,8 @@ class FetchLinkCardService < BaseService return if response.code != 200 || response.mime_type != 'text/html' - page = Nokogiri::HTML(response.to_s) + html = response.to_s + page = Nokogiri::HTML(html, nil, NKF.guess(html).to_s) card.type = :link card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content diff --git a/spec/fixtures/requests/sjis.txt b/spec/fixtures/requests/sjis.txt new file mode 100644 index 000000000..9041aa25d --- /dev/null +++ b/spec/fixtures/requests/sjis.txt @@ -0,0 +1,20 @@ +HTTP/1.1 200 OK +Server: nginx/1.11.10 +Date: Tue, 04 Jul 2017 16:43:39 GMT +Content-Type: text/html +Content-Length: 273 +Connection: keep-alive +Last-Modified: Tue, 04 Jul 2017 16:41:34 GMT +Accept-Ranges: bytes + + + + + + JSISのページ + + +

SJISのページ
+

+ + diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 9df41cf55..7d7f8e748 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -6,6 +6,8 @@ RSpec.describe FetchLinkCardService do before do stub_request(:head, 'http://example.xn--fiqs8s/').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt')) + stub_request(:head, 'http://example.com/sjis').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) + stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt')) stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404) subject.call(status) @@ -19,6 +21,14 @@ RSpec.describe FetchLinkCardService do expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once end end + + context do + let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis') } + + it 'works with SJIS' do + expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once + end + end end context 'in a remote status' do -- cgit From 8b2cad56374b2dbb6e7a445e7917810935c45536 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 7 Jul 2017 04:02:06 +0200 Subject: Refactor JSON templates to be generated with ActiveModelSerializers instead of Rabl (#4090) --- Gemfile | 1 + Gemfile.lock | 9 +++ .../api/v1/accounts/credentials_controller.rb | 4 +- .../v1/accounts/follower_accounts_controller.rb | 2 +- .../v1/accounts/following_accounts_controller.rb | 2 +- .../api/v1/accounts/relationships_controller.rb | 11 ++- .../api/v1/accounts/search_controller.rb | 3 +- .../api/v1/accounts/statuses_controller.rb | 5 +- app/controllers/api/v1/accounts_controller.rb | 38 +++------ app/controllers/api/v1/apps_controller.rb | 1 + app/controllers/api/v1/blocks_controller.rb | 1 + app/controllers/api/v1/favourites_controller.rb | 5 +- .../api/v1/follow_requests_controller.rb | 1 + app/controllers/api/v1/follows_controller.rb | 2 +- app/controllers/api/v1/instances_controller.rb | 4 +- app/controllers/api/v1/media_controller.rb | 1 + app/controllers/api/v1/mutes_controller.rb | 1 + app/controllers/api/v1/notifications_controller.rb | 7 +- app/controllers/api/v1/reports_controller.rb | 3 +- app/controllers/api/v1/search_controller.rb | 3 +- .../statuses/favourited_by_accounts_controller.rb | 2 +- .../api/v1/statuses/favourites_controller.rb | 4 +- .../api/v1/statuses/mutes_controller.rb | 4 +- .../statuses/reblogged_by_accounts_controller.rb | 2 +- .../api/v1/statuses/reblogs_controller.rb | 4 +- app/controllers/api/v1/statuses_controller.rb | 16 ++-- .../api/v1/timelines/home_controller.rb | 6 +- .../api/v1/timelines/public_controller.rb | 6 +- app/controllers/api/v1/timelines/tag_controller.rb | 6 +- app/lib/inline_rabl_scope.rb | 17 ---- app/lib/inline_renderer.rb | 36 +++++++-- app/models/context.rb | 5 ++ app/models/search.rb | 5 ++ app/presenters/account_relationships_presenter.rb | 15 ++++ app/presenters/status_relationships_presenter.rb | 19 +++++ app/serializers/rest/account_serializer.rb | 33 ++++++++ app/serializers/rest/application_serializer.rb | 14 ++++ app/serializers/rest/context_serializer.rb | 6 ++ app/serializers/rest/instance_serializer.rb | 30 +++++++ .../rest/media_attachment_serializer.rb | 24 ++++++ app/serializers/rest/notification_serializer.rb | 12 +++ app/serializers/rest/preview_card_serializer.rb | 14 ++++ app/serializers/rest/relationship_serializer.rb | 30 +++++++ app/serializers/rest/report_serializer.rb | 5 ++ app/serializers/rest/search_serializer.rb | 12 +++ app/serializers/rest/status_serializer.rb | 93 ++++++++++++++++++++++ app/services/fan_out_on_write_service.rb | 2 +- app/services/notify_service.rb | 2 +- app/views/api/v1/accounts/index.rabl | 2 - app/views/api/v1/accounts/relationship.rabl | 9 --- app/views/api/v1/accounts/relationships/index.rabl | 2 - app/views/api/v1/accounts/show.rabl | 12 --- app/views/api/v1/accounts/statuses/index.rabl | 2 - app/views/api/v1/apps/create.rabl | 4 - app/views/api/v1/apps/show.rabl | 3 - app/views/api/v1/blocks/index.rabl | 2 - app/views/api/v1/favourites/index.rabl | 2 - app/views/api/v1/follow_requests/index.rabl | 2 - app/views/api/v1/follows/show.rabl | 2 - app/views/api/v1/instances/show.rabl | 10 --- app/views/api/v1/media/create.rabl | 7 -- app/views/api/v1/mutes/index.rabl | 2 - app/views/api/v1/notifications/index.rabl | 2 - app/views/api/v1/notifications/show.rabl | 11 --- app/views/api/v1/reports/index.rabl | 2 - app/views/api/v1/reports/show.rabl | 2 - app/views/api/v1/search/index.rabl | 13 --- app/views/api/v1/statuses/_media.rabl | 6 -- app/views/api/v1/statuses/_mention.rabl | 4 - app/views/api/v1/statuses/_show.rabl | 29 ------- app/views/api/v1/statuses/_tags.rabl | 2 - app/views/api/v1/statuses/accounts.rabl | 2 - app/views/api/v1/statuses/card.rabl | 7 -- app/views/api/v1/statuses/context.rabl | 9 --- app/views/api/v1/statuses/index.rabl | 2 - app/views/api/v1/statuses/show.rabl | 15 ---- app/views/api/v1/timelines/show.rabl | 2 - app/views/home/initial_state.json.rabl | 4 +- app/workers/push_update_worker.rb | 2 +- spec/lib/inline_rabl_scope_spec.rb | 23 ------ 80 files changed, 425 insertions(+), 301 deletions(-) delete mode 100644 app/lib/inline_rabl_scope.rb create mode 100644 app/models/context.rb create mode 100644 app/models/search.rb create mode 100644 app/presenters/account_relationships_presenter.rb create mode 100644 app/presenters/status_relationships_presenter.rb create mode 100644 app/serializers/rest/account_serializer.rb create mode 100644 app/serializers/rest/application_serializer.rb create mode 100644 app/serializers/rest/context_serializer.rb create mode 100644 app/serializers/rest/instance_serializer.rb create mode 100644 app/serializers/rest/media_attachment_serializer.rb create mode 100644 app/serializers/rest/notification_serializer.rb create mode 100644 app/serializers/rest/preview_card_serializer.rb create mode 100644 app/serializers/rest/relationship_serializer.rb create mode 100644 app/serializers/rest/report_serializer.rb create mode 100644 app/serializers/rest/search_serializer.rb create mode 100644 app/serializers/rest/status_serializer.rb delete mode 100644 app/views/api/v1/accounts/index.rabl delete mode 100644 app/views/api/v1/accounts/relationship.rabl delete mode 100644 app/views/api/v1/accounts/relationships/index.rabl delete mode 100644 app/views/api/v1/accounts/show.rabl delete mode 100644 app/views/api/v1/accounts/statuses/index.rabl delete mode 100644 app/views/api/v1/apps/create.rabl delete mode 100644 app/views/api/v1/apps/show.rabl delete mode 100644 app/views/api/v1/blocks/index.rabl delete mode 100644 app/views/api/v1/favourites/index.rabl delete mode 100644 app/views/api/v1/follow_requests/index.rabl delete mode 100644 app/views/api/v1/follows/show.rabl delete mode 100644 app/views/api/v1/instances/show.rabl delete mode 100644 app/views/api/v1/media/create.rabl delete mode 100644 app/views/api/v1/mutes/index.rabl delete mode 100644 app/views/api/v1/notifications/index.rabl delete mode 100644 app/views/api/v1/notifications/show.rabl delete mode 100644 app/views/api/v1/reports/index.rabl delete mode 100644 app/views/api/v1/reports/show.rabl delete mode 100644 app/views/api/v1/search/index.rabl delete mode 100644 app/views/api/v1/statuses/_media.rabl delete mode 100644 app/views/api/v1/statuses/_mention.rabl delete mode 100644 app/views/api/v1/statuses/_show.rabl delete mode 100644 app/views/api/v1/statuses/_tags.rabl delete mode 100644 app/views/api/v1/statuses/accounts.rabl delete mode 100644 app/views/api/v1/statuses/card.rabl delete mode 100644 app/views/api/v1/statuses/context.rabl delete mode 100644 app/views/api/v1/statuses/index.rabl delete mode 100644 app/views/api/v1/statuses/show.rabl delete mode 100644 app/views/api/v1/timelines/show.rabl delete mode 100644 spec/lib/inline_rabl_scope_spec.rb (limited to 'app/services') diff --git a/Gemfile b/Gemfile index 6ee884a17..95c74eef9 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem 'aws-sdk', '~> 2.9' gem 'paperclip', '~> 5.1' gem 'paperclip-av-transcoder', '~> 0.6' +gem 'active_model_serializers', '~> 0.10' gem 'addressable', '~> 2.5' gem 'bootsnap' gem 'browser' diff --git a/Gemfile.lock b/Gemfile.lock index f0156529c..71f83f736 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) + active_model_serializers (0.10.6) + actionpack (>= 4.1, < 6) + activemodel (>= 4.1, < 6) + case_transform (>= 0.2) + jsonapi-renderer (>= 0.1.1.beta1, < 0.2) active_record_query_trace (1.5.4) activejob (5.1.2) activesupport (= 5.1.2) @@ -101,6 +106,8 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + case_transform (0.2) + activesupport chunky_png (1.3.8) cld3 (3.1.3) ffi (>= 1.1.0, < 1.10.0) @@ -200,6 +207,7 @@ GEM terminal-table (>= 1.5.1) jmespath (1.3.1) json (2.1.0) + jsonapi-renderer (0.1.2) kaminari (1.0.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.0.1) @@ -476,6 +484,7 @@ PLATFORMS ruby DEPENDENCIES + active_model_serializers (~> 0.10) active_record_query_trace (~> 1.5) addressable (~> 2.5) annotate (~> 2.7) diff --git a/app/controllers/api/v1/accounts/credentials_controller.rb b/app/controllers/api/v1/accounts/credentials_controller.rb index 1cf52ff10..8ee9a2416 100644 --- a/app/controllers/api/v1/accounts/credentials_controller.rb +++ b/app/controllers/api/v1/accounts/credentials_controller.rb @@ -6,13 +6,13 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController def show @account = current_account - render 'api/v1/accounts/show' + render json: @account, serializer: REST::AccountSerializer end def update current_account.update!(account_params) @account = current_account - render 'api/v1/accounts/show' + render json: @account, serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/accounts/follower_accounts_controller.rb b/app/controllers/api/v1/accounts/follower_accounts_controller.rb index 81aae56d3..80b0bef40 100644 --- a/app/controllers/api/v1/accounts/follower_accounts_controller.rb +++ b/app/controllers/api/v1/accounts/follower_accounts_controller.rb @@ -9,7 +9,7 @@ class Api::V1::Accounts::FollowerAccountsController < Api::BaseController def index @accounts = load_accounts - render 'api/v1/accounts/index' + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/accounts/following_accounts_controller.rb b/app/controllers/api/v1/accounts/following_accounts_controller.rb index 63c6d54b2..55cffdf37 100644 --- a/app/controllers/api/v1/accounts/following_accounts_controller.rb +++ b/app/controllers/api/v1/accounts/following_accounts_controller.rb @@ -9,7 +9,7 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController def index @accounts = load_accounts - render 'api/v1/accounts/index' + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/accounts/relationships_controller.rb b/app/controllers/api/v1/accounts/relationships_controller.rb index cb923ab91..a88cf2021 100644 --- a/app/controllers/api/v1/accounts/relationships_controller.rb +++ b/app/controllers/api/v1/accounts/relationships_controller.rb @@ -8,16 +8,15 @@ class Api::V1::Accounts::RelationshipsController < Api::BaseController def index @accounts = Account.where(id: account_ids).select('id') - @following = Account.following_map(account_ids, current_user.account_id) - @followed_by = Account.followed_by_map(account_ids, current_user.account_id) - @blocking = Account.blocking_map(account_ids, current_user.account_id) - @muting = Account.muting_map(account_ids, current_user.account_id) - @requested = Account.requested_map(account_ids, current_user.account_id) - @domain_blocking = Account.domain_blocking_map(account_ids, current_user.account_id) + render json: @accounts, each_serializer: REST::RelationshipSerializer, relationships: relationships end private + def relationships + AccountRelationshipsPresenter.new(@accounts, current_user.account_id) + end + def account_ids @_account_ids ||= Array(params[:id]).map(&:to_i) end diff --git a/app/controllers/api/v1/accounts/search_controller.rb b/app/controllers/api/v1/accounts/search_controller.rb index c4a8f97f2..2a5cac547 100644 --- a/app/controllers/api/v1/accounts/search_controller.rb +++ b/app/controllers/api/v1/accounts/search_controller.rb @@ -8,8 +8,7 @@ class Api::V1::Accounts::SearchController < Api::BaseController def show @accounts = account_search - - render 'api/v1/accounts/index' + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/accounts/statuses_controller.rb b/app/controllers/api/v1/accounts/statuses_controller.rb index 504ed8c07..d9ae5c089 100644 --- a/app/controllers/api/v1/accounts/statuses_controller.rb +++ b/app/controllers/api/v1/accounts/statuses_controller.rb @@ -9,6 +9,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController def index @statuses = load_statuses + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id) end private @@ -18,9 +19,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController end def load_statuses - cached_account_statuses.tap do |statuses| - set_maps(statuses) - end + cached_account_statuses end def cached_account_statuses diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 8fc0dd36f..f621aa245 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -8,49 +8,38 @@ class Api::V1::AccountsController < Api::BaseController respond_to :json - def show; end + def show + render json: @account, serializer: REST::AccountSerializer + end def follow FollowService.new.call(current_user.account, @account.acct) - set_relationship - render :relationship + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships end def block BlockService.new.call(current_user.account, @account) - - @following = { @account.id => false } - @followed_by = { @account.id => false } - @blocking = { @account.id => true } - @requested = { @account.id => false } - @muting = { @account.id => current_account.muting?(@account.id) } - @domain_blocking = { @account.id => current_account.domain_blocking?(@account.domain) } - - render :relationship + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships end def mute MuteService.new.call(current_user.account, @account) - set_relationship - render :relationship + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships end def unfollow UnfollowService.new.call(current_user.account, @account) - set_relationship - render :relationship + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships end def unblock UnblockService.new.call(current_user.account, @account) - set_relationship - render :relationship + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships end def unmute UnmuteService.new.call(current_user.account, @account) - set_relationship - render :relationship + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships end private @@ -59,12 +48,7 @@ class Api::V1::AccountsController < Api::BaseController @account = Account.find(params[:id]) end - def set_relationship - @following = Account.following_map([@account.id], current_user.account_id) - @followed_by = Account.followed_by_map([@account.id], current_user.account_id) - @blocking = Account.blocking_map([@account.id], current_user.account_id) - @muting = Account.muting_map([@account.id], current_user.account_id) - @requested = Account.requested_map([@account.id], current_user.account_id) - @domain_blocking = Account.domain_blocking_map([@account.id], current_user.account_id) + def relationships + AccountRelationshipsPresenter.new([@account.id], current_user.account_id) end end diff --git a/app/controllers/api/v1/apps_controller.rb b/app/controllers/api/v1/apps_controller.rb index 98e908948..44a27b20a 100644 --- a/app/controllers/api/v1/apps_controller.rb +++ b/app/controllers/api/v1/apps_controller.rb @@ -5,6 +5,7 @@ class Api::V1::AppsController < Api::BaseController def create @app = Doorkeeper::Application.create!(application_options) + render json: @app, serializer: REST::ApplicationSerializer end private diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index 1702953cf..a412e4341 100644 --- a/app/controllers/api/v1/blocks_controller.rb +++ b/app/controllers/api/v1/blocks_controller.rb @@ -9,6 +9,7 @@ class Api::V1::BlocksController < Api::BaseController def index @accounts = load_accounts + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/favourites_controller.rb b/app/controllers/api/v1/favourites_controller.rb index fe0819a3f..92c0a62a9 100644 --- a/app/controllers/api/v1/favourites_controller.rb +++ b/app/controllers/api/v1/favourites_controller.rb @@ -9,14 +9,13 @@ class Api::V1::FavouritesController < Api::BaseController def index @statuses = load_statuses + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id) end private def load_statuses - cached_favourites.tap do |statuses| - set_maps(statuses) - end + cached_favourites end def cached_favourites diff --git a/app/controllers/api/v1/follow_requests_controller.rb b/app/controllers/api/v1/follow_requests_controller.rb index eed22ef4f..b9f50d784 100644 --- a/app/controllers/api/v1/follow_requests_controller.rb +++ b/app/controllers/api/v1/follow_requests_controller.rb @@ -7,6 +7,7 @@ class Api::V1::FollowRequestsController < Api::BaseController def index @accounts = load_accounts + render json: @accounts, each_serializer: REST::AccountSerializer end def authorize diff --git a/app/controllers/api/v1/follows_controller.rb b/app/controllers/api/v1/follows_controller.rb index bcdb4e177..e01ae5c01 100644 --- a/app/controllers/api/v1/follows_controller.rb +++ b/app/controllers/api/v1/follows_controller.rb @@ -10,7 +10,7 @@ class Api::V1::FollowsController < Api::BaseController raise ActiveRecord::RecordNotFound if follow_params[:uri].blank? @account = FollowService.new.call(current_user.account, target_uri).try(:target_account) - render :show + render json: @account, serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/instances_controller.rb b/app/controllers/api/v1/instances_controller.rb index ce2181879..1c6971c18 100644 --- a/app/controllers/api/v1/instances_controller.rb +++ b/app/controllers/api/v1/instances_controller.rb @@ -3,5 +3,7 @@ class Api::V1::InstancesController < Api::BaseController respond_to :json - def show; end + def show + render json: {}, serializer: REST::InstanceSerializer + end end diff --git a/app/controllers/api/v1/media_controller.rb b/app/controllers/api/v1/media_controller.rb index 25a331319..8a1992fca 100644 --- a/app/controllers/api/v1/media_controller.rb +++ b/app/controllers/api/v1/media_controller.rb @@ -11,6 +11,7 @@ class Api::V1::MediaController < Api::BaseController def create @media = current_account.media_attachments.create!(file: media_params[:file]) + render json: @media, serializer: REST::MediaAttachmentSerializer rescue Paperclip::Errors::NotIdentifiedByImageMagickError render json: file_type_error, status: 422 rescue Paperclip::Error diff --git a/app/controllers/api/v1/mutes_controller.rb b/app/controllers/api/v1/mutes_controller.rb index 2a353df03..0c43cb943 100644 --- a/app/controllers/api/v1/mutes_controller.rb +++ b/app/controllers/api/v1/mutes_controller.rb @@ -9,6 +9,7 @@ class Api::V1::MutesController < Api::BaseController def index @accounts = load_accounts + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index a28e99f2f..8910b77e9 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -11,11 +11,12 @@ class Api::V1::NotificationsController < Api::BaseController def index @notifications = load_notifications - set_maps_for_notification_target_statuses + render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id) end def show @notification = current_account.notifications.find(params[:id]) + render json: @notification, serializer: REST::NotificationSerializer end def clear @@ -46,10 +47,6 @@ class Api::V1::NotificationsController < Api::BaseController current_account.notifications.browserable(exclude_types) end - def set_maps_for_notification_target_statuses - set_maps target_statuses_from_notifications - end - def target_statuses_from_notifications @notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status) end diff --git a/app/controllers/api/v1/reports_controller.rb b/app/controllers/api/v1/reports_controller.rb index 8e7070d07..9592cd4bd 100644 --- a/app/controllers/api/v1/reports_controller.rb +++ b/app/controllers/api/v1/reports_controller.rb @@ -9,6 +9,7 @@ class Api::V1::ReportsController < Api::BaseController def index @reports = current_account.reports + render json: @reports, each_serializer: REST::ReportSerializer end def create @@ -20,7 +21,7 @@ class Api::V1::ReportsController < Api::BaseController User.admins.includes(:account).each { |u| AdminMailer.new_report(u.account, @report).deliver_later } - render :show + render json: @report, serializer: REST::ReportSerializer end private diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index 8b832148c..1353682ea 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -6,7 +6,8 @@ class Api::V1::SearchController < Api::BaseController respond_to :json def index - @search = OpenStruct.new(search_results) + @search = Search.new(search_results) + render json: @search, serializer: REST::SearchSerializer end private diff --git a/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb b/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb index e58184939..f95cf9457 100644 --- a/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb +++ b/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb @@ -11,7 +11,7 @@ class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController def index @accounts = load_accounts - render 'api/v1/statuses/accounts' + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/statuses/favourites_controller.rb b/app/controllers/api/v1/statuses/favourites_controller.rb index b6fb13cc0..4c4b0c160 100644 --- a/app/controllers/api/v1/statuses/favourites_controller.rb +++ b/app/controllers/api/v1/statuses/favourites_controller.rb @@ -10,7 +10,7 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController def create @status = favourited_status - render 'api/v1/statuses/show' + render json: @status, serializer: REST::StatusSerializer end def destroy @@ -19,7 +19,7 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController UnfavouriteWorker.perform_async(current_user.account_id, @status.id) - render 'api/v1/statuses/show' + render json: @status, serializer: REST::StatusSerializer end private diff --git a/app/controllers/api/v1/statuses/mutes_controller.rb b/app/controllers/api/v1/statuses/mutes_controller.rb index eab88f2ef..a4bf0acdd 100644 --- a/app/controllers/api/v1/statuses/mutes_controller.rb +++ b/app/controllers/api/v1/statuses/mutes_controller.rb @@ -14,14 +14,14 @@ class Api::V1::Statuses::MutesController < Api::BaseController current_account.mute_conversation!(@conversation) @mutes_map = { @conversation.id => true } - render 'api/v1/statuses/show' + render json: @status, serializer: REST::StatusSerializer end def destroy current_account.unmute_conversation!(@conversation) @mutes_map = { @conversation.id => false } - render 'api/v1/statuses/show' + render json: @status, serializer: REST::StatusSerializer end private diff --git a/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb b/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb index 43593d3c5..175217e6e 100644 --- a/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb +++ b/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb @@ -11,7 +11,7 @@ class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController def index @accounts = load_accounts - render 'api/v1/statuses/accounts' + render json: @accounts, each_serializer: REST::AccountSerializer end private diff --git a/app/controllers/api/v1/statuses/reblogs_controller.rb b/app/controllers/api/v1/statuses/reblogs_controller.rb index ee9c5b3a6..f7f4b5a5c 100644 --- a/app/controllers/api/v1/statuses/reblogs_controller.rb +++ b/app/controllers/api/v1/statuses/reblogs_controller.rb @@ -10,7 +10,7 @@ class Api::V1::Statuses::ReblogsController < Api::BaseController def create @status = ReblogService.new.call(current_user.account, status_for_reblog) - render 'api/v1/statuses/show' + render json: @status, serializer: REST::StatusSerializer end def destroy @@ -20,7 +20,7 @@ class Api::V1::Statuses::ReblogsController < Api::BaseController authorize status_for_destroy, :unreblog? RemovalWorker.perform_async(status_for_destroy.id) - render 'api/v1/statuses/show' + render json: @status, serializer: REST::StatusSerializer end private diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index 9aa1cbc4d..9c7124d0f 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -13,6 +13,7 @@ class Api::V1::StatusesController < Api::BaseController def show cached = Rails.cache.read(@status.cache_key) @status = cached unless cached.nil? + render json: @status, serializer: REST::StatusSerializer end def context @@ -21,15 +22,20 @@ class Api::V1::StatusesController < Api::BaseController loaded_ancestors = cache_collection(ancestors_results, Status) loaded_descendants = cache_collection(descendants_results, Status) - @context = OpenStruct.new(ancestors: loaded_ancestors, descendants: loaded_descendants) - statuses = [@status] + @context[:ancestors] + @context[:descendants] + @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants) + statuses = [@status] + @context.ancestors + @context.descendants - set_maps(statuses) + render json: @context, serializer: REST::ContextSerializer, relationships: StatusRelationshipsPresenter.new(statuses, current_user&.account_id) end def card @card = PreviewCard.find_by(status: @status) - render_empty if @card.nil? + + if @card.nil? + render_empty + else + render json: @card, serializer: REST::PreviewCardSerializer + end end def create @@ -43,7 +49,7 @@ class Api::V1::StatusesController < Api::BaseController application: doorkeeper_token.application, idempotency: request.headers['Idempotency-Key']) - render :show + render json: @status, serializer: REST::StatusSerializer end def destroy diff --git a/app/controllers/api/v1/timelines/home_controller.rb b/app/controllers/api/v1/timelines/home_controller.rb index 511d2f65d..3dd27710c 100644 --- a/app/controllers/api/v1/timelines/home_controller.rb +++ b/app/controllers/api/v1/timelines/home_controller.rb @@ -9,15 +9,13 @@ class Api::V1::Timelines::HomeController < Api::BaseController def show @statuses = load_statuses - render 'api/v1/timelines/show' + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id) end private def load_statuses - cached_home_statuses.tap do |statuses| - set_maps(statuses) - end + cached_home_statuses end def cached_home_statuses diff --git a/app/controllers/api/v1/timelines/public_controller.rb b/app/controllers/api/v1/timelines/public_controller.rb index 305451cc7..49887778e 100644 --- a/app/controllers/api/v1/timelines/public_controller.rb +++ b/app/controllers/api/v1/timelines/public_controller.rb @@ -7,15 +7,13 @@ class Api::V1::Timelines::PublicController < Api::BaseController def show @statuses = load_statuses - render 'api/v1/timelines/show' + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id) end private def load_statuses - cached_public_statuses.tap do |statuses| - set_maps(statuses) - end + cached_public_statuses end def cached_public_statuses diff --git a/app/controllers/api/v1/timelines/tag_controller.rb b/app/controllers/api/v1/timelines/tag_controller.rb index 50afca7c7..08db04a39 100644 --- a/app/controllers/api/v1/timelines/tag_controller.rb +++ b/app/controllers/api/v1/timelines/tag_controller.rb @@ -8,7 +8,7 @@ class Api::V1::Timelines::TagController < Api::BaseController def show @statuses = load_statuses - render 'api/v1/timelines/show' + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id) end private @@ -18,9 +18,7 @@ class Api::V1::Timelines::TagController < Api::BaseController end def load_statuses - cached_tagged_statuses.tap do |statuses| - set_maps(statuses) - end + cached_tagged_statuses end def cached_tagged_statuses diff --git a/app/lib/inline_rabl_scope.rb b/app/lib/inline_rabl_scope.rb deleted file mode 100644 index 26adcb03a..000000000 --- a/app/lib/inline_rabl_scope.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class InlineRablScope - include RoutingHelper - - def initialize(account) - @account = account - end - - def current_user - @account.try(:user) - end - - def current_account - @account - end -end diff --git a/app/lib/inline_renderer.rb b/app/lib/inline_renderer.rb index 8e04ad1d5..7cd9758ec 100644 --- a/app/lib/inline_renderer.rb +++ b/app/lib/inline_renderer.rb @@ -1,13 +1,33 @@ # frozen_string_literal: true class InlineRenderer - def self.render(status, current_account, template) - Rabl::Renderer.new( - template, - status, - view_path: 'app/views', - format: :json, - scope: InlineRablScope.new(current_account) - ).render + def initialize(object, current_account, template) + @object = object + @current_account = current_account + @template = template + end + + def render + case @template + when :status + serializer = REST::StatusSerializer + when :notification + serializer = REST::NotificationSerializer + else + return + end + + serializable_resource = ActiveModelSerializers::SerializableResource.new(@object, serializer: serializer, scope: current_user, scope_name: :current_user) + serializable_resource.as_json + end + + def self.render(object, current_account, template) + new(object, current_account, template).render + end + + private + + def current_user + @current_account&.user end end diff --git a/app/models/context.rb b/app/models/context.rb new file mode 100644 index 000000000..cc667999e --- /dev/null +++ b/app/models/context.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class Context < ActiveModelSerializers::Model + attributes :ancestors, :descendants +end diff --git a/app/models/search.rb b/app/models/search.rb new file mode 100644 index 000000000..676c2a7f8 --- /dev/null +++ b/app/models/search.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class Search < ActiveModelSerializers::Model + attributes :accounts, :statuses, :hashtags +end diff --git a/app/presenters/account_relationships_presenter.rb b/app/presenters/account_relationships_presenter.rb new file mode 100644 index 000000000..657807863 --- /dev/null +++ b/app/presenters/account_relationships_presenter.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class AccountRelationshipsPresenter + attr_reader :following, :followed_by, :blocking, + :muting, :requested, :domain_blocking + + def initialize(account_ids, current_account_id) + @following = Account.following_map(account_ids, current_account_id) + @followed_by = Account.followed_by_map(account_ids, current_account_id) + @blocking = Account.blocking_map(account_ids, current_account_id) + @muting = Account.muting_map(account_ids, current_account_id) + @requested = Account.requested_map(account_ids, current_account_id) + @domain_blocking = Account.domain_blocking_map(account_ids, current_account_id) + end +end diff --git a/app/presenters/status_relationships_presenter.rb b/app/presenters/status_relationships_presenter.rb new file mode 100644 index 000000000..caf00791a --- /dev/null +++ b/app/presenters/status_relationships_presenter.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class StatusRelationshipsPresenter + attr_reader :reblogs_map, :favourites_map, :mutes_map + + def initialize(statuses, current_account_id = nil) + if current_account_id.nil? + @reblogs_map = {} + @favourites_map = {} + @mutes_map = {} + else + status_ids = statuses.compact.flat_map { |s| [s.id, s.reblog_of_id] }.uniq + conversation_ids = statuses.compact.map(&:conversation_id).compact.uniq + @reblogs_map = Status.reblogs_map(status_ids, current_account_id) + @favourites_map = Status.favourites_map(status_ids, current_account_id) + @mutes_map = Status.mutes_map(conversation_ids, current_account_id) + end + end +end diff --git a/app/serializers/rest/account_serializer.rb b/app/serializers/rest/account_serializer.rb new file mode 100644 index 000000000..012a4fd18 --- /dev/null +++ b/app/serializers/rest/account_serializer.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class REST::AccountSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :id, :username, :acct, :display_name, :locked, :created_at, + :note, :url, :avatar, :avatar_static, :header, :header_static, + :followers_count, :following_count, :statuses_count + + def note + Formatter.instance.simplified_format(object) + end + + def url + TagManager.instance.url_for(object) + end + + def avatar + full_asset_url(object.avatar_original_url) + end + + def avatar_static + full_asset_url(object.avatar_static_url) + end + + def header + full_asset_url(object.header_original_url) + end + + def header_static + full_asset_url(object.header_static_url) + end +end diff --git a/app/serializers/rest/application_serializer.rb b/app/serializers/rest/application_serializer.rb new file mode 100644 index 000000000..868a62f1e --- /dev/null +++ b/app/serializers/rest/application_serializer.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class REST::ApplicationSerializer < ActiveModel::Serializer + attributes :id, :name, :website, :redirect_uri, + :client_id, :client_secret + + def client_id + object.uid + end + + def client_secret + object.secret + end +end diff --git a/app/serializers/rest/context_serializer.rb b/app/serializers/rest/context_serializer.rb new file mode 100644 index 000000000..44515c85d --- /dev/null +++ b/app/serializers/rest/context_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class REST::ContextSerializer < ActiveModel::Serializer + has_many :ancestors, serializer: REST::StatusSerializer + has_many :descendants, serializer: REST::StatusSerializer +end diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb new file mode 100644 index 000000000..8e32f9cb3 --- /dev/null +++ b/app/serializers/rest/instance_serializer.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class REST::InstanceSerializer < ActiveModel::Serializer + attributes :uri, :title, :description, :email, + :version, :urls + + def uri + Rails.configuration.x.local_domain + end + + def title + Setting.site_title + end + + def description + Setting.site_description + end + + def email + Setting.site_contact_email + end + + def version + Mastodon::Version.to_s + end + + def urls + { streaming_api: Rails.configuration.x.streaming_api_base_url } + end +end diff --git a/app/serializers/rest/media_attachment_serializer.rb b/app/serializers/rest/media_attachment_serializer.rb new file mode 100644 index 000000000..9b07a686e --- /dev/null +++ b/app/serializers/rest/media_attachment_serializer.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class REST::MediaAttachmentSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :id, :type, :url, :preview_url, + :remote_url, :text_url, :meta + + def url + full_asset_url(object.file.url(:original)) + end + + def preview_url + full_asset_url(object.file.url(:small)) + end + + def text_url + medium_url(object.id) + end + + def meta + object.file.meta + end +end diff --git a/app/serializers/rest/notification_serializer.rb b/app/serializers/rest/notification_serializer.rb new file mode 100644 index 000000000..97fadf32e --- /dev/null +++ b/app/serializers/rest/notification_serializer.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class REST::NotificationSerializer < ActiveModel::Serializer + attributes :id, :type, :created_at + + belongs_to :from_account, key: :account, serializer: REST::AccountSerializer + belongs_to :status, if: :status_type?, serializer: REST::StatusSerializer + + def status_type? + [:favourite, :reblog, :mention].include?(object.type) + end +end diff --git a/app/serializers/rest/preview_card_serializer.rb b/app/serializers/rest/preview_card_serializer.rb new file mode 100644 index 000000000..9c460332c --- /dev/null +++ b/app/serializers/rest/preview_card_serializer.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class REST::PreviewCardSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :url, :title, :description, :type, + :author_name, :author_url, :provider_name, + :provider_url, :html, :width, :height, + :image + + def image + object.image? ? full_asset_url(object.image.url(:original)) : nil + end +end diff --git a/app/serializers/rest/relationship_serializer.rb b/app/serializers/rest/relationship_serializer.rb new file mode 100644 index 000000000..1d431aa1b --- /dev/null +++ b/app/serializers/rest/relationship_serializer.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class REST::RelationshipSerializer < ActiveModel::Serializer + attributes :id, :following, :followed_by, :blocking, + :muting, :requested, :domain_blocking + + def following + instance_options[:relationships].following[object.id] || false + end + + def followed_by + instance_options[:relationships].followed_by[object.id] || false + end + + def blocking + instance_options[:relationships].blocking[object.id] || false + end + + def muting + instance_options[:relationships].muting[object.id] || false + end + + def requested + instance_options[:relationships].requested[object.id] || false + end + + def domain_blocking + instance_options[:relationships].domain_blocking[object.id] || false + end +end diff --git a/app/serializers/rest/report_serializer.rb b/app/serializers/rest/report_serializer.rb new file mode 100644 index 000000000..0c6bd6556 --- /dev/null +++ b/app/serializers/rest/report_serializer.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class REST::ReportSerializer < ActiveModel::Serializer + attributes :id, :action_taken +end diff --git a/app/serializers/rest/search_serializer.rb b/app/serializers/rest/search_serializer.rb new file mode 100644 index 000000000..157f543ae --- /dev/null +++ b/app/serializers/rest/search_serializer.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class REST::SearchSerializer < ActiveModel::Serializer + attributes :hashtags + + has_many :accounts, serializer: REST::AccountSerializer + has_many :statuses, serializer: REST::StatusSerializer + + def hashtags + object.hashtags.map(&:name) + end +end diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb new file mode 100644 index 000000000..246b12a90 --- /dev/null +++ b/app/serializers/rest/status_serializer.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +class REST::StatusSerializer < ActiveModel::Serializer + attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id, + :sensitive, :spoiler_text, :visibility, :language, + :uri, :content, :url, :reblogs_count, :favourites_count + + attribute :favourited, if: :current_user? + attribute :reblogged, if: :current_user? + attribute :muted, if: :current_user? + + belongs_to :reblog, serializer: REST::StatusSerializer + belongs_to :application + belongs_to :account, serializer: REST::AccountSerializer + + has_many :media_attachments, serializer: REST::MediaAttachmentSerializer + has_many :mentions + has_many :tags + + def current_user? + !current_user.nil? + end + + def uri + TagManager.instance.uri_for(object) + end + + def content + Formatter.instance.format(object) + end + + def url + TagManager.instance.url_for(object) + end + + def favourited + if instance_options && instance_options[:relationships] + instance_options[:relationships].favourites_map[object.id] || false + else + current_user.account.favourited?(object) + end + end + + def reblogged + if instance_options && instance_options[:relationships] + instance_options[:relationships].reblogs_map[object.id] || false + else + current_user.account.reblogged?(object) + end + end + + def muted + if instance_options && instance_options[:relationships] + instance_options[:relationships].mutes_map[object.conversation_id] || false + else + current_user.account.muting_conversation?(object.conversation) + end + end + + class ApplicationSerializer < ActiveModel::Serializer + attributes :name, :website + end + + class MentionSerializer < ActiveModel::Serializer + attributes :id, :username, :url, :acct + + def id + object.account_id + end + + def username + object.account_username + end + + def url + TagManager.instance.url_for(object.account) + end + + def acct + object.account_acct + end + end + + class TagSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :name, :url + + def url + tag_url(object) + end + end +end diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb index 3b74696d5..47a47a735 100644 --- a/app/services/fan_out_on_write_service.rb +++ b/app/services/fan_out_on_write_service.rb @@ -54,7 +54,7 @@ class FanOutOnWriteService < BaseService end def render_anonymous_payload(status) - @payload = InlineRenderer.render(status, nil, 'api/v1/statuses/show') + @payload = InlineRenderer.render(status, nil, :status) @payload = Oj.dump(event: :update, payload: @payload) end diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb index 422d5f97e..407d385ea 100644 --- a/app/services/notify_service.rb +++ b/app/services/notify_service.rb @@ -60,7 +60,7 @@ class NotifyService < BaseService def create_notification @notification.save! return unless @notification.browserable? - Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, 'api/v1/notifications/show'))) + Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification))) end def send_email diff --git a/app/views/api/v1/accounts/index.rabl b/app/views/api/v1/accounts/index.rabl deleted file mode 100644 index 9f3b13a53..000000000 --- a/app/views/api/v1/accounts/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends 'api/v1/accounts/show' diff --git a/app/views/api/v1/accounts/relationship.rabl b/app/views/api/v1/accounts/relationship.rabl deleted file mode 100644 index 4f7763d9d..000000000 --- a/app/views/api/v1/accounts/relationship.rabl +++ /dev/null @@ -1,9 +0,0 @@ -object @account - -attribute :id -node(:following) { |account| @following[account.id] || false } -node(:followed_by) { |account| @followed_by[account.id] || false } -node(:blocking) { |account| @blocking[account.id] || false } -node(:muting) { |account| @muting[account.id] || false } -node(:requested) { |account| @requested[account.id] || false } -node(:domain_blocking) { |account| @domain_blocking[account.id] || false } diff --git a/app/views/api/v1/accounts/relationships/index.rabl b/app/views/api/v1/accounts/relationships/index.rabl deleted file mode 100644 index 022ea2ac4..000000000 --- a/app/views/api/v1/accounts/relationships/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends 'api/v1/accounts/relationship' diff --git a/app/views/api/v1/accounts/show.rabl b/app/views/api/v1/accounts/show.rabl deleted file mode 100644 index 8826aa22d..000000000 --- a/app/views/api/v1/accounts/show.rabl +++ /dev/null @@ -1,12 +0,0 @@ -object @account - -attributes :id, :username, :acct, :display_name, :locked, :created_at - -node(:note) { |account| Formatter.instance.simplified_format(account) } -node(:url) { |account| TagManager.instance.url_for(account) } -node(:avatar) { |account| full_asset_url(account.avatar_original_url) } -node(:avatar_static) { |account| full_asset_url(account.avatar_static_url) } -node(:header) { |account| full_asset_url(account.header_original_url) } -node(:header_static) { |account| full_asset_url(account.header_static_url) } - -attributes :followers_count, :following_count, :statuses_count diff --git a/app/views/api/v1/accounts/statuses/index.rabl b/app/views/api/v1/accounts/statuses/index.rabl deleted file mode 100644 index 44d29d91b..000000000 --- a/app/views/api/v1/accounts/statuses/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @statuses -extends 'api/v1/statuses/show' diff --git a/app/views/api/v1/apps/create.rabl b/app/views/api/v1/apps/create.rabl deleted file mode 100644 index 1ff6469a4..000000000 --- a/app/views/api/v1/apps/create.rabl +++ /dev/null @@ -1,4 +0,0 @@ -object @app -attributes :id, :redirect_uri -node(:client_id) { |app| app.uid } -node(:client_secret) { |app| app.secret } diff --git a/app/views/api/v1/apps/show.rabl b/app/views/api/v1/apps/show.rabl deleted file mode 100644 index 6d9e607db..000000000 --- a/app/views/api/v1/apps/show.rabl +++ /dev/null @@ -1,3 +0,0 @@ -object @application - -attributes :name, :website diff --git a/app/views/api/v1/blocks/index.rabl b/app/views/api/v1/blocks/index.rabl deleted file mode 100644 index 9f3b13a53..000000000 --- a/app/views/api/v1/blocks/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends 'api/v1/accounts/show' diff --git a/app/views/api/v1/favourites/index.rabl b/app/views/api/v1/favourites/index.rabl deleted file mode 100644 index 44d29d91b..000000000 --- a/app/views/api/v1/favourites/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @statuses -extends 'api/v1/statuses/show' diff --git a/app/views/api/v1/follow_requests/index.rabl b/app/views/api/v1/follow_requests/index.rabl deleted file mode 100644 index 9f3b13a53..000000000 --- a/app/views/api/v1/follow_requests/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends 'api/v1/accounts/show' diff --git a/app/views/api/v1/follows/show.rabl b/app/views/api/v1/follows/show.rabl deleted file mode 100644 index e07106164..000000000 --- a/app/views/api/v1/follows/show.rabl +++ /dev/null @@ -1,2 +0,0 @@ -object @account -extends('api/v1/accounts/show') diff --git a/app/views/api/v1/instances/show.rabl b/app/views/api/v1/instances/show.rabl deleted file mode 100644 index 05fb65031..000000000 --- a/app/views/api/v1/instances/show.rabl +++ /dev/null @@ -1,10 +0,0 @@ -object false - -node(:uri) { site_hostname } -node(:title) { Setting.site_title } -node(:description) { Setting.site_description } -node(:email) { Setting.site_contact_email } -node(:version) { Mastodon::Version.to_s } -node :urls do - { :streaming_api => Rails.configuration.x.streaming_api_base_url } -end diff --git a/app/views/api/v1/media/create.rabl b/app/views/api/v1/media/create.rabl deleted file mode 100644 index 53c13bbda..000000000 --- a/app/views/api/v1/media/create.rabl +++ /dev/null @@ -1,7 +0,0 @@ -object @media -attribute :id, :type - -node(:url) { |media| full_asset_url(media.file.url(:original)) } -node(:preview_url) { |media| full_asset_url(media.file.url(:small)) } -node(:text_url) { |media| medium_url(media) } -node(:meta) { |media| media.file.meta } diff --git a/app/views/api/v1/mutes/index.rabl b/app/views/api/v1/mutes/index.rabl deleted file mode 100644 index 9f3b13a53..000000000 --- a/app/views/api/v1/mutes/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends 'api/v1/accounts/show' diff --git a/app/views/api/v1/notifications/index.rabl b/app/views/api/v1/notifications/index.rabl deleted file mode 100644 index 6abc3da36..000000000 --- a/app/views/api/v1/notifications/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @notifications -extends 'api/v1/notifications/show' diff --git a/app/views/api/v1/notifications/show.rabl b/app/views/api/v1/notifications/show.rabl deleted file mode 100644 index ca34f2d5d..000000000 --- a/app/views/api/v1/notifications/show.rabl +++ /dev/null @@ -1,11 +0,0 @@ -object @notification - -attributes :id, :type, :created_at - -child from_account: :account do - extends 'api/v1/accounts/show' -end - -node(:status, if: lambda { |n| [:favourite, :reblog, :mention].include?(n.type) }) do |n| - partial 'api/v1/statuses/show', object: n.target_status -end diff --git a/app/views/api/v1/reports/index.rabl b/app/views/api/v1/reports/index.rabl deleted file mode 100644 index 4f0794027..000000000 --- a/app/views/api/v1/reports/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @reports -extends 'api/v1/reports/show' diff --git a/app/views/api/v1/reports/show.rabl b/app/views/api/v1/reports/show.rabl deleted file mode 100644 index 006db51e3..000000000 --- a/app/views/api/v1/reports/show.rabl +++ /dev/null @@ -1,2 +0,0 @@ -object @report -attributes :id, :action_taken diff --git a/app/views/api/v1/search/index.rabl b/app/views/api/v1/search/index.rabl deleted file mode 100644 index 8d1640f2d..000000000 --- a/app/views/api/v1/search/index.rabl +++ /dev/null @@ -1,13 +0,0 @@ -object @search - -child :accounts, object_root: false do - extends 'api/v1/accounts/show' -end - -node(:hashtags) do |search| - search.hashtags.map(&:name) -end - -child :statuses, object_root: false do - extends 'api/v1/statuses/show' -end diff --git a/app/views/api/v1/statuses/_media.rabl b/app/views/api/v1/statuses/_media.rabl deleted file mode 100644 index 07ac31888..000000000 --- a/app/views/api/v1/statuses/_media.rabl +++ /dev/null @@ -1,6 +0,0 @@ -attributes :id, :remote_url, :type - -node(:url) { |media| full_asset_url(media.file.url(:original)) } -node(:preview_url) { |media| full_asset_url(media.file.url(:small)) } -node(:text_url) { |media| media.local? ? medium_url(media) : nil } -node(:meta) { |media| media.file.meta } diff --git a/app/views/api/v1/statuses/_mention.rabl b/app/views/api/v1/statuses/_mention.rabl deleted file mode 100644 index 8c95fc9bd..000000000 --- a/app/views/api/v1/statuses/_mention.rabl +++ /dev/null @@ -1,4 +0,0 @@ -node(:url) { |mention| TagManager.instance.url_for(mention.account) } -node(:acct) { |mention| mention.account_acct } -node(:id) { |mention| mention.account_id } -node(:username) { |mention| mention.account_username } diff --git a/app/views/api/v1/statuses/_show.rabl b/app/views/api/v1/statuses/_show.rabl deleted file mode 100644 index fe3ec89ab..000000000 --- a/app/views/api/v1/statuses/_show.rabl +++ /dev/null @@ -1,29 +0,0 @@ -attributes :id, :created_at, :in_reply_to_id, - :in_reply_to_account_id, :sensitive, - :spoiler_text, :visibility, :language - -node(:uri) { |status| TagManager.instance.uri_for(status) } -node(:content) { |status| Formatter.instance.format(status) } -node(:url) { |status| TagManager.instance.url_for(status) } -node(:reblogs_count) { |status| defined?(@reblogs_counts_map) ? (@reblogs_counts_map[status.id] || 0) : status.reblogs_count } -node(:favourites_count) { |status| defined?(@favourites_counts_map) ? (@favourites_counts_map[status.id] || 0) : status.favourites_count } - -child :application do - extends 'api/v1/apps/show' -end - -child :account do - extends 'api/v1/accounts/show' -end - -child :media_attachments, object_root: false do - extends 'api/v1/statuses/_media' -end - -child :mentions, object_root: false do - extends 'api/v1/statuses/_mention' -end - -child :tags, object_root: false do - extends 'api/v1/statuses/_tags' -end diff --git a/app/views/api/v1/statuses/_tags.rabl b/app/views/api/v1/statuses/_tags.rabl deleted file mode 100644 index 25e7b0fac..000000000 --- a/app/views/api/v1/statuses/_tags.rabl +++ /dev/null @@ -1,2 +0,0 @@ -attribute :name -node(:url) { |tag| tag_url(tag) } diff --git a/app/views/api/v1/statuses/accounts.rabl b/app/views/api/v1/statuses/accounts.rabl deleted file mode 100644 index 9f3b13a53..000000000 --- a/app/views/api/v1/statuses/accounts.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @accounts -extends 'api/v1/accounts/show' diff --git a/app/views/api/v1/statuses/card.rabl b/app/views/api/v1/statuses/card.rabl deleted file mode 100644 index 5d8d7af3b..000000000 --- a/app/views/api/v1/statuses/card.rabl +++ /dev/null @@ -1,7 +0,0 @@ -object @card - -attributes :url, :title, :description, :type, - :author_name, :author_url, :provider_name, - :provider_url, :html, :width, :height - -node(:image) { |card| card.image? ? full_asset_url(card.image.url(:original)) : nil } diff --git a/app/views/api/v1/statuses/context.rabl b/app/views/api/v1/statuses/context.rabl deleted file mode 100644 index 0b62f26d5..000000000 --- a/app/views/api/v1/statuses/context.rabl +++ /dev/null @@ -1,9 +0,0 @@ -object @context - -node :ancestors do |context| - partial 'api/v1/statuses/index', object: context.ancestors -end - -node :descendants do |context| - partial 'api/v1/statuses/index', object: context.descendants -end diff --git a/app/views/api/v1/statuses/index.rabl b/app/views/api/v1/statuses/index.rabl deleted file mode 100644 index 0a0ed13c5..000000000 --- a/app/views/api/v1/statuses/index.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @statuses -extends('api/v1/statuses/show') diff --git a/app/views/api/v1/statuses/show.rabl b/app/views/api/v1/statuses/show.rabl deleted file mode 100644 index 4b33fb2c3..000000000 --- a/app/views/api/v1/statuses/show.rabl +++ /dev/null @@ -1,15 +0,0 @@ -object @status - -extends 'api/v1/statuses/_show' - -node(:favourited, if: proc { !current_account.nil? }) { |status| defined?(@favourites_map) ? @favourites_map[status.id] : current_account.favourited?(status) } -node(:reblogged, if: proc { !current_account.nil? }) { |status| defined?(@reblogs_map) ? @reblogs_map[status.id] : current_account.reblogged?(status) } -node(:muted, if: proc { !current_account.nil? }) { |status| defined?(@mutes_map) ? @mutes_map[status.conversation_id] : current_account.muting_conversation?(status.conversation) } - -child reblog: :reblog do - extends 'api/v1/statuses/_show' - - node(:favourited, if: proc { !current_account.nil? }) { |status| defined?(@favourites_map) ? @favourites_map[status.id] : current_account.favourited?(status) } - node(:reblogged, if: proc { !current_account.nil? }) { |status| defined?(@reblogs_map) ? @reblogs_map[status.id] : current_account.reblogged?(status) } - node(:muted, if: proc { !current_account.nil? }) { false } -end diff --git a/app/views/api/v1/timelines/show.rabl b/app/views/api/v1/timelines/show.rabl deleted file mode 100644 index 0a0ed13c5..000000000 --- a/app/views/api/v1/timelines/show.rabl +++ /dev/null @@ -1,2 +0,0 @@ -collection @statuses -extends('api/v1/statuses/show') diff --git a/app/views/home/initial_state.json.rabl b/app/views/home/initial_state.json.rabl index 291ff806b..c428a5a1f 100644 --- a/app/views/home/initial_state.json.rabl +++ b/app/views/home/initial_state.json.rabl @@ -24,8 +24,8 @@ end node(:accounts) do store = {} - store[current_account.id] = partial('api/v1/accounts/show', object: current_account) - store[@admin.id] = partial('api/v1/accounts/show', object: @admin) unless @admin.nil? + store[current_account.id] = ActiveModelSerializers::SerializableResource.new(current_account, serializer: REST::AccountSerializer) + store[@admin.id] = ActiveModelSerializers::SerializableResource.new(@admin, serializer: REST::AccountSerializer) unless @admin.nil? store end diff --git a/app/workers/push_update_worker.rb b/app/workers/push_update_worker.rb index fbcdcf634..697cbd6a6 100644 --- a/app/workers/push_update_worker.rb +++ b/app/workers/push_update_worker.rb @@ -6,7 +6,7 @@ class PushUpdateWorker def perform(account_id, status_id) account = Account.find(account_id) status = Status.find(status_id) - message = InlineRenderer.render(status, account, 'api/v1/statuses/show') + message = InlineRenderer.render(status, account, :status) Redis.current.publish("timeline:#{account.id}", Oj.dump(event: :update, payload: message, queued_at: (Time.now.to_f * 1000.0).to_i)) rescue ActiveRecord::RecordNotFound diff --git a/spec/lib/inline_rabl_scope_spec.rb b/spec/lib/inline_rabl_scope_spec.rb deleted file mode 100644 index 3fff176e4..000000000 --- a/spec/lib/inline_rabl_scope_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -describe InlineRablScope do - describe '#current_account' do - it 'returns the given account' do - account = Fabricate(:account) - expect(InlineRablScope.new(account).current_account).to eq account - end - end - - describe '#current_user' do - it 'returns nil if the given account is nil' do - expect(InlineRablScope.new(nil).current_user).to eq nil - end - - it 'returns user of account if the given account is not nil' do - user = Fabricate(:user) - expect(InlineRablScope.new(user.account).current_user).to eq user - end - end -end -- cgit From 1c1819a78a33cb7a90b499676c587f3c6dd7406f Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 7 Jul 2017 04:31:40 +0200 Subject: Fix feed author not being enforced in ProcessFeedService (#4092) Ensure the only allowed author of top-level entries in feed is the person the feed belongs to (a verified user). Ensure delete events only apply if the deleted item belonged to that user. --- app/services/process_feed_service.rb | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) (limited to 'app/services') diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb index fbdf92caa..028962d5b 100644 --- a/app/services/process_feed_service.rb +++ b/app/services/process_feed_service.rb @@ -42,7 +42,7 @@ class ProcessFeedService < BaseService private def create_status - if redis.exists("delete_upon_arrival:#{id}") + if redis.exists("delete_upon_arrival:#{@account.id}:#{id}") Rails.logger.debug "Delete for status #{id} was queued, ignoring" return end @@ -99,15 +99,13 @@ class ProcessFeedService < BaseService def delete_status Rails.logger.debug "Deleting remote status #{id}" - status = Status.find_by(uri: id) + status = Status.find_by(uri: id, account: @account) if status.nil? - redis.setex("delete_upon_arrival:#{id}", 6 * 3_600, id) + redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) else RemoveStatusService.new.call(status) end - - nil end def skip_unsupported_type? @@ -128,18 +126,7 @@ class ProcessFeedService < BaseService return [status, false] unless status.nil? - # If status embeds an author, find that author - # If that author cannot be found, don't record the status (do not misattribute) - if account?(entry) - begin - account = author_from_xml(entry) - return [nil, false] if account.nil? - rescue Goldfinger::Error - return [nil, false] - end - else - account = @account - end + account = @account return [nil, false] if account.suspended? -- cgit From 007ab330e6ffb1e07995d4e306473d457043e2eb Mon Sep 17 00:00:00 2001 From: nullkal Date: Sun, 9 Jul 2017 05:44:31 +0900 Subject: Use charlock_holmes instead of nkf at FetchLinkCardService (#4080) * Specs for language detection * Use CharlockHolmes instead of NKF * Correct mistakes * Correct style * Set hint_enc instead of falling back and strip_tags * Improve specs * Add dependencies --- .travis.yml | 1 + Aptfile | 1 + Dockerfile | 1 + Gemfile | 1 + Gemfile.lock | 2 ++ Vagrantfile | 1 + app/services/fetch_link_card_service.rb | 8 ++++++-- spec/fixtures/requests/koi8-r.txt | 20 +++++++++++++++++++ spec/fixtures/requests/sjis.txt | 4 ++-- spec/fixtures/requests/sjis_with_wrong_charset.txt | 20 +++++++++++++++++++ spec/services/fetch_link_card_service_spec.rb | 23 ++++++++++++++++++++++ 11 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/requests/koi8-r.txt create mode 100644 spec/fixtures/requests/sjis_with_wrong_charset.txt (limited to 'app/services') diff --git a/.travis.yml b/.travis.yml index 4bb332666..4d4dc0893 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,7 @@ addons: - g++-6 - libprotobuf-dev - protobuf-compiler + - libicu-dev rvm: - 2.3.4 diff --git a/Aptfile b/Aptfile index 0456343ef..3af0956e3 100644 --- a/Aptfile +++ b/Aptfile @@ -3,3 +3,4 @@ libprotobuf-dev ffmpeg libxdamage1 libxfixes3 +libicu-dev diff --git a/Dockerfile b/Dockerfile index 7033cddd4..97a691393 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,6 +25,7 @@ RUN echo "@edge https://nl.alpinelinux.org/alpine/edge/main" >> /etc/apk/reposit ffmpeg \ file \ git \ + icu-dev \ imagemagick@edge \ libpq \ libxml2 \ diff --git a/Gemfile b/Gemfile index 95c74eef9..b52685cba 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,7 @@ gem 'active_model_serializers', '~> 0.10' gem 'addressable', '~> 2.5' gem 'bootsnap' gem 'browser' +gem 'charlock_holmes', '~> 0.7.3' gem 'cld3', '~> 3.1' gem 'devise', '~> 4.2' gem 'devise-two-factor', '~> 3.0' diff --git a/Gemfile.lock b/Gemfile.lock index 71f83f736..de0d6a107 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -106,6 +106,7 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + charlock_holmes (0.7.3) case_transform (0.2) activesupport chunky_png (1.3.8) @@ -501,6 +502,7 @@ DEPENDENCIES capistrano-rbenv (~> 2.1) capistrano-yarn (~> 2.0) capybara (~> 2.14) + charlock_holmes (~> 0.7.3) cld3 (~> 3.1) climate_control (~> 0.2) devise (~> 4.2) diff --git a/Vagrantfile b/Vagrantfile index 1f56fcfb3..cbe6623b3 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -37,6 +37,7 @@ sudo apt-get install \ yarn \ libprotobuf-dev \ libreadline-dev \ + libicu-dev \ -y # Install rvm diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 8ddaa2bf4..6ef3abb66 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true -require 'nkf' class FetchLinkCardService < BaseService include HttpHelper @@ -86,7 +85,12 @@ class FetchLinkCardService < BaseService return if response.code != 200 || response.mime_type != 'text/html' html = response.to_s - page = Nokogiri::HTML(html, nil, NKF.guess(html).to_s) + + detector = CharlockHolmes::EncodingDetector.new + detector.strip_tags = true + + guess = detector.detect(html, response.charset) + page = Nokogiri::HTML(html, nil, guess&.fetch(:encoding)) card.type = :link card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content diff --git a/spec/fixtures/requests/koi8-r.txt b/spec/fixtures/requests/koi8-r.txt new file mode 100644 index 000000000..d4242af01 --- /dev/null +++ b/spec/fixtures/requests/koi8-r.txt @@ -0,0 +1,20 @@ +HTTP/1.1 200 OK +Server: nginx/1.11.10 +Date: Tue, 04 Jul 2017 16:43:39 GMT +Content-Type: text/html +Content-Length: 273 +Connection: keep-alive +Last-Modified: Tue, 04 Jul 2017 16:41:34 GMT +Accept-Ranges: bytes + + + + + + マモヒマラム ホヂノホチナヤ゚ ヤマフリヒマ ラ゚ XVI モヤ. ミメノラフナヒチヤリ ラホノヘチホナ ノホマモヤメチホテナラ゚. + + +

マモヒマラム ホヂノホチナヤ゚ ヤマフリヒマ ラ゚ XVI モヤ. ミメノラフナヒチヤリ ラホノヘチホナ ノホマモヤメチホテナラ゚.
+

+ + diff --git a/spec/fixtures/requests/sjis.txt b/spec/fixtures/requests/sjis.txt index 9041aa25d..faf18d35c 100644 --- a/spec/fixtures/requests/sjis.txt +++ b/spec/fixtures/requests/sjis.txt @@ -11,10 +11,10 @@ Accept-Ranges: bytes - JSISのページ + SJISのページ -

SJISのページ
+

私も同年ましていわゆる記念人ってものの時でしありです。もし時間に意味者は正しくどんな発会ませだまでが申し上げがいらっしゃるたには参考帰るたいだから、少しにもやっあっましなた。金からいうないのはどうも九月をできるだけたたくた。けっして岡田さんに反抗幸少し徴に云おでしょ金力こうした権力あなたか指図がというお出入りなくだろなありて、その昔は私か金力陰を怒らから、久原さんのものをがたのいつがしかるにご希望と向いばそれmanにご矛盾へ参りように同時にご演説がしでならので、多分もし表裏に変ったてくれです事で考えたた。しかも例えばごがたがとどまらものも実際むやみとありですて、この自分では申しんてとして世間に並べのに行かなかっな。


diff --git a/spec/fixtures/requests/sjis_with_wrong_charset.txt b/spec/fixtures/requests/sjis_with_wrong_charset.txt new file mode 100644 index 000000000..456750c6b --- /dev/null +++ b/spec/fixtures/requests/sjis_with_wrong_charset.txt @@ -0,0 +1,20 @@ +HTTP/1.1 200 OK +Server: nginx/1.11.10 +Date: Tue, 04 Jul 2017 16:43:39 GMT +Content-Type: text/html; charset=utf-8 +Content-Length: 273 +Connection: keep-alive +Last-Modified: Tue, 04 Jul 2017 16:41:34 GMT +Accept-Ranges: bytes + + + + + + SJISのページ + + +

私も同年ましていわゆる記念人ってものの時でしありです。もし時間に意味者は正しくどんな発会ませだまでが申し上げがいらっしゃるたには参考帰るたいだから、少しにもやっあっましなた。金からいうないのはどうも九月をできるだけたたくた。けっして岡田さんに反抗幸少し徴に云おでしょ金力こうした権力あなたか指図がというお出入りなくだろなありて、その昔は私か金力陰を怒らから、久原さんのものをがたのいつがしかるにご希望と向いばそれmanにご矛盾へ参りように同時にご演説がしでならので、多分もし表裏に変ったてくれです事で考えたた。しかも例えばごがたがとどまらものも実際むやみとありですて、この自分では申しんてとして世間に並べのに行かなかっな。
+

+ + diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 7d7f8e748..698eb0324 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -8,6 +8,10 @@ RSpec.describe FetchLinkCardService do stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt')) stub_request(:head, 'http://example.com/sjis').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt')) + stub_request(:head, 'http://example.com/sjis_with_wrong_charset').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) + stub_request(:get, 'http://example.com/sjis_with_wrong_charset').to_return(request_fixture('sjis_with_wrong_charset.txt')) + stub_request(:head, 'http://example.com/koi8-r').to_return(status: 200, headers: { 'Content-Type' => 'text/html' }) + stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt')) stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404) subject.call(status) @@ -27,6 +31,25 @@ RSpec.describe FetchLinkCardService do it 'works with SJIS' do expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once + expect(status.preview_card.title).to eq("SJIS縺ョ繝壹シ繧ク") + end + end + + context do + let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis_with_wrong_charset') } + + it 'works with SJIS even with wrong charset header' do + expect(a_request(:get, 'http://example.com/sjis_with_wrong_charset')).to have_been_made.at_least_once + expect(status.preview_card.title).to eq("SJIS縺ョ繝壹シ繧ク") + end + end + + context do + let(:status) { Fabricate(:status, text: 'Check out http://example.com/koi8-r') } + + it 'works with koi8-r' do + expect(a_request(:get, 'http://example.com/koi8-r')).to have_been_made.at_least_once + expect(status.preview_card.title).to eq("ミ慴セムミコミセミイム ミスミームミクミスミーミオムび ムひセミサム糊コミセ ミイム XVI ムム. ミソムミクミイミサミオミコミームび ミイミスミクミシミーミスミオ ミクミスミセムムびミーミスムミオミイム.") end end end -- cgit From 307f3e0dd77b338669648f830f7f74103d2d226f Mon Sep 17 00:00:00 2001 From: abcang Date: Mon, 10 Jul 2017 00:33:21 +0900 Subject: Rescue exceptions related to Goldfinger (#4044) * Rescue exceptions related to Goldfinger * Exclude Goldfinger::SSLError --- app/services/fetch_remote_account_service.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app/services') diff --git a/app/services/fetch_remote_account_service.rb b/app/services/fetch_remote_account_service.rb index 8eed0d454..1efac365b 100644 --- a/app/services/fetch_remote_account_service.rb +++ b/app/services/fetch_remote_account_service.rb @@ -32,5 +32,8 @@ class FetchRemoteAccountService < BaseService rescue Nokogiri::XML::XPath::SyntaxError Rails.logger.debug 'Invalid XML or missing namespace' nil + rescue Goldfinger::NotFoundError, Goldfinger::Error + Rails.logger.debug 'Exceptions related to Goldfinger occurs' + nil end end -- cgit From 617208053c2bf935d2dd3944bb2b9192a388f0b4 Mon Sep 17 00:00:00 2001 From: abcang Date: Mon, 10 Jul 2017 20:59:29 +0900 Subject: Rescue exceptions related to Goldfinger at FetchRemoteStatusService (#4138) --- app/services/fetch_remote_status_service.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app/services') diff --git a/app/services/fetch_remote_status_service.rb b/app/services/fetch_remote_status_service.rb index f414813ad..4cfd33d90 100644 --- a/app/services/fetch_remote_status_service.rb +++ b/app/services/fetch_remote_status_service.rb @@ -33,6 +33,9 @@ class FetchRemoteStatusService < BaseService rescue Nokogiri::XML::XPath::SyntaxError Rails.logger.debug 'Invalid XML or missing namespace' nil + rescue Goldfinger::NotFoundError, Goldfinger::Error + Rails.logger.debug 'Exceptions related to Goldfinger occurs' + nil end def confirmed_domain?(domain, account) -- cgit From 7bacdd718a143f54f47ddc3afa39504636be65c0 Mon Sep 17 00:00:00 2001 From: "Akihiko Odaki (@fn_aki@pawoo.net)" Date: Tue, 11 Jul 2017 08:00:01 +0900 Subject: Fix PrecomputeFeedService for filtered statuses (#4148) --- app/services/precompute_feed_service.rb | 4 ++-- spec/services/precompute_feed_service_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'app/services') diff --git a/app/services/precompute_feed_service.rb b/app/services/precompute_feed_service.rb index a32ba1dae..85635a008 100644 --- a/app/services/precompute_feed_service.rb +++ b/app/services/precompute_feed_service.rb @@ -13,7 +13,7 @@ class PrecomputeFeedService < BaseService attr_reader :account def populate_feed - pairs = statuses.reverse_each.map(&method(:process_status)) + pairs = statuses.reverse_each.lazy.reject(&method(:status_filtered?)).map(&method(:process_status)).to_a redis.pipelined do redis.zadd(account_home_key, pairs) if pairs.any? @@ -22,7 +22,7 @@ class PrecomputeFeedService < BaseService end def process_status(status) - [status.id, status.reblog? ? status.reblog_of_id : status.id] unless status_filtered?(status) + [status.id, status.reblog? ? status.reblog_of_id : status.id] end def status_filtered?(status) diff --git a/spec/services/precompute_feed_service_spec.rb b/spec/services/precompute_feed_service_spec.rb index e2294469c..dbd08ac1b 100644 --- a/spec/services/precompute_feed_service_spec.rb +++ b/spec/services/precompute_feed_service_spec.rb @@ -23,5 +23,17 @@ RSpec.describe PrecomputeFeedService do account = Fabricate(:account) subject.call(account) end + + it 'filters statuses' do + account = Fabricate(:account) + muted_account = Fabricate(:account) + Fabricate(:mute, account: account, target_account: muted_account) + reblog = Fabricate(:status, account: muted_account) + status = Fabricate(:status, account: account, reblog: reblog) + + subject.call(account) + + expect(Redis.current.zscore(FeedManager.instance.key(:home, account.id), reblog.id)).to eq nil + end end end -- cgit From 29f314a50209f64b551be7fae2744234bcca2627 Mon Sep 17 00:00:00 2001 From: "Akihiko Odaki (@fn_aki@pawoo.net)" Date: Tue, 11 Jul 2017 18:55:48 +0900 Subject: Remove redundant inclusion (#4150) --- app/services/process_feed_service.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/services') diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb index 028962d5b..c335d2159 100644 --- a/app/services/process_feed_service.rb +++ b/app/services/process_feed_service.rb @@ -20,8 +20,6 @@ class ProcessFeedService < BaseService end class ProcessEntry - include AuthorExtractor - def call(xml, account) @account = account @xml = xml -- cgit From 425acecfdb15093a265b191120fb2d4e4c4135c4 Mon Sep 17 00:00:00 2001 From: "Akihiko Odaki (@fn_aki@pawoo.net)" Date: Tue, 11 Jul 2017 20:37:05 +0900 Subject: Wrap methods of ProcessFeedService::ProcessEntry in classes (#4151) ProcessFeedService::ProcessEntry had many methods, so wrap them in classes representing activities. --- app/services/process_feed_service.rb | 393 ++++++++++++++++++----------------- 1 file changed, 205 insertions(+), 188 deletions(-) (limited to 'app/services') diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb index c335d2159..19ab0c858 100644 --- a/app/services/process_feed_service.rb +++ b/app/services/process_feed_service.rb @@ -22,268 +22,285 @@ class ProcessFeedService < BaseService 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 + @fetched = Activity.new(xml) + + return unless [:activity, :note, :comment].include?(@fetched.type) + + klass = case @fetched.verb + when :post + PostActivity + when :share + ShareActivity + when :delete + DeletionActivity + else + return + end + + @fetched = klass.new(xml, account) + @fetched.perform 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 + class Activity + def initialize(xml, account = nil) + @xml = xml + @account = account end - status, just_created = nil + def verb + raw = @xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content + TagManager::VERBS.key(raw) + rescue + :post + end - Rails.logger.debug "Creating remote status #{id}" + def type + raw = @xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content + TagManager::TYPES.key(raw) + rescue + :activity + end - if verb == :share - original_status = shared_status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS)) - return nil if original_status.nil? + def id + @xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content end - ApplicationRecord.transaction do - status, just_created = status_from_xml(@xml) + def url + link = @xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) + link.nil? ? nil : link['href'] + end - return if status.nil? - return status unless just_created + private - if verb == :share - status.reblog = original_status.reblog? ? original_status.reblog : original_status + 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.save! + Status.find_by(uri: uri) 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) + def redis + Redis.current end + end - notify_about_mentions!(status) unless status.reblog? - notify_about_reblog!(status) if status.reblog? && status.reblog.account.local? + class CreationActivity < Activity + def perform + if redis.exists("delete_upon_arrival:#{@account.id}:#{id}") + Rails.logger.debug "Delete for status #{id} was queued, ignoring" + return + end - Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution" + 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? + + return [nil, false] if @account.suspended? + + 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: converstation_to_persistent&.first, + 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 - LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text? - DistributionWorker.perform_async(status.id) + Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution" - status - end + LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text? + DistributionWorker.perform_async(status.id) - 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) + [status, true] end - end - def notify_about_reblog!(status) - NotifyService.new.call(status.reblog.account, status) - end + def content + @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content + end - def delete_status - Rails.logger.debug "Deleting remote status #{id}" - status = Status.find_by(uri: id, account: @account) + def content_language + @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' + end - if status.nil? - redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) - else - RemoveStatusService.new.call(status) + def content_warning + @xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || '' end - end - def skip_unsupported_type? - !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type)) - end + def visibility_scope + @xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public + end - def shared_status_from_xml(entry) - status = find_status(id(entry)) + def published + @xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content + end - return status unless status.nil? + def thread? + !@xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil? + end - FetchRemoteStatusService.new.call(url(entry)) - end + def thread + thr = @xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS) + [thr['ref'], thr['href']] + end - def status_from_xml(entry) - # Return early if status already exists in db - status = find_status(id(entry)) + private - return [status, false] unless status.nil? + def converstation_to_persistent + uri = @xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content + return if uri.nil? - account = @account + 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 - return [nil, false] if account.suspended? + found = Conversation.find_by(uri: uri) + found ? [found, false] : [Conversation.create!(uri: uri), true] + end - 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 - ) + def save_mentions(parent) + processed_account_ids = [] - mentions_from_xml(status, entry) - hashtags_from_xml(status, entry) - media_from_xml(status, entry) + @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'] - [status, true] - end + mentioned_account = account_from_href(link['href']) - def find_or_create_conversation(xml) - uri = xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content - return if uri.nil? + next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id) - 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 + mentioned_account.mentions.where(status: parent).first_or_create(status: parent) - Conversation.find_by(uri: uri) || Conversation.create!(uri: uri) - end + # So we can skip duplicate mentions + processed_account_ids << mentioned_account.id + end + 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) + def save_hashtags(parent) + tags = @xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?) + ProcessHashtagsService.new.call(parent, tags) end - Status.find_by(uri: uri) - end + def save_media(parent) + do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? - def mentions_from_xml(parent, xml) - processed_account_ids = [] + @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link| + next unless link['href'] - 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'] + 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 - mentioned_account = account_from_href(link['href']) + next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? - next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id) + media.save - mentioned_account.mentions.where(status: parent).first_or_create(status: parent) + next if do_not_download - # So we can skip duplicate mentions - processed_account_ids << mentioned_account.id + begin + media.file_remote_url = link['href'] + media.save! + rescue ActiveRecord::RecordInvalid + next + end + end end - end - def account_from_href(href) - url = Addressable::URI.parse(href).normalize + 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) + 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 - 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 + class ShareActivity < CreationActivity + def perform + status, just_created = super + NotifyService.new.call(reblog.account, status) if reblog&.account&.local? && just_created + status end - end - def id(xml = @xml) - xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content - end + def object + @xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS) + end - def verb(xml = @xml) - raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content - TagManager::VERBS.key(raw) - rescue - :post - end + private - def type(xml = @xml) - raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content - TagManager::TYPES.key(raw) - rescue - :activity - end + def status + reblog && super + end - def url(xml = @xml) - link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) - link.nil? ? nil : link['href'] - end + def reblog + return @reblog if defined? @reblog - def content(xml = @xml) - xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content + original_status = RemoteActivity.new(object).perform + @reblog = original_status.reblog? ? original_status.reblog : original_status + end end - def content_language(xml = @xml) - xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' - end + class PostActivity < CreationActivity + def perform + status, just_created = super - def content_warning(xml = @xml) - xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || '' - end + 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 - def visibility_scope(xml = @xml) - xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public - end + status + end - def published(xml = @xml) - xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content - end + private - def thread?(xml = @xml) - !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil? + def reblog + nil + end end - def thread(xml = @xml) - thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS) - [thr['ref'], thr['href']] - end + class DeletionActivity < Activity + def perform + Rails.logger.debug "Deleting remote status #{id}" + status = Status.find_by(uri: id, account: @account) - def account?(xml = @xml) - !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil? + if status.nil? + redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) + else + RemoveStatusService.new.call(status) + end + end end - def redis - Redis.current + class RemoteActivity < Activity + def perform + find_status(id) || FetchRemoteStatusService.new.call(url) + end end end end -- cgit From 1c6c6b271cb361b75a33bc98c8f26d18ea9af23f Mon Sep 17 00:00:00 2001 From: nightpool Date: Tue, 11 Jul 2017 12:19:16 -0400 Subject: Revert "Wrap methods of ProcessFeedService::ProcessEntry in classes (#4151)" (#4157) This reverts commit 425acecfdb15093a265b191120fb2d4e4c4135c4. --- app/services/process_feed_service.rb | 393 +++++++++++++++++------------------ 1 file changed, 188 insertions(+), 205 deletions(-) (limited to 'app/services') diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb index 19ab0c858..c335d2159 100644 --- a/app/services/process_feed_service.rb +++ b/app/services/process_feed_service.rb @@ -22,285 +22,268 @@ class ProcessFeedService < BaseService class ProcessEntry def call(xml, account) @account = account - @fetched = Activity.new(xml) - - return unless [:activity, :note, :comment].include?(@fetched.type) - - klass = case @fetched.verb - when :post - PostActivity - when :share - ShareActivity - when :delete - DeletionActivity - else - return - end - - @fetched = klass.new(xml, account) - @fetched.perform + @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 - class Activity - def initialize(xml, account = nil) - @xml = xml - @account = account - end + private - def verb - raw = @xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content - TagManager::VERBS.key(raw) - rescue - :post + def create_status + if redis.exists("delete_upon_arrival:#{@account.id}:#{id}") + Rails.logger.debug "Delete for status #{id} was queued, ignoring" + return end - def type - raw = @xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content - TagManager::TYPES.key(raw) - rescue - :activity - end + status, just_created = nil - def id - @xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content - end + Rails.logger.debug "Creating remote status #{id}" - def url - link = @xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) - link.nil? ? nil : link['href'] + 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 - private + ApplicationRecord.transaction do + status, just_created = status_from_xml(@xml) - 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) + return if status.nil? + return status unless just_created + + if verb == :share + status.reblog = original_status.reblog? ? original_status.reblog : original_status end - Status.find_by(uri: uri) + status.save! end - def redis - Redis.current + 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 - end - class CreationActivity < Activity - def perform - if redis.exists("delete_upon_arrival:#{@account.id}:#{id}") - Rails.logger.debug "Delete for status #{id} was queued, ignoring" - return - end + notify_about_mentions!(status) unless status.reblog? + notify_about_reblog!(status) if status.reblog? && status.reblog.account.local? - 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? - - return [nil, false] if @account.suspended? - - 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: converstation_to_persistent&.first, - 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" - 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) - LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text? - DistributionWorker.perform_async(status.id) + status + end - [status, true] + 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 content - @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content - end + def notify_about_reblog!(status) + NotifyService.new.call(status.reblog.account, status) + end - def content_language - @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' - end + def delete_status + Rails.logger.debug "Deleting remote status #{id}" + status = Status.find_by(uri: id, account: @account) - 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 + if status.nil? + redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) + else + RemoveStatusService.new.call(status) end + end - def published - @xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content - end + def skip_unsupported_type? + !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type)) + end - def thread? - !@xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil? - end + def shared_status_from_xml(entry) + status = find_status(id(entry)) - def thread - thr = @xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS) - [thr['ref'], thr['href']] - end + return status unless status.nil? - private + FetchRemoteStatusService.new.call(url(entry)) + end - def converstation_to_persistent - uri = @xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content - return if uri.nil? + def status_from_xml(entry) + # Return early if status already exists in db + status = find_status(id(entry)) - 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 + return [status, false] unless status.nil? - found = Conversation.find_by(uri: uri) - found ? [found, false] : [Conversation.create!(uri: uri), true] - end + account = @account - def save_mentions(parent) - processed_account_ids = [] + return [nil, false] if account.suspended? - @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'] + 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 + ) - mentioned_account = account_from_href(link['href']) + mentions_from_xml(status, entry) + hashtags_from_xml(status, entry) + media_from_xml(status, entry) - next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id) + [status, true] + end - mentioned_account.mentions.where(status: parent).first_or_create(status: parent) + def find_or_create_conversation(xml) + uri = xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content + return if uri.nil? - # So we can skip duplicate mentions - processed_account_ids << mentioned_account.id - end + 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 - def save_hashtags(parent) - tags = @xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?) - ProcessHashtagsService.new.call(parent, tags) + 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 - def save_media(parent) - do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? + Status.find_by(uri: uri) + end - @xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link| - next unless link['href'] + def mentions_from_xml(parent, xml) + processed_account_ids = [] - 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 + 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'] - next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? + mentioned_account = account_from_href(link['href']) - media.save + next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id) - next if do_not_download + mentioned_account.mentions.where(status: parent).first_or_create(status: parent) - begin - media.file_remote_url = link['href'] - media.save! - rescue ActiveRecord::RecordInvalid - next - end - end + # 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 + 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 + 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 - class ShareActivity < CreationActivity - def perform - status, just_created = super - NotifyService.new.call(reblog.account, status) if reblog&.account&.local? && just_created - status - 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 object - @xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS) - end + def media_from_xml(parent, xml) + do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media? - private + xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link| + next unless link['href'] - def status - reblog && super - end + 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 - def reblog - return @reblog if defined? @reblog + next if do_not_download - original_status = RemoteActivity.new(object).perform - @reblog = original_status.reblog? ? original_status.reblog : original_status + begin + media.file_remote_url = link['href'] + media.save! + rescue ActiveRecord::RecordInvalid + next + end end end - class PostActivity < CreationActivity - def perform - status, just_created = super + def id(xml = @xml) + xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content + end - 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 + def verb(xml = @xml) + raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content + TagManager::VERBS.key(raw) + rescue + :post + end - status - 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 - private + def content(xml = @xml) + xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content + end - def reblog - nil - end + def content_language(xml = @xml) + xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en' end - class DeletionActivity < Activity - def perform - Rails.logger.debug "Deleting remote status #{id}" - status = Status.find_by(uri: id, account: @account) + def content_warning(xml = @xml) + xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || '' + end - if status.nil? - redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id) - else - RemoveStatusService.new.call(status) - end - end + def visibility_scope(xml = @xml) + xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public end - class RemoteActivity < Activity - def perform - find_status(id) || FetchRemoteStatusService.new.call(url) - 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 end end -- cgit From 1764c32b9e295157b1900b9c66d3cdd76e2d3e0d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 12 Jul 2017 00:39:15 +0200 Subject: Fix #4067 - Do not make HTTP round-trip when resolving local URL (#4160) --- app/services/fetch_remote_resource_service.rb | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'app/services') diff --git a/app/services/fetch_remote_resource_service.rb b/app/services/fetch_remote_resource_service.rb index 5dfa3a0ff..2c1c1f05f 100644 --- a/app/services/fetch_remote_resource_service.rb +++ b/app/services/fetch_remote_resource_service.rb @@ -5,6 +5,9 @@ class FetchRemoteResourceService < BaseService def call(url) @url = url + + return process_local_url if local_url? + process_url unless fetched_atom_feed.nil? end @@ -38,4 +41,29 @@ class FetchRemoteResourceService < BaseService def xml_data @_xml_data ||= Nokogiri::XML(body, nil, 'utf-8') end + + def local_url? + TagManager.instance.local_url?(@url) + end + + def process_local_url + recognized_params = Rails.application.routes.recognize_path(@url) + + return unless recognized_params[:action] == 'show' + + if recognized_params[:controller] == 'stream_entries' + status = StreamEntry.find_by(id: recognized_params[:id])&.status + check_local_status(status) + elsif recognized_params[:controller] == 'statuses' + status = Status.find_by(id: recognized_params[:id]) + check_local_status(status) + elsif recognized_params[:controller] == 'accounts' + Account.find_local(recognized_params[:username]) + end + end + + def check_local_status(status) + return if status.nil? + status if status.public_visibility? || status.unlisted_visibility? + end end -- cgit