2020-07-19 18:39:19 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2020-07-19 23:04:10 +09:00
|
|
|
use App\CheckinWebhook;
|
|
|
|
use App\Ejaculation;
|
|
|
|
use App\Events\LinkDiscovered;
|
2020-07-19 18:39:19 +09:00
|
|
|
use App\Http\Controllers\Controller;
|
2020-07-21 00:29:48 +09:00
|
|
|
use App\Http\Resources\EjaculationResource;
|
2020-07-19 23:04:10 +09:00
|
|
|
use App\Tag;
|
|
|
|
use Carbon\Carbon;
|
2020-07-19 18:39:19 +09:00
|
|
|
use Illuminate\Http\Request;
|
2020-08-07 23:22:01 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-07-19 23:04:10 +09:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2020-08-09 10:59:44 +09:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2020-07-19 18:39:19 +09:00
|
|
|
|
|
|
|
class WebhookController extends Controller
|
|
|
|
{
|
2020-07-19 23:04:10 +09:00
|
|
|
public function checkin(CheckinWebhook $webhook, Request $request)
|
2020-07-19 18:39:19 +09:00
|
|
|
{
|
2020-07-19 23:04:10 +09:00
|
|
|
if (!$webhook->isAvailable()) {
|
|
|
|
return response()->json([
|
|
|
|
'status' => 404,
|
|
|
|
'error' => [
|
|
|
|
'message' => 'The webhook is unavailable'
|
|
|
|
]
|
|
|
|
], 404);
|
|
|
|
}
|
|
|
|
|
2020-08-09 10:59:44 +09:00
|
|
|
$validator = Validator::make($request->all(), [
|
2020-07-21 00:29:48 +09:00
|
|
|
'checked_in_at' => 'nullable|date|after_or_equal:2000-01-01 00:00:00|before_or_equal:2099-12-31 23:59:59',
|
2020-07-19 23:04:10 +09:00
|
|
|
'note' => 'nullable|string|max:500',
|
|
|
|
'link' => 'nullable|url|max:2000',
|
|
|
|
'tags' => 'nullable|array',
|
2020-07-21 22:55:11 +09:00
|
|
|
'tags.*' => ['string', 'not_regex:/[\s\r\n]/u', 'max:255'],
|
2020-07-19 23:04:10 +09:00
|
|
|
'is_private' => 'nullable|boolean',
|
|
|
|
'is_too_sensitive' => 'nullable|boolean',
|
|
|
|
], [
|
2020-07-21 22:55:11 +09:00
|
|
|
'tags.*.not_regex' => 'The :attribute cannot contain spaces, tabs and newlines.'
|
2020-07-19 23:04:10 +09:00
|
|
|
]);
|
|
|
|
|
2020-08-09 10:59:44 +09:00
|
|
|
try {
|
|
|
|
$inputs = $validator->validate();
|
|
|
|
} catch (ValidationException $e) {
|
2020-07-19 23:04:10 +09:00
|
|
|
return response()->json([
|
|
|
|
'status' => 422,
|
|
|
|
'error' => [
|
|
|
|
'message' => 'Validation failed',
|
|
|
|
'violations' => $validator->errors()->all(),
|
|
|
|
]
|
2020-07-24 13:56:06 +09:00
|
|
|
], 422);
|
2020-07-19 23:04:10 +09:00
|
|
|
}
|
|
|
|
|
2020-07-21 00:29:48 +09:00
|
|
|
$ejaculatedDate = empty($inputs['checked_in_at']) ? now() : new Carbon($inputs['checked_in_at']);
|
|
|
|
$ejaculatedDate = $ejaculatedDate->setTimezone(date_default_timezone_get())->startOfMinute();
|
2020-07-19 23:04:10 +09:00
|
|
|
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',
|
|
|
|
]
|
2020-07-24 13:56:06 +09:00
|
|
|
], 422);
|
2020-07-19 23:04:10 +09:00
|
|
|
}
|
|
|
|
|
2020-08-09 11:06:42 +09:00
|
|
|
$ejaculation = DB::transaction(function () use ($inputs, $webhook, $ejaculatedDate) {
|
2020-08-07 23:22:01 +09:00
|
|
|
$ejaculation = Ejaculation::create([
|
|
|
|
'user_id' => $webhook->user_id,
|
|
|
|
'ejaculated_date' => $ejaculatedDate,
|
|
|
|
'note' => $inputs['note'] ?? '',
|
|
|
|
'link' => $inputs['link'] ?? '',
|
|
|
|
'source' => Ejaculation::SOURCE_WEBHOOK,
|
2020-08-09 11:06:42 +09:00
|
|
|
'is_private' => (bool)($inputs['is_private'] ?? false),
|
|
|
|
'is_too_sensitive' => (bool)($inputs['is_too_sensitive'] ?? false),
|
2020-08-07 23:22:01 +09:00
|
|
|
'checkin_webhook_id' => $webhook->id
|
|
|
|
]);
|
2020-07-19 23:04:10 +09:00
|
|
|
|
2020-08-07 23:22:01 +09:00
|
|
|
$tagIds = [];
|
|
|
|
if (!empty($inputs['tags'])) {
|
|
|
|
foreach ($inputs['tags'] as $tag) {
|
|
|
|
$tag = trim($tag);
|
|
|
|
if ($tag === '') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-19 23:04:10 +09:00
|
|
|
|
2020-08-07 23:22:01 +09:00
|
|
|
$tag = Tag::firstOrCreate(['name' => $tag]);
|
|
|
|
$tagIds[] = $tag->id;
|
|
|
|
}
|
2020-07-19 23:04:10 +09:00
|
|
|
}
|
2020-08-07 23:22:01 +09:00
|
|
|
$ejaculation->tags()->sync($tagIds);
|
|
|
|
|
|
|
|
return $ejaculation;
|
|
|
|
});
|
2020-07-19 23:04:10 +09:00
|
|
|
|
|
|
|
if (!empty($ejaculation->link)) {
|
|
|
|
event(new LinkDiscovered($ejaculation->link));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'status' => 200,
|
2020-07-21 00:29:48 +09:00
|
|
|
'checkin' => new EjaculationResource($ejaculation)
|
2020-07-19 23:04:10 +09:00
|
|
|
]);
|
2020-07-19 18:39:19 +09:00
|
|
|
}
|
|
|
|
}
|