logo

pleroma

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

20191008132217_migrate_following_relationships.exs (2819B)


  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.MigrateFollowingRelationships do
  5. use Ecto.Migration
  6. def change do
  7. execute(import_following_from_users(), "")
  8. execute(import_following_from_activities(), restore_following_column())
  9. end
  10. defp import_following_from_users do
  11. """
  12. INSERT INTO following_relationships (follower_id, following_id, state, inserted_at, updated_at)
  13. SELECT
  14. relations.follower_id,
  15. following.id,
  16. 'accept',
  17. now(),
  18. now()
  19. FROM (
  20. SELECT
  21. users.id AS follower_id,
  22. unnest(users.following) AS following_ap_id
  23. FROM
  24. users
  25. WHERE
  26. users.following != '{}'
  27. AND users.local = false OR users.local = true AND users.email IS NOT NULL -- Exclude `internal/fetch` and `relay`
  28. ) AS relations
  29. JOIN users AS "following" ON "following".follower_address = relations.following_ap_id
  30. WHERE relations.follower_id != following.id
  31. ON CONFLICT DO NOTHING
  32. """
  33. end
  34. defp import_following_from_activities do
  35. """
  36. INSERT INTO
  37. following_relationships (
  38. follower_id,
  39. following_id,
  40. state,
  41. inserted_at,
  42. updated_at
  43. )
  44. SELECT
  45. followers.id,
  46. following.id,
  47. activities.data ->> 'state',
  48. (activities.data ->> 'published') :: timestamp,
  49. now()
  50. FROM
  51. activities
  52. JOIN users AS followers ON (activities.actor = followers.ap_id)
  53. JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
  54. WHERE
  55. activities.data ->> 'type' = 'Follow'
  56. AND activities.data ->> 'state' IN ('accept', 'pending', 'reject')
  57. ORDER BY activities.updated_at DESC
  58. ON CONFLICT DO NOTHING
  59. """
  60. end
  61. defp restore_following_column do
  62. """
  63. UPDATE
  64. users
  65. SET
  66. following = following_query.following_array,
  67. updated_at = now()
  68. FROM (
  69. SELECT
  70. follower.id AS follower_id,
  71. CASE follower.local
  72. WHEN TRUE THEN
  73. array_prepend(follower.follower_address, array_agg(following.follower_address))
  74. ELSE
  75. array_agg(following.follower_address)
  76. END AS following_array
  77. FROM
  78. following_relationships
  79. JOIN users AS follower ON follower.id = following_relationships.follower_id
  80. JOIN users AS following ON following.id = following_relationships.following_id
  81. GROUP BY
  82. follower.id) AS following_query
  83. WHERE
  84. following_query.follower_id = users.id
  85. """
  86. end
  87. end