Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85829587
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/pleroma/web/thread_mute.ex b/lib/pleroma/web/thread_mute.ex
index 146de0d80..b5bff86be 100644
--- a/lib/pleroma/web/thread_mute.ex
+++ b/lib/pleroma/web/thread_mute.ex
@@ -1,50 +1,60 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ThreadMute do
use Ecto.Schema
alias Pleroma.Web.ThreadMute
alias Pleroma.{Activity, Repo, User}
require Ecto.Query
schema "thread_mutes" do
belongs_to(:user, User, type: Pleroma.FlakeId)
field(:context, :string)
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
activity = Activity.get_by_id(id)
context = activity.data["context"]
- mute = %Pleroma.Web.ThreadMute{user_id: user.id, context: context}
- Repo.insert(mute)
- {:ok, activity}
+ changeset = changeset(%Pleroma.Web.ThreadMute{}, %{user_id: user.id, context: context})
+
+ case Repo.insert(changeset) do
+ {:ok, _} -> {:ok, activity}
+ {:error, _} -> {:error, "conversation is already muted"}
+ end
end
def remove_mute(user, id) do
user_id = Pleroma.FlakeId.from_string(user.id)
activity = Activity.get_by_id(id)
context = activity.data["context"]
Ecto.Query.from(m in ThreadMute, where: m.user_id == ^user_id and m.context == ^context)
|> Repo.delete_all()
{:ok, activity}
end
def muted?(user, activity) do
user_id = Pleroma.FlakeId.from_string(user.id)
context = activity.data["context"]
result =
Ecto.Query.from(m in ThreadMute,
where: m.user_id == ^user_id and m.context == ^context
)
|> Repo.all()
case result do
[] -> false
_ -> true
end
end
end
diff --git a/priv/repo/migrations/20190205114625_create_thread_mutes.exs b/priv/repo/migrations/20190205114625_create_thread_mutes.exs
index b16f557f0..8e9eccbae 100644
--- a/priv/repo/migrations/20190205114625_create_thread_mutes.exs
+++ b/priv/repo/migrations/20190205114625_create_thread_mutes.exs
@@ -1,12 +1,12 @@
defmodule Pleroma.Repo.Migrations.CreateThreadMutes do
use Ecto.Migration
def change do
create table(:thread_mutes) do
add :user_id, references(:users, type: :uuid, on_delete: :delete_all)
add :context, :string
end
- create index(:thread_mutes, [:user_id])
+ create unique_index(:thread_mutes, [:user_id, :context], name: :unique_index)
end
end
diff --git a/test/web/thread_mute_test.exs b/test/web/thread_mute_test.exs
index 119a06f52..f3a24613c 100644
--- a/test/web/thread_mute_test.exs
+++ b/test/web/thread_mute_test.exs
@@ -1,34 +1,37 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ThreadMuteTest do
use Pleroma.DataCase
import Pleroma.Web.ThreadMute
import Pleroma.Factory
describe "mute tests" do
setup do
user = insert(:user)
activity = insert(:note_activity)
[user: user, activity: activity]
end
test "add mute", %{user: user, activity: activity} do
- id = activity.id
- {:ok, _activity} = add_mute(user, id)
+ {:ok, _activity} = add_mute(user, activity.id)
assert muted?(user, activity)
end
test "remove mute", %{user: user, activity: activity} do
- id = activity.id
-
- add_mute(user, id)
- {:ok, _activity} = remove_mute(user, id)
+ add_mute(user, activity.id)
+ {:ok, _activity} = remove_mute(user, activity.id)
refute muted?(user, activity)
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
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Oct 31, 9:36 PM (12 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1801843
Default Alt Text
(4 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline