Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: move state init to a service #6103

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 7 additions & 103 deletions lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@
namespace OCA\Calendar\Controller;

use OC\App\CompareVersion;
use OCA\Calendar\Service\Appointments\AppointmentConfigService;
use OCA\Calendar\Service\CalendarInitialStateService;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IRequest;
use function in_array;

class ViewController extends Controller {
/** @var IConfig */
private $config;

/** @var AppointmentConfigService */
private $appointmentConfigService;

/** @var IInitialState */
private $initialStateService;

/** @var IAppManager */
private $appManager;
Expand All @@ -42,23 +35,19 @@ class ViewController extends Controller {

private IAppData $appData;

private CalendarInitialStateService $calendarInitialStateService;

public function __construct(string $appName,
IRequest $request,
IConfig $config,
AppointmentConfigService $appointmentConfigService,
IInitialState $initialStateService,
IAppManager $appManager,
CompareVersion $compareVersion,
?string $userId,
IAppData $appData) {
IAppData $appData,
CalendarInitialStateService $calendarInitialStateService) {
parent::__construct($appName, $request);
$this->config = $config;
$this->appointmentConfigService = $appointmentConfigService;
$this->initialStateService = $initialStateService;
$this->appManager = $appManager;
$this->compareVersion = $compareVersion;
$this->userId = $userId;
$this->appData = $appData;
$this->calendarInitialStateService = $calendarInitialStateService;
}

/**
Expand All @@ -70,96 +59,11 @@ public function __construct(string $appName,
* @return TemplateResponse
*/
public function index():TemplateResponse {
$defaultEventLimit = $this->config->getAppValue($this->appName, 'eventLimit', 'yes');
$defaultInitialView = $this->config->getAppValue($this->appName, 'currentView', 'dayGridMonth');
$defaultShowWeekends = $this->config->getAppValue($this->appName, 'showWeekends', 'yes');
$defaultWeekNumbers = $this->config->getAppValue($this->appName, 'showWeekNr', 'no');
$defaultSkipPopover = $this->config->getAppValue($this->appName, 'skipPopover', 'no');
$defaultTimezone = $this->config->getAppValue($this->appName, 'timezone', 'automatic');
$defaultSlotDuration = $this->config->getAppValue($this->appName, 'slotDuration', '00:30:00');
$defaultDefaultReminder = $this->config->getAppValue($this->appName, 'defaultReminder', 'none');
$defaultShowTasks = $this->config->getAppValue($this->appName, 'showTasks', 'yes');

$appVersion = $this->config->getAppValue($this->appName, 'installed_version', '');
$eventLimit = $this->config->getUserValue($this->userId, $this->appName, 'eventLimit', $defaultEventLimit) === 'yes';
$firstRun = $this->config->getUserValue($this->userId, $this->appName, 'firstRun', 'yes') === 'yes';
$initialView = $this->getView($this->config->getUserValue($this->userId, $this->appName, 'currentView', $defaultInitialView));
$showWeekends = $this->config->getUserValue($this->userId, $this->appName, 'showWeekends', $defaultShowWeekends) === 'yes';
$showWeekNumbers = $this->config->getUserValue($this->userId, $this->appName, 'showWeekNr', $defaultWeekNumbers) === 'yes';
$skipPopover = $this->config->getUserValue($this->userId, $this->appName, 'skipPopover', $defaultSkipPopover) === 'yes';
$timezone = $this->config->getUserValue($this->userId, $this->appName, 'timezone', $defaultTimezone);
$attachmentsFolder = $this->config->getUserValue($this->userId, 'dav', 'attachmentsFolder', '/Calendar');
$slotDuration = $this->config->getUserValue($this->userId, $this->appName, 'slotDuration', $defaultSlotDuration);
$defaultReminder = $this->config->getUserValue($this->userId, $this->appName, 'defaultReminder', $defaultDefaultReminder);
$showTasks = $this->config->getUserValue($this->userId, $this->appName, 'showTasks', $defaultShowTasks) === 'yes';
$hideEventExport = $this->config->getAppValue($this->appName, 'hideEventExport', 'no') === 'yes';
$disableAppointments = $this->config->getAppValue($this->appName, 'disableAppointments', 'no') === 'yes';
$forceEventAlarmType = $this->config->getAppValue($this->appName, 'forceEventAlarmType', '');
if (!in_array($forceEventAlarmType, ['DISPLAY', 'EMAIL'], true)) {
$forceEventAlarmType = false;
}
$canSubscribeLink = $this->config->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes';
$showResources = $this->config->getAppValue($this->appName, 'showResources', 'yes') === 'yes';
$publicCalendars = $this->config->getAppValue($this->appName, 'publicCalendars', '');

$talkEnabled = $this->appManager->isEnabledForUser('spreed');
$talkApiVersion = version_compare($this->appManager->getAppVersion('spreed'), '12.0.0', '>=') ? 'v4' : 'v1';
$tasksEnabled = $this->appManager->isEnabledForUser('tasks');

$circleVersion = $this->appManager->getAppVersion('circles');
$isCirclesEnabled = $this->appManager->isEnabledForUser('circles') === true;
// if circles is not installed, we use 0.0.0
$isCircleVersionCompatible = $this->compareVersion->isCompatible($circleVersion ? $circleVersion : '0.0.0', '22');

$this->initialStateService->provideInitialState('app_version', $appVersion);
$this->initialStateService->provideInitialState('event_limit', $eventLimit);
$this->initialStateService->provideInitialState('first_run', $firstRun);
$this->initialStateService->provideInitialState('initial_view', $initialView);
$this->initialStateService->provideInitialState('show_weekends', $showWeekends);
$this->initialStateService->provideInitialState('show_week_numbers', $showWeekNumbers);
$this->initialStateService->provideInitialState('skip_popover', $skipPopover);
$this->initialStateService->provideInitialState('talk_enabled', $talkEnabled);
$this->initialStateService->provideInitialState('talk_api_version', $talkApiVersion);
$this->initialStateService->provideInitialState('timezone', $timezone);
$this->initialStateService->provideInitialState('attachments_folder', $attachmentsFolder);
$this->initialStateService->provideInitialState('slot_duration', $slotDuration);
$this->initialStateService->provideInitialState('default_reminder', $defaultReminder);
$this->initialStateService->provideInitialState('show_tasks', $showTasks);
$this->initialStateService->provideInitialState('tasks_enabled', $tasksEnabled);
$this->initialStateService->provideInitialState('hide_event_export', $hideEventExport);
$this->initialStateService->provideInitialState('force_event_alarm_type', $forceEventAlarmType);
$this->initialStateService->provideInitialState('appointmentConfigs', $this->appointmentConfigService->getAllAppointmentConfigurations($this->userId));
$this->initialStateService->provideInitialState('disable_appointments', $disableAppointments);
$this->initialStateService->provideInitialState('can_subscribe_link', $canSubscribeLink);
$this->initialStateService->provideInitialState('show_resources', $showResources);
$this->initialStateService->provideInitialState('isCirclesEnabled', $isCirclesEnabled && $isCircleVersionCompatible);
$this->initialStateService->provideInitialState('publicCalendars', $publicCalendars);

$this->calendarInitialStateService->run();
return new TemplateResponse($this->appName, 'main');
}

/**
* Makes sure we don't use the old views anymore
*
* @param string $view
* @return string
*/
private function getView(string $view): string {
switch ($view) {
case 'agendaDay':
return 'timeGridDay';

case 'agendaWeek':
return 'timeGridWeek';

case 'month':
return 'dayGridMonth';

default:
return $view;
}
}

/**
* @NoAdminRequired
* @NoCSRFRequired
Expand Down
85 changes: 4 additions & 81 deletions lib/Listener/CalendarReferenceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,104 +9,27 @@

namespace OCA\Calendar\Listener;

use OC\App\CompareVersion;
use OCA\Calendar\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IInitialState;
use OCA\Calendar\Service\CalendarInitialStateService;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\IAppData;
use OCP\IConfig;



use OCP\Util;

/**
* @template-implements IEventListener<Event|RenderReferenceEvent>
*/
class CalendarReferenceListener implements IEventListener {

/** @var IInitialState */
private $initialStateService;

/** @var IAppManager */
private $appManager;

/** @var IConfig */
private $config;

/** @var CompareVersion */
private $compareVersion;

private IAppData $appData;

public function __construct(
IInitialState $initialStateService,
IAppManager $appManager,
IConfig $config,
IAppData $appData,
CompareVersion $compareVersion,
) {
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->appManager = $appManager;
$this->appData = $appData;
$this->compareVersion = $compareVersion;

private CalendarInitialStateService $calendarinitialStateService) {
$this->calendarinitialStateService = $calendarinitialStateService;
}

public function handle(Event $event): void {
if (!$event instanceof RenderReferenceEvent) {
return;
}
$defaultEventLimit = $this->config->getAppValue('calendar', 'eventLimit', 'yes');
$defaultInitialView = $this->config->getAppValue('calendar', 'currentView', 'dayGridMonth');
$defaultShowWeekends = $this->config->getAppValue('calendar', 'showWeekends', 'yes');
$defaultWeekNumbers = $this->config->getAppValue('calendar', 'showWeekNr', 'no');
$defaultSkipPopover = $this->config->getAppValue('calendar', 'skipPopover', 'no');
$defaultTimezone = $this->config->getAppValue('calendar', 'timezone', 'automatic');
$defaultSlotDuration = $this->config->getAppValue('calendar', 'slotDuration', '00:30:00');
$defaultDefaultReminder = $this->config->getAppValue('calendar', 'defaultReminder', 'none');

$appVersion = $this->config->getAppValue('calendar', 'installed_version', '');
$forceEventAlarmType = $this->config->getAppValue('calendar', 'forceEventAlarmType', '');
if (!in_array($forceEventAlarmType, ['DISPLAY', 'EMAIL'], true)) {
$forceEventAlarmType = false;
}
$showResources = $this->config->getAppValue('calendar', 'showResources', 'yes') === 'yes';
$publicCalendars = $this->config->getAppValue('calendar', 'publicCalendars', '');

$talkApiVersion = version_compare($this->appManager->getAppVersion('spreed'), '12.0.0', '>=') ? 'v4' : 'v1';
$tasksEnabled = $this->appManager->isEnabledForUser('tasks');

$circleVersion = $this->appManager->getAppVersion('circles');
$isCirclesEnabled = $this->appManager->isEnabledForUser('circles') === true;
// if circles is not installed, we use 0.0.0
$isCircleVersionCompatible = $this->compareVersion->isCompatible($circleVersion ? $circleVersion : '0.0.0', '22');

$this->initialStateService->provideInitialState('app_version', $appVersion);
$this->initialStateService->provideInitialState('event_limit', $defaultEventLimit);
$this->initialStateService->provideInitialState('first_run', false);
$this->initialStateService->provideInitialState('initial_view', $defaultInitialView);
$this->initialStateService->provideInitialState('show_weekends', $defaultShowWeekends);
$this->initialStateService->provideInitialState('show_week_numbers', $defaultWeekNumbers === 'yes');
$this->initialStateService->provideInitialState('skip_popover', true);
$this->initialStateService->provideInitialState('talk_enabled', false);
$this->initialStateService->provideInitialState('talk_api_version', $talkApiVersion);
$this->initialStateService->provideInitialState('show_tasks', false);
$this->initialStateService->provideInitialState('timezone', $defaultTimezone);
$this->initialStateService->provideInitialState('attachments_folder', '/Calendar');
$this->initialStateService->provideInitialState('slot_duration', $defaultSlotDuration);
$this->initialStateService->provideInitialState('default_reminder', $defaultDefaultReminder);
$this->initialStateService->provideInitialState('tasks_enabled', $tasksEnabled);
$this->initialStateService->provideInitialState('hide_event_export', true);
$this->initialStateService->provideInitialState('force_event_alarm_type', $forceEventAlarmType);
$this->initialStateService->provideInitialState('disable_appointments', true);
$this->initialStateService->provideInitialState('can_subscribe_link', false);
$this->initialStateService->provideInitialState('show_resources', $showResources);
$this->initialStateService->provideInitialState('publicCalendars', $publicCalendars);
$this->calendarinitialStateService->run();

Util::addScript(Application::APP_ID, 'calendar-reference');
}
Expand Down
125 changes: 125 additions & 0 deletions lib/Service/CalendarInitialStateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Calendar\Service;

use OC\App\CompareVersion;
use OCA\Calendar\Service\Appointments\AppointmentConfigService;
use OCP\App\IAppManager;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use function in_array;

class CalendarInitialStateService {

public function __construct(
private string $appName,
private IInitialState $initialStateService,
private IAppManager $appManager,
private IConfig $config,
private AppointmentConfigService $appointmentConfigService,
private CompareVersion $compareVersion,
private ?string $userId,
) {
$this->appName = $appName;
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->appointmentConfigService = $appointmentConfigService;
$this->appManager = $appManager;
$this->compareVersion = $compareVersion;
$this->userId = $userId;
}

hamza221 marked this conversation as resolved.
Show resolved Hide resolved
public function run(): void {
$defaultEventLimit = $this->config->getAppValue($this->appName, 'eventLimit', 'yes');
$defaultInitialView = $this->config->getAppValue($this->appName, 'currentView', 'dayGridMonth');
$defaultShowWeekends = $this->config->getAppValue($this->appName, 'showWeekends', 'yes');
$defaultWeekNumbers = $this->config->getAppValue($this->appName, 'showWeekNr', 'no');
$defaultSkipPopover = $this->config->getAppValue($this->appName, 'skipPopover', 'no');
$defaultTimezone = $this->config->getAppValue($this->appName, 'timezone', 'automatic');
$defaultSlotDuration = $this->config->getAppValue($this->appName, 'slotDuration', '00:30:00');
$defaultDefaultReminder = $this->config->getAppValue($this->appName, 'defaultReminder', 'none');
$defaultShowTasks = $this->config->getAppValue($this->appName, 'showTasks', 'yes');

$appVersion = $this->config->getAppValue($this->appName, 'installed_version', '');
$eventLimit = $this->config->getUserValue($this->userId, $this->appName, 'eventLimit', $defaultEventLimit) === 'yes';
$firstRun = $this->config->getUserValue($this->userId, $this->appName, 'firstRun', 'yes') === 'yes';
$initialView = $this->getView($this->config->getUserValue($this->userId, $this->appName, 'currentView', $defaultInitialView));
$showWeekends = $this->config->getUserValue($this->userId, $this->appName, 'showWeekends', $defaultShowWeekends) === 'yes';
$showWeekNumbers = $this->config->getUserValue($this->userId, $this->appName, 'showWeekNr', $defaultWeekNumbers) === 'yes';
$skipPopover = $this->config->getUserValue($this->userId, $this->appName, 'skipPopover', $defaultSkipPopover) === 'yes';
$timezone = $this->config->getUserValue($this->userId, $this->appName, 'timezone', $defaultTimezone);
$attachmentsFolder = $this->config->getUserValue($this->userId, 'dav', 'attachmentsFolder', '/Calendar');
$slotDuration = $this->config->getUserValue($this->userId, $this->appName, 'slotDuration', $defaultSlotDuration);
$defaultReminder = $this->config->getUserValue($this->userId, $this->appName, 'defaultReminder', $defaultDefaultReminder);
$showTasks = $this->config->getUserValue($this->userId, $this->appName, 'showTasks', $defaultShowTasks) === 'yes';
$hideEventExport = $this->config->getAppValue($this->appName, 'hideEventExport', 'no') === 'yes';
$disableAppointments = $this->config->getAppValue($this->appName, 'disableAppointments', 'no') === 'yes';
$forceEventAlarmType = $this->config->getAppValue($this->appName, 'forceEventAlarmType', '');
if (!in_array($forceEventAlarmType, ['DISPLAY', 'EMAIL'], true)) {
$forceEventAlarmType = false;
}
$canSubscribeLink = $this->config->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes';
$showResources = $this->config->getAppValue($this->appName, 'showResources', 'yes') === 'yes';
$publicCalendars = $this->config->getAppValue($this->appName, 'publicCalendars', '');

$talkEnabled = $this->appManager->isEnabledForUser('spreed');
$talkApiVersion = version_compare($this->appManager->getAppVersion('spreed'), '12.0.0', '>=') ? 'v4' : 'v1';
$tasksEnabled = $this->appManager->isEnabledForUser('tasks');

$circleVersion = $this->appManager->getAppVersion('circles');
$isCirclesEnabled = $this->appManager->isEnabledForUser('circles') === true;
// if circles is not installed, we use 0.0.0
$isCircleVersionCompatible = $this->compareVersion->isCompatible($circleVersion ? $circleVersion : '0.0.0', '22');

$this->initialStateService->provideInitialState('app_version', $appVersion);
$this->initialStateService->provideInitialState('event_limit', $eventLimit);
$this->initialStateService->provideInitialState('first_run', $firstRun);
$this->initialStateService->provideInitialState('initial_view', $initialView);
$this->initialStateService->provideInitialState('show_weekends', $showWeekends);
$this->initialStateService->provideInitialState('show_week_numbers', $showWeekNumbers);
$this->initialStateService->provideInitialState('skip_popover', $skipPopover);
$this->initialStateService->provideInitialState('talk_enabled', $talkEnabled);
$this->initialStateService->provideInitialState('talk_api_version', $talkApiVersion);
$this->initialStateService->provideInitialState('timezone', $timezone);
$this->initialStateService->provideInitialState('attachments_folder', $attachmentsFolder);
$this->initialStateService->provideInitialState('slot_duration', $slotDuration);
$this->initialStateService->provideInitialState('default_reminder', $defaultReminder);
$this->initialStateService->provideInitialState('show_tasks', $showTasks);
$this->initialStateService->provideInitialState('tasks_enabled', $tasksEnabled);
$this->initialStateService->provideInitialState('hide_event_export', $hideEventExport);
$this->initialStateService->provideInitialState('force_event_alarm_type', $forceEventAlarmType);
$this->initialStateService->provideInitialState('appointmentConfigs', $this->appointmentConfigService->getAllAppointmentConfigurations($this->userId));
$this->initialStateService->provideInitialState('disable_appointments', $disableAppointments);
$this->initialStateService->provideInitialState('can_subscribe_link', $canSubscribeLink);
$this->initialStateService->provideInitialState('show_resources', $showResources);
$this->initialStateService->provideInitialState('isCirclesEnabled', $isCirclesEnabled && $isCircleVersionCompatible);
$this->initialStateService->provideInitialState('publicCalendars', $publicCalendars);
}

hamza221 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Makes sure we don't use the old views anymore
*
* @param string $view
* @return string
*/
private function getView(string $view): string {
switch ($view) {
case 'agendaDay':
return 'timeGridDay';

case 'agendaWeek':
return 'timeGridWeek';

case 'month':
return 'dayGridMonth';

default:
return $view;
}
}
}
Loading
Loading