88use App \Models \Profile ;
99use App \Models \QuoteAuthorization ;
1010use App \Models \Video ;
11+ use App \Services \ActivityPubCacheService ;
1112use App \Services \ConfigService ;
1213use App \Services \HashidService ;
1314use App \Services \VideoService ;
1415use Illuminate \Http \Request ;
1516
1617class ObjectController extends Controller
1718{
19+ protected $ apCache ;
20+
21+ public function __construct (ActivityPubCacheService $ apCache )
22+ {
23+ $ this ->apCache = $ apCache ;
24+ }
25+
1826 public function showProfile (Request $ request , $ username )
1927 {
2028 $ config = app (ConfigService::class);
@@ -24,11 +32,7 @@ public function showProfile(Request $request, $username)
2432 return abort (404 , 'Not found ' );
2533 }
2634
27- if ($ config ->federationAuthorizedFetch ()) {
28- return abort (403 , 'You do not have permission to view this. ' );
29- }
30-
31- $ profile = Profile::whereUsername ($ username )->whereLocal (true )->first ();
35+ $ profile = Profile::active ()->whereUsername ($ username )->whereLocal (true )->first ();
3236
3337 if (! $ profile ) {
3438 return $ this ->activityPubError ('Record not found ' , 404 );
@@ -38,7 +42,12 @@ public function showProfile(Request $request, $username)
3842 return $ this ->activityPubError ('Record not available ' , 400 );
3943 }
4044
41- return $ this ->activityPubResponse ($ profile ->toActivityPub ());
45+ $ cacheKey = $ this ->apCache ->profileKey ($ profile ->id );
46+ $ activityPub = $ this ->apCache ->getOrCache ($ cacheKey , function () use ($ profile ) {
47+ return $ profile ->toActivityPub ();
48+ });
49+
50+ return $ this ->activityPubResponseWithCache ($ activityPub );
4251 }
4352
4453 $ profile = Profile::whereUsername ($ username )->first ();
@@ -69,10 +78,6 @@ public function showVideo(Request $request, $hashId)
6978 return abort (404 , 'Not found ' );
7079 }
7180
72- if ($ config ->federationAuthorizedFetch ()) {
73- return abort (403 , 'You do not have permission to view this. ' );
74- }
75-
7681 if ($ video ->visibility !== 1 ) {
7782 return $ this ->activityPubError ('You do not have permission to access this resource. ' , 403 );
7883 }
@@ -108,9 +113,13 @@ public function showVideo(Request $request, $hashId)
108113
109114 protected function renderVideoObject ($ video , $ hashId )
110115 {
111- $ res = CreateActivityBuilder::buildForVideoFlat ($ video ->profile , $ video );
116+ $ cacheKey = $ this ->apCache ->videoKey ($ video ->id );
117+
118+ $ res = $ this ->apCache ->getOrCache ($ cacheKey , function () use ($ video ) {
119+ return CreateActivityBuilder::buildForVideoFlat ($ video ->profile , $ video );
120+ });
112121
113- return $ this ->activityPubResponse ($ res );
122+ return $ this ->activityPubResponseWithCache ($ res );
114123 }
115124
116125 public function showVideoObject (Request $ request , $ actor , $ id )
@@ -171,18 +180,27 @@ protected function renderVideoCommentObject($video, $videoHashId, $hashId, $vid,
171180 {
172181 $ comment = Comment::with ('profile ' )->where ('visibility ' , 1 )->whereNull ('ap_id ' )->where ('status ' , 'active ' )->whereVideoId ($ vid )->findOrFail ($ cid );
173182 abort_if ($ comment ->profile ->status != 1 , 403 , 'Resource not available ' );
174- $ res = CreateActivityBuilder::buildForCommentFlat ($ comment ->profile , $ comment );
175183
176- return $ this ->activityPubResponse ($ res );
184+ $ cacheKey = $ this ->apCache ->commentKey ($ comment ->id );
185+
186+ $ res = $ this ->apCache ->getOrCache ($ cacheKey , function () use ($ comment ) {
187+ return CreateActivityBuilder::buildForCommentFlat ($ comment ->profile , $ comment );
188+ });
189+
190+ return $ this ->activityPubResponseWithCache ($ res );
177191 }
178192
179193 protected function renderVideoCommentReplyObject ($ video , $ comment , $ videoHashId , $ hashId , $ vid , $ cid )
180194 {
181195 abort_if ($ comment ->profile ->status != 1 , 403 , 'Resource not available ' );
182196
183- $ res = CreateActivityBuilder::buildForCommentReplyFlat ($ comment ->profile , $ comment );
197+ $ cacheKey = $ this ->apCache ->replyKey ($ comment ->id );
198+
199+ $ res = $ this ->apCache ->getOrCache ($ cacheKey , function () use ($ comment ) {
200+ return CreateActivityBuilder::buildForCommentReplyFlat ($ comment ->profile , $ comment );
201+ });
184202
185- return $ this ->activityPubResponse ($ res );
203+ return $ this ->activityPubResponseWithCache ($ res );
186204 }
187205
188206 public function getQuoteAuthorization ($ profileId , $ authId )
@@ -191,8 +209,27 @@ public function getQuoteAuthorization($profileId, $authId)
191209 ->where ('quoted_profile_id ' , $ profileId )
192210 ->firstOrFail ();
193211
194- return response ()->json ($ authorization ->toActivityPub ())
212+ $ cacheKey = "quote_auth: {$ authId }" ;
213+
214+ $ activityPub = $ this ->apCache ->getOrCache ($ cacheKey , function () use ($ authorization ) {
215+ return $ authorization ->toActivityPub ();
216+ }, 900 );
217+
218+ return $ this ->activityPubResponseWithCache ($ activityPub );
219+ }
220+
221+ /**
222+ * Return ActivityPub response with proper cache headers
223+ */
224+ protected function activityPubResponseWithCache ($ data )
225+ {
226+ $ json = is_array ($ data ) ? json_encode ($ data ) : $ data ;
227+ $ etag = $ this ->apCache ->getETag ($ json );
228+
229+ return response ($ json )
195230 ->header ('Content-Type ' , 'application/activity+json; charset=utf-8 ' )
196- ->header ('Cache-Control ' , 'public, max-age=300 ' );
231+ ->header ('Cache-Control ' , 'public, max-age=3600, s-maxage=7200, stale-while-revalidate=86400 ' )
232+ ->header ('ETag ' , $ etag )
233+ ->header ('Vary ' , 'Accept ' );
197234 }
198235}
0 commit comments