1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Workers.PollWorker do
7 Generates notifications when a poll ends.
9 use Pleroma.Workers.WorkerHelper, queue: "poll_notifications"
11 alias Pleroma.Activity
12 alias Pleroma.Notification
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)
23 def timeout(_job), do: :timer.seconds(5)
25 defp find_poll_activity(activity_id) do
26 with nil <- Activity.get_by_id(activity_id) do
27 {:error, :poll_activity_not_found}
31 def schedule_poll_end(%Activity{data: %{"type" => "Create"}, id: activity_id} = activity) do
32 with %Object{data: %{"type" => "Question", "closed" => closed}} when is_binary(closed) <-
33 Object.normalize(activity),
34 {:ok, end_time} <- NaiveDateTime.from_iso8601(closed),
35 :gt <- NaiveDateTime.compare(end_time, NaiveDateTime.utc_now()) do
38 activity_id: activity_id
40 |> new(scheduled_at: end_time)
43 _ -> {:error, activity}
47 def schedule_poll_end(activity), do: {:error, activity}