logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20190303120636_update_user_note_counters.exs (1239B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Repo.Migrations.UpdateUserNoteCounters do
  5. use Ecto.Migration
  6. @public "https://www.w3.org/ns/activitystreams#Public"
  7. def up do
  8. execute("""
  9. WITH public_note_count AS (
  10. SELECT
  11. data->>'actor' AS actor,
  12. count(id) AS count
  13. FROM objects
  14. WHERE data->>'type' = 'Note' AND (
  15. data->'cc' ? '#{@public}' OR data->'to' ? '#{@public}'
  16. )
  17. GROUP BY data->>'actor'
  18. )
  19. UPDATE users AS u
  20. SET "info" = jsonb_set(u.info, '{note_count}', o.count::varchar::jsonb, true)
  21. FROM public_note_count AS o
  22. WHERE u.ap_id = o.actor
  23. """)
  24. end
  25. def down do
  26. execute("""
  27. WITH public_note_count AS (
  28. SELECT
  29. data->>'actor' AS actor,
  30. count(id) AS count
  31. FROM objects
  32. WHERE data->>'type' = 'Note'
  33. GROUP BY data->>'actor'
  34. )
  35. UPDATE users AS u
  36. SET "info" = jsonb_set(u.info, '{note_count}', o.count::varchar::jsonb, true)
  37. FROM public_note_count AS o
  38. WHERE u.ap_id = o.actor
  39. """)
  40. end
  41. end