From 4f07fb1f0aee6d37d8b3c5fe0088cf36b3926372 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 15 Nov 2016 17:33:41 +0100 Subject: [PATCH] Fix #144 - Filter statuses from blocked users out of ancestors/descendants results --- app/controllers/api/v1/statuses_controller.rb | 2 +- app/models/status.rb | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index e1a417129f7..53578b2f727 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -12,7 +12,7 @@ class Api::V1::StatusesController < ApiController end def context - @context = OpenStruct.new(ancestors: @status.ancestors, descendants: @status.descendants) + @context = OpenStruct.new(ancestors: @status.ancestors(current_account), descendants: @status.descendants(current_account)) set_maps([@status] + @context[:ancestors] + @context[:descendants]) end diff --git a/app/models/status.rb b/app/models/status.rb index c6f5c8d6ca6..56f52975df1 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -66,18 +66,22 @@ class Status < ApplicationRecord attributes['favourites_count'] || favourites.count end - def ancestors + def ancestors(account = nil) ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id) + results = ids.map { |id| statuses[id].first } + results = results.reject { |status| account.blocking?(status.account) } unless account.nil? - ids.map { |id| statuses[id].first } + results end - def descendants + def descendants(account = nil) ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (SELECT id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree JOIN statuses ON statuses.in_reply_to_id = search_tree.id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path', id]) - [self]).pluck(:id) statuses = Status.where(id: ids).with_counters.with_includes.group_by(&:id) + results = ids.map { |id| statuses[id].first } + results = results.reject { |status| account.blocking?(status.account) } unless account.nil? - ids.map { |id| statuses[id].first } + results end class << self