1010namespace OC \Sharing ;
1111
1212use Exception ;
13+ use NCU \Sharing \Event \SharesDefaultSetEvent ;
1314use NCU \Sharing \Exception \ShareInvalidException ;
1415use NCU \Sharing \Exception \ShareNotFoundException ;
1516use NCU \Sharing \ISharingBackend ;
16- use NCU \Sharing \ISharingManager ;
1717use NCU \Sharing \ISharingRegistry ;
1818use NCU \Sharing \Permission \ISharePermissionType ;
1919use NCU \Sharing \Permission \SharePermission ;
3030use NCU \Sharing \Source \IShareSourceType ;
3131use NCU \Sharing \Source \ShareSource ;
3232use OCP \DB \QueryBuilder \IQueryBuilder ;
33+ use OCP \EventDispatcher \IEventDispatcher ;
3334use OCP \IAppConfig ;
3435use OCP \IDBConnection ;
3536use OCP \IL10N ;
@@ -50,7 +51,7 @@ public function __construct(
5051 private IUserManager $ userManager ,
5152 private IAppConfig $ appConfig ,
5253 private ISharingRegistry $ registry ,
53- private ISharingManager $ manager ,
54+ private IEventDispatcher $ eventDispatcher ,
5455 private ClassMapper $ classMapper ,
5556 ) {
5657 $ this ->l10n = $ factory ->get ('sharing ' );
@@ -981,6 +982,7 @@ private function list(ShareAccessContext $accessContext, ?string $filterShareID,
981982 }
982983 }
983984
985+ $ defaultSet = false ;
984986 foreach (array_keys ($ shares ) as $ id ) {
985987 foreach (array_keys ($ registryPropertyTypes ) as $ propertyTypeClass ) {
986988 $ share = $ shares [$ id ];
@@ -989,7 +991,8 @@ private function list(ShareAccessContext $accessContext, ?string $filterShareID,
989991 && isset ($ shareSourceTypeClasses [$ id ], $ shareRecipientTypeClasses [$ id ])
990992 && array_intersect ($ registryPropertyTypeCompatibleSourceTypeClasses [$ propertyTypeClass ], array_keys ($ shareSourceTypeClasses [$ id ])) !== []
991993 && array_intersect ($ registryPropertyTypeCompatibleRecipientTypeClasses [$ propertyTypeClass ], array_keys ($ shareRecipientTypeClasses [$ id ])) !== []) {
992- $ shares [$ id ] = $ this ->manager ->createSharePropertyDefaultValue ($ shares [$ id ], $ propertyTypeClass );
994+ $ shares [$ id ] = $ this ->createSharePropertyDefaultValue ($ shares [$ id ], $ propertyTypeClass );
995+ $ defaultSet = true ;
993996 }
994997 }
995998 }
@@ -998,11 +1001,95 @@ private function list(ShareAccessContext $accessContext, ?string $filterShareID,
9981001 foreach (array_keys ($ shareCompatiblePermissionTypeClasses [$ id ]) as $ permissionTypeClass ) {
9991002 $ share = $ shares [$ id ];
10001003 if (!isset ($ share ->permissions [$ permissionTypeClass ])) {
1001- $ shares [$ id ] = $ this ->manager ->createSharePermissionDefaultValue ($ shares [$ id ], $ permissionTypeClass );
1004+ $ shares [$ id ] = $ this ->createSharePermissionDefaultValue ($ shares [$ id ], $ permissionTypeClass );
1005+ $ defaultSet = true ;
10021006 }
10031007 }
10041008 }
10051009
1006- return array_values ($ shares );
1010+ $ shares = array_values ($ shares );
1011+ if ($ defaultSet && $ shares !== []) {
1012+ $ event = new SharesDefaultSetEvent ($ shares );
1013+ $ this ->eventDispatcher ->dispatchTyped ($ event );
1014+ $ shares = $ event ->getShares ();
1015+ }
1016+
1017+ return $ shares ;
1018+ }
1019+
1020+ /**
1021+ * @return non-negative-int
1022+ */
1023+ public function generateTimestamp (): int {
1024+ $ time = (int )(microtime (true ) * 1000.0 );
1025+ if ($ time < 0 ) {
1026+ throw new RuntimeException ('Have you invented time travel? ' );
1027+ }
1028+
1029+ return $ time ;
1030+ }
1031+
1032+ /**
1033+ * @param class-string<ISharePropertyType> $propertyTypeClass
1034+ */
1035+ public function createSharePropertyDefaultValue (Share $ share , string $ propertyTypeClass ): Share {
1036+ $ timestamp = $ this ->generateTimestamp ();
1037+ $ this ->setLastUpdated ([$ share ->id ], $ timestamp );
1038+
1039+ if (($ propertyType = $ this ->registry ->getPropertyTypes ()[$ propertyTypeClass ] ?? null ) === null ) {
1040+ throw new RuntimeException ('The property is not registered: ' . $ propertyTypeClass );
1041+ }
1042+
1043+ $ property = new ShareProperty ($ propertyTypeClass , $ propertyType ->getDefaultValue ($ share ));
1044+
1045+ $ this ->createShareProperty ($ share ->id , $ property );
1046+
1047+ $ properties = $ share ->properties ;
1048+ $ properties [$ propertyTypeClass ] = $ property ;
1049+
1050+ $ share = new Share (
1051+ $ share ->id ,
1052+ $ share ->owner ,
1053+ $ timestamp ,
1054+ $ share ->state ,
1055+ $ share ->sources ,
1056+ $ share ->recipients ,
1057+ $ properties ,
1058+ $ share ->permissions ,
1059+ );
1060+
1061+ return $ share ;
1062+ }
1063+
1064+ /**
1065+ * @param class-string<ISharePermissionType> $permissionTypeClass
1066+ */
1067+ public function createSharePermissionDefaultValue (Share $ share , string $ permissionTypeClass ): Share {
1068+ $ timestamp = $ this ->generateTimestamp ();
1069+ $ this ->setLastUpdated ([$ share ->id ], $ timestamp );
1070+
1071+ if (($ permissionType = $ this ->registry ->getPermissionTypes ()[$ permissionTypeClass ] ?? null ) === null ) {
1072+ throw new RuntimeException ('The permission is not registered: ' . $ permissionTypeClass );
1073+ }
1074+
1075+ $ permission = new SharePermission ($ permissionTypeClass , $ permissionType ->isEnabledByDefault ());
1076+
1077+ $ this ->createSharePermission ($ share ->id , $ permission );
1078+
1079+ $ permissions = $ share ->permissions ;
1080+ $ permissions [$ permissionTypeClass ] = $ permission ;
1081+
1082+ $ share = new Share (
1083+ $ share ->id ,
1084+ $ share ->owner ,
1085+ $ timestamp ,
1086+ $ share ->state ,
1087+ $ share ->sources ,
1088+ $ share ->recipients ,
1089+ $ share ->properties ,
1090+ $ permissions ,
1091+ );
1092+
1093+ return $ share ;
10071094 }
10081095}
0 commit comments