Store mutes expiration date

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-06-16 18:12:30 +02:00
parent 75f912c63f
commit b0f83aea29
6 changed files with 73 additions and 13 deletions

View File

@ -1461,15 +1461,28 @@ defmodule Pleroma.User do
notifications? = Map.get(params, :notifications, true) notifications? = Map.get(params, :notifications, true)
expires_in = Map.get(params, :expires_in, 0) expires_in = Map.get(params, :expires_in, 0)
with {:ok, user_mute} <- UserRelationship.create_mute(muter, mutee), expires_at =
if expires_in > 0 do
NaiveDateTime.utc_now()
|> NaiveDateTime.add(expires_in)
else
nil
end
with {:ok, user_mute} <- UserRelationship.create_mute(muter, mutee, expires_at),
{:ok, user_notification_mute} <- {:ok, user_notification_mute} <-
(notifications? && UserRelationship.create_notification_mute(muter, mutee)) || (notifications? &&
UserRelationship.create_notification_mute(
muter,
mutee,
expires_at
)) ||
{:ok, nil} do {:ok, nil} do
if expires_in > 0 do if expires_in > 0 do
Pleroma.Workers.MuteExpireWorker.enqueue( Pleroma.Workers.MuteExpireWorker.enqueue(
"unmute_user", "unmute_user",
%{"muter_id" => muter.id, "mutee_id" => mutee.id}, %{"muter_id" => muter.id, "mutee_id" => mutee.id},
schedule_in: expires_in scheduled_at: expires_at
) )
end end

View File

