Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 5a1f2965 authored by Christoph Wurst's avatar Christoph Wurst Committed by GitHub
Browse files

Merge pull request #3773 from nextcloud/fix/add-transparency-and-cancelled-check

Check for TRANSP and CANCELLED property when conflict checking appointment slots
parents 76ef5016 82acd3af
Loading
Loading
Loading
Loading
+26 −4
Original line number Diff line number Diff line
@@ -26,17 +26,22 @@ declare(strict_types=1);
namespace OCA\Calendar\Service\Appointments;

use DateInterval;
use DateTimeInterface;
use OCA\Calendar\Db\AppointmentConfig;
use OCP\Calendar\IManager;
use Psr\Log\LoggerInterface;
use function array_filter;

class EventConflictFilter {

	/** @var IManager */
	private $calendarManager;
	private $logger;

	public function __construct(IManager $calendarManager) {
	public function __construct(IManager $calendarManager,
					LoggerInterface $logger) {
		$this->calendarManager = $calendarManager;
		$this->logger = $logger;
	}

	/**
@@ -56,14 +61,31 @@ class EventConflictFilter {
		}
		// Always check the target calendar for conflicts
		$query->addSearchCalendar($config->getTargetCalendarUri());
		$query->addType('VEVENT');
		$preparationDuration = DateInterval::createFromDateString($config->getPreparationDuration() . ' seconds');
		$followUpDuration = DateInterval::createFromDateString($config->getFollowupDuration() . ' seconds');

		return array_filter($slots, function (Interval $slot) use ($followUpDuration, $preparationDuration, $query): bool {
		return array_filter($slots, function (Interval $slot) use ($followUpDuration, $preparationDuration, $query, $config): bool {
			$query->setTimerangeStart($slot->getStartAsObject()->sub($preparationDuration));
			$query->setTimerangeEnd($slot->getEndAsObject()->add($followUpDuration));

			$objects = $this->calendarManager->searchForPrincipal($query);
			$events = $this->calendarManager->searchForPrincipal($query);

			$objects = array_filter($events, static function (array $event) {
				$isCancelled = ($event['objects'][0]['STATUS'][0] ?? null) === 'CANCELLED';
				$isFree = ($event['objects'][0]['TRANSP'][0] ?? null) === 'TRANSPARENT';
				return !$isCancelled && !$isFree;
			});

			$uids = array_map(static function ($object) {
				return $object['uid'];
			}, $objects);

			$this->logger->debug('Appointment config ' . $config->getToken() . ' is looking within {start} and {followup} in calendar {calendarUri}. Conflicting UIDs are {uids}', [
				'start' => $slot->getStartAsObject()->sub($preparationDuration)->format(DateTimeInterface::ATOM),
				'end' => $slot->getEndAsObject()->add($followUpDuration)->format(DateTimeInterface::ATOM),
				'calendarUri' => $config->getTargetCalendarUri(),
				'uids' => implode(' : ', $uids)
			]);

			// If there is at least one event at this time then the slot is taken
			return empty($objects);
+7 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ use OCA\Calendar\Service\Appointments\Interval;
use OCP\Calendar\ICalendarQuery;
use OCP\Calendar\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

class EventConflictFilterTest extends TestCase {

@@ -41,6 +42,9 @@ class EventConflictFilterTest extends TestCase {
	/** @var EventConflictFilter */
	private $filter;

	/** @var mixed|MockObject|LoggerInterface */
	private $logger;

	protected function setUp(): void {
		parent::setUp();

@@ -49,9 +53,11 @@ class EventConflictFilterTest extends TestCase {
		}

		$this->calendarManager = $this->createMock(IManager::class);
		$this->logger = $this->createMock(LoggerInterface::class);

		$this->filter = new EventConflictFilter(
			$this->calendarManager,
			$this->logger
		);
	}

@@ -89,7 +95,7 @@ class EventConflictFilterTest extends TestCase {
			->method('searchForPrincipal')
			->with($query)
			->willReturn([
				['UID' => 'abc'],
				['uid' => 'abc'],
			]);

		$filtered = $this->filter->filter($config, $slots);