Add migration to fix spc users.
[akkoma] / lib / pleroma / spc_fixes / spc_fixes.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 alias Pleroma.Repo
6 alias Pleroma.User
7 import Ecto.Query
8
9 defmodule Pleroma.SpcFixes do
10 def upgrade_users do
11 query =
12 from(u in User,
13 where: fragment("? like ?", u.ap_id, "https://shitposter.club/user/%")
14 )
15
16 {:ok, file} = File.read("lib/pleroma/spc_fixes/users_conversion.txt")
17
18 mapping =
19 file
20 |> String.trim()
21 |> String.split("\n")
22 |> Enum.map(fn line ->
23 line
24 |> String.split("\t")
25 end)
26 |> Enum.reduce(%{}, fn [_id, old_ap_id, new_ap_id], acc ->
27 Map.put(acc, old_ap_id, String.trim(new_ap_id))
28 end)
29
30 # First, refetch all the old users.
31 _old_users =
32 query
33 |> Repo.all()
34 |> Enum.each(fn user ->
35 with ap_id when is_binary(ap_id) <- mapping[user.ap_id] do
36 # This fetches and updates the user.
37 User.get_or_fetch_by_ap_id(ap_id)
38 end
39 end)
40
41 # Now, fix follow relationships.
42 query =
43 from(u in User,
44 where: fragment("? like ?", u.ap_id, "https://shitposter.club/users/%")
45 )
46
47 query
48 |> Repo.all()
49 |> Enum.each(fn user ->
50 old_follower_address = User.ap_followers(user)
51
52 query =
53 from(u in User,
54 where: ^old_follower_address in u.following,
55 update: [
56 push: [following: ^user.follower_address]
57 ]
58 )
59
60 Repo.update_all(query, [])
61 end)
62 end
63 end