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