Filter Duplicate Boosts: Difference between revisions

no edit summary
No edit summary
No edit summary
 
Line 10: Line 10:


* Pull Request: [[Has Pull Request::https://github.com/NeuromatchAcademy/mastodon/pull/36]]
* Pull Request: [[Has Pull Request::https://github.com/NeuromatchAcademy/mastodon/pull/36]]
* Bugfix - respect local vs remote scope: [[Has Pull Request::https://github.com/NeuromatchAcademy/mastodon/pull/38]]


== Problem ==
== Problem ==
Line 94: Line 95:


<syntaxhighlight lang="ruby">
<syntaxhighlight lang="ruby">
  def max_boost_id_scope
    Status.where(<<~SQL.squish)
      "statuses"."id" = (
        SELECT MAX(id)
        FROM "statuses" "s2"
        WHERE "s2"."reblog_of_id" = "statuses"."reblog_of_id"
          #{'AND ("s2"."local" = true OR "s2"."uri" IS NULL)' if local_only?}
          #{'AND "s2"."local" = false AND "s2"."uri" IS NOT NULL' if remote_only?}
        )
    SQL
  end
   def without_duplicate_reblogs
   def without_duplicate_reblogs
     Status.where(statuses: { reblog_of_id: nil })
     Status.where(statuses: { reblog_of_id: nil })
           .or(Status.where(<<~SQL.squish))
           .or(max_boost_id_scope)
            "statuses"."id" = (
              SELECT MAX(id)
              FROM statuses s2
              WHERE s2.reblog_of_id = statuses.reblog_of_id
            )
          SQL
   end
   end
</syntaxhighlight>
</syntaxhighlight>
==== Caveats ====
Note that we are using string interpolations of the <code>Status.local</code> and <code>Status.remote</code> scopes because there isn't a way, as far as [[Jonny]] can tell, to change the table alias of a scope. This shouldn't be that big of an issue (if, eg. the definition of those scopes changes) because the tests should catch most cases.


==== Example Query ====
==== Example Query ====
Line 122: Line 133:
         (SELECT MAX(id)
         (SELECT MAX(id)
           FROM statuses s2
           FROM statuses s2
           WHERE s2.reblog_of_id = statuses.reblog_of_id))
           WHERE s2.reblog_of_id = statuses.reblog_of_id
            AND ("s2"."local" = true OR "s2"."uri" IS NULL)
      ))
   AND ("statuses"."local" = $2
   AND ("statuses"."local" = $2
       OR "statuses"."uri" IS NULL)
       OR "statuses"."uri" IS NULL)