aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/lib/Listener/EventListener.php
blob: 63ecc9c81f73e30a227dd61243608c8670be0bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace OCA\Files_Trashbin\Listener;

use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trashbin;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\BeforeFileSystemSetupEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\User\Events\BeforeUserDeletedEvent;

/** @template-implements IEventListener<NodeWrittenEvent|BeforeUserDeletedEvent|BeforeFileSystemSetupEvent> */
class EventListener implements IEventListener {
	public function __construct(
		private ?string $userId = null,
	) {
	}

	public function handle(Event $event): void {
		if ($event instanceof NodeWrittenEvent) {
			// Resize trash
			if (!empty($this->userId)) {
				Trashbin::resizeTrash($this->userId);
			}
		}

		// Clean up user specific settings if user gets deleted
		if ($event instanceof BeforeUserDeletedEvent) {
			Trashbin::deleteUser($event->getUser()->getUID());
		}

		if ($event instanceof BeforeFileSystemSetupEvent) {
			Storage::setupStorage();
		}
	}
}