Pleroma/lib/pleroma/web/thread_mute.ex

51 lines
1.3 KiB
Elixir
Raw Normal View History

2019-02-05 13:35:24 +01:00
# 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
2019-02-09 17:47:57 +01:00
alias Pleroma.Web.ThreadMute
alias Pleroma.{Activity, Repo, User}
require Ecto.Query
2019-02-05 13:35:24 +01:00
schema "thread_mutes" do
2019-02-07 22:25:07 +01:00
belongs_to(:user, User, type: Pleroma.FlakeId)
2019-02-05 13:35:24 +01:00
field(:context, :string)
end
def add_mute(user, id) do
2019-02-09 17:47:57 +01:00
activity = Activity.get_by_id(id)
context = activity.data["context"]
mute = %Pleroma.Web.ThreadMute{user_id: user.id, context: context}
Repo.insert(mute)
2019-02-09 17:47:57 +01:00
{:ok, activity}
2019-02-05 13:35:24 +01:00
end
def remove_mute(user, id) do
user_id = Pleroma.FlakeId.from_string(user.id)
2019-02-09 17:47:57 +01:00
activity = Activity.get_by_id(id)
context = activity.data["context"]
2019-02-08 13:21:34 +01:00
2019-02-09 17:47:57 +01:00
Ecto.Query.from(m in ThreadMute, where: m.user_id == ^user_id and m.context == ^context)
2019-02-08 13:21:34 +01:00
|> Repo.delete_all()
2019-02-09 17:47:57 +01:00
{: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
2019-02-05 13:35:24 +01:00
end
end