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

Commit 9fa07714 authored by Akhil's avatar Akhil 🙂
Browse files

Merge branch 'dev/jitsi-server-side' into 'murena-main'

Jitsi calendar

See merge request !44
parents 4aaa4400 71967e23
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@
	<author>Anna Larch</author>
	<author homepage="https://github.com/nextcloud/groupware">Nextcloud Groupware Team</author>
	<namespace>Calendar</namespace>
	<types>
		<dav/>
	</types>
	<documentation>
		<user>https://docs.nextcloud.com/server/latest/user_manual/en/groupware/calendar.html</user>
		<admin>https://docs.nextcloud.com/server/latest/admin_manual/groupware/calendar.html</admin>
+14 −0
Original line number Diff line number Diff line
@@ -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);
			}
		});

	}
}
+109 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace OCA\Calendar\Dav;

use Sabre\DAV\INode;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\VObject\Reader;

class BeforeCreateFilePlugin extends ServerPlugin {

	/**
	 * A reference to the main Server class.
	 *
	 * @var \Sabre\DAV\Server
	 */
	protected $server;

	/**
	 * Initialize the plugin.
	 *
	 * This is called automatically be the Server class after this plugin is
	 * added with Sabre\DAV\Server::addPlugin()
	 */
	public function initialize(Server $server) {
		$this->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);
		}
	}
}