aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKaren Konou <konoukaren@gmail.com>2019-02-11 11:59:51 +0100
committerKaren Konou <konoukaren@gmail.com>2019-02-11 12:04:02 +0100
commitc01ef574c192488c2643a20b4064439757613449 (patch)
treeaab1f1142133e1d789c71092061a9c97a73bee67 /lib
parentcc21fc5f537510416a088adff085b675de1be58e (diff)
downloadpleroma-c01ef574c192488c2643a20b4064439757613449.tar.gz
Refactor as per Rin's suggestions, add endpoint tests
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/notification.ex4
-rw-r--r--lib/pleroma/thread_mute.ex45
-rw-r--r--lib/pleroma/web/common_api/common_api.ex25
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex6
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex3
-rw-r--r--lib/pleroma/web/thread_mute.ex62
6 files changed, 77 insertions, 68 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index ce4826b23..9ebfd5cb0 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -6,7 +6,7 @@ defmodule Pleroma.Notification do
use Ecto.Schema
alias Pleroma.{User, Activity, Notification, Repo}
alias Pleroma.Web.CommonAPI.Utils
- alias Pleroma.Web.ThreadMute
+ alias Pleroma.Web.CommonAPI
import Ecto.Query
schema "notifications" do
@@ -113,7 +113,7 @@ defmodule Pleroma.Notification do
# TODO move to sql, too.
def create_notification(%Activity{} = activity, %User{} = user) do
unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
- ThreadMute.muted?(user, activity) or user.ap_id == activity.data["actor"] or
+ CommonAPI.thread_muted?(user, activity) or user.ap_id == activity.data["actor"] or
(activity.data["type"] == "Follow" and
Enum.any?(Notification.for_user(user), fn notif ->
notif.activity.data["type"] == "Follow" and
diff --git a/lib/pleroma/thread_mute.ex b/lib/pleroma/thread_mute.ex
new file mode 100644
index 000000000..0b577113d
--- /dev/null
+++ b/lib/pleroma/thread_mute.ex
@@ -0,0 +1,45 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ThreadMute do
+ use Ecto.Schema
+ alias Pleroma.{Repo, User, ThreadMute}
+ 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 query(user_id, context) do
+ user_id = Pleroma.FlakeId.from_string(user_id)
+
+ ThreadMute
+ |> Ecto.Query.where(user_id: ^user_id)
+ |> Ecto.Query.where(context: ^context)
+ end
+
+ def add_mute(user_id, context) do
+ %ThreadMute{}
+ |> changeset(%{user_id: user_id, context: context})
+ |> Repo.insert()
+ end
+
+ def remove_mute(user_id, context) do
+ query(user_id, context)
+ |> Repo.delete_all()
+ end
+
+ def check_muted(user_id, context) do
+ query(user_id, context)
+ |> Repo.all()
+ end
+end
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index 7084da6de..7782c64dd 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -3,7 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.CommonAPI do
- alias Pleroma.{User, Repo, Activity, Object}
+ alias Pleroma.{User, Repo, Activity, Object, ThreadMute}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Formatter
@@ -216,4 +216,27 @@ defmodule Pleroma.Web.CommonAPI do
{:error, "Could not unpin"}
end
end
+
+ def add_mute(user, activity) do
+ with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
+ {:ok, activity}
+ else
+ {:error, _} -> {:error, "conversation is already muted"}
+ end
+ end
+
+ def remove_mute(user, activity) do
+ ThreadMute.remove_mute(user.id, activity.data["context"])
+ {:ok, activity}
+ end
+
+ def thread_muted?(%{id: nil} = _user, _activity), do: false
+
+ def thread_muted?(user, activity) do
+ with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
+ false
+ else
+ _ -> true
+ end
+ end
end
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 073e0a5ea..18fa16b03 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -446,7 +446,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
def mute_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with {:ok, activity} <- Pleroma.Web.ThreadMute.add_mute(user, id) do
+ activity = Activity.get_by_id(id)
+ with {:ok, activity} <- CommonAPI.add_mute(user, activity) do
conn
|> put_view(StatusView)
|> try_render("status.json", %{activity: activity, for: user, as: :activity})
@@ -459,7 +460,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
def unmute_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with {:ok, activity} <- Pleroma.Web.ThreadMute.remove_mute(user, id) do
+ activity = Activity.get_by_id(id)
+ with {:ok, activity} <- CommonAPI.remove_mute(user, activity) do
conn
|> put_view(StatusView)
|> try_render("status.json", %{activity: activity, for: user, as: :activity})
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index d6176a68a..368b79ea7 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
alias Pleroma.HTML
alias Pleroma.Repo
alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MediaProxy
alias Pleroma.Web.MastodonAPI.AccountView
@@ -160,7 +161,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
reblogged: present?(repeated),
favourited: present?(favorited),
bookmarked: present?(bookmarked),
- muted: Pleroma.Web.ThreadMute.muted?(user, activity),
+ muted: CommonAPI.thread_muted?(user, activity),
pinned: pinned?(activity, user),
sensitive: sensitive,
spoiler_text: object["summary"] || "",
diff --git a/lib/pleroma/web/thread_mute.ex b/lib/pleroma/web/thread_mute.ex
deleted file mode 100644
index 695ea2512..000000000
--- a/lib/pleroma/web/thread_mute.ex
+++ /dev/null
@@ -1,62 +0,0 @@
-# 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 query(user, context) do
- user_id = Pleroma.FlakeId.from_string(user.id)
-
- ThreadMute
- |> Ecto.Query.where(user_id: ^user_id)
- |> Ecto.Query.where(context: ^context)
- end
-
- def add_mute(user, id) do
- activity = Activity.get_by_id(id)
-
- with changeset <-
- changeset(%ThreadMute{}, %{user_id: user.id, context: activity.data["context"]}),
- {:ok, _} <- Repo.insert(changeset) do
- {:ok, activity}
- else
- {:error, _} -> {:error, "conversation is already muted"}
- end
- end
-
- def remove_mute(user, id) do
- activity = Activity.get_by_id(id)
-
- query(user, activity.data["context"])
- |> Repo.delete_all()
-
- {:ok, activity}
- end
-
- def muted?(%{id: nil} = _user, _), do: false
-
- def muted?(user, activity) do
- with query <- query(user, activity.data["context"]),
- [] <- Repo.all(query) do
- false
- else
- _ -> true
- end
- end
-end