aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/daemons
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/daemons')
-rw-r--r--lib/pleroma/daemons/activity_expiration_daemon.ex66
-rw-r--r--lib/pleroma/daemons/digest_email_daemon.ex42
-rw-r--r--lib/pleroma/daemons/scheduled_activity_daemon.ex62
3 files changed, 170 insertions, 0 deletions
diff --git a/lib/pleroma/daemons/activity_expiration_daemon.ex b/lib/pleroma/daemons/activity_expiration_daemon.ex
new file mode 100644
index 000000000..cab7628c4
--- /dev/null
+++ b/lib/pleroma/daemons/activity_expiration_daemon.ex
@@ -0,0 +1,66 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Daemons.ActivityExpirationDaemon do
+ alias Pleroma.Activity
+ alias Pleroma.ActivityExpiration
+ alias Pleroma.Config
+ alias Pleroma.Repo
+ alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
+
+ require Logger
+ use GenServer
+ import Ecto.Query
+
+ @schedule_interval :timer.minutes(1)
+
+ def start_link(_) do
+ GenServer.start_link(__MODULE__, nil)
+ end
+
+ @impl true
+ def init(_) do
+ if Config.get([ActivityExpiration, :enabled]) do
+ schedule_next()
+ {:ok, nil}
+ else
+ :ignore
+ end
+ end
+
+ def perform(:execute, expiration_id) do
+ try do
+ expiration =
+ ActivityExpiration
+ |> where([e], e.id == ^expiration_id)
+ |> Repo.one!()
+
+ activity = Activity.get_by_id_with_object(expiration.activity_id)
+ user = User.get_by_ap_id(activity.object.data["actor"])
+ CommonAPI.delete(activity.id, user)
+ rescue
+ error ->
+ Logger.error("#{__MODULE__} Couldn't delete expired activity: #{inspect(error)}")
+ end
+ end
+
+ @impl true
+ def handle_info(:perform, state) do
+ ActivityExpiration.due_expirations(@schedule_interval)
+ |> Enum.each(fn expiration ->
+ Pleroma.Workers.ActivityExpirationWorker.enqueue(
+ "activity_expiration",
+ %{"activity_expiration_id" => expiration.id}
+ )
+ end)
+
+ schedule_next()
+ {:noreply, state}
+ end
+
+ defp schedule_next do
+ Process.send_after(self(), :perform, @schedule_interval)
+ end
+end
diff --git a/lib/pleroma/daemons/digest_email_daemon.ex b/lib/pleroma/daemons/digest_email_daemon.ex
new file mode 100644
index 000000000..462ad2c55
--- /dev/null
+++ b/lib/pleroma/daemons/digest_email_daemon.ex
@@ -0,0 +1,42 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Daemons.DigestEmailDaemon do
+ alias Pleroma.Repo
+ alias Pleroma.Workers.DigestEmailsWorker
+
+ import Ecto.Query
+
+ def perform do
+ config = Pleroma.Config.get([:email_notifications, :digest])
+ negative_interval = -Map.fetch!(config, :interval)
+ inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
+ inactive_users_query = Pleroma.User.list_inactive_users_query(inactivity_threshold)
+
+ now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
+
+ from(u in inactive_users_query,
+ where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
+ where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
+ select: u
+ )
+ |> Repo.all()
+ |> Enum.each(fn user ->
+ DigestEmailsWorker.enqueue("digest_email", %{"user_id" => user.id})
+ end)
+ end
+
+ @doc """
+ Send digest email to the given user.
+ Updates `last_digest_emailed_at` field for the user and returns the updated user.
+ """
+ @spec perform(Pleroma.User.t()) :: Pleroma.User.t()
+ def perform(user) do
+ with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
+ Pleroma.Emails.Mailer.deliver_async(email)
+ end
+
+ Pleroma.User.touch_last_digest_emailed_at(user)
+ end
+end
diff --git a/lib/pleroma/daemons/scheduled_activity_daemon.ex b/lib/pleroma/daemons/scheduled_activity_daemon.ex
new file mode 100644
index 000000000..aee5f723a
--- /dev/null
+++ b/lib/pleroma/daemons/scheduled_activity_daemon.ex
@@ -0,0 +1,62 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Daemons.ScheduledActivityDaemon do
+ @moduledoc """
+ Sends scheduled activities to the job queue.
+ """
+
+ alias Pleroma.Config
+ alias Pleroma.ScheduledActivity
+ alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
+
+ use GenServer
+ require Logger
+
+ @schedule_interval :timer.minutes(1)
+
+ def start_link(_) do
+ GenServer.start_link(__MODULE__, nil)
+ end
+
+ def init(_) do
+ if Config.get([ScheduledActivity, :enabled]) do
+ schedule_next()
+ {:ok, nil}
+ else
+ :ignore
+ end
+ end
+
+ def perform(:execute, scheduled_activity_id) do
+ try do
+ {:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity_id)
+ %User{} = user = User.get_cached_by_id(scheduled_activity.user_id)
+ {:ok, _result} = CommonAPI.post(user, scheduled_activity.params)
+ rescue
+ error ->
+ Logger.error(
+ "#{__MODULE__} Couldn't create a status from the scheduled activity: #{inspect(error)}"
+ )
+ end
+ end
+
+ def handle_info(:perform, state) do
+ ScheduledActivity.due_activities(@schedule_interval)
+ |> Enum.each(fn scheduled_activity ->
+ Pleroma.Workers.ScheduledActivityWorker.enqueue(
+ "execute",
+ %{"activity_id" => scheduled_activity.id}
+ )
+ end)
+
+ schedule_next()
+ {:noreply, state}
+ end
+
+ defp schedule_next do
+ Process.send_after(self(), :perform, @schedule_interval)
+ end
+end