now using jobs when posting activities
This commit is contained in:
parent
1740649ff7
commit
563c520aee
3
TODO.md
3
TODO.md
@ -54,3 +54,6 @@
|
|||||||
- [x] Fix that weird json encoding in the object field of an activity
|
- [x] Fix that weird json encoding in the object field of an activity
|
||||||
- [ ] The profile attachments are not working, they are not being federalised somehow (the interests thingy)
|
- [ ] The profile attachments are not working, they are not being federalised somehow (the interests thingy)
|
||||||
- [ ] Endpoints for getting notes (/ap/v1/note/{note})
|
- [ ] Endpoints for getting notes (/ap/v1/note/{note})
|
||||||
|
- [ ] Fix hashtags on post update
|
||||||
|
- [x] Use jobs when posting activities
|
||||||
|
- [ ] Sign the get activities for mastodon when secure mode is enable
|
||||||
|
@ -173,8 +173,9 @@ class APOutboxController extends Controller
|
|||||||
"object" => $object_actor->id,
|
"object" => $object_actor->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
// TODO: Check if it was successfully sent
|
||||||
return response ()->json ([ "error" => "failed to post activity" ], 500);
|
/* if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
||||||
|
return response ()->json ([ "error" => "failed to post activity" ], 500); */
|
||||||
|
|
||||||
return [
|
return [
|
||||||
"success" => "followed"
|
"success" => "followed"
|
||||||
@ -197,8 +198,9 @@ class APOutboxController extends Controller
|
|||||||
$unfollow_activity = TypeActivity::craft_undo ($follow_activity, $user->actor ()->first ());
|
$unfollow_activity = TypeActivity::craft_undo ($follow_activity, $user->actor ()->first ());
|
||||||
$response = TypeActivity::post_activity ($unfollow_activity, $user->actor ()->first (), $object_actor);
|
$response = TypeActivity::post_activity ($unfollow_activity, $user->actor ()->first (), $object_actor);
|
||||||
|
|
||||||
if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
// TODO: Check if it was successfully sent
|
||||||
return response ()->json ([ "error" => "failed to post activity" ], 500);
|
/* if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
||||||
|
return response ()->json ([ "error" => "failed to post activity" ], 500); */
|
||||||
|
|
||||||
$follow_activity->delete ();
|
$follow_activity->delete ();
|
||||||
return [
|
return [
|
||||||
@ -243,8 +245,9 @@ class APOutboxController extends Controller
|
|||||||
|
|
||||||
$response = TypeActivity::post_activity ($like_activity, $actor, $object->get_actor ()->first ());
|
$response = TypeActivity::post_activity ($like_activity, $actor, $object->get_actor ()->first ());
|
||||||
|
|
||||||
if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
// TODO: Check if it was successfully sent
|
||||||
return response ()->json ([ "error" => "failed to post activity" ], 500);
|
/* if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
||||||
|
return response ()->json ([ "error" => "failed to post activity" ], 500); */
|
||||||
|
|
||||||
return [
|
return [
|
||||||
"success" => "liked"
|
"success" => "liked"
|
||||||
|
101
app/Jobs/PostActivityJob.php
Normal file
101
app/Jobs/PostActivityJob.php
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\Actor;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
|
use App\Models\Activity;
|
||||||
|
|
||||||
|
use App\Types\TypeActivity;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Foundation\Queue\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
|
||||||
|
class PostActivityJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
|
||||||
|
public $tries = 3;
|
||||||
|
|
||||||
|
protected $activity;
|
||||||
|
protected $actor;
|
||||||
|
protected $target;
|
||||||
|
protected $should_sign;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*/
|
||||||
|
public function __construct(Activity $activity, Actor $actor, $target, $should_sign = false)
|
||||||
|
{
|
||||||
|
$this->activity = $activity;
|
||||||
|
$this->actor = $actor;
|
||||||
|
$this->target = $target;
|
||||||
|
$this->should_sign = $should_sign;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$crafted_activity = TypeActivity::craft_response($this->activity);
|
||||||
|
|
||||||
|
if ($this->should_sign)
|
||||||
|
{
|
||||||
|
$crafted_activity ["to"] = [
|
||||||
|
"https://www.w3.org/ns/activitystreams#Public",
|
||||||
|
];
|
||||||
|
|
||||||
|
$crafted_activity ["cc"] = [
|
||||||
|
$this->actor->following
|
||||||
|
];
|
||||||
|
|
||||||
|
$key = TypeActivity::get_private_key($this->actor);
|
||||||
|
$activity_json = json_encode($crafted_activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||||
|
$signature = TypeActivity::sign($activity_json, $key);
|
||||||
|
|
||||||
|
$crafted_activity ["signature"] = [
|
||||||
|
"type" => "RsaSignature2017",
|
||||||
|
"creator" => $this->actor->actor_id . "#main-key",
|
||||||
|
"created" => gmdate("Y-m-d\TH:i:s\Z"),
|
||||||
|
"signatureValue" => base64_encode($signature)
|
||||||
|
];
|
||||||
|
|
||||||
|
$activity_json = json_encode($crafted_activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activity_json = json_encode($crafted_activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||||
|
$activity_json = mb_convert_encoding($activity_json, "UTF-8");
|
||||||
|
|
||||||
|
$headers = TypeActivity::craft_signed_headers($activity_json, $this->actor, $this->target);
|
||||||
|
if (!$headers)
|
||||||
|
{
|
||||||
|
throw new \Exception("Failed to craft headers");
|
||||||
|
}
|
||||||
|
|
||||||
|
$target_inbox = null;
|
||||||
|
|
||||||
|
if ($this->target instanceof Actor)
|
||||||
|
{
|
||||||
|
$target_inbox = $this->target->inbox;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$target_inbox = $this->target;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = new Client ();
|
||||||
|
$response = $client->post($target_inbox, [
|
||||||
|
"headers" => $headers,
|
||||||
|
"body" => $activity_json,
|
||||||
|
"debug" => true
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function failed (\Exception $exception)
|
||||||
|
{
|
||||||
|
Log::error("Failed to post activity: " . $exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,8 @@ use App\Models\Actor;
|
|||||||
use App\Models\Activity;
|
use App\Models\Activity;
|
||||||
use App\Models\Instance;
|
use App\Models\Instance;
|
||||||
|
|
||||||
|
use App\Jobs\PostActivityJob;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
@ -194,74 +196,11 @@ class TypeActivity {
|
|||||||
|
|
||||||
public static function post_activity (Activity $activity, Actor $source, $target, $should_sign = false)
|
public static function post_activity (Activity $activity, Actor $source, $target, $should_sign = false)
|
||||||
{
|
{
|
||||||
$crafted_activity = TypeActivity::craft_response ($activity);
|
PostActivityJob::dispatch ($activity, $source, $target, $should_sign);
|
||||||
|
|
||||||
if ($should_sign)
|
return [
|
||||||
{
|
"success" => "activity posted"
|
||||||
$crafted_activity["to"] = [
|
|
||||||
"https://www.w3.org/ns/activitystreams#Public"
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$crafted_activity["cc"] = [
|
|
||||||
$source->following
|
|
||||||
];
|
|
||||||
|
|
||||||
$key = TypeActivity::get_private_key ($source);
|
|
||||||
$activity_json = json_encode ($crafted_activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);
|
|
||||||
$signature = TypeActivity::sign ($activity_json, $key);
|
|
||||||
|
|
||||||
$crafted_activity ["signature"] = [
|
|
||||||
"type" => "RsaSignature2017",
|
|
||||||
"creator" => $source->actor_id . "#main-key",
|
|
||||||
"created" => gmdate ("Y-m-d\TH:i:s\Z"),
|
|
||||||
"signatureValue" => base64_encode ($signature)
|
|
||||||
];
|
|
||||||
|
|
||||||
$activity_json = json_encode ($crafted_activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);
|
|
||||||
}
|
|
||||||
|
|
||||||
$activity_json = json_encode ($crafted_activity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);
|
|
||||||
$activity_json = mb_convert_encoding ($activity_json, "UTF-8");
|
|
||||||
|
|
||||||
$headers = TypeActivity::craft_signed_headers ($activity_json, $source, $target);
|
|
||||||
if (!$headers)
|
|
||||||
{
|
|
||||||
Log::error ("Failed to craft headers");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$target_inbox = null;
|
|
||||||
|
|
||||||
if ($target instanceof Actor)
|
|
||||||
{
|
|
||||||
$target_inbox = $target->inbox;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$target_inbox = $target;
|
|
||||||
}
|
|
||||||
|
|
||||||
$client = new Client ();
|
|
||||||
$response = $client->post ($target_inbox, [
|
|
||||||
"headers" => $headers,
|
|
||||||
"body" => $activity_json,
|
|
||||||
"debug" => true
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
catch (RequestException $e)
|
|
||||||
{
|
|
||||||
$response = $e->getResponse ();
|
|
||||||
if ($response)
|
|
||||||
{
|
|
||||||
Log::error ("Failed to post activity: " . $response->getBody ());
|
|
||||||
}
|
|
||||||
|
|
||||||
Log::error ("Failed to post activity: " . $e->getMessage ());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function post_to_instances (Activity $activity, Actor $source)
|
public static function post_to_instances (Activity $activity, Actor $source)
|
||||||
@ -272,11 +211,12 @@ class TypeActivity {
|
|||||||
if (!$instance->inbox)
|
if (!$instance->inbox)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$response = TypeActivity::post_activity ($activity, $source, $instance->inbox, true);
|
TypeActivity::post_activity ($activity, $source, $instance->inbox, true);
|
||||||
if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
// TODO: Check if it was successfully posted
|
||||||
|
/* if (!$response || $response->getStatusCode () < 200 || $response->getStatusCode () >= 300)
|
||||||
{
|
{
|
||||||
Log::info ("failed to post activity to " . $instance->inbox);
|
Log::info ("failed to post activity to " . $instance->inbox);
|
||||||
}
|
} */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,29 +59,37 @@
|
|||||||
@if (!auth ()->user ()->is ($user))
|
@if (!auth ()->user ()->is ($user))
|
||||||
<div class="f-col">
|
<div class="f-col">
|
||||||
@if (auth ()->user ()->actor->friends_with ($actor))
|
@if (auth ()->user ()->actor->friends_with ($actor))
|
||||||
|
<a href="#">
|
||||||
<form action="{{ route ('user.unfriend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
<form action="{{ route ('user.unfriend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
||||||
@csrf
|
@csrf
|
||||||
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
||||||
<img loading="lazy" src="/resources/icons/delete.png" alt=""> Remove Friend
|
<img loading="lazy" src="/resources/icons/delete.png" alt=""> Remove Friend
|
||||||
</form>
|
</form>
|
||||||
|
</a>
|
||||||
@elseif (in_array ($actor->actor_id, auth ()->user ()->received_requests ()))
|
@elseif (in_array ($actor->actor_id, auth ()->user ()->received_requests ()))
|
||||||
|
<a href="#">
|
||||||
<form action="{{ route ('user.friend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
<form action="{{ route ('user.friend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
||||||
@csrf
|
@csrf
|
||||||
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
||||||
<img loading="lazy" src="/resources/icons/add.png" alt=""> Accept Friend Request
|
<img loading="lazy" src="/resources/icons/add.png" alt=""> Accept Friend Request
|
||||||
</form>
|
</form>
|
||||||
|
</a>
|
||||||
@elseif (in_array ($actor->actor_id, auth ()->user ()->sent_requests ()))
|
@elseif (in_array ($actor->actor_id, auth ()->user ()->sent_requests ()))
|
||||||
|
<a href="#">
|
||||||
<form action="{{ route ('user.unfriend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
<form action="{{ route ('user.unfriend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
||||||
@csrf
|
@csrf
|
||||||
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
||||||
<img loading="lazy" src="/resources/icons/hourglass.png" alt=""> Cancel Request
|
<img loading="lazy" src="/resources/icons/hourglass.png" alt=""> Cancel Request
|
||||||
</form>
|
</form>
|
||||||
|
</a>
|
||||||
@else
|
@else
|
||||||
|
<a href="#">
|
||||||
<form action="{{ route ('user.friend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
<form action="{{ route ('user.friend') }}" onclick="this.submit ()" method="post" style="cursor: pointer">
|
||||||
@csrf
|
@csrf
|
||||||
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
<input type="hidden" name="object" value="{{ $actor->actor_id }}">
|
||||||
<img loading="lazy" src="/resources/icons/add.png" alt=""> Add to Friends
|
<img loading="lazy" src="/resources/icons/add.png" alt=""> Add to Friends
|
||||||
</form>
|
</form>
|
||||||
|
</a>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
@else
|
@else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user