logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20200406100225_users_add_emoji.exs (1190B)


  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. defmodule Pleroma.Repo.Migrations.UsersPopulateEmoji do
  5. use Ecto.Migration
  6. import Ecto.Query
  7. alias Pleroma.User
  8. alias Pleroma.Repo
  9. def up do
  10. execute("ALTER TABLE users ALTER COLUMN emoji SET DEFAULT '{}'::jsonb")
  11. execute("UPDATE users SET emoji = DEFAULT WHERE emoji = '[]'::jsonb")
  12. from(u in User)
  13. |> select([u], struct(u, [:id, :ap_id, :source_data]))
  14. |> Repo.stream()
  15. |> Enum.each(fn user ->
  16. emoji =
  17. user.source_data
  18. |> Map.get("tag", [])
  19. |> Enum.filter(fn
  20. %{"type" => "Emoji"} -> true
  21. _ -> false
  22. end)
  23. |> Enum.reduce(%{}, fn %{"icon" => %{"url" => url}, "name" => name}, acc ->
  24. Map.put(acc, String.trim(name, ":"), url)
  25. end)
  26. user
  27. |> Ecto.Changeset.cast(%{emoji: emoji}, [:emoji])
  28. |> Repo.update()
  29. end)
  30. end
  31. def down do
  32. execute("ALTER TABLE users ALTER COLUMN emoji SET DEFAULT '[]'::jsonb")
  33. execute("UPDATE users SET emoji = DEFAULT WHERE emoji = '{}'::jsonb")
  34. end
  35. end