diff --git a/README.md b/README.md index fb949be..e2a71b8 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ Notice that the styles were taken from [AnySpace](https://anyspace.3to.moe/about ## TODO: - [-] Activitypub - - [x] Accounts (users can now be found in mastodon instances) + - [x] Accounts - [ ] Posts - [ ] Local posts should be federated - [ ] Remote posts should be fetched - [x] Follows - - [ ] I cannot follow myself + - [x] I cannot follow myself - [ ] Check when waiting for approval - [ ] Likes - [ ] Comments @@ -31,9 +31,9 @@ Notice that the styles were taken from [AnySpace](https://anyspace.3to.moe/about - [ ] Mark account as private (in federation manual approval is needed) - [ ] Allow custom CSS - [ ] Profile audio - - [ ] Friends (they are known as follows in the activitypub protocol) - - [ ] Add friends - - [ ] Remove friends + - [x] Friends (they are known as follows in the activitypub protocol) + - [x] Add friends + - [x] Remove friends - [ ] Posts (everything should be federated) - [ ] Create posts - [ ] Delete posts diff --git a/app/Http/Controllers/AP/APInstanceInboxController.php b/app/Http/Controllers/AP/APInstanceInboxController.php index 287cac8..c196e6e 100644 --- a/app/Http/Controllers/AP/APInstanceInboxController.php +++ b/app/Http/Controllers/AP/APInstanceInboxController.php @@ -5,13 +5,65 @@ namespace App\Http\Controllers\AP; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; +use App\Models\Actor; +use App\Models\Activity; + +use App\Types\TypeActor; +use App\Types\TypeActivity; + use App\Http\Controllers\Controller; class APInstanceInboxController extends Controller { public function inbox () { - Log::info ("APInstanceInboxController:inbox"); - Log::info (json_encode (request ()->all ())); + $activity = request ()->all (); + $activity_type = $activity['type']; + + switch ($activity_type) + { + case "Update": + return $this->handle_update ($activity); + break; + + default: + Log::info ("APInstanceInboxController:inbox"); + Log::info (json_encode (request ()->all ())); + break; + } + + return response ()->json (["status" => "ok"]); + } + + public function handle_update ($activity) + { + if (TypeActivity::activity_exists ($activity ["id"])) + return response ()->json (["status" => "ok"]); + + $activity ["activity_id"] = $activity ["id"]; + $new_activity = Activity::create ($activity); + + $object = $activity ["object"]; + + switch ($object ["type"]) + { + case "Person": + return $this->handle_update_person ($object); + break; + } + + return response ()->json (["status" => "ok"]); + } + + public function handle_update_person ($person) + { + $actor = Actor::where ("actor_id", $person ["id"])->first (); + if (!$actor) + $actor = TypeActor::create_from_request ($person); + + TypeActor::update_from_request ($actor, $person); + $actor->save (); + + return response ()->json (["status" => "ok"]); } } diff --git a/app/Types/TypeActor.php b/app/Types/TypeActor.php index 80f42f1..75aca13 100644 --- a/app/Types/TypeActor.php +++ b/app/Types/TypeActor.php @@ -106,10 +106,8 @@ class TypeActor { return $response; } - public static function create_from_request ($request) + public static function update_from_request (Actor $actor, $request) { - $actor = new Actor (); - // Use null coalescing operator `??` for safety $actor->actor_id = $request['id'] ?? ''; $actor->local_actor_id = TypeActor::actor_build_private_id ($actor->actor_id) ?? ''; @@ -136,8 +134,6 @@ class TypeActor { // Handle nested keys in `publicKey` $actor->public_key = $request['publicKey']['publicKeyPem'] ?? ''; - $actor->save (); - $instances = Instance::where ("inbox", $actor->sharedInbox); if (!$instances->first ()) { @@ -149,6 +145,15 @@ class TypeActor { return $actor; } + public static function create_from_request ($request) + { + $actor = new Actor (); + TypeActor::update_from_request ($actor, $request); + + $actor->save (); + return $actor; + } + public static function obtain_actor_info ($actor_id) { $parsed_url = parse_url ($actor_id);