Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: introduce max attendees for events (#34313)

Samea9f2f532b37
diff --git a/plugins/discourse-calendar/app/controllers/discourse_post_event/invitees_controller.rb b/plugins/discourse-calendar/app/controllers/discourse_post_event/invitees_controller.rb
index 96bb04da5ce..918e9642542 100644
--- a/plugins/discourse-calendar/app/controllers/discourse_post_event/invitees_controller.rb
+++ b/plugins/discourse-calendar/app/controllers/discourse_post_event/invitees_controller.rb
@@ -52,8 +52,19 @@ module DiscoursePostEvent
def update
invitee = Invitee.find_by(id: params[:invitee_id], post_id: params[:event_id])
guardian.ensure_can_act_on_invitee!(invitee)
- invitee.update_attendance!(invitee_params[:status])
- render json: InviteeSerializer.new(invitee)
+ begin
+ invitee.update_attendance!(invitee_params[:status])
+ render json: InviteeSerializer.new(invitee)
+ rescue Discourse::InvalidParameters => e
+ if e.message == "max_attendees"
+ render_json_error(
+ I18n.t("discourse_post_event.errors.models.event.max_attendees_reached"),
+ 422,
+ )
+ else
+ raise
+ end
+ end
end
def create
@@ -73,8 +84,19 @@ module DiscoursePostEvent
raise Discourse::InvalidAccess if !guardian.can_act_on_discourse_post_event?(event)
end
- invitee = Invitee.create_attendance!(user.id, params[:event_id], invitee_params[:status])
- render json: InviteeSerializer.new(invitee)
+ begin
+ invitee = Invitee.create_attendance!(user.id, params[:event_id], invitee_params[:status])
+ render json: InviteeSerializer.new(invitee)
+ rescue Discourse::InvalidParameters => e
+ if e.message == "max_attendees"
+ render_json_error(
+ I18n.t("discourse_post_event.errors.models.event.max_attendees_reached"),
+ 422,
+ )
+ else
+ raise
+ end
+ end
end
def destroy
diff --git a/plugins/discourse-calendar/app/models/discourse_post_event/event.rb b/plugins/discourse-calendar/app/models/discourse_post_event/event.rb
index 8a27163d47b..371fb9acc8f 100644
--- a/plugins/discourse-calendar/app/models/discourse_post_event/event.rb
+++ b/plugins/discourse-calendar/app/models/discourse_post_event/event.rb
@@ -30,6 +30,7 @@ module DiscoursePostEvent
},
unless: ->(event) { event.name.blank? }
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }
+ validates :max_attendees, numericality: { only_integer: true, greater_than: 0, allow_nil: true }
validate :raw_invitees_length
validate :ends_before_start
@@ -242,6 +243,15 @@ module DiscoursePostEvent
(self.starts_at..finishes_at).cover?(Time.now)
end
+ def going_count
+ invitees.where(status: Invitee.statuses[:going]).count
+ end
+
+ def at_capacity?
+ return false if max_attendees.blank?
+ going_count >= max_attendees
+ end
+
def self.statuses
@statuses ||= Enum.new(standalone: 0, public: 1, private: 2)
end
@@ -331,6 +341,7 @@ module DiscoursePostEvent
minimal: event_params[:minimal],
closed: event_params[:closed] || false,
chat_enabled: event_params[:"chat-enabled"]&.downcase == "true",
+ max_attendees: event_params[:"max-attendees"]&.to_i,
}
params[:custom_fields] = {}
@@ -504,4 +515,5 @@ end
# chat_channel_id :bigint
# recurrence_until :datetime
# show_local_time :boolean default(FALSE), not null
+# max_attendees :integer
#
diff --git a/plugins/discourse-calendar/app/models/discourse_post_event/invitee.rb b/plugins/discourse-calendar/app/models/discourse_post_event/invitee.rb
index 570d38657ee..d2811edcefd 100644
--- a/plugins/discourse-calendar/app/models/discourse_post_event/invitee.rb
+++ b/plugins/discourse-calendar/app/models/discourse_post_event/invitee.rb
@@ -19,6 +19,11 @@ module DiscoursePostEvent
end
def self.create_attendance!(user_id, post_id, status)
+ event = Event.find(post_id)
+ if status.to_sym == :going && event.at_capacity?
+ raise Discourse::InvalidParameters.new(:max_attendees)
+ end
+
invitee =
Invitee.create!(status: Invitee.statuses[status.to_sym], post_id: post_id, user_id: user_id)
invitee.event.publish_update!
@@ -31,6 +36,11 @@ module DiscoursePostEvent
end
def update_attendance!(status)
+ if status && status.to_sym == :going && event.at_capacity? &&
+ self.status != Invitee.statuses[:going]
+ raise Discourse::InvalidParameters.new(:max_attendees)
+ end
+
new_status = Invitee.statuses[status.to_sym]
status_changed = self.status != new_status
self.update(status: new_status)
diff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb
index c298cfa287c..ee0a603fbbf 100644
--- a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb
+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb
@@ -29,6 +29,9 @@ module DiscoursePostEvent
attributes :watching_invitee
attributes :chat_enabled
attributes :channel
+ attributes :rrule
+ attributes :max_attendees
+ attributes :at_capacity
def channel
::Chat::ChannelSerializer.new(object.chat_channel, root: false, scope:)
@@ -38,6 +41,10 @@ module DiscoursePostEvent
object.chat_enabled && defined?(::Chat::ChannelSerializer) && object.chat_channel.present?
end
+ def at_capacity
+ object.at_capacity?
+ end
+
def can_act_on_discourse_post_event
scope.can_act_on_discourse_post_event?(object)
end
diff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_stats_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_stats_serializer.rb
index 4e3674df602..b82d51b5d18 100644
--- a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_stats_serializer.rb
+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_stats_serializer.rb
@@ -6,6 +6,7 @@ module DiscoursePostEvent
attributes :interested
attributes :not_going
attributes :invited
+ attributes :capacity
def invited
unanswered = counts[nil] || 0
@@ -32,5 +33,9 @@ module DiscoursePostEvent
def counts
@counts ||= object.invitees.group(:status).count
end
+
+ def capacity
+ object.max_attendees
+ end
end
end
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/status.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/status.gjs
index 191eceec464..7dbaf53b2ba 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/status.gjs
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/status.gjs
@@ -2,6 +2,7 @@ import Component from "@glimmer/component";
import { concat, fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
+import { and, eq, not } from "truth-helpers";
import DButton from "discourse/components/d-button";
import PluginOutlet from "discourse/components/plugin-outlet";
import concatClass from "discourse/helpers/concat-class";
@@ -81,6 +82,7 @@ export default class DiscoursePostEventStatus extends Component {
postId: this.args.event.id,
});
} catch (e) {
+ // in case of 422 full, backend returns error string, let popup handle
popupAjaxError(e);
}
}
@@ -132,8 +134,16 @@ export default class DiscoursePostEventStatus extends Component {
>
<DButton
class="going-button"
+ @disabled={{and
+ @event.atCapacity
+ (not (eq this.watchingInviteeStatus "going"))
+ }}
@icon="check"
- @label="discourse_post_event.models.invitee.status.going"
+ @label={{if
+ @event.atCapacity
+ "discourse_post_event.models.event.full"
+ "discourse_post_event.models.invitee.status.going"
+ }}
@action={{fn this.changeWatchingInviteeStatus "going"}}
/>
</PluginOutlet>
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs
index c5d90bae553..b24bffe77f7 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs
@@ -1,6 +1,5 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
-import { Input, Textarea } from "@ember/component";
import { concat, fn, get } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
@@ -32,6 +31,7 @@ export default class PostEventBuilder extends Component {
@tracked flash = null;
@tracked isSaving = false;
+ @tracked maxAttendeesInput = this.args.model.event.maxAttendees;
@tracked startsAt = moment(this.event.startsAt).tz(
this.event.timezone || "UTC"
@@ -177,6 +177,15 @@ export default class PostEventBuilder extends Component {
this.event.customFields[field] = e.target.value;
}
+ @action
+ setMaxAttendees(e) {
+ const raw = e.target.value;
+ const value = parseInt(raw, 10);
+ this.event.maxAttendees =
+ Number.isFinite(value) && value > 0 ? value : null;
+ this.maxAttendeesInput = raw;
+ }
+
@action
onChangeDates(dates) {
this.event.startsAt = dates.from;
@@ -232,6 +241,50 @@ export default class PostEventBuilder extends Component {
: null;
}
+ // Native input handlers
+ @action
+ setName(e) {
+ this.event.name = e.target.value;
+ }
+
+ @action
+ setLocation(e) {
+ this.event.location = e.target.value;
+ }
+
+ @action
+ setUrl(e) {
+ this.event.url = e.target.value;
+ }
+
+ @action
+ setDescription(e) {
+ this.event.description = e.target.value;
+ }
+
+ @action
+ setShowLocalTime(e) {
+ this.event.showLocalTime = e.target.checked;
+ }
+
+ @action
+ setMinimal(e) {
+ this.event.minimal = e.target.checked;
+ }
+
+ @action
+ setChatEnabled(e) {
+ this.event.chatEnabled = e.target.checked;
+ }
+
+ @action
+ setReminderValue(reminder, e) {
+ const val = e.target.value;
+ // keep numeric when possible
+ const parsed = val === "" ? null : Number(val);
+ reminder.value = Number.isFinite(parsed) ? parsed : val;
+ }
+
@action
async destroyPostEvent() {
try {
@@ -365,8 +418,10 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.name.label"
class="name"
>
- <Input
- @value={{@model.event.name}}
+ <input
+ type="text"
+ value={{@model.event.name}}
+ {{on "input" this.setName}}
placeholder={{i18n
"discourse_post_event.builder_modal.name.placeholder"
}}
@@ -377,8 +432,10 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.location.label"
class="location"
>
- <Input
- @value={{@model.event.location}}
+ <input
+ type="text"
+ value={{@model.event.location}}
+ {{on "input" this.setLocation}}
placeholder={{i18n
"discourse_post_event.builder_modal.location.placeholder"
}}
@@ -390,8 +447,10 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.url.label"
class="url"
>
- <Input
- @value={{@model.event.url}}
+ <input
+ type="url"
+ value={{@model.event.url}}
+ {{on "input" this.setUrl}}
placeholder={{i18n
"discourse_post_event.builder_modal.url.placeholder"
}}
@@ -403,11 +462,28 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.description.label"
class="description"
>
- <Textarea
- @value={{@model.event.description}}
+ <textarea
+ value={{@model.event.description}}
+ {{on "input" this.setDescription}}
placeholder={{i18n
"discourse_post_event.builder_modal.description.placeholder"
}}
+ ></textarea>
+ </EventField>
+
+ <EventField
+ class="max-attendees"
+ @label="discourse_post_event.builder_modal.max_attendees.label"
+ >
+ <input
+ type="number"
+ min="1"
+ step="1"
+ value={{this.maxAttendeesInput}}
+ {{on "input" this.setMaxAttendees}}
+ placeholder={{i18n
+ "discourse_post_event.builder_modal.max_attendees.placeholder"
+ }}
/>
</EventField>
@@ -427,9 +503,10 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.show_local_time.label"
>
<label class="checkbox-label">
- <Input
- @type="checkbox"
- @checked={{@model.event.showLocalTime}}
+ <input
+ type="checkbox"
+ checked={{@model.event.showLocalTime}}
+ {{on "input" this.setShowLocalTime}}
/>
<span class="message">
{{i18n
@@ -533,13 +610,16 @@ export default class PostEventBuilder extends Component {
class="reminder-type"
/>
- <Input
- @value={{reminder.value}}
+ <input
+ type="number"
+ class="reminder-value"
min="0"
+ step="1"
+ value={{reminder.value}}
+ {{on "input" (fn this.setReminderValue reminder)}}
placeholder={{i18n
"discourse_post_event.builder_modal.name.placeholder"
}}
- class="reminder-value"
/>
<ComboBox
@@ -563,7 +643,6 @@ export default class PostEventBuilder extends Component {
@icon="xmark"
class="remove-reminder"
/>
-
</div>
{{/each}}
</div>
@@ -610,7 +689,11 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.minimal.label"
>
<label class="checkbox-label">
- <Input @type="checkbox" @checked={{@model.event.minimal}} />
+ <input
+ type="checkbox"
+ checked={{@model.event.minimal}}
+ {{on "input" this.setMinimal}}
+ />
<span class="message">
{{i18n
"discourse_post_event.builder_modal.minimal.checkbox_label"
@@ -625,9 +708,10 @@ export default class PostEventBuilder extends Component {
@label="discourse_post_event.builder_modal.allow_chat.label"
>
<label class="checkbox-label">
- <Input
- @type="checkbox"
- @checked={{@model.event.chatEnabled}}
+ <input
+ type="checkbox"
+ checked={{@model.event.chatEnabled}}
+ {{on "input" this.setChatEnabled}}
/>
<span class="message">
{{i18n
@@ -651,15 +735,14 @@ export default class PostEventBuilder extends Component {
<span class="label custom-field-label">
{{allowedCustomField}}
</span>
- <Input
+ <input
+ type="text"
+ class="custom-field-input"
+ value={{get @model.event.customFields allowedCustomField}}
{{on "input" (fn this.setCustomField allowedCustomField)}}
- @value={{readonly
- (get @model.event.customFields allowedCustomField)
- }}
placeholder={{i18n
"discourse_post_event.builder_modal.custom_fields.placeholder"
}}
- class="custom-field-input"
/>
{{/each}}
</EventField>
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js b/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js
index 0e0d2f84050..b58539cdf82 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js
@@ -55,6 +55,10 @@ export function buildParams(startsAt, endsAt, event, siteSettings) {
params.chatEnabled = "true";
}
+ if (event.maxAttendees) {
+ params.maxAttendees = `${event.maxAttendees}`;
+ }
+
if (endsAt) {
params.end = moment(endsAt).tz(eventTz).format("YYYY-MM-DD HH:mm");
}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js b/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js
index e7a32cbc1b7..887411ca6d7 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js
@@ -46,6 +46,8 @@ export default class DiscoursePostEventEvent {
@tracked isClosed;
@tracked isExpired;
@tracked isStandalone;
+ @tracked maxAttendees;
+ @tracked atCapacity;
@tracked recurrenceUntil;
@tracked recurrence;
@tracked customFields;
@@ -80,6 +82,8 @@ export default class DiscoursePostEventEvent {
this.isStandalone = args.is_standalone;
this.minimal = args.minimal;
this.chatEnabled = args.chat_enabled;
+ this.maxAttendees = args.max_attendees;
+ this.atCapacity = args.at_capacity;
this.recurrence = args.recurrence;
this.recurrenceUntil = args.recurrence_until;
this.canUpdateAttendance = args.can_update_attendance;
@@ -164,6 +168,8 @@ export default class DiscoursePostEventEvent {
this.minimal = event.minimal;
this.chatEnabled = event.chatEnabled;
this.rrule = event.rrule;
+ this.maxAttendees = event.maxAttendees;
+ this.atCapacity = event.atCapacity;
this.recurrence = event.recurrence;
this.recurrenceUntil = event.recurrenceUntil;
this.canUpdateAttendance = event.canUpdateAttendance;
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/services/discourse-post-event-api.js b/plugins/discourse-calendar/assets/javascripts/discourse/services/discourse-post-event-api.js
index 81ee439d014..8dfeceecbca 100644
--- a/plugins/discourse-calendar/assets/javascripts/discourse/services/discourse-post-event-api.js
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/services/discourse-post-event-api.js
@@ -63,6 +63,11 @@ export default class DiscoursePostEventApi extends Service {
event.shouldDisplayInvitees =
result.invitee.meta.event_should_display_invitees;
+ const capacity = Number(event.maxAttendees);
+ if (!Number.isNaN(capacity) && capacity > 0) {
+ event.atCapacity = Number(event.stats.going) >= capacity;
+ }
+
return event;
}
@@ -76,6 +81,15 @@ export default class DiscoursePostEventApi extends Service {
if (event.watchingInvitee?.id === invitee.id) {
event.watchingInvitee = null;
}
+
+ if (invitee?.status === "going" && Number(event.stats?.going) > 0) {
+ event.stats.going = Number(event.stats.going) - 1;
+ }
+
+ const capacity = Number(event.maxAttendees);
+ if (!Number.isNaN(capacity) && capacity > 0) {
+ event.atCapacity = Number(event.stats?.going) >= capacity;
+ }
}
async joinEvent(event, data = {}) {
@@ -90,10 +104,19 @@ export default class DiscoursePostEventApi extends Service {
event.sampleInvitees.push(event.watchingInvitee);
}
+ if (invitee?.status === "going") {
+ event.stats.going = Number(event.stats.going || 0) + 1;
+ }
+
event.stats = result.invitee.meta.event_stats;
event.shouldDisplayInvitees =
result.invitee.meta.event_should_display_invitees;
+ const capacity = Number(event.maxAttendees);
+ if (!Number.isNaN(capacity) && capacity > 0) {
+ event.atCapacity = Number(event.stats.going) >= capacity;
+ }
+
return invitee;
}
diff --git a/plugins/discourse-calendar/config/locales/client.en.yml b/plugins/discourse-calendar/config/locales/client.en.yml
index 021ecf720a2..2faa7717c17 100644
--- a/plugins/discourse-calendar/config/locales/client.en.yml
+++ b/plugins/discourse-calendar/config/locales/client.en.yml
@@ -407,6 +407,7 @@ en:
event:
expired: "Expired"
closed: "Closed"
+ full: "Full"
status:
standalone:
title: "Standalone"
@@ -464,6 +465,9 @@ en:
minimal:
label: "Minimal event"
checkbox_label: "Hide Going/Not going buttons and invitees status"
+ max_attendees:
+ label: "Maximum attendees"
+ placeholder: "No limit"
allow_chat:
label: "Chat integration"
checkbox_label: "Create and manage event specific chat channel"
diff --git a/plugins/discourse-calendar/config/locales/server.en.yml b/plugins/discourse-calendar/config/locales/server.en.yml
index effad75473b..ad67308e9bb 100644
--- a/plugins/discourse-calendar/config/locales/server.en.yml
+++ b/plugins/discourse-calendar/config/locales/server.en.yml
@@ -83,6 +83,7 @@ en:
raw_invitees:
only_group: "An event accepts only group names."
ends_at_before_starts_at: "An event can't end before it starts."
+ max_attendees_reached: "This event is full."
start_must_be_present_and_a_valid_date: "An event requires a valid start date."
end_must_be_a_valid_date: "End date must be a valid date."
invalid_recurrence: "Recurrence must be one of: every_month, every_week, every_two_weeks, every_four_weeks, every_day, every_weekday."
diff --git a/plugins/discourse-calendar/db/migrate/20250811120000_add_max_attendees_to_events.rb b/plugins/discourse-calendar/db/migrate/20250811120000_add_max_attendees_to_events.rb
new file mode 100644
index 00000000000..0887eb43069
--- /dev/null
+++ b/plugins/discourse-calendar/db/migrate/20250811120000_add_max_attendees_to_events.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+class AddMaxAttendeesToEvents < ActiveRecord::Migration[7.0]
+ def up
+ add_column :discourse_post_event_events, :max_attendees, :integer
+ end
+
+ def down
+ remove_column :discourse_post_event_events, :max_attendees
+ end
+end
diff --git a/plugins/discourse-calendar/jobs/regular/discourse_post_event/bulk_invite.rb b/plugins/discourse-calendar/jobs/regular/discourse_post_event/bulk_invite.rb
index e3ae223c5e8..aa1d3e7ca44 100644
--- a/plugins/discourse-calendar/jobs/regular/discourse_post_event/bulk_invite.rb
+++ b/plugins/discourse-calendar/jobs/regular/discourse_post_event/bulk_invite.rb
@@ -71,7 +71,15 @@ module Jobs
end
users.each do |user_id|
- create_attendance(user_id, @event.post.id, invitee["attendance"] || "going")
+ # Respect capacity: skip creating new going when full
+ attendance = invitee["attendance"] || "going"
+ if attendance == "going" && @event.at_capacity?
+ save_log "Skipping '#{invitee["identifier"]}' due to max attendees reached"
+ @failed += 1
+ next
+ end
+
+ create_attendance(user_id, @event.post.id, attendance)
end
@processed += 1
diff --git a/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb b/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb
index b1b742c2031..f81c466136c 100644
--- a/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb
+++ b/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb
@@ -18,6 +18,7 @@ module DiscoursePostEvent
:minimal,
:closed,
:"chat-enabled",
+ :"max-attendees",
]
def self.extract_events(post)
diff --git a/plugins/discourse-calendar/spec/fabricators/invitee_fabricator.rb b/plugins/discourse-calendar/spec/fabricators/invitee_fabricator.rb
new file mode 100644
index 00000000000..a9da22690d2
--- /dev/null
+++ b/plugins/discourse-calendar/spec/fabricators/invitee_fabricator.rb
@@ -0,0 +1,3 @@
+# frozen_string_literal: true
+
+Fabricator(:invitee, from: "DiscoursePostEvent::Invitee") {}
diff --git a/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb b/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb
index 18d43df9a14..a3e5505a8bb 100644
--- a/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb
+++ b/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb
@@ -731,3 +731,22 @@ describe DiscoursePostEvent::Event do
end
end
end
+
+describe DiscoursePostEvent::Event, "#capacity" do
+ before do
+ Jobs.run_immediately!
+ SiteSetting.calendar_enabled = true
+ SiteSetting.discourse_post_event_enabled = true
+ end
+
+ it "detects capacity when max_attendees set" do
+ creator = Fabricate(:user)
+ topic = Fabricate(:topic, user: creator)
+ post = Fabricate(:post, user: creator, topic: topic)
+ event = Fabricate(:event, post: post, max_attendees: 1)
+ event.create_invitees(
+ [{ user_id: creator.id, status: DiscoursePostEvent::Invitee.statuses[:going] }],
+ )
+ expect(event.at_capacity?).to eq(true)
+ end
+end
diff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
index 5f69a08e406..b42c81bf82d 100644
--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb
@@ -410,4 +410,46 @@ module DiscoursePostEvent
end
end
end
+
+ describe "bulk invite respects capacity" do
+ before do
+ SiteSetting.calendar_enabled = true
+ SiteSetting.discourse_post_event_enabled = true
+ end
+
+ let(:user) { Fabricate(:user, admin: true) }
+ let(:topic) { Fabricate(:topic, user: user) }
+ let(:post1) { Fabricate(:post, user: user, topic: topic) }
+ let!(:event) { Fabricate(:event, post: post1, max_attendees: 1) }
+
+ it "skips creating going when full" do
+ sign_in(user)
+ user1 = Fabricate(:user)
+ user2 = Fabricate(:user)
+
+ expect_enqueued_with(
+ job: :discourse_post_event_bulk_invite,
+ args: {
+ "event_id" => event.id,
+ "invitees" => [
+ { "identifier" => user1.username, "attendance" => "going" },
+ { "identifier" => user2.username, "attendance" => "going" },
+ ],
+ "current_user_id" => user.id,
+ },
+ ) do
+ post "/discourse-post-event/events/#{event.id}/bulk-invite.json",
+ params: {
+ invitees: [
+ { "identifier" => user1.username, "attendance" => "going" },
+ { "identifier" => user2.username, "attendance" => "going" },
+ ],
+ }
+ end
+
+ Jobs.run_immediately!
+ event.reload
+ expect(event.invitees.with_status(:going).count).to be <= 1
+ end
+ end
end
diff --git a/plugins/discourse-calendar/spec/requests/invitees_controller_spec.rb b/plugins/discourse-calendar/spec/requests/invitees_controller_spec.rb
index 7cc3b07b292..cfcbaa36d77 100644
--- a/plugins/discourse-calendar/spec/requests/invitees_controller_spec.rb
+++ b/plugins/discourse-calendar/spec/requests/invitees_controller_spec.rb
@@ -300,6 +300,101 @@ module DiscoursePostEvent
expect(invitee.post_id).to eq(post_1.id)
end
end
+
+ context "when event has max attendees and is full" do
+ fab!(:post_2) { create_post(user: Fabricate(:admin), category: Fabricate(:category)) }
+ fab!(:user_a) { Fabricate(:user) }
+ fab!(:user_b) { Fabricate(:user) }
+ fab!(:post_event_full) do
+ pe = Fabricate(:event, post: post_2, max_attendees: 1)
+ pe.create_invitees([{ user_id: user_a.id, status: Invitee.statuses[:going] }])
+ pe
+ end
+
+ context "when updating an invitee" do
+ fab!(:invitee) do
+ Fabricate(
+ :invitee,
+ post_id: post_2.id,
+ user_id: user_b.id,
+ status: Invitee.statuses[:interested],
+ )
+ end
+
+ context "when invitee was interested" do
+ it "returns 422 when trying to join as going" do
+ sign_in(user_b)
+
+ put "/discourse-post-event/events/#{post_event_full.id}/invitees/#{invitee.id}.json",
+ params: {
+ invitee: {
+ status: "going",
+ },
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].join).to include("full")
+ end
+ end
+
+ context "when invitee was going" do
+ it "allows upading to interested" do
+ sign_in(user_b)
+
+ put "/discourse-post-event/events/#{post_event_full.id}/invitees/#{invitee.id}.json",
+ params: {
+ invitee: {
+ status: "interested",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ end
+ end
+ end
+
+ context "when creating an invitee" do
+ it "returns 422 when trying to join as going" do
+ sign_in(user_b)
+
+ post "/discourse-post-event/events/#{post_event_full.id}/invitees.json",
+ params: {
+ invitee: {
+ status: "going",
+ },
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].join).to include("full")
+ end
+
+ it "allows interested when full" do
+ sign_in(user_b)
+
+ post "/discourse-post-event/events/#{post_event_full.id}/invitees.json",
+ params: {
+ invitee: {
+ status: "interested",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ end
+
+ it "allows not_going when full" do
+ sign_in(user_b)
+
+ post "/discourse-post-event/events/#{post_event_full.id}/invitees.json",
+ params: {
+ invitee: {
+ status: "not_going",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ end
+ end
+ end
end
end
end
diff --git a/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb b/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb
index 200dfa2395e..c6a615073b0 100644
--- a/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb
+++ b/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb
@@ -35,7 +35,13 @@ describe DiscoursePostEvent::EventSerializer do
it "returns the correct stats" do
json = DiscoursePostEvent::EventSerializer.new(private_event, scope: Guardian.new).as_json
- expect(json[:event][:stats]).to eq(going: 1, interested: 0, invited: 2, not_going: 0)
+ expect(json[:event][:stats]).to eq(
+ going: 1,
+ interested: 0,
+ invited: 2,
+ not_going: 0,
+ capacity: nil,
+ )
end
end
end
diff --git a/plugins/discourse-calendar/spec/system/post_event_spec.rb b/plugins/discourse-calendar/spec/system/post_event_spec.rb
index 690dccf232e..3f7d9d5a76a 100644
--- a/plugins/discourse-calendar/spec/system/post_event_spec.rb
+++ b/plugins/discourse-calendar/spec/system/post_event_spec.rb
@@ -80,6 +80,37 @@ describe "Post event", type: :system do
end
end
+ context "with max attendees" do
+ it "updates the going button label from Full after toggling" do
+ post =
+ PostCreator.create(
+ admin,
+ title: "Max attendees event",
+ raw: "[event status='public' start='2222-02-22 14:22' max-attendees='1']\n[/event]",
+ )
+
+ visit(post.topic.url)
+
+ # First click: join the event; since max is 1, it reaches capacity and shows Full
+ post_event_page.going
+ expect(page).to have_css(
+ ".going-button",
+ text: I18n.t("js.discourse_post_event.models.event.full"),
+ )
+
+ # Second click: leave the event; label should no longer be Full
+ post_event_page.going
+ expect(page).to have_no_css(
+ ".going-button",
+ text: I18n.t("js.discourse_post_event.models.event.full"),
+ )
+ expect(page).to have_css(
+ ".going-button",
+ text: I18n.t("js.discourse_post_event.models.invitee.status.going"),
+ )
+ end
+ end
+
context "when showing local time", timezone: "Australia/Brisbane" do
it "correctly shows month/day" do
page.driver.with_playwright_page do |pw_page|