2019-02-11 10:59:51 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-02-11 10:59:51 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.ThreadMute do
|
|
|
|
use Ecto.Schema
|
2019-03-05 02:52:23 +00:00
|
|
|
|
2019-03-05 01:45:07 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.ThreadMute
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
|
2019-02-11 10:59:51 +00:00
|
|
|
require Ecto.Query
|
|
|
|
|
|
|
|
schema "thread_mutes" do
|
2019-09-18 14:54:31 +00:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
2019-02-11 10:59:51 +00:00
|
|
|
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
|
2020-03-25 14:01:45 +00:00
|
|
|
user_binary_id = User.binary_id(user_id)
|
2019-02-11 10:59:51 +00:00
|
|
|
|
|
|
|
ThreadMute
|
2020-03-25 14:01:45 +00:00
|
|
|
|> Ecto.Query.where(user_id: ^user_binary_id)
|
2019-02-11 10:59:51 +00:00
|
|
|
|> 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
|