Revert "Release 20200823.1100"
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\CheckinWebhook;
|
||||
use App\Ejaculation;
|
||||
use App\Events\LinkDiscovered;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\EjaculationResource;
|
||||
use App\Tag;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class WebhookController extends Controller
|
||||
{
|
||||
public function checkin(CheckinWebhook $webhook, Request $request)
|
||||
{
|
||||
if (!$webhook->isAvailable()) {
|
||||
return response()->json([
|
||||
'status' => 404,
|
||||
'error' => [
|
||||
'message' => 'The webhook is unavailable'
|
||||
]
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'checked_in_at' => 'nullable|date|after_or_equal:2000-01-01 00:00:00|before_or_equal:2099-12-31 23:59:59',
|
||||
'note' => 'nullable|string|max:500',
|
||||
'link' => 'nullable|url|max:2000',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => ['string', 'not_regex:/[\s\r\n]/u', 'max:255'],
|
||||
'is_private' => 'nullable|boolean',
|
||||
'is_too_sensitive' => 'nullable|boolean',
|
||||
], [
|
||||
'tags.*.not_regex' => 'The :attribute cannot contain spaces, tabs and newlines.'
|
||||
]);
|
||||
|
||||
try {
|
||||
$inputs = $validator->validate();
|
||||
} catch (ValidationException $e) {
|
||||
return response()->json([
|
||||
'status' => 422,
|
||||
'error' => [
|
||||
'message' => 'Validation failed',
|
||||
'violations' => $validator->errors()->all(),
|
||||
]
|
||||
], 422);
|
||||
}
|
||||
|
||||
$ejaculatedDate = empty($inputs['checked_in_at']) ? now() : new Carbon($inputs['checked_in_at']);
|
||||
$ejaculatedDate = $ejaculatedDate->setTimezone(date_default_timezone_get())->startOfMinute();
|
||||
if (Ejaculation::where(['user_id' => $webhook->user_id, 'ejaculated_date' => $ejaculatedDate])->count()) {
|
||||
return response()->json([
|
||||
'status' => 422,
|
||||
'error' => [
|
||||
'message' => 'Checkin already exists in this time',
|
||||
]
|
||||
], 422);
|
||||
}
|
||||
|
||||
$ejaculation = DB::transaction(function () use ($inputs, $webhook, $ejaculatedDate) {
|
||||
$ejaculation = Ejaculation::create([
|
||||
'user_id' => $webhook->user_id,
|
||||
'ejaculated_date' => $ejaculatedDate,
|
||||
'note' => $inputs['note'] ?? '',
|
||||
'link' => $inputs['link'] ?? '',
|
||||
'source' => Ejaculation::SOURCE_WEBHOOK,
|
||||
'is_private' => (bool)($inputs['is_private'] ?? false),
|
||||
'is_too_sensitive' => (bool)($inputs['is_too_sensitive'] ?? false),
|
||||
'checkin_webhook_id' => $webhook->id
|
||||
]);
|
||||
|
||||
$tagIds = [];
|
||||
if (!empty($inputs['tags'])) {
|
||||
foreach ($inputs['tags'] as $tag) {
|
||||
$tag = trim($tag);
|
||||
if ($tag === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tag = Tag::firstOrCreate(['name' => $tag]);
|
||||
$tagIds[] = $tag->id;
|
||||
}
|
||||
}
|
||||
$ejaculation->tags()->sync($tagIds);
|
||||
|
||||
return $ejaculation;
|
||||
});
|
||||
|
||||
if (!empty($ejaculation->link)) {
|
||||
event(new LinkDiscovered($ejaculation->link));
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'checkin' => new EjaculationResource($ejaculation)
|
||||
]);
|
||||
}
|
||||
}
|
@@ -16,26 +16,17 @@ class EjaculationController extends Controller
|
||||
{
|
||||
public function create(Request $request)
|
||||
{
|
||||
$tags = old('tags') ?? $request->input('tags', '');
|
||||
if (!empty($tags)) {
|
||||
$tags = explode(' ', $tags);
|
||||
}
|
||||
|
||||
$errors = $request->session()->get('errors');
|
||||
$initialState = [
|
||||
'fields' => [
|
||||
'date' => old('date') ?? $request->input('date', date('Y/m/d')),
|
||||
'time' => old('time') ?? $request->input('time', date('H:i')),
|
||||
'link' => old('link') ?? $request->input('link', ''),
|
||||
'tags' => $tags,
|
||||
'note' => old('note') ?? $request->input('note', ''),
|
||||
'is_private' => old('is_private') ?? $request->input('is_private', 0) == 1,
|
||||
'is_too_sensitive' => old('is_too_sensitive') ?? $request->input('is_too_sensitive', 0) == 1
|
||||
],
|
||||
'errors' => isset($errors) ? $errors->getMessages() : null
|
||||
$defaults = [
|
||||
'date' => $request->input('date', date('Y/m/d')),
|
||||
'time' => $request->input('time', date('H:i')),
|
||||
'link' => $request->input('link', ''),
|
||||
'tags' => $request->input('tags', ''),
|
||||
'note' => $request->input('note', ''),
|
||||
'is_private' => $request->input('is_private', 0) == 1,
|
||||
'is_too_sensitive' => $request->input('is_too_sensitive', 0) == 1
|
||||
];
|
||||
|
||||
return view('ejaculation.checkin')->with('initialState', $initialState);
|
||||
return view('ejaculation.checkin')->with('defaults', $defaults);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
@@ -121,36 +112,13 @@ class EjaculationController extends Controller
|
||||
return view('ejaculation.show')->with(compact('user', 'ejaculation', 'ejaculatedSpan'));
|
||||
}
|
||||
|
||||
public function edit(Request $request, $id)
|
||||
public function edit($id)
|
||||
{
|
||||
$ejaculation = Ejaculation::findOrFail($id);
|
||||
|
||||
$this->authorize('edit', $ejaculation);
|
||||
|
||||
if (old('tags') === null) {
|
||||
$tags = $ejaculation->tags->pluck('name');
|
||||
} else {
|
||||
$tags = old('tags');
|
||||
if (!empty($tags)) {
|
||||
$tags = explode(' ', $tags);
|
||||
}
|
||||
}
|
||||
|
||||
$errors = $request->session()->get('errors');
|
||||
$initialState = [
|
||||
'fields' => [
|
||||
'date' => old('date') ?? $ejaculation->ejaculated_date->format('Y/m/d'),
|
||||
'time' => old('time') ?? $ejaculation->ejaculated_date->format('H:i'),
|
||||
'link' => old('link') ?? $ejaculation->link,
|
||||
'tags' => $tags,
|
||||
'note' => old('note') ?? $ejaculation->note,
|
||||
'is_private' => is_bool(old('is_private')) ? old('is_private') : $ejaculation->note,
|
||||
'is_too_sensitive' => is_bool(old('is_too_sensitive')) ? old('is_too_sensitive') : $ejaculation->is_too_sensitive
|
||||
],
|
||||
'errors' => isset($errors) ? $errors->getMessages() : null
|
||||
];
|
||||
|
||||
return view('ejaculation.edit')->with(compact('ejaculation', 'initialState'));
|
||||
return view('ejaculation.edit')->with(compact('ejaculation'));
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
|
@@ -71,7 +71,7 @@ SQL
|
||||
->select('ejaculations.*')
|
||||
->with('user', 'tags')
|
||||
->withLikes()
|
||||
->visibleToTimeline()
|
||||
->onlyWebCheckin()
|
||||
->take(21)
|
||||
->get();
|
||||
|
||||
|
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\CheckinWebhook;
|
||||
use App\DeactivatedUser;
|
||||
use App\Ejaculation;
|
||||
use App\Exceptions\CsvImportException;
|
||||
@@ -76,46 +75,6 @@ class SettingController extends Controller
|
||||
return redirect()->route('setting.privacy')->with('status', 'プライバシー設定を更新しました。');
|
||||
}
|
||||
|
||||
public function webhooks()
|
||||
{
|
||||
$webhooks = Auth::user()->checkinWebhooks;
|
||||
$webhooksLimit = CheckinWebhook::PER_USER_LIMIT;
|
||||
|
||||
return view('setting.webhooks')->with(compact('webhooks', 'webhooksLimit'));
|
||||
}
|
||||
|
||||
public function storeWebhooks(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('checkin_webhooks', 'name')->where(function ($query) {
|
||||
return $query->where('user_id', Auth::id());
|
||||
})
|
||||
]
|
||||
], [], [
|
||||
'name' => '名前'
|
||||
]);
|
||||
|
||||
if (Auth::user()->checkinWebhooks()->count() >= CheckinWebhook::PER_USER_LIMIT) {
|
||||
return redirect()->route('setting.webhooks')
|
||||
->with('status', CheckinWebhook::PER_USER_LIMIT . '件以上のWebhookを作成することはできません。');
|
||||
}
|
||||
|
||||
Auth::user()->checkinWebhooks()->create($validated);
|
||||
|
||||
return redirect()->route('setting.webhooks')->with('status', '作成しました。');
|
||||
}
|
||||
|
||||
public function destroyWebhooks(CheckinWebhook $webhook)
|
||||
{
|
||||
$webhook->delete();
|
||||
|
||||
return redirect()->route('setting.webhooks')->with('status', '削除しました。');
|
||||
}
|
||||
|
||||
public function import()
|
||||
{
|
||||
return view('setting.import');
|
||||
|
@@ -19,7 +19,7 @@ class TimelineController extends Controller
|
||||
->select('ejaculations.*')
|
||||
->with('user', 'tags')
|
||||
->withLikes()
|
||||
->visibleToTimeline()
|
||||
->onlyWebCheckin()
|
||||
->paginate(21);
|
||||
|
||||
return view('timeline.public')->with(compact('ejaculations'));
|
||||
|
@@ -38,17 +38,15 @@ class Kernel extends HttpKernel
|
||||
\App\Http\Middleware\NormalizeLineEnding::class,
|
||||
],
|
||||
|
||||
// 現時点では内部APIしかないので、認証の手間を省くためにステートフルにしている。
|
||||
'api' => [
|
||||
\App\Http\Middleware\EnforceJson::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
|
||||
'stateful' => [
|
||||
\App\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
]
|
||||
'throttle:60,1',
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Request headerに Accept: application/json を上書きする。APIエンドポイント用。
|
||||
*/
|
||||
class EnforceJson
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$request->headers->set('Accept', 'application/json');
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class EjaculationResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'checked_in_at' => $this->ejaculated_date->format(\DateTime::ATOM),
|
||||
'note' => $this->note,
|
||||
'link' => $this->link,
|
||||
'tags' => $this->tags->pluck('name'),
|
||||
'source' => $this->source,
|
||||
'is_private' => $this->is_private,
|
||||
'is_too_sensitive' => $this->is_too_sensitive,
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user