Merge pull request #32 from eai04191/feature-Metadata-Expires

This commit is contained in:
shibafu 2019-01-16 00:38:10 +09:00
commit 0d4a61ef15
5 changed files with 48 additions and 10 deletions

View File

@ -43,14 +43,14 @@ class LinkCollector
// 無かったら取得 // 無かったら取得
// TODO: ある程度古かったら再取得とかありだと思う // TODO: ある程度古かったら再取得とかありだと思う
$metadata = Metadata::find($url); $metadata = Metadata::find($url);
if ($metadata == null) { if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) {
try { try {
$resolved = $this->metadataResolver->resolve($url); $resolved = $this->metadataResolver->resolve($url);
Metadata::create([ Metadata::updateOrCreate(['url' => $url], [
'url' => $url,
'title' => $resolved->title, 'title' => $resolved->title,
'description' => $resolved->description, 'description' => $resolved->description,
'image' => $resolved->image 'image' => $resolved->image,
'expires_at' => $resolved->expires_at
]); ]);
} catch (TransferException $e) { } catch (TransferException $e) {
// 何らかの通信エラーによってメタデータの取得に失敗した時、とりあえずエラーログにURLを残す // 何らかの通信エラーによってメタデータの取得に失敗した時、とりあえずエラーログにURLを残す

View File

@ -10,6 +10,8 @@ class Metadata extends Model
protected $primaryKey = 'url'; protected $primaryKey = 'url';
protected $keyType = 'string'; protected $keyType = 'string';
protected $fillable = ['url', 'title', 'description', 'image']; protected $fillable = ['url', 'title', 'description', 'image', 'expires_at'];
protected $visible = ['url', 'title', 'description', 'image']; protected $visible = ['url', 'title', 'description', 'image', 'expires_at'];
protected $dates = ['created_at', 'updated_at', 'expires_at'];
} }

View File

@ -2,9 +2,13 @@
namespace App\MetadataResolver; namespace App\MetadataResolver;
use Carbon\Carbon;
class Metadata class Metadata
{ {
public $title = ''; public $title = '';
public $description = ''; public $description = '';
public $image = ''; public $image = '';
/** @var Carbon|null */
public $expires_at = null;
} }

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddExpiresOnMetadata extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('metadata', function (Blueprint $table) {
$table->timestamp('expires_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('metadata', function (Blueprint $table) {
$table->removeColumn('expires_at');
});
}
}

View File

@ -26,13 +26,13 @@ Route::get('/checkin/card', function (Request $request, MetadataResolver $resolv
$url = $formatter->normalizeUrl($request->input('url')); $url = $formatter->normalizeUrl($request->input('url'));
$metadata = App\Metadata::find($url); $metadata = App\Metadata::find($url);
if ($metadata == null) { if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) {
$resolved = $resolver->resolve($url); $resolved = $resolver->resolve($url);
$metadata = App\Metadata::create([ $metadata = App\Metadata::updateOrCreate(['url' => $url], [
'url' => $url,
'title' => $resolved->title, 'title' => $resolved->title,
'description' => $resolved->description, 'description' => $resolved->description,
'image' => $resolved->image 'image' => $resolved->image,
'expires_at' => $resolved->expires_at
]); ]);
} }