オカズリンクのデータを保存するようにした (#4)
This commit is contained in:
parent
f51aaea94c
commit
dfe149e969
24
app/Events/LinkDiscovered.php
Normal file
24
app/Events/LinkDiscovered.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class LinkDiscovered
|
||||
{
|
||||
use Dispatchable, SerializesModels;
|
||||
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param string $url
|
||||
*/
|
||||
public function __construct(string $url)
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\LinkDiscovered;
|
||||
use App\Tag;
|
||||
use App\User;
|
||||
use Carbon\Carbon;
|
||||
@ -71,6 +72,10 @@ class EjaculationController extends Controller
|
||||
}
|
||||
$ejaculation->tags()->sync($tagIds);
|
||||
|
||||
if (!empty($ejaculation->link)) {
|
||||
event(new LinkDiscovered($ejaculation->link));
|
||||
}
|
||||
|
||||
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインしました!');
|
||||
}
|
||||
|
||||
@ -148,6 +153,10 @@ class EjaculationController extends Controller
|
||||
}
|
||||
$ejaculation->tags()->sync($tagIds);
|
||||
|
||||
if (!empty($ejaculation->link)) {
|
||||
event(new LinkDiscovered($ejaculation->link));
|
||||
}
|
||||
|
||||
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインを修正しました!');
|
||||
}
|
||||
|
||||
|
54
app/Listeners/LinkCollector.php
Normal file
54
app/Listeners/LinkCollector.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\LinkDiscovered;
|
||||
use App\Metadata;
|
||||
use App\MetadataResolver\MetadataResolver;
|
||||
use App\Utilities\Formatter;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LinkCollector
|
||||
{
|
||||
/** @var Formatter */
|
||||
private $formatter;
|
||||
/** @var MetadataResolver */
|
||||
private $metadataResolver;
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Formatter $formatter, MetadataResolver $metadataResolver)
|
||||
{
|
||||
$this->formatter = $formatter;
|
||||
$this->metadataResolver = $metadataResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param LinkDiscovered $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(LinkDiscovered $event)
|
||||
{
|
||||
// URLの正規化
|
||||
$url = $this->formatter->normalizeUrl($event->url);
|
||||
|
||||
// 無かったら取得
|
||||
// TODO: ある程度古かったら再取得とかありだと思う
|
||||
$metadata = Metadata::find($url);
|
||||
if ($metadata == null) {
|
||||
$resolved = $this->metadataResolver->resolve($url);
|
||||
Metadata::create([
|
||||
'url' => $url,
|
||||
'title' => $resolved->title,
|
||||
'description' => $resolved->description,
|
||||
'image' => $resolved->image
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
15
app/Metadata.php
Normal file
15
app/Metadata.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Metadata extends Model
|
||||
{
|
||||
public $incrementing = false;
|
||||
protected $primaryKey = 'url';
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected $fillable = ['url', 'title', 'description', 'image'];
|
||||
protected $visible = ['url', 'title', 'description', 'image'];
|
||||
}
|
@ -13,9 +13,9 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
'App\Events\Event' => [
|
||||
'App\Listeners\EventListener',
|
||||
],
|
||||
'App\Events\LinkDiscovered' => [
|
||||
'App\Listeners\LinkCollector'
|
||||
]
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -36,4 +36,29 @@ class Formatter
|
||||
{
|
||||
return $this->linkify->processUrls($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* URLを正規化します。
|
||||
* @param string $url URL
|
||||
* @return string 正規化されたURL
|
||||
*/
|
||||
public function normalizeUrl($url)
|
||||
{
|
||||
// Decode
|
||||
$url = urldecode($url);
|
||||
|
||||
// Remove Hashbang
|
||||
$url = preg_replace('~/#!/~u', '/', $url);
|
||||
|
||||
// Sort query parameters
|
||||
$query = parse_url($url, PHP_URL_QUERY);
|
||||
if (!empty($query)) {
|
||||
$url = str_replace_last('?' . $query, '', $url);
|
||||
parse_str($query, $params);
|
||||
ksort($params);
|
||||
$url = $url . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateMetadataTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('metadata', function (Blueprint $table) {
|
||||
$table->string('url');
|
||||
$table->string('title');
|
||||
$table->string('description');
|
||||
$table->string('image');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('url');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('metadata');
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\MetadataResolver\MetadataResolver;
|
||||
use App\Utilities\Formatter;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/*
|
||||
@ -18,13 +19,23 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
});
|
||||
|
||||
Route::get('/checkin/card', function (Request $request, MetadataResolver $resolver) {
|
||||
Route::get('/checkin/card', function (Request $request, MetadataResolver $resolver, Formatter $formatter) {
|
||||
$request->validate([
|
||||
'url:required|url'
|
||||
]);
|
||||
$url = $request->input('url');
|
||||
$url = $formatter->normalizeUrl($request->input('url'));
|
||||
|
||||
$metadata = App\Metadata::find($url);
|
||||
if ($metadata == null) {
|
||||
$resolved = $resolver->resolve($url);
|
||||
$metadata = App\Metadata::create([
|
||||
'url' => $url,
|
||||
'title' => $resolved->title,
|
||||
'description' => $resolved->description,
|
||||
'image' => $resolved->image
|
||||
]);
|
||||
}
|
||||
|
||||
$metadata = $resolver->resolve($url);
|
||||
$response = response()->json($metadata);
|
||||
if (!config('app.debug')) {
|
||||
$response = $response->setCache(['public' => true, 'max_age' => 86400]);
|
||||
|
Loading…
Reference in New Issue
Block a user