defmodule Mix.Tasks.FixAvatars do use Mix.Task alias Pleroma.{User, Repo} alias Pleroma.Web.CommonAPI require Logger defp fix_user_avatar( %User{ avatar: %{ "url" => [%{"href" => "https://s3.wasabisys.com/pleroma-site/" <> base}] = new_url_data } = avatar_data } = user ) do new_url = Pleroma.Web.base_url() <> "/media/" <> base Logger.info("avatar: #{User.avatar_url(user)} -> #{new_url}") new_url_data = new_url_data |> Enum.at(0) |> Map.put("href", new_url) avatar_data = Map.put(avatar_data, "url", [new_url_data]) User.update_changeset(user, %{avatar: avatar_data}) end defp fix_user_avatar(%User{} = user), do: User.update_changeset(user, %{}) defp fix_user_banner( %User{ info: %User.Info{ banner: %{ "url" => [%{"href" => "https://s3.wasabisys.com/pleroma-site/" <> base}] = new_url_data } = banner_data } } = user ) do new_url = Pleroma.Web.base_url() <> "/media/" <> base Logger.info("banner: #{User.banner_url(user)} -> #{new_url}") new_url_data = new_url_data |> Enum.at(0) |> Map.put("href", new_url) banner_data = Map.put(banner_data, "url", [new_url_data]) User.Info.profile_update(user.info, %{banner: banner_data}) end defp fix_user_banner(%User{} = user), do: User.Info.profile_update(user.info, %{}) defp fix_user(%User{} = user) do with changeset <- fix_user_avatar(user), info_changeset <- fix_user_banner(user), changeset <- Ecto.Changeset.put_embed(changeset, :info, info_changeset), {:ok, user} <- User.update_and_set_cache(changeset) do CommonAPI.update(user) else e -> Logger.error("Error while updating user: #{inspect(e)}") end end def run([]) do Mix.Task.run("app.start") User.active_local_user_query() |> Repo.all() |> Enum.map(&fix_user/1) end end