1 defmodule Pleroma.Repo.Migrations.MigrateFollowingRelationships do
5 execute(import_following_from_users(), "")
6 execute(import_following_from_activities(), restore_following_column())
9 defp import_following_from_users do
11 INSERT INTO following_relationships (follower_id, following_id, state, inserted_at, updated_at)
13 relations.follower_id,
20 users.id AS follower_id,
21 unnest(users.following) AS following_ap_id
25 users.following != '{}'
26 AND users.local = false OR users.local = true AND users.email IS NOT NULL -- Exclude `internal/fetch` and `relay`
28 JOIN users AS "following" ON "following".follower_address = relations.following_ap_id
30 WHERE relations.follower_id != following.id
31 ON CONFLICT DO NOTHING
35 defp import_following_from_activities do
38 following_relationships (
48 activities.data ->> 'state',
49 (activities.data ->> 'published') :: timestamp,
53 JOIN users AS followers ON (activities.actor = followers.ap_id)
54 JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
56 activities.data ->> 'type' = 'Follow'
57 AND activities.data ->> 'state' IN ('accept', 'pending', 'reject')
58 AND NOT (followers.info ? 'deactivated' AND followers.info -> 'deactivated' @> 'true')
59 ORDER BY activities.updated_at DESC
60 ON CONFLICT DO NOTHING
64 defp restore_following_column do
69 following = following_query.following_array,
73 follwer.id AS follower_id,
76 array_prepend(follwer.follower_address, array_agg(following.follower_address))
78 array_agg(following.follower_address)
79 END AS following_array
81 following_relationships
82 JOIN users AS follwer ON follwer.id = following_relationships.follower_id
83 JOIN users AS FOLLOWING ON following.id = following_relationships.following_id
85 follwer.id) AS following_query
87 following_query.follower_id = users.id