Add model to store syndication target data
This commit is contained in:
parent
7cd9956b92
commit
ea8395a651
5 changed files with 154 additions and 9 deletions
|
@ -6,6 +6,7 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\Http\Responses\MicropubResponses;
|
||||
use App\Models\Place;
|
||||
use App\Models\SyndicationTarget;
|
||||
use App\Services\Micropub\HCardService;
|
||||
use App\Services\Micropub\HEntryService;
|
||||
use App\Services\Micropub\UpdateService;
|
||||
|
@ -121,21 +122,19 @@ class MicropubController extends Controller
|
|||
{
|
||||
try {
|
||||
$tokenData = $this->tokenService->validateToken(request()->input('access_token'));
|
||||
} catch (RequiredConstraintsViolated | InvalidTokenStructure $exception) {
|
||||
$micropubResponses = new MicropubResponses();
|
||||
|
||||
return $micropubResponses->invalidTokenResponse();
|
||||
} catch (RequiredConstraintsViolated | InvalidTokenStructure) {
|
||||
return (new MicropubResponses())->invalidTokenResponse();
|
||||
}
|
||||
|
||||
if (request()->input('q') === 'syndicate-to') {
|
||||
return response()->json([
|
||||
'syndicate-to' => config('syndication.targets'),
|
||||
'syndicate-to' => SyndicationTarget::all(),
|
||||
]);
|
||||
}
|
||||
|
||||
if (request()->input('q') == 'config') {
|
||||
if (request()->input('q') === 'config') {
|
||||
return response()->json([
|
||||
'syndicate-to' => config('syndication.targets'),
|
||||
'syndicate-to' => SyndicationTarget::all(),
|
||||
'media-endpoint' => route('media-endpoint'),
|
||||
]);
|
||||
}
|
||||
|
|
68
app/Models/SyndicationTarget.php
Normal file
68
app/Models/SyndicationTarget.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SyndicationTarget extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are visible when serializing the model.
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected $visible = [
|
||||
'uid',
|
||||
'name',
|
||||
'service',
|
||||
'user',
|
||||
];
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = [
|
||||
'service',
|
||||
'user',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the service data as a single attribute.
|
||||
*
|
||||
* @vreturn Attribute
|
||||
*/
|
||||
protected function service(): Attribute
|
||||
{
|
||||
return Attribute::get(
|
||||
get: fn ($value, $attributes) => [
|
||||
'name' => $attributes['service_name'],
|
||||
'url' => $attributes['service_url'],
|
||||
'photo' => $attributes['service_photo'],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user data as a single attribute.
|
||||
*
|
||||
* @vreturn Attribute
|
||||
*/
|
||||
protected function user(): Attribute
|
||||
{
|
||||
return Attribute::get(
|
||||
get: fn ($value, $attributes) => [
|
||||
'name' => $attributes['user_name'],
|
||||
'url' => $attributes['user_url'],
|
||||
'photo' => $attributes['user_photo'],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
30
database/factories/SyndicationTargetFactory.php
Normal file
30
database/factories/SyndicationTargetFactory.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\SyndicationTarget>
|
||||
*/
|
||||
class SyndicationTargetFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'uid' => $this->faker->url,
|
||||
'name' => $this->faker->name,
|
||||
'service_name' => $this->faker->name,
|
||||
'service_url' => $this->faker->url,
|
||||
'service_photo' => $this->faker->url,
|
||||
'user_name' => $this->faker->name,
|
||||
'user_url' => $this->faker->url,
|
||||
'user_photo' => $this->faker->url,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('syndication_targets', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('uid');
|
||||
$table->string('name');
|
||||
$table->string('service_name');
|
||||
$table->string('service_url');
|
||||
$table->string('service_photo');
|
||||
$table->string('user_name');
|
||||
$table->string('user_url');
|
||||
$table->string('user_photo');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('syndication_targets');
|
||||
}
|
||||
};
|
|
@ -9,6 +9,7 @@ use App\Jobs\SyndicateNoteToTwitter;
|
|||
use App\Models\Media;
|
||||
use App\Models\Note;
|
||||
use App\Models\Place;
|
||||
use App\Models\SyndicationTarget;
|
||||
use Carbon\Carbon;
|
||||
use Faker\Factory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
@ -51,10 +52,18 @@ class MicropubControllerTest extends TestCase
|
|||
}
|
||||
|
||||
/** @test */
|
||||
public function micropubClientsCanRequestSyndicationTargets(): void
|
||||
public function micropubClientsCanRequestSyndicationTargetsCanBeEmpty(): void
|
||||
{
|
||||
$response = $this->get('/api/post?q=syndicate-to', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
||||
$response->assertJsonFragment(['uid' => 'https://twitter.com/jonnybarnes']);
|
||||
$response->assertJsonFragment(['syndicate-to' => []]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function micropubClientsCanRequestSyndicationTargetsPopulatesFromModel(): void
|
||||
{
|
||||
$syndicationTarget = SyndicationTarget::factory()->create();
|
||||
$response = $this->get('/api/post?q=syndicate-to', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
||||
$response->assertJsonFragment(['uid' => $syndicationTarget->uid]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue