8000 Add boundless recurring huddl support by murjax · Pull Request #665 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -6690,6 +6690,10 @@ body .form-row-inline {

body .form-row-inline > div { display: flex; flex-direction: column; gap: 6px; }

/* Top-align fields instead of their bottoms, so a help line under one
column doesn't drag the other column's field down with it. */
body .form-row-inline.is-top-aligned { align-items: flex-start; }

/* Slug control */
body .slug-control {
display: flex;
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ config :huddlz, Oban,
crontab: [
{"@reboot", Huddlz.Sitemaps.Refresh},
{"*/15 * * * *", Huddlz.Sitemaps.Refresh},
{"0 3 * * *", Huddlz.Audit.Prune}
{"0 3 * * *", Huddlz.Audit.Prune},
{"0 4 * * *", Huddlz.Communities.Workers.SweepRecurringSeries}
]}
]

Expand Down
24 changes: 9 additions & 15 deletions lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ defmodule Huddlz.Communities.Huddl do
index [:lifecycle_state, :ends_at],
name: "huddlz_lifecycle_state_ends_at_index"

index [:huddl_template_id, :starts_at],
name: "huddlz_huddl_template_id_starts_at_index"

index "ST_MakePoint(longitude, latitude)",
name: "huddlz_location_gist_index",
using: "GIST",
Expand Down Expand Up @@ -291,11 +294,6 @@ defmodule Huddlz.Communities.Huddl do
message "is required for recurring huddlz"
end

validate present(:repeat_until) do
where argument_equals(:is_recurring, true)
message "is required for recurring huddlz"
end

change Huddlz.Communities.Huddl.Changes.SetCreatorToActor
change Huddlz.Communities.Huddl.Changes.AddCreatorAsAttendee
change Huddlz.Communities.Huddl.Changes.DefaultTimeZoneFromGroup
Expand Down Expand Up @@ -424,16 +422,12 @@ defmodule Huddlz.Communities.Huddl do

require_atomic? false

validate present(:frequency) do
where argument_equals(:edit_type, "all")
message "is required when editing the whole series"
end

8000 validate present(:repeat_until) do
where argument_equals(:edit_type, "all")
message "is required when editing the whole series"
end

# `frequency` is not required here: the web form always submits it
# (Huddlz.HuddlLive.Edit presets it from the series' current cadence),
# but an API/GraphQL caller editing the whole series to change only,
# say, the title omits it — and omission means "leave the cadence
# alone", the same way an omitted `repeat_until` leaves the end date
# alone. See `Changes.EditRecurringHuddlz.reconcile_series/4`.
change Huddlz.Communities.Huddl.Changes.DefaultTimeZoneFromGroup
change Huddlz.Communities.Huddl.Changes.ApplySavedLocation
change Huddlz.Communities.Huddl.Changes.CalculateDateTimeFromInputs
Expand Down
30 changes: 24 additions & 6 deletions lib/huddlz/communities/huddl/changes/add_huddl_template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ defmodule Huddlz.Communities.Huddl.Changes.AddHuddlTemplate do
use Ash.Resource.Change

alias Huddlz.Communities.HuddlTemplate
alias Huddlz.Communities.Workers.RegenerateRecurringSeries
alias Huddlz.Communities.Workers.MaintainRecurringSeries

def change(changeset, _opts, _context) do
if Ash.Changeset.get_argument(changeset, :is_recurring) == true do
changeset
|> Ash.Changeset.before_action(&create_and_link_template/1)
|> Ash.Changeset.after_action(&set_source_huddl/2)
|> Ash.Changeset.after_action(&enqueue_generation/2)
else
changeset
Expand Down Expand Up @@ -44,12 +45,29 @@ defmodule Huddlz.Communities.Huddl.Changes.AddHuddlTemplate do
Ash.Changeset.force_change_attribute(changeset, :huddl_template_id, template.id)
end

# Generating up to 104 instances (each a full create) is too much work for the
# request transaction, so defer it to Oban once the parent huddl commits. The
# job insert is transactional, so it is rolled back if the create fails.
# The template is created before the huddl exists, so it cannot point back
# at it yet. Once the huddl has an id, record it as the source whose details
# every generated occurrence copies.
defp set_source_huddl(changeset, huddl) do
HuddlTemplate
|> Ash.get!(huddl.huddl_template_id, authorize?: false)
|> Ash.Changeset.for_update(
:update,
%{source_huddl_id: huddl.id},
Huddlz.Audit.nested_opts(changeset)
)
|> Ash.update!(authorize?: false)

