summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20191029101340_migrate_missing_follow_requests.exs
blob: 90b18efc87dbd7a2af0022aacadd78e788930e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
defmodule Pleroma.Repo.Migrations.MigrateMissingFollowingRelationships do
  use Ecto.Migration

  def change do
    execute(import_pending_follows_from_activities(), "")
  end

  defp import_pending_follows_from_activities do
    """
    INSERT INTO
        following_relationships (
            follower_id,
            following_id,
            state,
            inserted_at,
            updated_at
        )
    SELECT
        followers.id,
        following.id,
        activities.data ->> 'state',
        (activities.data ->> 'published') :: timestamp,
        now()
    FROM
        activities
        JOIN users AS followers ON (activities.actor = followers.ap_id)
        JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
    WHERE
        activities.data ->> 'type' = 'Follow'
        AND activities.data ->> 'state' = 'pending'
    ORDER BY activities.updated_at DESC
    ON CONFLICT DO NOTHING
    """
  end
end