2022-02-26 06:11:42 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2022-02-26 06:11:42 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-04-03 11:03:32 +00:00
|
|
|
defmodule Pleroma.Repo.Migrations.UsersPopulateEmoji do
|
|
|
|
use Ecto.Migration
|
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
def up do
|
|
|
|
execute("ALTER TABLE users ALTER COLUMN emoji SET DEFAULT '{}'::jsonb")
|
|
|
|
execute("UPDATE users SET emoji = DEFAULT WHERE emoji = '[]'::jsonb")
|
|
|
|
|
|
|
|
from(u in User)
|
|
|
|
|> select([u], struct(u, [:id, :ap_id, :source_data]))
|
|
|
|
|> Repo.stream()
|
|
|
|
|> Enum.each(fn user ->
|
|
|
|
emoji =
|
|
|
|
user.source_data
|
|
|
|
|> Map.get("tag", [])
|
2020-04-17 21:55:56 +00:00
|
|
|
|> Enum.filter(fn
|
2020-04-17 22:48:40 +00:00
|
|
|
%{"type" => "Emoji"} -> true
|
2020-04-17 21:55:56 +00:00
|
|
|
_ -> false
|
|
|
|
end)
|
2020-04-03 11:03:32 +00:00
|
|
|
|> Enum.reduce(%{}, fn %{"icon" => %{"url" => url}, "name" => name}, acc ->
|
|
|
|
Map.put(acc, String.trim(name, ":"), url)
|
|
|
|
end)
|
|
|
|
|
|
|
|
user
|
|
|
|
|> Ecto.Changeset.cast(%{emoji: emoji}, [:emoji])
|
|
|
|
|> Repo.update()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
|
|
|
execute("ALTER TABLE users ALTER COLUMN emoji SET DEFAULT '[]'::jsonb")
|
|
|
|
execute("UPDATE users SET emoji = DEFAULT WHERE emoji = '{}'::jsonb")
|
|
|
|
end
|
|
|
|
end
|