Skip to content

Commit

Permalink
Allow to force rename a conflicting calendar
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Dec 8, 2020
1 parent e02c9ec commit 1013ca6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
4 changes: 3 additions & 1 deletion apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2744,11 +2744,13 @@ public function purgeAllCachedEventsForSubscription($subscriptionId) {
* @param string $uriName
* @param string $uriOrigin
* @param string $uriDestination
* @param string $newUriName (optional) the new uriName
*/
public function moveCalendar($uriName, $uriOrigin, $uriDestination) {
public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) {
$query = $this->db->getQueryBuilder();
$query->update('calendars')
->set('principaluri', $query->createNamedParameter($uriDestination))
->set('uri', $query->createNamedParameter($newUriName ?: $uriName))
->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
->execute();
Expand Down
59 changes: 54 additions & 5 deletions apps/dav/lib/Command/MoveCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ protected function configure() {
->addArgument('destinationuid',
InputArgument::REQUIRED,
'User who will receive the calendar')
->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares");
->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares")
->addOption('force-rename', null, InputOption::VALUE_NONE, "Rename the calendar in case of conflicts");
}

protected function execute(InputInterface $input, OutputInterface $output): int {
Expand All @@ -122,25 +123,73 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$name = $input->getArgument('name');
$newName = null;

$calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);

if (null === $calendar) {
throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user.");
}

if (null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) {
throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
// Calendar already exists
if ($this->calendarExists($userDestination, $name)) {
if ($input->getOption('force-rename')) {
// Try to find a suitable name
$newName = $this->getNewCalendarName($userDestination, $name);

// If we didn't find a suitable value after all the iterations, give up
if ($this->calendarExists($userDestination, $newName)) {
throw new \InvalidArgumentException("Unable to find a suitable calendar name for <$userDestination> with initial name <$name>.");
}
} else {
throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
}
}

$this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));

$this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination);
$this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination, $newName);

$this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>");
$this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination> as <$newName>");
return 0;
}

/**
* Check if the calendar exists for user
*
* @param string $userDestination
* @param string $name
* @return bool
*/
protected function calendarExists(string $userDestination, string $name): bool {
return null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name);
}

/**
* Try to find a suitable new calendar name that
* doesn't exists for the provided user
*
* @param string $userDestination
* @param string $name
* @return string
*/
protected function getNewCalendarName(string $userDestination, string $name): string {
$increment = 1;
$newName = $name . '-' . $increment;
while ($increment <= 10) {
$this->io->writeln("Trying calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
if (!$this->calendarExists($userDestination, $newName)) {
// New name is good to go
$this->io->writeln("Found proper new calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
break;
}
$newName = $name . '-' . $increment;
$increment++;
}

return $newName;
}

/**
* Check that moving the calendar won't break shares
*
Expand Down

0 comments on commit 1013ca6

Please sign in to comment.