it works!!

This commit is contained in:
Karen Konou 2019-02-09 17:47:57 +01:00
parent 4430a0ad12
commit a0d732ec55
3 changed files with 40 additions and 15 deletions

View file

@ -6,6 +6,7 @@ defmodule Pleroma.Notification do
use Ecto.Schema use Ecto.Schema
alias Pleroma.{User, Activity, Notification, Repo} alias Pleroma.{User, Activity, Notification, Repo}
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.ThreadMute
import Ecto.Query import Ecto.Query
schema "notifications" do schema "notifications" do
@ -112,6 +113,7 @@ defmodule Pleroma.Notification do
# TODO move to sql, too. # TODO move to sql, too.
def create_notification(%Activity{} = activity, %User{} = user) do def create_notification(%Activity{} = activity, %User{} = user) do
unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
ThreadMute.muted?(user, activity) or
user.ap_id == activity.data["actor"] or user.ap_id == activity.data["actor"] or
(activity.data["type"] == "Follow" and (activity.data["type"] == "Follow" and
Enum.any?(Notification.for_user(user), fn notif -> Enum.any?(Notification.for_user(user), fn notif ->

View file

@ -4,6 +4,7 @@
defmodule Pleroma.Web.ThreadMute do defmodule Pleroma.Web.ThreadMute do
use Ecto.Schema use Ecto.Schema
alias Pleroma.Web.ThreadMute
alias Pleroma.{Activity, Repo, User} alias Pleroma.{Activity, Repo, User}
require Ecto.Query require Ecto.Query
@ -13,16 +14,37 @@ defmodule Pleroma.Web.ThreadMute do
end end
def add_mute(user, id) do def add_mute(user, id) do
%{data: %{"context" => context}} = Activity.get_by_id(id) activity = Activity.get_by_id(id)
context = activity.data["context"]
mute = %Pleroma.Web.ThreadMute{user_id: user.id, context: context} mute = %Pleroma.Web.ThreadMute{user_id: user.id, context: context}
Repo.insert(mute) Repo.insert(mute)
{:ok, activity}
end end
def remove_mute(user, id) do def remove_mute(user, id) do
user_id = Pleroma.FlakeId.from_string(user.id) user_id = Pleroma.FlakeId.from_string(user.id)
%{data: %{"context" => context}} = Activity.get_by_id(id) activity = Activity.get_by_id(id)
context = activity.data["context"]
Ecto.Query.from(m in "thread_mutes", where: m.user_id == ^user_id and m.context == ^context) Ecto.Query.from(m in ThreadMute, where: m.user_id == ^user_id and m.context == ^context)
|> Repo.delete_all() |> 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
end end

View file

@ -10,31 +10,32 @@ defmodule Pleroma.Web.ThreadMuteTest do
describe "mute tests" do describe "mute tests" do
setup do setup do
user = insert(:user, %{id: "1"}) user = insert(:user)
activity = activity = insert(:note_activity)
insert(:note_activity, %{
data: %{
"context" => "http://localhost:4000/contexts/361ca23e-ffa7-4773-b981-a355a18dc592"
}
})
[user: user, activity: activity] [user: user, activity: activity]
end end
test "add mute", %{user: user, activity: activity} do test "add mute", %{user: user, activity: activity} do
id = activity.id id = activity.id
{:ok, mute} = add_mute(user, id) {:ok, _activity} = add_mute(user, id)
assert mute.user_id == "1"
assert mute.context == "http://localhost:4000/contexts/361ca23e-ffa7-4773-b981-a355a18dc592"
end end
test "remove mute", %{user: user, activity: activity} do test "remove mute", %{user: user, activity: activity} do
id = activity.id id = activity.id
add_mute(user, id) add_mute(user, id)
{1, nil} = remove_mute(user, id) {:ok, _activity} = remove_mute(user, id)
end
test "check mute", %{user: user, activity: activity} do
id = activity.id
add_mute(user, id)
assert muted?(user, activity)
remove_mute(user, id)
refute muted?(user, activity)
end end
end end
end end