From b562b3b4001c3669b5feba5036e7994af9cc8ec3 Mon Sep 17 00:00:00 2001 From: shibafu Date: Fri, 5 Jan 2018 00:26:48 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AA=E3=82=AB=E3=82=BA=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=AF=E3=81=AE=E4=B8=80=E6=AC=A1=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Ejaculation.php | 2 +- .../Controllers/EjaculationController.php | 4 ++ app/Http/Controllers/UserController.php | 1 + ..._01_04_230948_add_link_to_ejaculations.php | 32 +++++++++++++ resources/views/ejaculation/checkin.blade.php | 23 ++------- resources/views/ejaculation/edit.blade.php | 23 ++------- resources/views/ejaculation/show.blade.php | 31 ++++++++++-- resources/views/user/profile.blade.php | 30 ++++++++++-- routes/api.php | 48 +++++++++++++++++++ 9 files changed, 147 insertions(+), 47 deletions(-) create mode 100644 database/migrations/2018_01_04_230948_add_link_to_ejaculations.php diff --git a/app/Ejaculation.php b/app/Ejaculation.php index 8b975ef..8410e48 100644 --- a/app/Ejaculation.php +++ b/app/Ejaculation.php @@ -10,7 +10,7 @@ class Ejaculation extends Model protected $fillable = [ 'user_id', 'ejaculated_date', - 'note', 'geo_latitude', 'geo_longitude', + 'note', 'geo_latitude', 'geo_longitude', 'link', 'is_private' ]; diff --git a/app/Http/Controllers/EjaculationController.php b/app/Http/Controllers/EjaculationController.php index b37f0c9..ecba43f 100644 --- a/app/Http/Controllers/EjaculationController.php +++ b/app/Http/Controllers/EjaculationController.php @@ -27,6 +27,7 @@ class EjaculationController extends Controller 'date' => 'required|date_format:Y/m/d', 'time' => 'required|date_format:H:i', 'note' => 'nullable|string|max:500', + 'link' => 'nullable|url', ])->after(function ($validator) use ($request, $inputs) { // 日時の重複チェック if (!$validator->errors()->hasAny(['date', 'time'])) { @@ -41,6 +42,7 @@ class EjaculationController extends Controller 'user_id' => Auth::id(), 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), 'note' => $inputs['note'] ?? '', + 'link' => $inputs['link'] ?? '', 'is_private' => $request->has('is_private') ?? false ]); @@ -88,6 +90,7 @@ class EjaculationController extends Controller 'date' => 'required|date_format:Y/m/d', 'time' => 'required|date_format:H:i', 'note' => 'nullable|string|max:500', + 'link' => 'nullable|url', ])->after(function ($validator) use ($id, $request, $inputs) { // 日時の重複チェック if (!$validator->errors()->hasAny(['date', 'time'])) { @@ -101,6 +104,7 @@ class EjaculationController extends Controller $ejaculation->fill([ 'ejaculated_date' => Carbon::createFromFormat('Y/m/d H:i', $inputs['date'] . ' ' . $inputs['time']), 'note' => $inputs['note'] ?? '', + 'link' => $inputs['link'] ?? '', 'is_private' => $request->has('is_private') ?? false ])->save(); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index dddee85..268a309 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -26,6 +26,7 @@ id, ejaculated_date, note, is_private, +link, to_char(lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC), 'YYYY/MM/DD HH24:MI') AS before_date, to_char(ejaculated_date - (lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)), 'FMDDD日 FMHH24時間 FMMI分') AS ejaculated_span SQL diff --git a/database/migrations/2018_01_04_230948_add_link_to_ejaculations.php b/database/migrations/2018_01_04_230948_add_link_to_ejaculations.php new file mode 100644 index 0000000..22665fb --- /dev/null +++ b/database/migrations/2018_01_04_230948_add_link_to_ejaculations.php @@ -0,0 +1,32 @@ +string('link')->default(''); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('ejaculations', function (Blueprint $table) { + $table->removeColumn('link'); + }); + } +} diff --git a/resources/views/ejaculation/checkin.blade.php b/resources/views/ejaculation/checkin.blade.php index 9295880..3675f9e 100644 --- a/resources/views/ejaculation/checkin.blade.php +++ b/resources/views/ejaculation/checkin.blade.php @@ -44,32 +44,19 @@ + --}}
- + オカズのURLを貼り付けて登録することができます。 -
-
-
-
このオカズで合っていますか?
-
-
- -
-

タイトル

- 概要 -
-
- 決定 - キャンセル -
-
+ @if ($errors->has('link')) +
{{ $errors->first('link') }}
+ @endif
- --}}
diff --git a/resources/views/ejaculation/edit.blade.php b/resources/views/ejaculation/edit.blade.php index 322bfbf..f37837e 100644 --- a/resources/views/ejaculation/edit.blade.php +++ b/resources/views/ejaculation/edit.blade.php @@ -45,32 +45,19 @@
+ --}}
- + オカズのURLを貼り付けて登録することができます。 -
-
-
-
このオカズで合っていますか?
-
-
- -
-

