1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ScheduledActivity do
10 alias Pleroma.ScheduledActivity
12 alias Pleroma.Web.CommonAPI.Utils
17 @min_offset :timer.minutes(5)
19 schema "scheduled_activities" do
20 belongs_to(:user, User, type: Pleroma.FlakeId)
21 field(:scheduled_at, :naive_datetime)
27 def changeset(%ScheduledActivity{} = scheduled_activity, attrs) do
29 |> cast(attrs, [:scheduled_at, :params])
30 |> validate_required([:scheduled_at, :params])
31 |> validate_scheduled_at()
32 |> with_media_attachments()
35 defp with_media_attachments(
36 %{changes: %{params: %{"media_ids" => media_ids} = params}} = changeset
38 when is_list(media_ids) do
39 media_attachments = Utils.attachments_from_ids(%{"media_ids" => media_ids})
43 |> Map.put("media_attachments", media_attachments)
44 |> Map.put("media_ids", media_ids)
46 put_change(changeset, :params, params)
49 defp with_media_attachments(changeset), do: changeset
51 def update_changeset(%ScheduledActivity{} = scheduled_activity, attrs) do
53 |> cast(attrs, [:scheduled_at])
54 |> validate_required([:scheduled_at])
55 |> validate_scheduled_at()
58 def validate_scheduled_at(changeset) do
59 validate_change(changeset, :scheduled_at, fn _, scheduled_at ->
61 not far_enough?(scheduled_at) ->
62 [scheduled_at: "must be at least 5 minutes from now"]
64 exceeds_daily_user_limit?(changeset.data.user_id, scheduled_at) ->
65 [scheduled_at: "daily limit exceeded"]
67 exceeds_total_user_limit?(changeset.data.user_id) ->
68 [scheduled_at: "total limit exceeded"]
76 def exceeds_daily_user_limit?(user_id, scheduled_at) do
78 |> where(user_id: ^user_id)
79 |> where([sa], type(sa.scheduled_at, :date) == type(^scheduled_at, :date))
80 |> select([sa], count(sa.id))
82 |> Kernel.>=(Config.get([ScheduledActivity, :daily_user_limit]))
85 def exceeds_total_user_limit?(user_id) do
87 |> where(user_id: ^user_id)
88 |> select([sa], count(sa.id))
90 |> Kernel.>=(Config.get([ScheduledActivity, :total_user_limit]))
93 def far_enough?(scheduled_at) when is_binary(scheduled_at) do
94 with {:ok, scheduled_at} <- Ecto.Type.cast(:naive_datetime, scheduled_at) do
95 far_enough?(scheduled_at)
101 def far_enough?(scheduled_at) do
102 now = NaiveDateTime.utc_now()
103 diff = NaiveDateTime.diff(scheduled_at, now, :millisecond)
107 def new(%User{} = user, attrs) do
108 %ScheduledActivity{user_id: user.id}
112 def create(%User{} = user, attrs) do
118 def get(%User{} = user, scheduled_activity_id) do
120 |> where(user_id: ^user.id)
121 |> where(id: ^scheduled_activity_id)
125 def update(%ScheduledActivity{} = scheduled_activity, attrs) do
127 |> update_changeset(attrs)
131 def delete(%ScheduledActivity{} = scheduled_activity) do
136 def delete(id) when is_binary(id) or is_integer(id) do
142 {1, [scheduled_activity]} -> {:ok, scheduled_activity}
147 def for_user_query(%User{} = user) do
149 |> where(user_id: ^user.id)
152 def due_activities(offset \\ 0) do
154 NaiveDateTime.utc_now()
155 |> NaiveDateTime.add(offset, :millisecond)
158 |> where([sa], sa.scheduled_at < ^naive_datetime)