Merge develop
This commit is contained in:
commit
c5830ac037
194 changed files with 1788 additions and 7811 deletions
|
|
@ -1,23 +0,0 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Workers.Cron.ClearOauthTokenWorker do
|
||||
@moduledoc """
|
||||
The worker to cleanup expired oAuth tokens.
|
||||
"""
|
||||
|
||||
use Oban.Worker, queue: "background"
|
||||
|
||||
alias Pleroma.Config
|
||||
alias Pleroma.Web.OAuth.Token
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_job) do
|
||||
if Config.get([:oauth2, :clean_expired_tokens], false) do
|
||||
Token.delete_expired_tokens()
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker do
|
||||
@moduledoc """
|
||||
The worker to purge expired activities.
|
||||
"""
|
||||
|
||||
use Oban.Worker, queue: "activity_expiration"
|
||||
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.ActivityExpiration
|
||||
alias Pleroma.Config
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.CommonAPI
|
||||
|
||||
require Logger
|
||||
|
||||
@interval :timer.minutes(1)
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_job) do
|
||||
if Config.get([ActivityExpiration, :enabled]) do
|
||||
Enum.each(ActivityExpiration.due_expirations(@interval), &delete_activity/1)
|
||||
end
|
||||
after
|
||||
:ok
|
||||
end
|
||||
|
||||
def delete_activity(%ActivityExpiration{activity_id: activity_id}) do
|
||||
with {:activity, %Activity{} = activity} <-
|
||||
{:activity, Activity.get_by_id_with_object(activity_id)},
|
||||
{:user, %User{} = user} <- {:user, User.get_by_ap_id(activity.object.data["actor"])} do
|
||||
CommonAPI.delete(activity.id, user)
|
||||
else
|
||||
{:activity, _} ->
|
||||
Logger.error(
|
||||
"#{__MODULE__} Couldn't delete expired activity: not found activity ##{activity_id}"
|
||||
)
|
||||
|
||||
{:user, _} ->
|
||||
Logger.error(
|
||||
"#{__MODULE__} Couldn't delete expired activity: not found actor of ##{activity_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
72
lib/pleroma/workers/purge_expired_activity.ex
Normal file
72
lib/pleroma/workers/purge_expired_activity.ex
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Workers.PurgeExpiredActivity do
|
||||
@moduledoc """
|
||||
Worker which purges expired activity.
|
||||
"""
|
||||
|
||||
use Oban.Worker, queue: :activity_expiration, max_attempts: 1
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias Pleroma.Activity
|
||||
|
||||
@spec enqueue(map()) ::
|
||||
{:ok, Oban.Job.t()}
|
||||
| {:error, :expired_activities_disabled}
|
||||
| {:error, :expiration_too_close}
|
||||
def enqueue(args) do
|
||||
with true <- enabled?() do
|
||||
{scheduled_at, args} = Map.pop(args, :expires_at)
|
||||
|
||||
args
|
||||
|> new(scheduled_at: scheduled_at)
|
||||
|> Oban.insert()
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def perform(%Oban.Job{args: %{"activity_id" => id}}) do
|
||||
with %Activity{} = activity <- find_activity(id),
|
||||
%Pleroma.User{} = user <- find_user(activity.object.data["actor"]) do
|
||||
Pleroma.Web.CommonAPI.delete(activity.id, user)
|
||||
end
|
||||
end
|
||||
|
||||
defp enabled? do
|
||||
with false <- Pleroma.Config.get([__MODULE__, :enabled], false) do
|
||||
{:error, :expired_activities_disabled}
|
||||
end
|
||||
end
|
||||
|
||||
defp find_activity(id) do
|
||||
with nil <- Activity.get_by_id_with_object(id) do
|
||||
{:error, :activity_not_found}
|
||||
end
|
||||
end
|
||||
|
||||
defp find_user(ap_id) do
|
||||
with nil <- Pleroma.User.get_by_ap_id(ap_id) do
|
||||
{:error, :user_not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def get_expiration(id) do
|
||||
from(j in Oban.Job,
|
||||
where: j.state == "scheduled",
|
||||
where: j.queue == "activity_expiration",
|
||||
where: fragment("?->>'activity_id' = ?", j.args, ^id)
|
||||
)
|
||||
|> Pleroma.Repo.one()
|
||||
end
|
||||
|
||||
@spec expires_late_enough?(DateTime.t()) :: boolean()
|
||||
def expires_late_enough?(scheduled_at) do
|
||||
now = DateTime.utc_now()
|
||||
diff = DateTime.diff(scheduled_at, now, :millisecond)
|
||||
min_lifetime = Pleroma.Config.get([__MODULE__, :min_lifetime], 600)
|
||||
diff > :timer.seconds(min_lifetime)
|
||||
end
|
||||
end
|
||||
29
lib/pleroma/workers/purge_expired_token.ex
Normal file
29
lib/pleroma/workers/purge_expired_token.ex
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Workers.PurgeExpiredToken do
|
||||
@moduledoc """
|
||||
Worker which purges expired OAuth tokens
|
||||
"""
|
||||
|
||||
use Oban.Worker, queue: :token_expiration, max_attempts: 1
|
||||
|
||||
@spec enqueue(%{token_id: integer(), valid_until: DateTime.t(), mod: module()}) ::
|
||||
{:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
|
||||
def enqueue(args) do
|
||||
{scheduled_at, args} = Map.pop(args, :valid_until)
|
||||
|
||||
args
|
||||
|> __MODULE__.new(scheduled_at: scheduled_at)
|
||||
|> Oban.insert()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def perform(%Oban.Job{args: %{"token_id" => id, "mod" => module}}) do
|
||||
module
|
||||
|> String.to_existing_atom()
|
||||
|> Pleroma.Repo.get(id)
|
||||
|> Pleroma.Repo.delete()
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue