Skip to content

Commit

Permalink
Implement calendar export and import
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Feb 16, 2022
1 parent 393d581 commit cbb81df
Show file tree
Hide file tree
Showing 10 changed files with 727 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<command>OCA\DAV\Command\SyncBirthdayCalendar</command>
<command>OCA\DAV\Command\SyncSystemAddressBook</command>
<command>OCA\DAV\Command\RemoveInvalidShares</command>
<command>OCA\DAV\Command\ExportCalendars</command>
<command>OCA\DAV\Command\ImportCalendar</command>
</commands>

<settings>
Expand Down
5 changes: 5 additions & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
'OCA\\DAV\\Command\\CreateAddressBook' => $baseDir . '/../lib/Command/CreateAddressBook.php',
'OCA\\DAV\\Command\\CreateCalendar' => $baseDir . '/../lib/Command/CreateCalendar.php',
'OCA\\DAV\\Command\\DeleteCalendar' => $baseDir . '/../lib/Command/DeleteCalendar.php',
'OCA\\DAV\\Command\\ExportCalendars' => $baseDir . '/../lib/Command/ExportCalendars.php',
'OCA\\DAV\\Command\\ImportCalendar' => $baseDir . '/../lib/Command/ImportCalendar.php',
'OCA\\DAV\\Command\\ListCalendars' => $baseDir . '/../lib/Command/ListCalendars.php',
'OCA\\DAV\\Command\\MoveCalendar' => $baseDir . '/../lib/Command/MoveCalendar.php',
'OCA\\DAV\\Command\\RemoveInvalidShares' => $baseDir . '/../lib/Command/RemoveInvalidShares.php',
Expand Down Expand Up @@ -298,4 +300,7 @@
'OCA\\DAV\\Upload\\UploadFile' => $baseDir . '/../lib/Upload/UploadFile.php',
'OCA\\DAV\\Upload\\UploadFolder' => $baseDir . '/../lib/Upload/UploadFolder.php',
'OCA\\DAV\\Upload\\UploadHome' => $baseDir . '/../lib/Upload/UploadHome.php',
'OCA\\DAV\\UserMigration\\CalendarMigrator' => $baseDir . '/../lib/UserMigration/CalendarMigrator.php',
'OCA\\DAV\\UserMigration\\CalendarMigratorException' => $baseDir . '/../lib/UserMigration/CalendarMigratorException.php',
'OCA\\DAV\\UserMigration\\InvalidCalendarException' => $baseDir . '/../lib/UserMigration/InvalidCalendarException.php',
);
5 changes: 5 additions & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Command\\CreateAddressBook' => __DIR__ . '/..' . '/../lib/Command/CreateAddressBook.php',
'OCA\\DAV\\Command\\CreateCalendar' => __DIR__ . '/..' . '/../lib/Command/CreateCalendar.php',
'OCA\\DAV\\Command\\DeleteCalendar' => __DIR__ . '/..' . '/../lib/Command/DeleteCalendar.php',
'OCA\\DAV\\Command\\ExportCalendars' => __DIR__ . '/..' . '/../lib/Command/ExportCalendars.php',
'OCA\\DAV\\Command\\ImportCalendar' => __DIR__ . '/..' . '/../lib/Command/ImportCalendar.php',
'OCA\\DAV\\Command\\ListCalendars' => __DIR__ . '/..' . '/../lib/Command/ListCalendars.php',
'OCA\\DAV\\Command\\MoveCalendar' => __DIR__ . '/..' . '/../lib/Command/MoveCalendar.php',
'OCA\\DAV\\Command\\RemoveInvalidShares' => __DIR__ . '/..' . '/../lib/Command/RemoveInvalidShares.php',
Expand Down Expand Up @@ -313,6 +315,9 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Upload\\UploadFile' => __DIR__ . '/..' . '/../lib/Upload/UploadFile.php',
'OCA\\DAV\\Upload\\UploadFolder' => __DIR__ . '/..' . '/../lib/Upload/UploadFolder.php',
'OCA\\DAV\\Upload\\UploadHome' => __DIR__ . '/..' . '/../lib/Upload/UploadHome.php',
'OCA\\DAV\\UserMigration\\CalendarMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/CalendarMigrator.php',
'OCA\\DAV\\UserMigration\\CalendarMigratorException' => __DIR__ . '/..' . '/../lib/UserMigration/CalendarMigratorException.php',
'OCA\\DAV\\UserMigration\\InvalidCalendarException' => __DIR__ . '/..' . '/../lib/UserMigration/InvalidCalendarException.php',
);

public static function getInitializer(ClassLoader $loader)
Expand Down
7 changes: 7 additions & 0 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function getKey() {
return $this->calendarInfo['id'];
}

/**
* @inheritDoc
*/
public function getUri() {
return $this->calendarInfo['uri'];
}

/**
* In comparison to getKey() this function returns a human readable (maybe translated) name
* @return null|string
Expand Down
83 changes: 83 additions & 0 deletions apps/dav/lib/Command/ExportCalendars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/**
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
*
* @author Christopher Ng <chrng8@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\DAV\Command;

use OC\Core\Command\Base;
use OCA\DAV\UserMigration\CalendarMigrator;
use OCA\DAV\UserMigration\CalendarMigratorException;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExportCalendars extends Base {

/** @var IUserManager */
private $userManager;

/** @var CalendarMigrator */
private $calendarMigrator;

public function __construct(
IUserManager $userManager,
CalendarMigrator $calendarMigrator
) {
parent::__construct();
$this->userManager = $userManager;
$this->calendarMigrator = $calendarMigrator;
}

protected function configure() {
$this
->setName('dav:export-calendars')
->setDescription('Export the calendars of a user')
->addArgument(
'user',
InputArgument::REQUIRED,
'User to export',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $this->userManager->get($input->getArgument('user'));

if (!$user instanceof IUser) {
$output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
return 1;
}

try {
$this->calendarMigrator->export($user, $output);
} catch (CalendarMigratorException $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return $e->getCode() !== 0 ? $e->getCode() : 1;
}

return 0;
}
}
94 changes: 94 additions & 0 deletions apps/dav/lib/Command/ImportCalendar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

/**
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
*
* @author Christopher Ng <chrng8@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\DAV\Command;

use OC\Core\Command\Base;
use OCA\DAV\UserMigration\CalendarMigrator;
use OCA\DAV\UserMigration\CalendarMigratorException;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ImportCalendar extends Base {

/** @var IUserManager */
private $userManager;

/** @var CalendarMigrator */
private $calendarMigrator;

public function __construct(
IUserManager $userManager,
CalendarMigrator $calendarMigrator
) {
parent::__construct();
$this->userManager = $userManager;
$this->calendarMigrator = $calendarMigrator;
}

protected function configure() {
$this
->setName('dav:import-calendar')
->setDescription('Import a calendar to a user\'s account')
->addArgument(
'user',
InputArgument::REQUIRED,
'User to import the calendar for',
)
->addArgument(
'path',
InputArgument::REQUIRED,
'Path to the *.ics file',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $this->userManager->get($input->getArgument('user'));

[
'basename' => $filename,
'dirname' => $srcDir,
] = pathinfo($input->getArgument('path'));


if (!$user instanceof IUser) {
$output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
return 1;
}

try {
$this->calendarMigrator->import($user, $srcDir, $filename, $output);
} catch (CalendarMigratorException $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return $e->getCode() !== 0 ? $e->getCode() : 1;
}

return 0;
}
}
Loading

0 comments on commit cbb81df

Please sign in to comment.