タイトル

- 概要 -
-
- 決定 - キャンセル -
-
+ @if ($errors->has('link')) +
{{ $errors->first('link') }}
+ @endif
- --}}
diff --git a/resources/views/ejaculation/show.blade.php b/resources/views/ejaculation/show.blade.php index 5bf8fea..ad51a6c 100644 --- a/resources/views/ejaculation/show.blade.php +++ b/resources/views/ejaculation/show.blade.php @@ -46,17 +46,20 @@

@endif - {{-- -
- - Thumbnail + @if (!empty($ejaculation->link)) + - --}} +

+ {{ $ejaculation->link }} +

+ @endif @if (!empty($ejaculation->note))

@@ -87,6 +90,7 @@ @endsection @push('script') + @endpush \ No newline at end of file diff --git a/resources/views/user/profile.blade.php b/resources/views/user/profile.blade.php index 6df8292..0aac214 100644 --- a/resources/views/user/profile.blade.php +++ b/resources/views/user/profile.blade.php @@ -32,17 +32,20 @@

@endif - {{-- -
- - Thumbnail + @if (!empty($ejaculation->link)) + - --}} +

+ {{ $ejaculation->link }} +

+ @endif @if (!empty($ejaculation->note))

@@ -104,5 +107,22 @@ form.attr('action', form.attr('action').replace('@', modal.data('id'))); form.submit(); }); + + $('.link-card').each(function () { + var $this = $(this); + $.ajax({ + url: '{{ url('/api/ogp') }}', + method: 'get', + type: 'json', + data: { + url: $this.find('a').attr('href') + } + }).then(function (data) { + $this.find('.card-title').text(data.title); + $this.find('.card-text').text(data.description); + $this.find('img').attr('src', data.image); + $this.removeClass('d-none'); + }); + }); @endpush \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index c641ca5..75a62bd 100644 --- a/routes/api.php +++ b/routes/api.php @@ -16,3 +16,51 @@ use Illuminate\Http\Request; Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); + +Route::get('/ogp', function (Request $request) { + $request->validate([ + 'url:required|url' + ]); + + $client = new GuzzleHttp\Client(); + $res = $client->get($request->input('url')); + if ($res->getStatusCode() === 200) { + $dom = new DOMDocument(); + @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); + $xpath = new DOMXPath($dom); + + $result = [ + 'title' => '', + 'description' => '', + 'image' => '' + ]; + + $titleNode = $xpath->query('//meta[@*="og:title"]'); + foreach ($titleNode as $node) { + if (!empty($node->getAttribute('content'))) { + $result['title'] = $node->getAttribute('content'); + break; + } + } + + $descriptionNode = $xpath->query('//meta[@*="og:description"]'); + foreach ($descriptionNode as $node) { + if (!empty($node->getAttribute('content'))) { + $result['description'] = $node->getAttribute('content'); + break; + } + } + + $imageNode = $xpath->query('//meta[@*="og:image"]'); + foreach ($imageNode as $node) { + if (!empty($node->getAttribute('content'))) { + $result['image'] = $node->getAttribute('content'); + break; + } + } + + return response()->json($result); + } else { + abort($res->getStatusCode()); + } +}); \ No newline at end of file