2017-08-27 04:44:53 +09:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
2019-04-05 23:10:26 +09:00
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2017-08-27 04:44:53 +09:00
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-04-05 23:10:26 +09:00
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-04-09 22:55:14 +09:00
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-04-14 16:46:20 +09:00
|
|
|
|
use Staudenmeir\EloquentEagerLimit\HasEagerLimit;
|
2017-08-27 04:44:53 +09:00
|
|
|
|
|
|
|
|
|
class Ejaculation extends Model
|
|
|
|
|
{
|
2019-04-14 16:46:20 +09:00
|
|
|
|
use HasEagerLimit;
|
2017-08-27 04:44:53 +09:00
|
|
|
|
|
2020-02-18 02:03:49 +09:00
|
|
|
|
const SOURCE_WEB = 'web';
|
|
|
|
|
const SOURCE_CSV = 'csv';
|
2020-07-19 23:04:10 +09:00
|
|
|
|
const SOURCE_WEBHOOK = 'webhook';
|
2020-02-18 02:03:49 +09:00
|
|
|
|
|
2017-08-27 04:44:53 +09:00
|
|
|
|
protected $fillable = [
|
|
|
|
|
'user_id', 'ejaculated_date',
|
2020-05-24 19:39:24 +09:00
|
|
|
|
'note', 'geo_latitude', 'geo_longitude', 'link', 'source',
|
2020-11-08 15:38:38 +09:00
|
|
|
|
'is_private', 'is_too_sensitive', 'discard_elapsed_time',
|
2020-07-24 12:12:35 +09:00
|
|
|
|
'checkin_webhook_id'
|
2017-08-27 04:44:53 +09:00
|
|
|
|
];
|
2017-11-05 01:26:52 +09:00
|
|
|
|
|
|
|
|
|
protected $dates = [
|
|
|
|
|
'ejaculated_date'
|
|
|
|
|
];
|
2018-01-07 22:19:33 +09:00
|
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo('App\User');
|
|
|
|
|
}
|
2018-01-08 08:50:22 +09:00
|
|
|
|
|
|
|
|
|
public function tags()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany('App\Tag')->withTimestamps();
|
|
|
|
|
}
|
2018-06-02 23:33:16 +09:00
|
|
|
|
|
|
|
|
|
public function textTags()
|
|
|
|
|
{
|
2019-01-15 00:05:01 +09:00
|
|
|
|
return implode(' ', $this->tags->map(function ($v) {
|
|
|
|
|
return $v->name;
|
|
|
|
|
})->all());
|
2018-06-02 23:33:16 +09:00
|
|
|
|
}
|
2019-04-05 23:10:26 +09:00
|
|
|
|
|
|
|
|
|
public function likes()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Like::class);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 14:19:57 +09:00
|
|
|
|
public function scopeVisibleToTimeline(Builder $query)
|
2020-05-21 23:58:58 +09:00
|
|
|
|
{
|
2020-07-24 17:51:48 +09:00
|
|
|
|
return $query->whereIn('ejaculations.source', [Ejaculation::SOURCE_WEB, Ejaculation::SOURCE_WEBHOOK]);
|
2020-05-21 23:58:58 +09:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-05 23:10:26 +09:00
|
|
|
|
public function scopeWithLikes(Builder $query)
|
|
|
|
|
{
|
|
|
|
|
if (Auth::check()) {
|
2019-04-14 16:47:58 +09:00
|
|
|
|
// TODO - このスコープを使うことでlikesが常に直近10件で絞られるのは汚染されすぎ感がある。別名を付与できないか?
|
|
|
|
|
// - (ejaculation_id, user_id) でユニークなわけですが、is_liked はサブクエリ発行させるのとLeft JoinしてNULLかどうかで結果を見るのどっちがいいんでしょうね
|
2019-04-07 23:27:24 +09:00
|
|
|
|
return $query
|
2019-04-14 16:46:20 +09:00
|
|
|
|
->with([
|
|
|
|
|
'likes' => function ($query) {
|
|
|
|
|
$query->latest()->take(10);
|
|
|
|
|
},
|
|
|
|
|
'likes.user' => function ($query) {
|
|
|
|
|
$query->where('is_protected', false)
|
2019-09-15 01:55:17 +09:00
|
|
|
|
->where('private_likes', false)
|
2019-04-14 16:46:20 +09:00
|
|
|
|
->orWhere('id', Auth::id());
|
|
|
|
|
}
|
|
|
|
|
])
|
2019-04-07 23:27:24 +09:00
|
|
|
|
->withCount([
|
|
|
|
|
'likes',
|
|
|
|
|
'likes as is_liked' => function ($query) {
|
|
|
|
|
$query->where('user_id', Auth::id());
|
|
|
|
|
}
|
|
|
|
|
]);
|
2019-04-05 23:10:26 +09:00
|
|
|
|
} else {
|
2019-04-07 23:27:24 +09:00
|
|
|
|
return $query
|
2019-04-14 16:46:20 +09:00
|
|
|
|
->with([
|
|
|
|
|
'likes' => function ($query) {
|
|
|
|
|
$query->latest()->take(10);
|
|
|
|
|
},
|
|
|
|
|
'likes.user' => function ($query) {
|
2019-09-15 01:55:17 +09:00
|
|
|
|
$query->where('is_protected', false)
|
|
|
|
|
->where('private_likes', false);
|
2019-04-14 16:46:20 +09:00
|
|
|
|
}
|
|
|
|
|
])
|
2019-04-07 23:27:24 +09:00
|
|
|
|
->withCount('likes')
|
2019-04-09 22:55:14 +09:00
|
|
|
|
->addSelect(DB::raw('0 as is_liked'));
|
2019-04-05 23:10:26 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-08 16:32:37 +09:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* このチェックインと同じ情報を流用してチェックインするためのURLを生成
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function makeCheckinURL(): string
|
|
|
|
|
{
|
|
|
|
|
return route('checkin', [
|
|
|
|
|
'link' => $this->link,
|
|
|
|
|
'tags' => $this->textTags(),
|
2020-08-23 13:06:11 +09:00
|
|
|
|
'is_private' => $this->is_private,
|
2019-09-08 16:32:37 +09:00
|
|
|
|
'is_too_sensitive' => $this->is_too_sensitive,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-11-08 23:07:51 +09:00
|
|
|
|
|
|
|
|
|
public function ejaculatedSpan(): string
|
|
|
|
|
{
|
|
|
|
|
if (array_key_exists('ejaculated_span', $this->attributes)) {
|
|
|
|
|
if ($this->ejaculated_span === null) {
|
|
|
|
|
return '精通';
|
|
|
|
|
}
|
|
|
|
|
if ($this->discard_elapsed_time) {
|
|
|
|
|
return '0日 0時間 0分'; // TODO: 気の効いたフレーズにする
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->ejaculated_span;
|
|
|
|
|
} else {
|
|
|
|
|
$previous = Ejaculation::select('ejaculated_date')
|
|
|
|
|
->where('user_id', $this->user_id)
|
|
|
|
|
->where('ejaculated_date', '<', $this->ejaculated_date)
|
|
|
|
|
->orderByDesc('ejaculated_date')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($previous === null) {
|
|
|
|
|
return '精通';
|
|
|
|
|
}
|
|
|
|
|
if ($this->discard_elapsed_time) {
|
|
|
|
|
return '0日 0時間 0分';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->ejaculated_date->diff($previous->ejaculated_date)->format('%a日 %h時間 %i分');
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
|
}
|