Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable3.5] fix(appointments): Rate limit config creation and booking #5683

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions lib/Controller/AppointmentConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\Calendar\Service\Appointments\AppointmentConfigService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use function array_keys;
Expand Down Expand Up @@ -148,7 +149,9 @@ private function validateAvailability(array $availability): void {
* @param int|null $end
* @param int|null $futureLimit
* @return JsonResponse
* @UserRateThrottle(limit=10, period=1200)
*/
#[UserRateLimit(limit: 10, period: 1200)]
public function create(
string $name,
string $description,
Expand Down
7 changes: 7 additions & 0 deletions lib/Controller/BookingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use OCA\Calendar\Service\Appointments\BookingService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -163,7 +165,12 @@ public function getBookableSlots(int $appointmentConfigId,
* @param string $description
* @param string $timeZone
* @return JsonResponse
*
* @AnonRateThrottle(limit=10, period=1200)
* @UserRateThrottle(limit=10, period=300)
*/
#[AnonRateLimit(limit: 10, period: 1200)]
#[UserRateLimit(limit: 10, period: 300)]
public function bookSlot(int $appointmentConfigId,
int $start,
int $end,
Expand Down
11 changes: 10 additions & 1 deletion src/components/AppointmentConfigModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@
</div>
</fieldset>
</div>
<button class="primary appointment-config-modal__submit-button"
<div v-if="rateLimitingReached">
{{ t('calendar', 'It seems a rate limit has been reached. Please try again later.') }}
</div>
<button class="appointment-config-modal__submit-button"
:disabled="!editing.name || editing.length === 0"
@click="save">
{{ saveButtonText }}
Expand Down Expand Up @@ -184,6 +187,7 @@
enablePreparationDuration: false,
enableFollowupDuration: false,
enableFutureLimit: false,
rateLimitingReached: false,
showConfirmation: false,
}
},
Expand Down Expand Up @@ -267,6 +271,8 @@
this.editing.calendarFreeBusyUris = this.editing.calendarFreeBusyUris.filter(uri => uri !== this.calendarUrlToUri(calendar.url))
},
async save() {
this.rateLimitingReached = false

Check warning on line 274 in src/components/AppointmentConfigModal.vue

View check run for this annotation

Codecov / codecov/patch

src/components/AppointmentConfigModal.vue#L274

Added line #L274 was not covered by tests

if (!this.enablePreparationDuration) {
this.editing.preparationDuration = this.defaultConfig.preparationDuration
}
Expand All @@ -292,6 +298,9 @@
}
this.showConfirmation = true
} catch (error) {
if (error?.response?.status === 429) {
this.rateLimitingReached = true

Check warning on line 302 in src/components/AppointmentConfigModal.vue

View check run for this annotation

Codecov / codecov/patch

src/components/AppointmentConfigModal.vue#L301-L302

Added lines #L301 - L302 were not covered by tests
}
logger.error('Failed to save config', { error, config, isNew: this.isNew })
}
},
Expand Down
8 changes: 8 additions & 0 deletions src/components/Appointments/AppointmentDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
autocomplete="off" />
</div>
</div>
<div v-if="showRateLimitingWarning"
class="booking-error">
{{ $t('calendar', 'It seems a rate limit has been reached. Please try again later.') }}
</div>
<div v-if="showError"
class="booking-error">
{{ $t('calendar', 'Could not book the appointment. Please try again later or contact the organizer.') }}
Expand Down Expand Up @@ -101,6 +105,10 @@ export default {
required: true,
type: String,
},
showRateLimitingWarning: {
required: true,
type: Boolean,
},
showError: {
required: true,
type: Boolean,
Expand Down
9 changes: 8 additions & 1 deletion src/views/Appointments/Booking.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
:visitor-info="visitorInfo"
:time-zone-id="timeZone"
:show-error="bookingError"
:show-rate-limiting-warning="bookingRateLimit"
@save="onSave"
@close="selectedSlot = undefined" />
</div>
Expand Down Expand Up @@ -146,6 +147,7 @@
selectedSlot: undefined,
bookingConfirmed: false,
bookingError: false,
bookingRateLimit: false,
}
},
watch: {
Expand Down Expand Up @@ -206,6 +208,7 @@
})

this.bookingError = false
this.bookingRateLimit = false

Check warning on line 211 in src/views/Appointments/Booking.vue

View check run for this annotation

Codecov / codecov/patch

src/views/Appointments/Booking.vue#L211

Added line #L211 was not covered by tests
try {
await bookSlot(this.config, slot, displayName, email, description, timeZone)

Expand All @@ -215,7 +218,11 @@
this.bookingConfirmed = true
} catch (e) {
console.error('could not book appointment', e)
this.bookingError = true
if (e?.response?.status === 429) {
this.bookingRateLimit = true
} else {
this.bookingError = true

Check warning on line 224 in src/views/Appointments/Booking.vue

View check run for this annotation

Codecov / codecov/patch

src/views/Appointments/Booking.vue#L221-L224

Added lines #L221 - L224 were not covered by tests
}
}
},
onSlotClicked(slot) {
Expand Down
Loading