Filter Duplicate Boosts: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 8: Line 8:
|Active Status=Inactive
|Active Status=Inactive
}}
}}
* Pull Request: [[Has Pull Request::https://github.com/NeuromatchAcademy/mastodon/pull/36]]


== Problem ==
== Problem ==
Line 82: Line 84:
* Controller - Filter repeated boosts from the cached feed when serving the public_feed endpoint
* Controller - Filter repeated boosts from the cached feed when serving the public_feed endpoint
* Frontend - Filter repeated boosts in the web UI (but wouldn't work on apps, which is bad)
* Frontend - Filter repeated boosts in the web UI (but wouldn't work on apps, which is bad)
== Implementation ==
Actually pretty damn simple. Add an additional scope in <code>public_feed.rb</code>
The core of the scope is this:
<syntaxhighlight lang="ruby">
  def without_duplicate_reblogs(limit, max_id, since_id, min_id)
    inner_query = Status.select('DISTINCT ON (reblog_of_id) statuses.id').reorder(reblog_of_id: :desc, id: :desc)
    Status.where(statuses: { reblog_of_id: nil })
          .or(Status.where(id: inner_query))
</syntaxhighlight>
which selects either
* Statuses that aren't a boost (<code>reblog_of_id == nil</code>)
* Statuses that are a boost
** sorted by the boosted status ID and the ID of the boost itself,
** using postgres' [DISTINCT ON https://www.postgresql.org/docs/current/sql-select.html] clause to select only the first matching row
** since <code>id</code> is a snowflake ID, and thus chronological, this will be the most recent boost.
There are some problems with this naive implementation though:
* The inner query will select all boosts from all time, every time, because the outer pagination parameters passed to <code>PublicFeed.get</code> don't propagate to the inner query
* The inner query necessarily needs to sort by <code>reblog_of_id</code> first, so just applying a LIMIT will filter on the recency of the ''original post'' rather than the ''boost,'' meaning we will miss most boosts most of the time
So we add the pagination information from <code>PublicFeed.get</code>, mimicking <code>Paginable.to_a_paginated_by_id</code>'s use of the parameters to make a WHERE filter selecting whatever statuses would also be included in the given page that is being fetched
<syntaxhighlight lang="ruby">
  def without_duplicate_reblogs(limit, max_id, since_id, min_id)
    inner_query = Status.select('DISTINCT ON (reblog_of_id) statuses.id').reorder(reblog_of_id: :desc, id: :desc)
    if min_id.present?
      inner_query = inner_query.where(min_id < :id)
    elsif since_id.present?
      inner_query = inner_query.where(since_id < :id)
    end
    inner_query = inner_query.where(max_id > :id) if max_id.present?
    inner_query = inner_query.limit(limit) if limit.present?
    Status.where(statuses: { reblog_of_id: nil })
          .or(Status.where(id: inner_query))
  end
</syntaxhighlight>
But that still doesn't quite get us there, since the first page of a local feed load doesn't have any pagination information in it except for the limit, so we still have the same problem as before. So instead we create a set of <code>n</code> candidate statuses where <code>n</code> is some arbitrary multiple of limit such that we assume that whatever is being filtered out by the other scopes is less than <code>n</code> - ie. we are considering the same set of candidate statuses as the outer scope. This should be relatively fast because we're just taking a slice of the index, rather than applying some complex SQL shit.
Recall that this is still one of several scopes ANDed together, so we will not return any boosts that are filtered by the other scopes, even if they are present in this scope.
<syntaxhighlight lang="ruby">
  def without_duplicate_reblogs(limit, max_id, since_id, min_id)
    candidate_statuses = Status.select(:id).reorder(id: :desc)
    if min_id.present?
      candidate_statuses = candidate_statuses.where(min_id < :id)
    elsif since_id.present?
      candidate_statuses = candidate_statuses.where(since_id < :id)
    end
    candidate_statuses = candidate_statuses.where(max_id > :id) if max_id.present?
    if limit.present?
      limit *= 5
      candidate_statuses = candidate_statuses.limit(limit)
    end
    inner_query = Status
                  .where(id: candidate_statuses)
                  .select('DISTINCT ON (reblog_of_id) statuses.id')
                  .reorder(reblog_of_id: :desc, id: :desc)
    Status.where(statuses: { reblog_of_id: nil })
          .or(Status.where(id: inner_query))
  end
</syntaxhighlight>


== References ==
== References ==


* https://github.com/glitch-soc/mastodon/issues/2481
* https://github.com/glitch-soc/mastodon/issues/2481