/
kev
/
Nextcloud_File-Changes-Tracker
Обзор
Документация
Войти
/
kev
/
Nextcloud_File-Changes-Tracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
lib/Service/NotificationService.php
146 строк
5 KB
Evgeny Konovalov
first_commit
16 фев 2026, 17:45
16 фев 2026, 17:45
476f06c
Код
Авторство
О чём код?
<?php declare(strict_types=1); namespace OCA\FileChangesTracker\Service; use OCA\FileChangesTracker\Db\EventLog; use OCA\FileChangesTracker\Db\FolderConfig; use OCA\FileChangesTracker\Db\NotificationSettingsMapper; use OCP\IGroupManager; use OCP\IUserManager; use OCP\Notification\IManager as INotificationManager; /** * Сервис для отправки уведомлений */ class NotificationService { private INotificationManager $notificationManager; private IUserManager $userManager; private IGroupManager $groupManager; private NotificationSettingsMapper $settingsMapper; public function __construct( INotificationManager $notificationManager, IUserManager $userManager, IGroupManager $groupManager, NotificationSettingsMapper $settingsMapper ) { $this->notificationManager = $notificationManager; $this->userManager = $userManager; $this->groupManager = $groupManager; $this->settingsMapper = $settingsMapper; } /** * Отправить уведомление об изменении файла */ public function notifyFileChange(FolderConfig $config, EventLog $event): void { // Проверяем глобальную галочку уведомлений if (!$config->getNotificationsEnabled()) { return; } // Получаем детальные настройки уведомлений $settings = $this->settingsMapper->findByFolderConfigId($config->getId()); // Если настроек нет - создаем по умолчанию if ($settings === null) { $settings = $this->settingsMapper->createDefault($config->getId()); } // Проверяем мгновенные уведомления if (!$settings->getInstantNotifications()) { return; } // Проверяем настройки по типам событий $eventType = $event->getEventType(); $shouldNotify = false; switch ($eventType) { case 'created': $shouldNotify = $settings->getNotifyOnCreate(); break; case 'modified': $shouldNotify = $settings->getNotifyOnModify(); break; case 'deleted': $shouldNotify = $settings->getNotifyOnDelete(); break; case 'renamed': $shouldNotify = $settings->getNotifyOnRename(); break; } if (!$shouldNotify) { return; } // Отправляем уведомления получателям $recipients = $this->getRecipients($config); foreach ($recipients as $userId) { $this->sendNotification($userId, $config, $event); } } /** * Получить список получателей уведомлений */ private function getRecipients(FolderConfig $config): array { $recipients = []; if ($config->getResponsibleType() === 'user') { $recipients[] = $config->getResponsibleId(); } elseif ($config->getResponsibleType() === 'group') { $group = $this->groupManager->get($config->getResponsibleId()); if ($group) { $users = $group->getUsers(); foreach ($users as $user) { $recipients[] = $user->getUID(); } } } return $recipients; } /** * Отправить уведомление конкретному пользователю */ private function sendNotification(string $userId, FolderConfig $config, EventLog $event): void { $notification = $this->notificationManager->createNotification(); $notification->setApp('file_changes_tracker') ->setUser($userId) ->setDateTime(new \DateTime()) ->setObject('file_change', (string)$event->getId()) ->setSubject('file_change', [ 'event_type' => $event->getEventType(), 'file_path' => $event->getFilePath(), 'user_display_name' => $event->getUserDisplayName(), 'folder_path' => $config->getFolderPath(), ]); $this->notificationManager->notify($notification); } /** * Получить текст уведомления */ public function getNotificationText(string $eventType, string $filePath, string $userDisplayName): string { switch ($eventType) { case 'created': return sprintf('Пользователь %s создал файл: %s', $userDisplayName, $filePath); case 'modified': return sprintf('Пользователь %s изменил файл: %s', $userDisplayName, $filePath); case 'deleted': return sprintf('Пользователь %s удалил файл: %s', $userDisplayName, $filePath); case 'renamed': return sprintf('Пользователь %s переименовал файл: %s', $userDisplayName, $filePath); default: return sprintf('Пользователь %s выполнил действие с файлом: %s', $userDisplayName, $filePath); } } }