diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 669435cb..ea0516d3 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -11,13 +11,51 @@ use OCA\OpenCatalogi\Dashboard\CatalogWidget; use OCA\OpenCatalogi\Dashboard\UnpublishedPublicationsWidget; use OCA\OpenCatalogi\Dashboard\UnpublishedAttachmentsWidget; +use OCP\IConfig; +/** + * Main Application class for OpenCatalogi + */ class Application extends App implements IBootstrap { public const APP_ID = 'opencatalogi'; /** @psalm-suppress PossiblyUnusedMethod */ public function __construct() { parent::__construct(self::APP_ID); + + // Listen for the post installation event + $container = $this->getContainer(); + $this->listen($container); + } + + /** + * Register event listeners + */ + private function listen($container): void { + $dispatcher = $container->getServer()->getEventDispatcher(); + + // Listen for app installation + $dispatcher->addListener('app.register', function() use ($container) { + // Get app config to check if initial sync has been done + $config = $container->get(IConfig::class); + $initialSyncDone = $config->getAppValue(self::APP_ID, 'initial_sync_done', 'false'); + + // Only run if initial sync hasn't been done + if ($initialSyncDone === 'false') { + try { + // Get DirectoryService and run sync + $directoryService = $container->get(\OCA\OpenCatalogi\Service\DirectoryService::class); + $directoryService->doCronSync(); + + // Mark initial sync as done + $config->setAppValue(self::APP_ID, 'initial_sync_done', 'true'); + } catch (\Exception $e) { + \OC::$server->getLogger()->error('Failed to run initial directory sync: ' . $e->getMessage(), [ + 'app' => self::APP_ID + ]); + } + } + }); } public function register(IRegistrationContext $context): void {