5
5
namespace App \Controller ;
6
6
7
7
use App \ActivityPub \ActorHandle ;
8
+ use App \DTO \SearchDto ;
8
9
use App \Entity \Magazine ;
9
10
use App \Entity \User ;
11
+ use App \Form \SearchType ;
10
12
use App \Message \ActivityPub \Inbox \ActivityMessage ;
11
13
use App \Service \ActivityPub \ApHttpClient ;
12
14
use App \Service \ActivityPubManager ;
@@ -33,52 +35,63 @@ public function __construct(
33
35
34
36
public function __invoke (Request $ request ): Response
35
37
{
36
- $ query = $ request ->query ->get ('q ' ) ? trim ($ request ->query ->get ('q ' )) : null ;
37
-
38
- if (!$ query ) {
39
- return $ this ->render (
40
- 'search/front.html.twig ' ,
41
- [
42
- 'objects ' => [],
43
- 'results ' => [],
44
- 'q ' => '' ,
45
- ]
46
- );
47
- }
48
-
49
- $ this ->logger ->debug ('searching for {query} ' , ['query ' => $ query ]);
50
-
51
- $ objects = [];
38
+ $ dto = new SearchDto ();
39
+ $ form = $ this ->createForm (SearchType::class, $ dto , ['csrf_protection ' => false ]);
40
+ try {
41
+ $ form = $ form ->handleRequest ($ request );
42
+ if ($ form ->isSubmitted () && $ form ->isValid ()) {
43
+ /** @var SearchDto $dto */
44
+ $ dto = $ form ->getData ();
45
+ $ query = $ dto ->q ;
46
+ $ this ->logger ->debug ('searching for {query} ' , ['query ' => $ query ]);
47
+
48
+ $ objects = [];
49
+
50
+ // looking up handles (users and mags)
51
+ if (str_contains ($ query , '@ ' ) && $ this ->federatedSearchAllowed ()) {
52
+ if ($ handle = ActorHandle::parse ($ query )) {
53
+ $ this ->logger ->debug ('searching for a matched webfinger {query} ' , ['query ' => $ query ]);
54
+ $ objects = array_merge ($ objects , $ this ->lookupHandle ($ handle ));
55
+ } else {
56
+ $ this ->logger ->debug ("query doesn't look like a valid handle... " , ['query ' => $ query ]);
57
+ }
58
+ }
52
59
53
- // looking up handles (users and mags)
54
- if (str_contains ($ query , '@ ' ) && $ this ->federatedSearchAllowed ()) {
55
- if ($ handle = ActorHandle::parse ($ query )) {
56
- $ this ->logger ->debug ('searching for a matched webfinger {query} ' , ['query ' => $ query ]);
57
- $ objects = array_merge ($ objects , $ this ->lookupHandle ($ handle ));
58
- } else {
59
- $ this ->logger ->debug ("query doesn't look like a valid handle... " , ['query ' => $ query ]);
60
- }
61
- }
60
+ // looking up object by AP id (i.e. urls)
61
+ if (false !== filter_var ($ query , FILTER_VALIDATE_URL )) {
62
+ $ objects = $ this ->manager ->findByApId ($ query );
63
+ if (!$ objects ) {
64
+ $ body = $ this ->apHttpClient ->getActivityObject ($ query , false );
65
+ $ this ->bus ->dispatch (new ActivityMessage ($ body ));
66
+ }
67
+ }
62
68
63
- // looking up object by AP id (i.e. urls)
64
- if (false !== filter_var ($ query , FILTER_VALIDATE_URL )) {
65
- $ objects = $ this ->manager ->findByApId ($ query );
66
- if (!$ objects ) {
67
- $ body = $ this ->apHttpClient ->getActivityObject ($ query , false );
68
- $ this ->bus ->dispatch (new ActivityMessage ($ body ));
69
+ $ user = $ this ->getUser ();
70
+ $ res = $ this ->manager ->findPaginated ($ user , $ query , $ this ->getPageNb ($ request ), authorId: $ dto ->user ?->getId(), magazineId: $ dto ->magazine ?->getId(), specificType: $ dto ->type );
71
+
72
+ $ this ->logger ->debug ('results: {num} ' , ['num ' => $ res ->count ()]);
73
+
74
+ return $ this ->render (
75
+ 'search/front.html.twig ' ,
76
+ [
77
+ 'objects ' => $ objects ,
78
+ 'results ' => $ this ->overviewManager ->buildList ($ res ),
79
+ 'pagination ' => $ res ,
80
+ 'form ' => $ form ->createView (),
81
+ 'q ' => $ query ,
82
+ ]
83
+ );
69
84
}
85
+ } catch (\Exception $ e ) {
86
+ $ this ->logger ->error ($ e );
70
87
}
71
88
72
- $ user = $ this ->getUser ();
73
- $ res = $ this ->manager ->findPaginated ($ user , $ query , $ this ->getPageNb ($ request ));
74
-
75
89
return $ this ->render (
76
90
'search/front.html.twig ' ,
77
91
[
78
- 'objects ' => $ objects ,
79
- 'results ' => $ this ->overviewManager ->buildList ($ res ),
80
- 'pagination ' => $ res ,
81
- 'q ' => $ request ->query ->get ('q ' ),
92
+ 'objects ' => [],
93
+ 'results ' => [],
94
+ 'form ' => $ form ->createView (),
82
95
]
83
96
);
84
97
}
0 commit comments