First
[anni] / priv / repo / migrations / 20191029101340_migrate_missing_follow_requests.exs
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
5 defmodule Pleroma.Repo.Migrations.MigrateMissingFollowingRelationships do
6   use Ecto.Migration
7
8   def change do
9     execute(import_pending_follows_from_activities(), "")
10   end
11
12   defp import_pending_follows_from_activities do
13     """
14     INSERT INTO
15         following_relationships (
16             follower_id,
17             following_id,
18             state,
19             inserted_at,
20             updated_at
21         )
22     SELECT
23         followers.id,
24         following.id,
25         activities.data ->> 'state',
26         (activities.data ->> 'published') :: timestamp,
27         now()
28     FROM
29         activities
30         JOIN users AS followers ON (activities.actor = followers.ap_id)
31         JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
32     WHERE
33         activities.data ->> 'type' = 'Follow'
34         AND activities.data ->> 'state' = 'pending'
35     ORDER BY activities.updated_at DESC
36     ON CONFLICT DO NOTHING
37     """
38   end
39 end