diff --git a/appinfo/info.xml b/appinfo/info.xml index 449f49c5dea2a2315cd94af9daa8c0c669414900..3f09c08ae79195bdc6b84be25dc0e400e280a0a7 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -20,6 +20,9 @@ Anna Larch Nextcloud Groupware Team Calendar + + + https://docs.nextcloud.com/server/latest/user_manual/en/groupware/calendar.html https://docs.nextcloud.com/server/latest/admin_manual/groupware/calendar.html diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 39c29d82f8a5c1336b34b47178204ee36dee0e32..3156b97c2879cea34af27799d8d801b19e2c68de 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace OCA\Calendar\AppInfo; use OCA\Calendar\Dashboard\CalendarWidget; +use OCA\Calendar\Dav\BeforeCreateFilePlugin; use OCA\Calendar\Listener\UserDeletedListener; use OCA\Calendar\Middleware\InvitationMiddleware; use OCA\Calendar\Notification\Notifier; @@ -35,6 +36,7 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\BackgroundJob\IJobList; use OCP\Calendar\IManager; use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; @@ -43,6 +45,7 @@ use OCP\IRequest; use OCP\IUserManager; use OCP\L10N\IFactory as L10NFactory; use OCP\Mail\IMailer; +use OCP\SabrePluginEvent; use OCP\User\Events\UserDeletedEvent; use function method_exists; @@ -126,5 +129,16 @@ class Application extends App implements IBootstrap { // executed in the order that it is registered $containerinvite->registerMiddleware("InvitationMiddleware"); + + $serverContainer = $context->getServerContainer(); + $eventDispatcher = $serverContainer->get(IEventDispatcher::class); + $eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) use ($context) { + $eventServer = $event->getServer(); + if ($eventServer !== null) { + $plugin = $context->getAppContainer()->get(BeforeCreateFilePlugin::class); + $eventServer->addPlugin($plugin); + } + }); + } } diff --git a/lib/Dav/BeforeCreateFilePlugin.php b/lib/Dav/BeforeCreateFilePlugin.php new file mode 100644 index 0000000000000000000000000000000000000000..373fa13f3f4f5de87815f9d1654df7bf180fa3d4 --- /dev/null +++ b/lib/Dav/BeforeCreateFilePlugin.php @@ -0,0 +1,109 @@ +server = $server; + $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); + $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); + } + + + + /** + * Assign jitsi link to location + * + * @param string $uri target file URI + * @param resource $data data + * @param INode $parent Sabre Node + * @param bool $modified modified + * @return + */ + + public function beforeCreateFile($uri, &$data, INode $parent, &$modified) { + if (is_resource($data)) { + $data = stream_get_contents($data); + } + // Parse the data as a vCalendar object using Sabre\VObject. + try { + $vCalendar = Reader::read($data); + } catch (\Exception $e) { + // The data is not a valid vCalendar object. + return; + } + $this->processEventData($data); + + } + + /** + * Assign jitsi link to location + * + * @param string $uri target file URI + * @param INode $parent Sabre Node + * @param resource $data data + * @param bool $modified modified + * @return + */ + + public function beforeWriteContent($uri, INode $parent, &$data, &$modified) { + if (is_resource($data)) { + $data = stream_get_contents($data); + } + // Parse the data as a vCalendar object using Sabre\VObject. + try { + $vCalendar = Reader::read($data); + } catch (\Exception $e) { + // The data is not a valid vCalendar object. + return; + } + $this->processEventData($data); + } + + /** + * Process event data and assign a Jitsi link to the event location. + * + * @param string $data Data + */ + private function processEventData(&$data) { + // Check if the data contains 'ATTENDEE;' but not 'LOCATION:' + if (strpos($data, 'ATTENDEE;') !== false && strpos($data, 'LOCATION:') === false) { + // Set the base URL for the meeting + $baseURL = 'https://meet.jit.si/'; + + // Generate a random alphanumeric string of length 8 + $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; + $randomString = substr(str_shuffle($chars), 0, 8); + + // Append the current date to the random string + $randomString .= date('dmy'); + + // Create the complete meeting URL + $meetingURL = $baseURL . $randomString; + + // Replace the BEGIN:VEVENT/ with 'BEGIN:VEVENT' followed by 'LOCATION:' + $data = preg_replace('/BEGIN:VEVENT/', "BEGIN:VEVENT\nLOCATION:$meetingURL", $data); + } + } +}