88 */
99namespace OCA \Comments \Search ;
1010
11+ use OCP \Comments \IComment ;
12+ use OCP \Comments \ICommentsManager ;
13+ use OCP \Files \Folder ;
14+ use OCP \Files \InvalidPathException ;
15+ use OCP \Files \IRootFolder ;
16+ use OCP \Files \Node ;
17+ use OCP \Files \NotFoundException ;
1118use OCP \IL10N ;
1219use OCP \IURLGenerator ;
1320use OCP \IUser ;
1623use OCP \Search \ISearchQuery ;
1724use OCP \Search \SearchResult ;
1825use OCP \Search \SearchResultEntry ;
19- use function array_map ;
2026
2127class CommentsSearchProvider implements IProvider {
2228 public function __construct (
2329 private IUserManager $ userManager ,
2430 private IL10N $ l10n ,
2531 private IURLGenerator $ urlGenerator ,
26- private LegacyProvider $ legacyProvider ,
32+ private ICommentsManager $ commentsManager ,
33+ private IRootFolder $ rootFolder ,
2734 ) {
2835 }
2936
@@ -44,30 +51,76 @@ public function getOrder(string $route, array $routeParameters): int {
4451 }
4552
4653 public function search (IUser $ user , ISearchQuery $ query ): SearchResult {
54+ $ userFolder = $ this ->rootFolder ->getUserFolder ($ user ->getUID ());
55+
56+ if ($ userFolder === null ) {
57+ return SearchResult::complete ($ this ->l10n ->t ('Comments ' ), []);
58+ }
59+
60+ $ result = [];
61+ $ numComments = 50 ;
62+ $ offset = 0 ;
63+
64+ while (count ($ result ) < $ numComments ) {
65+ $ comments = $ this ->commentsManager ->search ($ query ->getTerm (), 'files ' , '' , 'comment ' , $ offset , $ numComments );
66+
67+ foreach ($ comments as $ comment ) {
68+ if ($ comment ->getActorType () !== 'users ' ) {
69+ continue ;
70+ }
71+
72+ $ displayName = $ this ->commentsManager ->resolveDisplayName ('user ' , $ comment ->getActorId ());
73+
74+ try {
75+ $ file = $ this ->getFileForComment ($ userFolder , $ comment );
76+
77+ $ isUser = $ this ->userManager ->userExists ($ comment ->getActorId ());
78+ $ avatarUrl = $ isUser
79+ ? $ this ->urlGenerator ->linkToRouteAbsolute ('core.avatar.getAvatar ' , ['userId ' => $ comment ->getActorId (), 'size ' => 42 ])
80+ : $ this ->urlGenerator ->linkToRouteAbsolute ('core.GuestAvatar.getAvatar ' , ['guestName ' => $ comment ->getActorId (), 'size ' => 42 ]);
81+ $ link = $ this ->urlGenerator ->linkToRoute (
82+ 'files.View.showFile ' ,
83+ ['fileid ' => $ file ->getId ()]
84+ );
85+
86+ $ result [] = new SearchResultEntry (
87+ $ avatarUrl ,
88+ $ displayName ,
89+ $ file ->getPath (),
90+ $ link ,
91+ '' ,
92+ true
93+ );
94+ } catch (NotFoundException |InvalidPathException $ e ) {
95+ continue ;
96+ }
97+ }
98+
99+ if (count ($ comments ) < $ numComments ) {
100+ // Didn't find more comments when we tried to get, so there are no more comments.
101+ break ;
102+ }
103+
104+ $ offset += $ numComments ;
105+ $ numComments = 50 - count ($ result );
106+ }
107+
108+
47109 return SearchResult::complete (
48110 $ this ->l10n ->t ('Comments ' ),
49- array_map (function (Result $ result ) {
50- $ path = $ result ->path ;
51- $ isUser = $ this ->userManager ->userExists ($ result ->authorId );
52- $ avatarUrl = $ isUser
53- ? $ this ->urlGenerator ->linkToRouteAbsolute ('core.avatar.getAvatar ' , ['userId ' => $ result ->authorId , 'size ' => 42 ])
54- : $ this ->urlGenerator ->linkToRouteAbsolute ('core.GuestAvatar.getAvatar ' , ['guestName ' => $ result ->authorId , 'size ' => 42 ]);
55- $ link = $ this ->urlGenerator ->linkToRoute (
56- 'files.View.showFile ' ,
57- ['fileid ' => $ result ->fileId ]
58- );
59- $ searchResultEntry = new SearchResultEntry (
60- $ avatarUrl ,
61- $ result ->name ,
62- $ path ,
63- $ link ,
64- '' ,
65- true
66- );
67- $ searchResultEntry ->addAttribute ('fileId ' , (string )$ result ->fileId );
68- $ searchResultEntry ->addAttribute ('path ' , $ path );
69- return $ searchResultEntry ;
70- }, $ this ->legacyProvider ->search ($ query ->getTerm ()))
111+ $ result ,
71112 );
72113 }
114+
115+ /**
116+ * @throws NotFoundException
117+ */
118+ protected function getFileForComment (Folder $ userFolder , IComment $ comment ): Node {
119+ $ nodes = $ userFolder ->getById ((int )$ comment ->getObjectId ());
120+ if (empty ($ nodes )) {
121+ throw new NotFoundException ('File not found ' );
122+ }
123+
124+ return array_shift ($ nodes );
125+ }
73126}
0 commit comments