summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200802170532_fix_legacy_tags.exs
blob: 72c62d16c15a80e1e577f20a8368d7c257a3d65f (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
36
37
38
39
40
41
42
43
44
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

# Fix legacy tags set by AdminFE that don't align with TagPolicy MRF

defmodule Pleroma.Repo.Migrations.FixLegacyTags do
  use Ecto.Migration
  alias Pleroma.Repo
  alias Pleroma.User
  import Ecto.Query

  @old_new_map %{
    "force_nsfw" => "mrf_tag:media-force-nsfw",
    "strip_media" => "mrf_tag:media-strip",
    "force_unlisted" => "mrf_tag:force-unlisted",
    "sandbox" => "mrf_tag:sandbox",
    "disable_remote_subscription" => "mrf_tag:disable-remote-subscription",
    "disable_any_subscription" => "mrf_tag:disable-any-subscription"
  }

  def change do
    legacy_tags = Map.keys(@old_new_map)

    from(u in User,
      where: fragment("? && ?", u.tags, ^legacy_tags),
      select: struct(u, [:tags, :id])
    )
    |> Repo.chunk_stream(100)
    |> Enum.each(fn user ->
      fix_tags_changeset(user)
      |> Repo.update()
    end)
  end

  defp fix_tags_changeset(%User{tags: tags} = user) do
    new_tags =
      Enum.map(tags, fn tag ->
        Map.get(@old_new_map, tag, tag)
      end)

    Ecto.Changeset.change(user, tags: new_tags)
  end
end