瀏覽代碼

Update AccountController, dispatch Accept Follow activity if applicable

Daniel Supernault 3 年之前
父節點
當前提交
4dc9365acb

+ 5 - 5
app/FollowRequest.php

@@ -11,6 +11,11 @@ class FollowRequest extends Model
 	protected $casts = [
 	protected $casts = [
 		'activity' => 'array',
 		'activity' => 'array',
 	];
 	];
+
+    public function actor()
+    {
+        return $this->belongsTo(Profile::class, 'follower_id', 'id');
+    }
 	
 	
     public function follower()
     public function follower()
     {
     {
@@ -22,11 +27,6 @@ class FollowRequest extends Model
         return $this->belongsTo(Profile::class, 'following_id', 'id');
         return $this->belongsTo(Profile::class, 'following_id', 'id');
     }
     }
 
 
-    public function actor()
-    {
-        return $this->belongsTo(Profile::class, 'follower_id', 'id');
-    }
-
     public function target()
     public function target()
     {
     {
         return $this->belongsTo(Profile::class, 'following_id', 'id');
         return $this->belongsTo(Profile::class, 'following_id', 'id');

+ 8 - 2
app/Http/Controllers/AccountController.php

@@ -29,6 +29,7 @@ use App\Transformer\Api\Mastodon\v1\AccountTransformer;
 use App\Services\AccountService;
 use App\Services\AccountService;
 use App\Services\UserFilterService;
 use App\Services\UserFilterService;
 use App\Services\RelationshipService;
 use App\Services\RelationshipService;
+use App\Jobs\FollowPipeline\FollowAcceptPipeline;
 
 
 class AccountController extends Controller
 class AccountController extends Controller
 {
 {
@@ -394,8 +395,13 @@ class AccountController extends Controller
 			$follow->profile_id = $follower->id;
 			$follow->profile_id = $follower->id;
 			$follow->following_id = $pid;
 			$follow->following_id = $pid;
 			$follow->save();
 			$follow->save();
-			FollowPipeline::dispatch($follow);
-			$followRequest->delete();
+
+            if($follower->domain != null && $follower->private_key === null) {
+                FollowAcceptPipeline::dispatch($followRequest);
+            } else {
+                FollowPipeline::dispatch($follow);
+                $followRequest->delete();
+            }
 			break;
 			break;
 
 
 			case 'reject':
 			case 'reject':

+ 69 - 0
app/Jobs/FollowPipeline/FollowAcceptPipeline.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Jobs\FollowPipeline;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+use Cache, Log;
+use Illuminate\Support\Facades\Redis;
+use League\Fractal;
+use League\Fractal\Serializer\ArraySerializer;
+use App\FollowRequest;
+use App\Util\ActivityPub\Helpers;
+use App\Transformer\ActivityPub\Verb\AcceptFollow;
+
+class FollowAcceptPipeline implements ShouldQueue
+{
+	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+	protected $followRequest;
+
+	/**
+	 * Delete the job if its models no longer exist.
+	 *
+	 * @var bool
+	 */
+	public $deleteWhenMissingModels = true;
+
+	/**
+	 * Create a new job instance.
+	 *
+	 * @return void
+	 */
+	public function __construct(FollowRequest $followRequest)
+	{
+		$this->followRequest = $followRequest;
+	}
+
+	/**
+	 * Execute the job.
+	 *
+	 * @return void
+	 */
+	public function handle()
+	{
+		$follow = $this->followRequest;
+		$actor = $follow->actor;
+		$target = $follow->target;
+
+		if($actor->domain == null || $actor->inbox_url == null || !$target->private_key) {
+			return;
+		}
+
+		$fractal = new Fractal\Manager();
+		$fractal->setSerializer(new ArraySerializer());
+		$resource = new Fractal\Resource\Item($follow, new AcceptFollow());
+		$activity = $fractal->createData($resource)->toArray();
+		$url = $actor->sharedInbox ?? $actor->inbox_url;
+
+		Helpers::sendSignedObject($target, $url, $activity);
+
+		$follow->delete();
+
+		return;
+	}
+}

+ 1 - 1
app/Transformer/ActivityPub/Verb/AcceptFollow.php

@@ -16,7 +16,7 @@ class AcceptFollow extends Fractal\TransformerAbstract
 			'actor'     => $follow->target->permalink(),
 			'actor'     => $follow->target->permalink(),
 			'object' 	=> [
 			'object' 	=> [
 				'type' 		=> 'Follow',
 				'type' 		=> 'Follow',
-				'id'        => $follow->activity ? $follow->activity['id'] : null,
+				'id'        => $follow->activity && isset($follow->activity['id']) ? $follow->activity['id'] : null,
 				'actor'		=> $follow->actor->permalink(),
 				'actor'		=> $follow->actor->permalink(),
 				'object'	=> $follow->target->permalink()
 				'object'	=> $follow->target->permalink()
 			]
 			]