@ -18,16 +18,17 @@ defmodule Pleroma.UserRelationship do
belongs_to(:source, User, type: FlakeId.Ecto.CompatType) belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
belongs_to(:target, User, type: FlakeId.Ecto.CompatType) belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
field(:relationship_type, Pleroma.UserRelationship.Type) field(:relationship_type, Pleroma.UserRelationship.Type)
field(:expires_at, :naive_datetime)
timestamps(updated_at: false) timestamps(updated_at: false)
end end
for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
# `def create_block/2`, `def create_mute/2`, `def create_reblog_mute/2`, # `def create_block/3`, `def create_mute/3`, `def create_reblog_mute/3`,
# `def create_notification_mute/2`, `def create_inverse_subscription/2`, # `def create_notification_mute/3`, `def create_inverse_subscription/3`,
# `def endorsement/2` # `def endorsement/3`
def unquote(:"create_#{relationship_type}")(source, target), def unquote(:"create_#{relationship_type}")(source, target, expires_at \\ nil),
do: create(unquote(relationship_type), source, target) do: create(unquote(relationship_type), source, target, expires_at)
# `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`, # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
# `def delete_notification_mute/2`, `def delete_inverse_subscription/2`, # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`,
@ -37,9 +38,15 @@ defmodule Pleroma.UserRelationship do
# `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`, # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
# `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`, # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`,
# `def inverse_endorsement?/2` # `def inverse_endorsement_exists?/2`
def unquote(:"#{relationship_type}_exists?")(source, target), def unquote(:"#{relationship_type}_exists?")(source, target),
do: exists?(unquote(relationship_type), source, target) do: exists?(unquote(relationship_type), source, target)
# `def get_block_expire_date/2`, `def get_mute_expire_date/2`,
# `def get_reblog_mute_expire_date/2`, `def get_notification_mute_exists?/2`,
# `def get_inverse_subscription_expire_date/2`, `def get_inverse_endorsement_expire_date/2`
def unquote(:"get_#{relationship_type}_expire_date")(source, target),
do: get_expire_date(unquote(relationship_type), source, target)
end end
def user_relationship_types, do: Keyword.keys(user_relationship_mappings()) def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
@ -48,7 +55,7 @@ defmodule Pleroma.UserRelationship do
def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
user_relationship user_relationship
|> cast(params, [:relationship_type, :source_id, :target_id]) |> cast(params, [:relationship_type, :source_id, :target_id, :expires_at])
|> validate_required([:relationship_type, :source_id, :target_id]) |> validate_required([:relationship_type, :source_id, :target_id])
|> unique_constraint(:relationship_type, |> unique_constraint(:relationship_type,
name: :user_relationships_source_id_relationship_type_target_id_index name: :user_relationships_source_id_relationship_type_target_id_index
@ -62,12 +69,26 @@ defmodule Pleroma.UserRelationship do
|> Repo.exists?() |> Repo.exists?()
end end
def create(relationship_type, %User{} = source, %User{} = target) do def get_expire_date(relationship_type, %User{} = source, %User{} = target) do
%UserRelationship{expires_at: expires_at} =
UserRelationship
|> where(
relationship_type: ^relationship_type,
source_id: ^source.id,
target_id: ^target.id
)
|> Repo.one!()
expires_at
end
def create(relationship_type, %User{} = source, %User{} = target, expires_at \\ nil) do
%UserRelationship{} %UserRelationship{}
|> changeset(%{ |> changeset(%{
relationship_type: relationship_type, relationship_type: relationship_type,
source_id: source.id, source_id: source.id,
target_id: target.id target_id: target.id,
expires_at: expires_at
}) })
|> Repo.insert( |> Repo.insert(
on_conflict: {:replace_all_except, [:id]}, on_conflict: {:replace_all_except, [:id]},

View File

@ -33,6 +33,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
header: %Schema{type: :string, format: :uri}, header: %Schema{type: :string, format: :uri},
id: FlakeID, id: FlakeID,
locked: %Schema{type: :boolean}, locked: %Schema{type: :boolean},
mute_expires_at: %Schema{type: :string, format: "date-time", nullable: true},
note: %Schema{type: :string, format: :html}, note: %Schema{type: :string, format: :html},
statuses_count: %Schema{type: :integer}, statuses_count: %Schema{type: :integer},
url: %Schema{type: :string, format: :uri}, url: %Schema{type: :string, format: :uri},

View File

@ -499,7 +499,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
users: users, users: users,
for: user, for: user,
as: :user, as: :user,
embed_relationships: embed_relationships?(params) embed_relationships: embed_relationships?(params),
mutes: true
) )
end end

View File

@ -311,6 +311,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|> maybe_put_unread_conversation_count(user, opts[:for]) |> maybe_put_unread_conversation_count(user, opts[:for])
|> maybe_put_unread_notification_count(user, opts[:for]) |> maybe_put_unread_notification_count(user, opts[:for])
|> maybe_put_email_address(user, opts[:for]) |> maybe_put_email_address(user, opts[:for])
|> maybe_put_mute_expires_at(user, opts[:for], opts)
|> maybe_show_birthday(user, opts[:for]) |> maybe_show_birthday(user, opts[:for])
end end
@ -434,6 +435,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
defp maybe_put_email_address(data, _, _), do: data defp maybe_put_email_address(data, _, _), do: data
defp maybe_put_mute_expires_at(data, %User{} = user, target, %{mutes: true}) do
Map.put(
data,
:mute_expires_at,
UserRelationship.get_mute_expire_date(target, user)
)
end
defp maybe_put_mute_expires_at(data, _, _, _), do: data
defp maybe_show_birthday(data, %User{id: user_id} = user, %User{id: user_id}) do defp maybe_show_birthday(data, %User{id: user_id} = user, %User{id: user_id}) do
data data
|> Kernel.put_in([:pleroma, :birthday], user.birthday) |> Kernel.put_in([:pleroma, :birthday], user.birthday)

View File

@ -0,0 +1,13 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Repo.Migrations.AddExpiresAtToUserRelationships do
use Ecto.Migration
def change do
alter table(:user_relationships) do
add_if_not_exists(:expires_at, :naive_datetime)
end
end
end