forked from AkkomaGang/akkoma
Add unique index and unique constraint check, uniqueness test fails
This commit is contained in:
parent
478a05b4c9
commit
6a150de3bd
3 changed files with 23 additions and 10 deletions
|
@ -13,12 +13,22 @@ defmodule Pleroma.Web.ThreadMute do
|
||||||
field(:context, :string)
|
field(:context, :string)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def changeset(mute, params \\ %{}) do
|
||||||
|
mute
|
||||||
|
|> Ecto.Changeset.cast(params, [:user_id, :context])
|
||||||
|
|> Ecto.Changeset.foreign_key_constraint(:user_id)
|
||||||
|
|> Ecto.Changeset.unique_constraint(:user_id, name: :unique_index)
|
||||||
|
end
|
||||||
|
|
||||||
def add_mute(user, id) do
|
def add_mute(user, id) do
|
||||||
activity = Activity.get_by_id(id)
|
activity = Activity.get_by_id(id)
|
||||||
context = activity.data["context"]
|
context = activity.data["context"]
|
||||||
mute = %Pleroma.Web.ThreadMute{user_id: user.id, context: context}
|
changeset = changeset(%Pleroma.Web.ThreadMute{}, %{user_id: user.id, context: context})
|
||||||
Repo.insert(mute)
|
|
||||||
{:ok, activity}
|
case Repo.insert(changeset) do
|
||||||
|
{:ok, _} -> {:ok, activity}
|
||||||
|
{:error, _} -> {:error, "conversation is already muted"}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_mute(user, id) do
|
def remove_mute(user, id) do
|
||||||
|
|
|
@ -7,6 +7,6 @@ def change do
|
||||||
add :context, :string
|
add :context, :string
|
||||||
end
|
end
|
||||||
|
|
||||||
create index(:thread_mutes, [:user_id])
|
create unique_index(:thread_mutes, [:user_id, :context], name: :unique_index)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,17 +18,20 @@ defmodule Pleroma.Web.ThreadMuteTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "add mute", %{user: user, activity: activity} do
|
test "add mute", %{user: user, activity: activity} do
|
||||||
id = activity.id
|
{:ok, _activity} = add_mute(user, activity.id)
|
||||||
{:ok, _activity} = add_mute(user, id)
|
|
||||||
assert muted?(user, activity)
|
assert muted?(user, activity)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "remove mute", %{user: user, activity: activity} do
|
test "remove mute", %{user: user, activity: activity} do
|
||||||
id = activity.id
|
add_mute(user, activity.id)
|
||||||
|
{:ok, _activity} = remove_mute(user, activity.id)
|
||||||
add_mute(user, id)
|
|
||||||
{:ok, _activity} = remove_mute(user, id)
|
|
||||||
refute muted?(user, activity)
|
refute muted?(user, activity)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "check that mutes can't be duplicate", %{user: user, activity: activity} do
|
||||||
|
add_mute(user, activity.id)
|
||||||
|
assert muted?(user, activity)
|
||||||
|
{:error, _} = add_mute(user, activity.id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue