aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKaren Konou <konoukaren@gmail.com>2019-02-10 09:31:20 +0100
committerKaren Konou <konoukaren@gmail.com>2019-02-10 10:42:30 +0100
commitcc21fc5f537510416a088adff085b675de1be58e (patch)
tree6c5b6c94bf8d224d5f5a261f268f351a4ecc7c8e /lib
parent6a150de3bd416cfe0b4870deee2e6557791345f8 (diff)
downloadpleroma-cc21fc5f537510416a088adff085b675de1be58e.tar.gz
refactor, status view updating, error handling
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex5
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex2
-rw-r--r--lib/pleroma/web/thread_mute.ex38
3 files changed, 26 insertions, 19 deletions
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index a93f4297b..073e0a5ea 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -450,6 +450,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
conn
|> put_view(StatusView)
|> try_render("status.json", %{activity: activity, for: user, as: :activity})
+ else
+ {:error, reason} ->
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(:bad_request, Jason.encode!(%{"error" => reason}))
end
end
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index a227d742d..d6176a68a 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -160,7 +160,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
reblogged: present?(repeated),
favourited: present?(favorited),
bookmarked: present?(bookmarked),
- muted: false,
+ muted: Pleroma.Web.ThreadMute.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
index b5bff86be..695ea2512 100644
--- a/lib/pleroma/web/thread_mute.ex
+++ b/lib/pleroma/web/thread_mute.ex
@@ -20,40 +20,42 @@ defmodule Pleroma.Web.ThreadMute do
|> 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)
- context = activity.data["context"]
- changeset = changeset(%Pleroma.Web.ThreadMute{}, %{user_id: user.id, context: context})
- case Repo.insert(changeset) do
- {:ok, _} -> {:ok, activity}
+ 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
- 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)
+ query(user, activity.data["context"])
|> Repo.delete_all()
{:ok, activity}
end
- def muted?(user, activity) do
- user_id = Pleroma.FlakeId.from_string(user.id)
- context = activity.data["context"]
+ def muted?(%{id: nil} = _user, _), do: false
- result =
- Ecto.Query.from(m in ThreadMute,
- where: m.user_id == ^user_id and m.context == ^context
- )
- |> Repo.all()
-
- case result do
- [] -> false
+ def muted?(user, activity) do
+ with query <- query(user, activity.data["context"]),
+ [] <- Repo.all(query) do
+ false
+ else
_ -> true
end
end