[#1335] Reorganized users.mutes as relation to UserMute entity.
This commit is contained in:
parent
5cf2c7422b
commit
c31ddce51e
16 changed files with 258 additions and 64 deletions
|
|
@ -30,9 +30,8 @@ defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserBlocks do
|
|||
|
||||
for blockee_ap_id <- blockee_ap_ids do
|
||||
blockee_id = blockee_id_by_ap_id[blockee_ap_id]
|
||||
blockee_uuid = blockee_id && Ecto.UUID.cast!(blockee_id)
|
||||
|
||||
with {:ok, blockee_uuid} <- Ecto.UUID.cast(blockee_id) do
|
||||
with {:ok, blockee_uuid} <- blockee_id && Ecto.UUID.cast(blockee_id) do
|
||||
execute(
|
||||
"INSERT INTO user_blocks(blocker_id, blockee_id, inserted_at) " <>
|
||||
"VALUES('#{blocker_uuid}'::uuid, '#{blockee_uuid}'::uuid, now()) " <>
|
||||
|
|
|
|||
14
priv/repo/migrations/20191112151559_create_user_mutes.exs
Normal file
14
priv/repo/migrations/20191112151559_create_user_mutes.exs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
defmodule Pleroma.Repo.Migrations.CreateUserMutes do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create_if_not_exists table(:user_mutes) do
|
||||
add(:muter_id, references(:users, type: :uuid, on_delete: :delete_all))
|
||||
add(:mutee_id, references(:users, type: :uuid, on_delete: :delete_all))
|
||||
|
||||
timestamps(updated_at: false)
|
||||
end
|
||||
|
||||
create_if_not_exists(unique_index(:user_mutes, [:muter_id, :mutee_id]))
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserMutes do
|
||||
use Ecto.Migration
|
||||
|
||||
alias Ecto.Adapters.SQL
|
||||
alias Pleroma.Repo
|
||||
|
||||
require Logger
|
||||
|
||||
def up do
|
||||
{:ok, %{rows: mute_rows}} = SQL.query(Repo, "SELECT id, mutes FROM users WHERE mutes != '{}'")
|
||||
|
||||
mutee_ap_ids =
|
||||
Enum.flat_map(
|
||||
mute_rows,
|
||||
fn [_, ap_ids] -> ap_ids end
|
||||
)
|
||||
|> Enum.uniq()
|
||||
|
||||
# Selecting ids of all mutees at once in order to reduce the number of SELECT queries
|
||||
{:ok, %{rows: mutee_ap_id_id}} =
|
||||
SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [mutee_ap_ids])
|
||||
|
||||
mutee_id_by_ap_id = Enum.into(mutee_ap_id_id, %{}, fn [k, v] -> {k, v} end)
|
||||
|
||||
Enum.each(
|
||||
mute_rows,
|
||||
fn [muter_id, mutee_ap_ids] ->
|
||||
muter_uuid = Ecto.UUID.cast!(muter_id)
|
||||
|
||||
for mutee_ap_id <- mutee_ap_ids do
|
||||
mutee_id = mutee_id_by_ap_id[mutee_ap_id]
|
||||
|
||||
with {:ok, mutee_uuid} <- mutee_id && Ecto.UUID.cast(mutee_id) do
|
||||
execute(
|
||||
"INSERT INTO user_mutes(muter_id, mutee_id, inserted_at) " <>
|
||||
"VALUES('#{muter_uuid}'::uuid, '#{mutee_uuid}'::uuid, now()) " <>
|
||||
"ON CONFLICT (muter_id, mutee_id) DO NOTHING"
|
||||
)
|
||||
else
|
||||
_ -> Logger.warn("Missing reference: (#{muter_uuid}, #{mutee_id})")
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
def down, do: :noop
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue