Skip to content

Commit 8fc7d60

Browse files
committed
Add Comment + CommentReply QuotePost support
1 parent 27a479d commit 8fc7d60

3 files changed

Lines changed: 80 additions & 22 deletions

File tree

app/Federation/ActivityBuilders/CreateActivityBuilder.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,8 @@ public static function buildForComment(Profile $actor, Comment $comment): array
148148
$activityId = $comment->getObjectUrl('/activity');
149149
$commentObject = self::buildCommentObject($actor, $comment);
150150

151-
return [
152-
'@context' => [
153-
'https://www.w3.org/ns/activitystreams',
154-
'https://w3id.org/security/v1',
155-
],
151+
$commentObj = [
152+
'@context' => app(ActivityPubContext::class)->forComment($comment),
156153
'id' => $activityId,
157154
'type' => 'Create',
158155
'actor' => $actor->getActorId(),
@@ -161,6 +158,12 @@ public static function buildForComment(Profile $actor, Comment $comment): array
161158
'cc' => $commentObject['cc'],
162159
'object' => $commentObject,
163160
];
161+
162+
if ($comment->visibility === 1) {
163+
$commentObj['interactionPolicy'] = app(ActivityPubContext::class)->forCommentInteractionPolicy($comment);
164+
}
165+
166+
return $commentObj;
164167
}
165168

166169
/**
@@ -173,10 +176,7 @@ public static function buildForComment(Profile $actor, Comment $comment): array
173176
public static function buildForCommentFlat(Profile $actor, Comment $comment): array
174177
{
175178
return [
176-
'@context' => [
177-
'https://www.w3.org/ns/activitystreams',
178-
'https://w3id.org/security/v1',
179-
],
179+
'@context' => app(ActivityPubContext::class)->forComment($comment),
180180
...self::buildCommentObject($actor, $comment),
181181
];
182182
}
@@ -204,6 +204,10 @@ private static function buildCommentObject(Profile $actor, Comment $comment): ar
204204
'cc' => [],
205205
];
206206

207+
if ($comment->visibility === 1) {
208+
$commentObject['interactionPolicy'] = app(ActivityPubContext::class)->forCommentInteractionPolicy($comment);
209+
}
210+
207211
$mentions = [];
208212

209213
if ($comment->caption) {
@@ -260,11 +264,8 @@ public static function buildForCommentReply(Profile $actor, CommentReply $commen
260264
$activityId = $comment->getObjectUrl('/activity');
261265
$commentObject = self::buildCommentReplyObject($actor, $comment);
262266

263-
return [
264-
'@context' => [
265-
'https://www.w3.org/ns/activitystreams',
266-
'https://w3id.org/security/v1',
267-
],
267+
$commentObj = [
268+
'@context' => app(ActivityPubContext::class)->forCommentReply($comment),
268269
'id' => $activityId,
269270
'type' => 'Create',
270271
'actor' => $actor->getActorId(),
@@ -273,6 +274,12 @@ public static function buildForCommentReply(Profile $actor, CommentReply $commen
273274
'cc' => $commentObject['cc'],
274275
'object' => $commentObject,
275276
];
277+
278+
if ($comment->visibility === 1) {
279+
$commentObj['interactionPolicy'] = app(ActivityPubContext::class)->forCommentReplyInteractionPolicy($comment);
280+
}
281+
282+
return $commentObj;
276283
}
277284

278285
/**
@@ -298,6 +305,10 @@ private static function buildCommentReplyObject(Profile $actor, CommentReply $co
298305
'cc' => [],
299306
];
300307

308+
if ($comment->visibility === 1) {
309+
$commentObject['interactionPolicy'] = app(ActivityPubContext::class)->forCommentReplyInteractionPolicy($comment);
310+
}
311+
301312
$mentions = [];
302313

303314
$commentObject['tag'] = [];
@@ -350,10 +361,7 @@ private static function buildCommentReplyObject(Profile $actor, CommentReply $co
350361
public static function buildForCommentReplyFlat(Profile $actor, CommentReply $comment): array
351362
{
352363
return [
353-
'@context' => [
354-
'https://www.w3.org/ns/activitystreams',
355-
'https://w3id.org/security/v1',
356-
],
364+
'@context' => app(ActivityPubContext::class)->forCommentReply($comment),
357365
...self::buildCommentReplyObject($actor, $comment),
358366
];
359367
}

app/Http/Controllers/ObjectController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function showVideo(Request $request, $hashId)
8989
$cid = HashidService::safeDecode($replyHashId);
9090
$vid = $id;
9191
$videoHashId = $hashId;
92-
$comment = CommentReply::with('parent')->findOrFail($cid);
92+
$comment = CommentReply::with('parent')->where('visibility', 1)->whereNull('ap_id')->where('status', 'active')->findOrFail($cid);
9393

9494
return $this->renderVideoCommentReplyObject($video, $comment, $videoHashId, $hashId, $vid, $cid);
9595
} else {
@@ -143,7 +143,7 @@ public function showComment(Request $request, $videoHashId, $hashId)
143143

144144
public function showCommentObject(Request $request, $actor, $id)
145145
{
146-
$comment = Comment::with('profile')->findOrFail($id);
146+
$comment = Comment::with('profile')->where('visibility', 1)->whereNull('ap_id')->where('status', 'active')->findOrFail($id);
147147
abort_if($comment->profile->status != 1, 403, 'Resource not available');
148148
$video = Video::whereStatus(2)->findOrFail($comment->video_id);
149149
$videoHashId = HashidService::safeEncode($video->id);
@@ -156,7 +156,7 @@ public function showCommentObject(Request $request, $actor, $id)
156156

157157
public function showReplyObject(Request $request, $actor, $id)
158158
{
159-
$comment = CommentReply::with(['parent', 'profile'])->findOrFail($id);
159+
$comment = CommentReply::with(['parent', 'profile'])->where('visibility', 1)->whereNull('ap_id')->where('status', 'active')->findOrFail($id);
160160
abort_if($comment->profile->status != 1, 403, 'Resource not available');
161161
$video = Video::whereStatus(2)->findOrFail($comment->video_id);
162162
$videoHashId = HashidService::safeEncode($video->id);
@@ -169,7 +169,7 @@ public function showReplyObject(Request $request, $actor, $id)
169169

170170
protected function renderVideoCommentObject($video, $videoHashId, $hashId, $vid, $cid)
171171
{
172-
$comment = Comment::with('profile')->whereVideoId($vid)->findOrFail($cid);
172+
$comment = Comment::with('profile')->where('visibility', 1)->whereNull('ap_id')->where('status', 'active')->whereVideoId($vid)->findOrFail($cid);
173173
abort_if($comment->profile->status != 1, 403, 'Resource not available');
174174
$res = CreateActivityBuilder::buildForCommentFlat($comment->profile, $comment);
175175

app/Support/ActivityPubContext.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Support;
44

5+
use App\Models\Comment;
6+
use App\Models\CommentReply;
57
use App\Models\Video;
68

79
class ActivityPubContext
@@ -17,6 +19,54 @@ public function forVideo(Video $video)
1719
};
1820
}
1921

22+
public function forComment(Comment $comment)
23+
{
24+
$visibility = $comment->visibility ?? 2;
25+
26+
return match ($visibility) {
27+
1 => $this->getVideoPublic(),
28+
2, 3, 4, 5 => $this->getDefault(),
29+
default => $this->getDefault(),
30+
};
31+
}
32+
33+
public function forCommentInteractionPolicy(Comment $comment)
34+
{
35+
$visibility = $comment->visibility ?? 2;
36+
37+
return $visibility === 1 ? [
38+
'canQuote' => [
39+
'automaticApproval' => [
40+
'https://www.w3.org/ns/activitystreams#Public',
41+
],
42+
],
43+
] : [];
44+
}
45+
46+
public function forCommentReply(CommentReply $comment)
47+
{
48+
$visibility = $comment->visibility ?? 2;
49+
50+
return match ($visibility) {
51+
1 => $this->getVideoPublic(),
52+
2, 3, 4, 5 => $this->getDefault(),
53+
default => $this->getDefault(),
54+
};
55+
}
56+
57+
public function forCommentReplyInteractionPolicy(CommentReply $comment)
58+
{
59+
$visibility = $comment->visibility ?? 2;
60+
61+
return $visibility === 1 ? [
62+
'canQuote' => [
63+
'automaticApproval' => [
64+
'https://www.w3.org/ns/activitystreams#Public',
65+
],
66+
],
67+
] : [];
68+
}
69+
2070
public function forVideoInteractionPolicy(Video $video)
2171
{
2272
$visibility = $video->visibility ?? 2;

0 commit comments

Comments
 (0)