fix cron wroker
[akkoma] / lib / pleroma / workers / cron / new_users_digest_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.Cron.NewUsersDigestWorker do
6 alias Pleroma.Activity
7 alias Pleroma.Repo
8 alias Pleroma.User
9
10 import Ecto.Query
11
12 use Pleroma.Workers.WorkerHelper, queue: "new_users_digest"
13
14 @impl Oban.Worker
15 def perform(_job) do
16 if Pleroma.Config.get([Pleroma.Emails.NewUsersDigestEmail, :enabled]) do
17 today = NaiveDateTime.utc_now() |> Timex.beginning_of_day()
18
19 a_day_ago =
20 today
21 |> Timex.shift(days: -1)
22 |> Timex.beginning_of_day()
23
24 users_and_statuses =
25 %{
26 local: true,
27 order_by: :inserted_at
28 }
29 |> User.Query.build()
30 |> where([u], u.inserted_at >= ^a_day_ago and u.inserted_at < ^today)
31 |> Repo.all()
32 |> Enum.map(fn user ->
33 latest_status =
34 Activity
35 |> Activity.Queries.by_actor(user.ap_id)
36 |> Activity.Queries.by_type("Create")
37 |> Activity.with_preloaded_object()
38 |> order_by(desc: :inserted_at)
39 |> limit(1)
40 |> Repo.one()
41
42 total_statuses =
43 Activity
44 |> Activity.Queries.by_actor(user.ap_id)
45 |> Activity.Queries.by_type("Create")
46 |> Repo.aggregate(:count, :id)
47
48 {user, total_statuses, latest_status}
49 end)
50
51 if users_and_statuses != [] do
52 %{is_admin: true}
53 |> User.Query.build()
54 |> where([u], not is_nil(u.email))
55 |> Repo.all()
56 |> Enum.map(&Pleroma.Emails.NewUsersDigestEmail.new_users(&1, users_and_statuses))
57 |> Enum.each(&Pleroma.Emails.Mailer.deliver/1)
58 end
59 end
60
61 :ok
62 end
63 end