From 938a4d695737865709dcc3c4dddd21a6a5f82bbe Mon Sep 17 00:00:00 2001 From: eai04191 Date: Tue, 15 Jan 2019 15:31:15 +0900 Subject: [PATCH 1/6] =?UTF-8?q?metadata=E3=83=86=E3=83=BC=E3=83=96?= =?UTF-8?q?=E3=83=AB=E3=81=ABexpires=5Fat=E3=82=AB=E3=83=A9=E3=83=A0?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit メタデータの有効期限が現在より過去の場合、メタデータを再取得する --- app/Metadata.php | 4 +-- ...9_01_15_143800_add_expires_on_metadata.php | 32 +++++++++++++++++++ routes/api.php | 5 +-- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2019_01_15_143800_add_expires_on_metadata.php diff --git a/app/Metadata.php b/app/Metadata.php index 98badb6..ef80075 100644 --- a/app/Metadata.php +++ b/app/Metadata.php @@ -10,6 +10,6 @@ class Metadata extends Model protected $primaryKey = 'url'; protected $keyType = 'string'; - protected $fillable = ['url', 'title', 'description', 'image']; - protected $visible = ['url', 'title', 'description', 'image']; + protected $fillable = ['url', 'title', 'description', 'image', 'expires_at']; + protected $visible = ['url', 'title', 'description', 'image', 'expires_at']; } diff --git a/database/migrations/2019_01_15_143800_add_expires_on_metadata.php b/database/migrations/2019_01_15_143800_add_expires_on_metadata.php new file mode 100644 index 0000000..46891e1 --- /dev/null +++ b/database/migrations/2019_01_15_143800_add_expires_on_metadata.php @@ -0,0 +1,32 @@ +timestamp('expires_at')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('metadata', function (Blueprint $table) { + $table->removeColumn('expires_at'); + }); + } +} diff --git a/routes/api.php b/routes/api.php index 73e72cd..3f2b134 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,13 +26,14 @@ Route::get('/checkin/card', function (Request $request, MetadataResolver $resolv $url = $formatter->normalizeUrl($request->input('url')); $metadata = App\Metadata::find($url); - if ($metadata == null) { + if ($metadata == null || new DateTime($metadata->expires_at) < new DateTime("now")) { $resolved = $resolver->resolve($url); $metadata = App\Metadata::create([ 'url' => $url, 'title' => $resolved->title, 'description' => $resolved->description, - 'image' => $resolved->image + 'image' => $resolved->image, + 'expires_at' => $resolved->expires_at ]); } From 5f01cc3430ac9258187ff8f6f43e50719310581c Mon Sep 17 00:00:00 2001 From: shibafu Date: Tue, 15 Jan 2019 23:56:04 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E3=83=A1=E3=82=BF=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=82=A8=E3=83=B3=E3=83=86=E3=82=A3=E3=83=86=E3=82=A3?= =?UTF-8?q?=E3=81=ABexpires=5Fat=E3=83=97=E3=83=AD=E3=83=91=E3=83=86?= =?UTF-8?q?=E3=82=A3=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/MetadataResolver/Metadata.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/MetadataResolver/Metadata.php b/app/MetadataResolver/Metadata.php index 070dc52..dbe8654 100644 --- a/app/MetadataResolver/Metadata.php +++ b/app/MetadataResolver/Metadata.php @@ -2,9 +2,13 @@ namespace App\MetadataResolver; +use Carbon\Carbon; + class Metadata { public $title = ''; public $description = ''; public $image = ''; + /** @var Carbon|null */ + public $expires_at = null; } From acb9b5821db3c8ff756e6802e64d0418580e1762 Mon Sep 17 00:00:00 2001 From: shibafu Date: Tue, 15 Jan 2019 23:58:14 +0900 Subject: [PATCH 3/6] =?UTF-8?q?expires=5Fat=E3=81=AB=E6=9C=89=E5=8A=B9?= =?UTF-8?q?=E3=81=AA=E5=80=A4=E3=81=8C=E8=A8=AD=E5=AE=9A=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=AE=E3=81=BF?= =?UTF-8?q?=E3=80=81=E3=83=A1=E3=82=BF=E3=83=87=E3=83=BC=E3=82=BF=E3=81=AE?= =?UTF-8?q?=E6=9C=89=E5=8A=B9=E6=9C=9F=E9=99=90=E5=88=A4=E5=AE=9A=E3=82=92?= =?UTF-8?q?=E8=A1=8C=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api.php b/routes/api.php index 3f2b134..db4aa59 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,7 +26,7 @@ Route::get('/checkin/card', function (Request $request, MetadataResolver $resolv $url = $formatter->normalizeUrl($request->input('url')); $metadata = App\Metadata::find($url); - if ($metadata == null || new DateTime($metadata->expires_at) < new DateTime("now")) { + if ($metadata == null || ($metadata->expires_at !== null && new DateTime($metadata->expires_at) < new DateTime("now"))) { $resolved = $resolver->resolve($url); $metadata = App\Metadata::create([ 'url' => $url, From 810eea2a59a18512e18ef33dfaa2ec4a62ede951 Mon Sep 17 00:00:00 2001 From: shibafu Date: Wed, 16 Jan 2019 00:15:41 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Metadata.expires=5Fat=E3=82=92Carbon?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Metadata.php | 2 ++ routes/api.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Metadata.php b/app/Metadata.php index ef80075..dc44009 100644 --- a/app/Metadata.php +++ b/app/Metadata.php @@ -12,4 +12,6 @@ class Metadata extends Model protected $fillable = ['url', 'title', 'description', 'image', 'expires_at']; protected $visible = ['url', 'title', 'description', 'image', 'expires_at']; + + protected $dates = ['created_at', 'updated_at', 'expires_at']; } diff --git a/routes/api.php b/routes/api.php index db4aa59..37e9139 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,7 +26,7 @@ Route::get('/checkin/card', function (Request $request, MetadataResolver $resolv $url = $formatter->normalizeUrl($request->input('url')); $metadata = App\Metadata::find($url); - if ($metadata == null || ($metadata->expires_at !== null && new DateTime($metadata->expires_at) < new DateTime("now"))) { + if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) { $resolved = $resolver->resolve($url); $metadata = App\Metadata::create([ 'url' => $url, From cd26ef6236952e90ec834e602ea60a0a5f680102 Mon Sep 17 00:00:00 2001 From: shibafu Date: Wed, 16 Jan 2019 00:17:43 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E3=83=A1=E3=82=BF=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=81=AE=E6=9B=B4=E6=96=B0=E6=99=82=E3=81=AB=E5=A4=9A?= =?UTF-8?q?=E9=87=8D=E7=99=BB=E9=8C=B2=E3=81=95=E3=82=8C=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/api.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/routes/api.php b/routes/api.php index 37e9139..eb38041 100644 --- a/routes/api.php +++ b/routes/api.php @@ -28,8 +28,7 @@ Route::get('/checkin/card', function (Request $request, MetadataResolver $resolv $metadata = App\Metadata::find($url); if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) { $resolved = $resolver->resolve($url); - $metadata = App\Metadata::create([ - 'url' => $url, + $metadata = App\Metadata::updateOrCreate(['url' => $url], [ 'title' => $resolved->title, 'description' => $resolved->description, 'image' => $resolved->image, From cef23a64cb7a5547fb9d9680ac30994ed98bfa98 Mon Sep 17 00:00:00 2001 From: shibafu Date: Wed, 16 Jan 2019 00:21:19 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E3=83=A1=E3=82=BF=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=81=AE=E5=86=8D=E5=8F=96=E5=BE=97=E5=88=A4=E5=AE=9A?= =?UTF-8?q?=E3=82=92=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E6=99=82=E3=81=AE=E5=87=A6=E7=90=86=E3=81=AB=E3=82=82=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Listeners/LinkCollector.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Listeners/LinkCollector.php b/app/Listeners/LinkCollector.php index 81e97f1..0484f95 100644 --- a/app/Listeners/LinkCollector.php +++ b/app/Listeners/LinkCollector.php @@ -43,14 +43,14 @@ class LinkCollector // 無かったら取得 // TODO: ある程度古かったら再取得とかありだと思う $metadata = Metadata::find($url); - if ($metadata == null) { + if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) { try { $resolved = $this->metadataResolver->resolve($url); - Metadata::create([ - 'url' => $url, + Metadata::updateOrCreate(['url' => $url], [ 'title' => $resolved->title, 'description' => $resolved->description, - 'image' => $resolved->image + 'image' => $resolved->image, + 'expires_at' => $resolved->expires_at ]); } catch (TransferException $e) { // 何らかの通信エラーによってメタデータの取得に失敗した時、とりあえずエラーログにURLを残す