2020-06-17 15:40:20 +03:00
< ? php
declare ( strict_types = 1 );
/**
2024-05-01 18:21:16 +03:00
* SPDX - FileCopyrightText : 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2020-06-17 15:40:20 +03:00
*/
namespace OCA\Activity ;
2024-04-17 16:45:29 +03:00
use OCP\Activity\Exceptions\UnknownActivityException ;
2020-06-17 15:40:20 +03:00
use OCP\Activity\IEvent ;
use OCP\Activity\IManager as ActivityManager ;
use OCP\IL10N ;
2023-12-12 11:03:03 +03:00
use OCP\Notification\AlreadyProcessedException ;
2023-06-05 11:35:20 +03:00
use OCP\Notification\IManager as NotificationManager ;
2020-06-17 15:40:20 +03:00
use OCP\Notification\INotification ;
use OCP\Notification\INotifier ;
2024-04-17 16:45:29 +03:00
use Psr\Log\LoggerInterface ;
2020-06-17 15:40:20 +03:00
class NotificationGenerator implements INotifier {
2023-10-06 18:35:14 +03:00
public function __construct (
protected Data $data ,
protected ActivityManager $activityManager ,
protected NotificationManager $notificationManager ,
protected UserSettings $userSettings ,
2024-04-17 16:45:29 +03:00
protected IL10N $l10n ,
protected LoggerInterface $logger ,
) {
2020-06-17 15:40:20 +03:00
}
2022-02-01 20:56:43 +03:00
public function deferNotifications () : bool {
return $this -> notificationManager -> defer ();
}
public function flushNotifications () {
$this -> notificationManager -> flush ();
}
2020-06-17 15:40:20 +03:00
public function sendNotificationForEvent ( IEvent $event , int $activityId ) {
$selfAction = $event -> getAffectedUser () === $event -> getAuthor ();
2021-07-01 14:59:44 +03:00
$notifySetting = $this -> userSettings -> getUserSetting ( $event -> getAffectedUser (), 'notification' , $event -> getType ());
2020-06-17 15:40:20 +03:00
2021-03-18 16:25:26 +03:00
if ( $notifySetting && ! $selfAction && $event -> getGenerateNotification ()) {
2020-06-17 15:40:20 +03:00
$this -> notificationManager -> notify ( $this -> getNotificationForEvent ( $event , $activityId ));
}
}
private function getNotificationForEvent ( IEvent $event , int $activityId ) : INotification {
$notification = $this -> notificationManager -> createNotification ();
$notification -> setApp ( $event -> getApp ());
$notification -> setUser ( $event -> getAffectedUser ());
$notification -> setDateTime ( \DateTime :: createFromFormat ( 'U' , ( string ) $event -> getTimestamp ()));
$notification -> setObject ( 'activity_notification' , ( string ) $activityId );
2022-05-24 18:07:55 +03:00
$notification -> setSubject ( $event -> getSubject (), $event -> getSubjectParameters ());
2020-06-17 15:40:20 +03:00
2021-07-01 15:00:04 +03:00
if ( $event -> getRichSubject ()) {
$notification -> setRichSubject ( $event -> getRichSubject (), $event -> getRichSubjectParameters ());
}
if ( $event -> getRichMessage ()) {
2020-06-17 15:40:20 +03:00
$notification -> setRichMessage ( $event -> getRichMessage (), $event -> getRichMessageParameters ());
}
if ( $event -> getMessage ()) {
2022-05-24 18:07:55 +03:00
$notification -> setMessage ( $event -> getMessage (), $event -> getMessageParameters ());
2020-06-17 15:40:20 +03:00
}
2021-11-22 18:49:55 +03:00
if ( $event -> getLink ()) {
$notification -> setLink ( $event -> getLink ());
}
2020-06-17 15:40:20 +03:00
return $notification ;
}
private function populateEvent ( IEvent $event , string $language ) {
2021-01-05 11:36:45 +03:00
$this -> activityManager -> setFormattingObject ( $event -> getObjectType (), $event -> getObjectId ());
2020-06-17 15:40:20 +03:00
foreach ( $this -> activityManager -> getProviders () as $provider ) {
try {
$event = $provider -> parse ( $language , $event );
2024-04-17 16:45:29 +03:00
} catch ( UnknownActivityException ) {
2020-06-17 15:40:20 +03:00
} catch ( \InvalidArgumentException $e ) {
2024-04-17 16:45:29 +03:00
// todo 33.0.0 Log as warning
// todo 39.0.0 Log as error
$this -> logger -> debug ( get_class ( $provider ) . '::parse() threw \InvalidArgumentException which is deprecated. Throw \OCP\Activity\Exceptions\UnknownActivityException when the event is not known to your provider and otherwise handle all \InvalidArgumentException yourself.' );
2020-06-17 15:40:20 +03:00
}
}
2021-01-05 11:36:45 +03:00
$this -> activityManager -> setFormattingObject ( '' , 0 );
2020-06-17 15:40:20 +03:00
return $event ;
}
public function getID () : string {
return 'activity' ;
}
public function getName () : string {
return 'Activity' ;
}
public function prepare ( INotification $notification , string $languageCode ) : INotification {
if ( $notification -> getObjectType () !== 'activity_notification' ) {
throw new \InvalidArgumentException ();
}
$event = $this -> data -> getById (( int ) $notification -> getObjectId ());
2023-12-12 11:03:03 +03:00
if ( ! $event ) {
throw new AlreadyProcessedException ();
}
if ( $event -> getAffectedUser () !== $notification -> getUser ()) {
2020-06-17 15:40:20 +03:00
throw new \InvalidArgumentException ();
}
2021-01-05 11:36:45 +03:00
$this -> activityManager -> setCurrentUserId ( $notification -> getUser ());
2020-06-17 15:40:20 +03:00
$event = $this -> populateEvent ( $event , $languageCode );
2021-01-05 11:36:45 +03:00
$this -> activityManager -> setCurrentUserId ( null );
2020-06-17 15:40:20 +03:00
return $this -> getDisplayNotificationForEvent ( $event , $event -> getObjectId ());
}
private function getDisplayNotificationForEvent ( IEvent $event , int $activityId ) : INotification {
$notification = $this -> getNotificationForEvent ( $event , $activityId );
$notification -> setRichSubject ( $event -> getRichSubject (), $event -> getRichSubjectParameters ());
$notification -> setParsedSubject ( $event -> getParsedSubject ());
2021-07-01 15:00:04 +03:00
if ( $event -> getIcon ()) {
$notification -> setIcon ( $event -> getIcon ());
}
if ( $event -> getRichMessage ()) {
2020-06-17 15:40:20 +03:00
$notification -> setRichMessage ( $event -> getRichMessage (), $event -> getRichMessageParameters ());
}
2021-07-01 15:00:04 +03:00
if ( $event -> getParsedMessage ()) {
2022-05-24 18:07:55 +03:00
$notification -> setParsedMessage ( $event -> getParsedMessage ());
2020-06-17 15:40:20 +03:00
}
return $notification ;
}
}