{:ok, huddl}
end

# Generating a window of occurrences, each a full create, is too much work
# for the request transaction, so defer it to Oban once the parent huddl
# commits. The job insert is transactional, so it is rolled back if the
# create fails.
defp enqueue_generation(_changeset, huddl) do
%{huddl_id: huddl.id}
|> RegenerateRecurringSeries.new()
%{huddl_template_id: huddl.huddl_template_id}
|> MaintainRecurringSeries.new()
|> Oban.insert!()

{:ok, huddl}
Expand Down
53 changes: 33 additions & 20 deletions lib/huddlz/communities/huddl/changes/edit_recurring_huddlz.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ defmodule Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz do
end

defp reconcile_series(changeset, huddl, actor, changed_fields) do
repeat_until = Ash.Changeset.get_argument(changeset, :repeat_until)
frequency = Ash.Changeset.get_argument(changeset, :frequency)

# The update result doesn't carry loaded relationships, and API/GraphQL
Expand All @@ -43,29 +42,26 @@ defmodule Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz do

huddl_template ->
changed_fields =
if schedule_changed?(huddl_template, repeat_until, frequency) do
if schedule_changed?(huddl_template, changeset, frequency) do
[:schedule | changed_fields]
else
changed_fields
end

schedule = series_schedule(huddl_template, changeset.data, huddl, frequency)
attrs =
huddl_template
|> series_schedule(changeset.data, huddl, frequency)
|> Map.merge(%{frequency: frequency, source_huddl_id: huddl.id})
|> put_repeat_until(changeset)

{:ok, huddl_template} =
huddl_template
|> Ash.Changeset.for_update(
:update,
Map.merge(schedule, %{
repeat_until: repeat_until,
frequency: frequency
}),
Huddlz.Audit.nested_opts(changeset)
)
|> Ash.Changeset.for_update(:update, attrs, Huddlz.Audit.nested_opts(changeset))
|> Ash.update(authorize?: false)

# Synchronous: "edit all" is a rare organizer action, bounded at the
# series cap, and immediate consistency is preferable here. The create
# path defers its fan-out to RegenerateRecurringSeries instead.
# Synchronous: "edit all" is a rare organizer action, bounded by the
# number of occurrences that already exist, and immediate consistency
# is preferable here. Creating occurrences is the scheduled job's work.
case RecurrenceHelper.reconcile_future_instances(huddl, huddl_template, actor, %{
paper_trail_metadata: changeset.context[:paper_trail_metadata] || %{}
}) do
Expand All @@ -79,6 +75,20 @@ defmodule Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz do
end
end

# `nil` means two different things on this argument. For the web form, which
# always submits the field, it means "boundless". For an API caller who only
# changed the title, it means "not supplied". Only the argument's presence
# tells them apart, so an omitted end date must never un-bound a series.
defp put_repeat_until(attrs, changeset) do
if repeat_until_supplied?(changeset) do
Map.put(attrs, :repeat_until, Ash.Changeset.get_argument(changeset, :repeat_until))
else
attrs
end
end

defp repeat_until_supplied?(changeset), do: Map.has_key?(changeset.arguments, :repeat_until)

defp series_schedule(%{unit: :month} = template, original, huddl, frequency)
when frequency in [nil, :monthly, "monthly"] do
previous = HuddlTemplate.wall_clock_schedule(original)
Expand Down Expand Up @@ -108,14 +118,17 @@ defmodule Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz do
HuddlTemplate.wall_clock_schedule(huddl)
end

defp schedule_changed?(template, repeat_until, frequency) do
repeat_until_changed?(template, repeat_until) or frequency_changed?(template, frequency)
defp schedule_changed?(template, changeset, frequency) do
repeat_until_changed?(template, changeset) or frequency_changed?(template, frequency)
end

defp repeat_until_changed?(_template, nil), do: false

defp repeat_until_changed?(template, repeat_until) do
DateTime.to_date(template.repeat_until) != repeat_until
defp repeat_until_changed?(template, changeset) do
if repeat_until_supplied?(changeset) do
current = template.repeat_until && DateTime.to_date(template.repeat_until)
current != Ash.Changeset.get_argument(changeset, :repeat_until)
else
false
end
end

defp frequency_changed?(_template, nil), do: false
Expand Down
Loading
Loading
0