First
[anni] / priv / repo / migrations / 20200802170532_fix_legacy_tags.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 # Fix legacy tags set by AdminFE that don't align with TagPolicy MRF
6
7 defmodule Pleroma.Repo.Migrations.FixLegacyTags do
8   use Ecto.Migration
9   alias Pleroma.Repo
10   alias Pleroma.User
11   import Ecto.Query
12
13   @old_new_map %{
14     "force_nsfw" => "mrf_tag:media-force-nsfw",
15     "strip_media" => "mrf_tag:media-strip",
16     "force_unlisted" => "mrf_tag:force-unlisted",
17     "sandbox" => "mrf_tag:sandbox",
18     "disable_remote_subscription" => "mrf_tag:disable-remote-subscription",
19     "disable_any_subscription" => "mrf_tag:disable-any-subscription"
20   }
21
22   def change do
23     legacy_tags = Map.keys(@old_new_map)
24
25     from(u in User,
26       where: fragment("? && ?", u.tags, ^legacy_tags),
27       select: struct(u, [:tags, :id])
28     )
29     |> Repo.chunk_stream(100)
30     |> Enum.each(fn user ->
31       fix_tags_changeset(user)
32       |> Repo.update()
33     end)
34   end
35
36   defp fix_tags_changeset(%User{tags: tags} = user) do
37     new_tags =
38       Enum.map(tags, fn tag ->
39         Map.get(@old_new_map, tag, tag)
40       end)
41
42     Ecto.Changeset.change(user, tags: new_tags)
43   end
44 end