1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Repo.Migrations.MigrateFollowingRelationships do
9 execute(import_following_from_users(), "")
10 execute(import_following_from_activities(), restore_following_column())
13 defp import_following_from_users do
15 INSERT INTO following_relationships (follower_id, following_id, state, inserted_at, updated_at)
17 relations.follower_id,
24 users.id AS follower_id,
25 unnest(users.following) AS following_ap_id
29 users.following != '{}'
30 AND users.local = false OR users.local = true AND users.email IS NOT NULL -- Exclude `internal/fetch` and `relay`
32 JOIN users AS "following" ON "following".follower_address = relations.following_ap_id
34 WHERE relations.follower_id != following.id
35 ON CONFLICT DO NOTHING
39 defp import_following_from_activities do
42 following_relationships (
52 activities.data ->> 'state',
53 (activities.data ->> 'published') :: timestamp,
57 JOIN users AS followers ON (activities.actor = followers.ap_id)
58 JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
60 activities.data ->> 'type' = 'Follow'
61 AND activities.data ->> 'state' IN ('accept', 'pending', 'reject')
62 ORDER BY activities.updated_at DESC
63 ON CONFLICT DO NOTHING
67 defp restore_following_column do
72 following = following_query.following_array,
76 follower.id AS follower_id,
79 array_prepend(follower.follower_address, array_agg(following.follower_address))
81 array_agg(following.follower_address)
82 END AS following_array
84 following_relationships
85 JOIN users AS follower ON follower.id = following_relationships.follower_id
86 JOIN users AS following ON following.id = following_relationships.following_id
88 follower.id) AS following_query
90 following_query.follower_id = users.id