66 */
77namespace OC \Collaboration \Collaborators ;
88
9+ use OCP \Collaboration \AutoComplete \AutoCompleteFilterEvent ;
10+ use OCP \Collaboration \AutoComplete \IManager ;
911use OCP \Collaboration \Collaborators \ISearch ;
1012use OCP \Collaboration \Collaborators \ISearchPlugin ;
1113use OCP \Collaboration \Collaborators \ISearchResult ;
1214use OCP \Collaboration \Collaborators \SearchResultType ;
15+ use OCP \EventDispatcher \IEventDispatcher ;
1316use OCP \IContainer ;
1417use OCP \Share \IShare ;
1518
1619class Search implements ISearch {
20+ /** @var array<IShare::TYPE_*, list<class-string<ISearchPlugin>>> $pluginList */
1721 protected array $ pluginList = [];
1822
1923 public function __construct (
20- private IContainer $ container ,
24+ private readonly IContainer $ container ,
25+ private readonly IEventDispatcher $ eventDispatcher ,
2126 ) {
2227 }
2328
24- /**
25- * @param string $search
26- * @param bool $ lookup
27- * @param int|null $limit
28- * @param int|null $offset
29- * @throws \OCP\AppFramework\QueryException
30- */
31- public function search ( $ search , array $ shareTypes , $ lookup , $ limit , $ offset ): array {
29+ #[\Override]
30+ public function search ( string $ search, array $ shareTypes , bool $ lookup , int $ limit , int $ offset ): array {
31+ [ $ results , $ more ] = $ this -> filteredSearch ( $ search , $ shareTypes , $ lookup, null , null , $ limit , $ offset , false );
32+ return [ $ results , $ more ];
33+ }
34+
35+ #[\Override]
36+ public function filteredSearch ( string $ search , array $ shareTypes , bool $ lookup , ? string $ itemType , ? string $ itemId , int $ limit , int $ offset, bool $ sendFilterEvent = true ): array {
3237 $ hasMoreResults = false ;
3338
3439 // Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
@@ -43,7 +48,7 @@ public function search($search, array $shareTypes, $lookup, $limit, $offset): ar
4348 }
4449 foreach ($ this ->pluginList [$ type ] as $ plugin ) {
4550 /** @var ISearchPlugin $searchPlugin */
46- $ searchPlugin = $ this ->container ->resolve ($ plugin );
51+ $ searchPlugin = $ this ->container ->get ($ plugin );
4752 if ($ searchPlugin instanceof UserPlugin && $ lookup ) {
4853 // we are in GlobalScale, we ignore local accounts and prefer the result from lookup
4954 continue ;
@@ -54,7 +59,7 @@ public function search($search, array $shareTypes, $lookup, $limit, $offset): ar
5459
5560 // Get from lookup server, not a separate share type
5661 if ($ lookup ) {
57- $ searchPlugin = $ this ->container ->resolve (LookupPlugin::class);
62+ $ searchPlugin = $ this ->container ->get (LookupPlugin::class);
5863 $ hasMoreResults = $ searchPlugin ->search ($ search , $ limit , $ offset , $ searchResult ) || $ hasMoreResults ;
5964 }
6065
@@ -81,11 +86,43 @@ public function search($search, array $shareTypes, $lookup, $limit, $offset): ar
8186 $ searchResult ->unsetResult ($ emailType );
8287 }
8388
84- return [$ searchResult ->asArray (), $ hasMoreResults ];
89+ $ results = $ searchResult ->asArray ();
90+ if ($ sendFilterEvent ) {
91+ $ event = new AutoCompleteEvent ([
92+ 'search ' => $ search ,
93+ 'results ' => $ results ,
94+ 'itemType ' => $ itemType ,
95+ 'itemId ' => $ itemId ,
96+ 'sorter ' => $ sorter ,
97+ 'shareTypes ' => $ shareTypes ,
98+ 'limit ' => $ limit ,
99+ ]);
100+ $ this ->eventDispatcher ->dispatch (IManager::class . '::filterResults ' , $ event );
101+ $ results = $ event ->getResults ();
102+
103+ $ event = new AutoCompleteFilterEvent (
104+ $ results ,
105+ $ search ,
106+ $ itemType ,
107+ $ itemId ,
108+ null ,
109+ $ shareTypes ,
110+ $ limit ,
111+ );
112+ $ this ->eventDispatcher ->dispatchTyped ($ event );
113+ $ results = $ event ->getResults ();
114+ }
115+
116+ return [$ results , $ hasMoreResults ];
85117 }
86118
87119 public function registerPlugin (array $ pluginInfo ): void {
88- $ shareType = constant (IShare::class . ':: ' . substr ($ pluginInfo ['shareType ' ], strlen ('SHARE_ ' )));
120+ /** @psalm-suppress InvalidScalarArgument For legacy reasons */
121+ if (str_starts_with ($ pluginInfo ['shareType ' ], 'SHARE_ ' )) {
122+ $ shareType = constant (IShare::class . ':: ' . substr ($ pluginInfo ['shareType ' ], strlen ('SHARE_ ' )));
123+ } else {
124+ $ shareType = $ pluginInfo ['shareType ' ];
125+ }
89126 if ($ shareType === null ) {
90127 throw new \InvalidArgumentException ('Provided ShareType is invalid ' );
91128 }
0 commit comments