Add option to modify HTTP pool size
[akkoma] / lib / pleroma / workers / poll_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.PollWorker do
6 @moduledoc """
7 Generates notifications when a poll ends.
8 """
9 use Pleroma.Workers.WorkerHelper, queue: "poll_notifications"
10
11 alias Pleroma.Activity
12 alias Pleroma.Notification
13 alias Pleroma.Object
14
15 @impl Oban.Worker
16 def perform(%Job{args: %{"op" => "poll_end", "activity_id" => activity_id}}) do
17 with %Activity{} = activity <- find_poll_activity(activity_id) do
18 Notification.create_poll_notifications(activity)
19 end
20 end
21
22 defp find_poll_activity(activity_id) do
23 with nil <- Activity.get_by_id(activity_id) do
24 {:error, :poll_activity_not_found}
25 end
26 end
27
28 def schedule_poll_end(%Activity{data: %{"type" => "Create"}, id: activity_id} = activity) do
29 with %Object{data: %{"type" => "Question", "closed" => closed}} when is_binary(closed) <-
30 Object.normalize(activity),
31 {:ok, end_time} <- NaiveDateTime.from_iso8601(closed),
32 :gt <- NaiveDateTime.compare(end_time, NaiveDateTime.utc_now()) do
33 %{
34 op: "poll_end",
35 activity_id: activity_id
36 }
37 |> new(scheduled_at: end_time)
38 |> Oban.insert()
39 else
40 _ -> {:error, activity}
41 end
42 end
43
44 def schedule_poll_end(activity), do: {:error, activity}
45 end