2019-10-29 14:22:15 +03:00
< ? php
2019-12-03 21:57:53 +03:00
2019-10-29 14:22:15 +03:00
declare ( strict_types = 1 );
2019-12-03 21:57:53 +03:00
2019-10-29 14:22:15 +03:00
/**
* @ copyright Copyright ( c ) 2019 , Roeland Jago Douma < roeland @ famdouma . nl >
*
2019-12-03 21:57:53 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2019-10-29 14:22:15 +03:00
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2019-12-19 15:16:31 +03:00
* @ author Sascha Wiswedel < sascha . wiswedel @ nextcloud . com >
2019-10-29 14:22:15 +03:00
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
2019-12-03 21:57:53 +03:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2019-10-29 14:22:15 +03:00
*
*/
namespace OCA\Files\Notification ;
2019-12-11 14:58:44 +03:00
use OCA\Files\Db\TransferOwnershipMapper ;
use OCP\AppFramework\Db\DoesNotExistException ;
use OCP\AppFramework\Utility\ITimeFactory ;
2019-10-29 14:22:15 +03:00
use OCP\IURLGenerator ;
2020-04-08 17:09:03 +03:00
use OCP\IUser ;
use OCP\IUserManager ;
2019-10-29 14:22:15 +03:00
use OCP\L10N\IFactory ;
use OCP\Notification\IAction ;
2019-12-11 14:58:44 +03:00
use OCP\Notification\IDismissableNotifier ;
use OCP\Notification\IManager ;
2019-10-29 14:22:15 +03:00
use OCP\Notification\INotification ;
use OCP\Notification\INotifier ;
2019-12-11 14:58:44 +03:00
class Notifier implements INotifier , IDismissableNotifier {
2019-10-29 14:22:15 +03:00
/** @var IFactory */
protected $l10nFactory ;
/** @var IURLGenerator */
protected $urlGenerator ;
2019-12-11 14:58:44 +03:00
/** @var TransferOwnershipMapper */
private $mapper ;
/** @var IManager */
private $notificationManager ;
2020-04-08 17:09:03 +03:00
/** @var IUserManager */
private $userManager ;
2019-12-11 14:58:44 +03:00
/** @var ITimeFactory */
private $timeFactory ;
2019-10-29 14:22:15 +03:00
2019-12-11 14:58:44 +03:00
public function __construct ( IFactory $l10nFactory ,
IURLGenerator $urlGenerator ,
TransferOwnershipMapper $mapper ,
IManager $notificationManager ,
2020-04-08 17:09:03 +03:00
IUserManager $userManager ,
2019-12-11 14:58:44 +03:00
ITimeFactory $timeFactory ) {
2019-10-29 14:22:15 +03:00
$this -> l10nFactory = $l10nFactory ;
$this -> urlGenerator = $urlGenerator ;
2019-12-11 14:58:44 +03:00
$this -> mapper = $mapper ;
$this -> notificationManager = $notificationManager ;
2020-04-08 17:09:03 +03:00
$this -> userManager = $userManager ;
2019-12-11 14:58:44 +03:00
$this -> timeFactory = $timeFactory ;
2019-10-29 14:22:15 +03:00
}
public function getID () : string {
return 'files' ;
}
public function getName () : string {
return $this -> l10nFactory -> get ( 'files' ) -> t ( 'Files' );
}
/**
* @ param INotification $notification
* @ param string $languageCode The code of the language that should be used to prepare the notification
* @ return INotification
* @ throws \InvalidArgumentException When the notification was not prepared by a notifier
*/
public function prepare ( INotification $notification , string $languageCode ) : INotification {
if ( $notification -> getApp () !== 'files' ) {
throw new \InvalidArgumentException ( 'Unhandled app' );
}
if ( $notification -> getSubject () === 'transferownershipRequest' ) {
return $this -> handleTransferownershipRequest ( $notification , $languageCode );
}
if ( $notification -> getSubject () === 'transferOwnershipFailedSource' ) {
return $this -> handleTransferOwnershipFailedSource ( $notification , $languageCode );
}
if ( $notification -> getSubject () === 'transferOwnershipFailedTarget' ) {
return $this -> handleTransferOwnershipFailedTarget ( $notification , $languageCode );
}
if ( $notification -> getSubject () === 'transferOwnershipDoneSource' ) {
return $this -> handleTransferOwnershipDoneSource ( $notification , $languageCode );
}
if ( $notification -> getSubject () === 'transferOwnershipDoneTarget' ) {
return $this -> handleTransferOwnershipDoneTarget ( $notification , $languageCode );
}
throw new \InvalidArgumentException ( 'Unhandled subject' );
}
public function handleTransferownershipRequest ( INotification $notification , string $languageCode ) : INotification {
$l = $this -> l10nFactory -> get ( 'files' , $languageCode );
$id = $notification -> getObjectId ();
$param = $notification -> getSubjectParameters ();
$approveAction = $notification -> createAction ()
-> setParsedLabel ( $l -> t ( 'Accept' ))
-> setPrimary ( true )
-> setLink (
$this -> urlGenerator -> getAbsoluteURL (
$this -> urlGenerator -> linkTo (
'' ,
'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
)
),
IAction :: TYPE_POST
);
$disapproveAction = $notification -> createAction ()
2019-12-06 16:51:33 +03:00
-> setParsedLabel ( $l -> t ( 'Reject' ))
2019-10-29 14:22:15 +03:00
-> setPrimary ( false )
-> setLink (
$this -> urlGenerator -> getAbsoluteURL (
$this -> urlGenerator -> linkTo (
'' ,
'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
)
),
IAction :: TYPE_DELETE
);
2020-04-08 17:09:03 +03:00
$sourceUser = $this -> getUser ( $param [ 'sourceUser' ]);
2019-10-29 14:22:15 +03:00
$notification -> addParsedAction ( $approveAction )
-> addParsedAction ( $disapproveAction )
-> setRichSubject (
2019-12-06 16:48:00 +03:00
$l -> t ( 'Incoming ownership transfer from {user}' ),
2019-10-29 14:22:15 +03:00
[
'user' => [
'type' => 'user' ,
2020-04-08 17:09:03 +03:00
'id' => $sourceUser -> getUID (),
'name' => $sourceUser -> getDisplayName (),
2019-10-29 14:22:15 +03:00
],
])
2020-04-08 17:09:03 +03:00
-> setParsedSubject ( str_replace ( '{user}' , $sourceUser -> getDisplayName (), $l -> t ( 'Incoming ownership transfer from {user}' )))
2019-10-29 14:22:15 +03:00
-> setRichMessage (
2019-12-16 23:54:20 +03:00
$l -> t ( " Do you want to accept { path}? \n \n Note: The transfer process after accepting may take up to 1 hour. " ),
2019-10-29 14:22:15 +03:00
[
'path' => [
'type' => 'highlight' ,
'id' => $param [ 'targetUser' ] . '::' . $param [ 'nodeName' ],
'name' => $param [ 'nodeName' ],
]
])
2019-12-16 23:54:20 +03:00
-> setParsedMessage ( str_replace ( '{path}' , $param [ 'nodeName' ], $l -> t ( " Do you want to accept { path}? \n \n Note: The transfer process after accepting may take up to 1 hour. " )));
2019-10-29 14:22:15 +03:00
return $notification ;
}
public function handleTransferOwnershipFailedSource ( INotification $notification , string $languageCode ) : INotification {
$l = $this -> l10nFactory -> get ( 'files' , $languageCode );
$param = $notification -> getSubjectParameters ();
2020-04-08 17:09:03 +03:00
$targetUser = $this -> getUser ( $param [ 'targetUser' ]);
2019-12-06 16:48:00 +03:00
$notification -> setRichSubject ( $l -> t ( 'Ownership transfer failed' ))
-> setParsedSubject ( $l -> t ( 'Ownership transfer failed' ))
2019-12-03 19:52:33 +03:00
2019-10-29 14:22:15 +03:00
-> setRichMessage (
2019-12-06 16:48:00 +03:00
$l -> t ( 'Your ownership transfer of {path} to {user} failed.' ),
2019-10-29 14:22:15 +03:00
[
'path' => [
'type' => 'highlight' ,
'id' => $param [ 'targetUser' ] . '::' . $param [ 'nodeName' ],
'name' => $param [ 'nodeName' ],
],
'user' => [
'type' => 'user' ,
2020-04-08 17:09:03 +03:00
'id' => $targetUser -> getUID (),
'name' => $targetUser -> getDisplayName (),
2019-10-29 14:22:15 +03:00
],
])
2020-04-08 17:09:03 +03:00
-> setParsedMessage ( str_replace ([ '{path}' , '{user}' ], [ $param [ 'nodeName' ], $targetUser -> getDisplayName ()], $l -> t ( 'Your ownership transfer of {path} to {user} failed.' )));
2019-10-29 14:22:15 +03:00
return $notification ;
}
public function handleTransferOwnershipFailedTarget ( INotification $notification , string $languageCode ) : INotification {
$l = $this -> l10nFactory -> get ( 'files' , $languageCode );
$param = $notification -> getSubjectParameters ();
2020-04-08 17:09:03 +03:00
$sourceUser = $this -> getUser ( $param [ 'sourceUser' ]);
2019-12-06 16:48:00 +03:00
$notification -> setRichSubject ( $l -> t ( 'Ownership transfer failed' ))
-> setParsedSubject ( $l -> t ( 'Ownership transfer failed' ))
2019-12-03 19:52:33 +03:00
2019-10-29 14:22:15 +03:00
-> setRichMessage (
2019-12-06 16:48:00 +03:00
$l -> t ( 'The ownership transfer of {path} from {user} failed.' ),
2019-10-29 14:22:15 +03:00
[
'path' => [
'type' => 'highlight' ,
'id' => $param [ 'sourceUser' ] . '::' . $param [ 'nodeName' ],
'name' => $param [ 'nodeName' ],
],
'user' => [
'type' => 'user' ,
2020-04-08 17:09:03 +03:00
'id' => $sourceUser -> getUID (),
'name' => $sourceUser -> getDisplayName (),
2019-10-29 14:22:15 +03:00
],
])
2020-04-08 17:09:03 +03:00
-> setParsedMessage ( str_replace ([ '{path}' , '{user}' ], [ $param [ 'nodeName' ], $sourceUser -> getDisplayName ()], $l -> t ( 'The ownership transfer of {path} from {user} failed.' )));
2019-10-29 14:22:15 +03:00
return $notification ;
}
public function handleTransferOwnershipDoneSource ( INotification $notification , string $languageCode ) : INotification {
$l = $this -> l10nFactory -> get ( 'files' , $languageCode );
$param = $notification -> getSubjectParameters ();
2020-04-08 17:09:03 +03:00
$targetUser = $this -> getUser ( $param [ 'targetUser' ]);
2019-12-06 16:48:00 +03:00
$notification -> setRichSubject ( $l -> t ( 'Ownership transfer done' ))
-> setParsedSubject ( $l -> t ( 'Ownership transfer done' ))
2019-12-03 19:52:33 +03:00
2019-10-29 14:22:15 +03:00
-> setRichMessage (
2019-12-06 16:48:00 +03:00
$l -> t ( 'Your ownership transfer of {path} to {user} has completed.' ),
2019-10-29 14:22:15 +03:00
[
'path' => [
'type' => 'highlight' ,
'id' => $param [ 'targetUser' ] . '::' . $param [ 'nodeName' ],
'name' => $param [ 'nodeName' ],
],
'user' => [
'type' => 'user' ,
2020-04-08 17:09:03 +03:00
'id' => $targetUser -> getUID (),
'name' => $targetUser -> getDisplayName (),
2019-10-29 14:22:15 +03:00
],
])
2020-04-08 17:09:03 +03:00
-> setParsedMessage ( str_replace ([ '{path}' , '{user}' ], [ $param [ 'nodeName' ], $targetUser -> getDisplayName ()], $l -> t ( 'Your ownership transfer of {path} to {user} has completed.' )));
2019-10-29 14:22:15 +03:00
return $notification ;
}
public function handleTransferOwnershipDoneTarget ( INotification $notification , string $languageCode ) : INotification {
$l = $this -> l10nFactory -> get ( 'files' , $languageCode );
$param = $notification -> getSubjectParameters ();
2020-04-08 17:09:03 +03:00
$sourceUser = $this -> getUser ( $param [ 'sourceUser' ]);
2019-12-06 16:48:00 +03:00
$notification -> setRichSubject ( $l -> t ( 'Ownership transfer done' ))
-> setParsedSubject ( $l -> t ( 'Ownership transfer done' ))
2019-12-03 19:52:33 +03:00
2019-10-29 14:22:15 +03:00
-> setRichMessage (
2019-12-06 16:48:00 +03:00
$l -> t ( 'The ownership transfer of {path} from {user} has completed.' ),
2019-10-29 14:22:15 +03:00
[
'path' => [
'type' => 'highlight' ,
'id' => $param [ 'sourceUser' ] . '::' . $param [ 'nodeName' ],
'name' => $param [ 'nodeName' ],
],
'user' => [
'type' => 'user' ,
2020-04-08 17:09:03 +03:00
'id' => $sourceUser -> getUID (),
'name' => $sourceUser -> getDisplayName (),
2019-10-29 14:22:15 +03:00
],
])
2020-04-08 17:09:03 +03:00
-> setParsedMessage ( str_replace ([ '{path}' , '{user}' ], [ $param [ 'nodeName' ], $sourceUser -> getDisplayName ()], $l -> t ( 'The ownership transfer of {path} from {user} has completed.' )));
2019-10-29 14:22:15 +03:00
return $notification ;
}
2019-12-11 14:58:44 +03:00
public function dismissNotification ( INotification $notification ) : void {
if ( $notification -> getApp () !== 'files' ) {
throw new \InvalidArgumentException ( 'Unhandled app' );
}
// TODO: This should all be moved to a service that also the transferownershipContoller uses.
try {
$transferOwnership = $this -> mapper -> getById (( int ) $notification -> getObjectId ());
} catch ( DoesNotExistException $e ) {
return ;
}
$notification = $this -> notificationManager -> createNotification ();
$notification -> setUser ( $transferOwnership -> getSourceUser ())
-> setApp ( 'files' )
-> setDateTime ( $this -> timeFactory -> getDateTime ())
-> setSubject ( 'transferownershipRequestDenied' , [
'sourceUser' => $transferOwnership -> getSourceUser (),
'targetUser' => $transferOwnership -> getTargetUser (),
'nodeName' => $transferOwnership -> getNodeName ()
])
-> setObject ( 'transfer' , ( string ) $transferOwnership -> getId ());
$this -> notificationManager -> notify ( $notification );
$this -> mapper -> delete ( $transferOwnership );
}
2020-04-08 17:09:03 +03:00
protected function getUser ( string $userId ) : IUser {
$user = $this -> userManager -> get ( $userId );
if ( $user instanceof IUser ) {
return $user ;
}
throw new \InvalidArgumentException ( 'User not found' );
}
2019-10-29 14:22:15 +03:00
}