diff --git a/app/Http/Controllers/Microblog/VoteController.php b/app/Http/Controllers/Microblog/VoteController.php index 692db5eca..9288ee90a 100644 --- a/app/Http/Controllers/Microblog/VoteController.php +++ b/app/Http/Controllers/Microblog/VoteController.php @@ -8,13 +8,13 @@ use Coyote\Microblog; use Coyote\Notifications\Microblog\VotedNotification; use Coyote\Reputation; +use Coyote\Services\Stream\Activities\Vote as Stream_Vote; +use Coyote\Services\Stream\Objects\Comment as Stream_Comment; +use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog; use Coyote\Services\UrlBuilder; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; use Illuminate\Http\Request; -use Coyote\Services\Stream\Activities\Vote as Stream_Vote; -use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog; -use Coyote\Services\Stream\Objects\Comment as Stream_Comment; /** * Ocena glosow na dany wpis na mikro (lub wyswietlanie loginow ktorzy oddali ow glos) @@ -73,7 +73,7 @@ public function post(Microblog $microblog, Request $request) $microblog->votes++; } - $microblog->score = $microblog->getScore(); + $microblog->resetScore(); $target = null; // reputacje przypisujemy tylko za ocene wpisu a nie komentarza!! @@ -108,7 +108,7 @@ public function post(Microblog $microblog, Request $request) public function voters(Microblog $microblog) { - $microblog->load(['voters', 'voters.user' => fn ($query) => $query->select('id', 'name')->withTrashed()]); + $microblog->load(['voters', 'voters.user' => fn($query) => $query->select('id', 'name')->withTrashed()]); return ['id' => $microblog->id, 'parent_id' => $microblog->parent_id, 'users' => $microblog->voters->pluck('user.name')]; } diff --git a/app/Models/Microblog.php b/app/Models/Microblog.php index 310af176f..6ec367c5c 100644 --- a/app/Models/Microblog.php +++ b/app/Models/Microblog.php @@ -1,7 +1,7 @@ 'bool']; + protected $attributes = [ + 'votes' => 0, + 'views' => 1, + ]; - /** - * Domyslne wartosci dla nowego modelu - * - * @var array - */ - protected $attributes = ['votes' => 0, 'views' => 1]; - - /** - * Html version of the entry. - * - * @var null|string - */ - private $html = null; + private ?string $html = null; public static function boot() { parent::boot(); - - static::creating(function (Microblog $model) { - // nadajemy domyslna wartosc sortowania przy dodawaniu elementu - $model->score = $model->getScore(); - }); - + static::creating(fn(Microblog $model) => $model->resetScore()); static::addGlobalScope(resolve(UserRelationsScope::class)); } - /** - * @return \Illuminate\Database\Eloquent\Relations\MorphMany - */ - public function assets() + public function assets(): MorphMany { return $this->morphMany(Asset::class, 'content'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\MorphToMany - */ - public function tags() + public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'resource', 'tag_resources'); } - /** - * Prosty "algorytm" do generowania rankingu danego wpisu na podstawie ocen i czasu dodania - * - * @return int - */ - public function getScore() + public function resetScore(): void { - $timestamp = $this->created_at ? $this->created_at->timestamp : time(); - - return (int) (($this->votes * 5) + (($timestamp - 1380153600) / 3600)); + $this->score = $this->getScore(); } - public function setHtmlAttribute($value) + private function getScore(): int { - $this->html = $value; + $timestamp = $this->created_at ? $this->created_at->timestamp : time(); + $hours = (int)($timestamp - 1380153600) / 3600; // since 26 september, 2023. + return $this->votes * 5 + $hours; } - /** - * @return null|string - */ - public function getHtmlAttribute() + public function comments(): HasMany { - if ($this->html !== null) { - return $this->html; - } + return $this + ->hasMany(self::class, 'parent_id', 'id') + ->orderBy('microblogs.id', 'ASC'); + } - return $this->html = app('parser.post')->parse($this->text); + public function subscribers(): MorphMany + { + return $this->morphMany(Subscription::class, 'resource'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function comments() + public function voters(): HasMany { - return $this->hasMany(self::class, 'parent_id', 'id')->orderBy('microblogs.id', 'ASC'); + return $this->hasMany(Vote::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\MorphMany - */ - public function subscribers() + public function page(): MorphOne { - return $this->morphMany(Subscription::class, 'resource'); + return $this->morphOne(Page::class, 'content'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function voters() + public function parent(): HasOne { - return $this->hasMany(Vote::class); + return $this + ->hasOne(Microblog::class, 'id', 'parent_id') + ->withoutGlobalScope(UserRelationsScope::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\MorphOne - */ - public function page() + public function user(): BelongsTo { - return $this->morphOne('Coyote\Page', 'content'); + return $this->belongsTo(User::class) + ->select(['id', 'name', 'deleted_at', 'is_blocked', 'photo', 'is_online', 'reputation']) + ->withTrashed(); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasOne - */ - public function parent() + public function setHtmlAttribute(string $value): void { - return $this->hasOne('Coyote\Microblog', 'id', 'parent_id')->withoutGlobalScope(UserRelationsScope::class); + $this->html = $value; } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function user() + public function getHtmlAttribute(): string { - return $this->belongsTo(User::class)->select(['id', 'name', 'deleted_at', 'is_blocked', 'photo', 'is_online', 'reputation'])->withTrashed(); + if ($this->html === null) { + $this->html = app('parser.post')->parse($this->text); + } + return $this->html; } public function scopeIncludeIsSubscribed(Builder $builder, int $userId): Builder { $this->addSelectIfNull($builder); - return $builder ->addSelect(new Expression('CASE WHEN mw.user_id IS NULL THEN false ELSE true END AS is_subscribed')) - ->leftJoin('subscriptions AS mw', function ($join) use ($userId) { - $join->on('mw.resource_id', '=', 'microblogs.id')->where('mw.resource_type', '=', static::class)->where('mw.user_id', '=', $userId); - }); + ->leftJoin('subscriptions AS mw', fn(JoinClause $join) => $join + ->on('mw.resource_id', '=', 'microblogs.id') + ->where('mw.resource_type', '=', static::class) + ->where('mw.user_id', '=', $userId)); } public function scopeIncludeIsVoted(Builder $builder, int $userId): Builder { $this->addSelectIfNull($builder); - return $builder ->addSelect(new Expression('CASE WHEN mv.id IS NULL THEN false ELSE true END AS is_voted')) - ->leftJoin('microblog_votes AS mv', function ($join) use ($userId) { - $join->on('mv.microblog_id', '=', 'microblogs.id')->where('mv.user_id', '=', $userId); - }); + ->leftJoin('microblog_votes AS mv', fn(JoinClause $join) => $join + ->on('mv.microblog_id', '=', 'microblogs.id') + ->where('mv.user_id', '=', $userId)); } - private function addSelectIfNull(Builder $builder) + private function addSelectIfNull(Builder $builder): void { - if (is_null($builder->getQuery()->columns)) { + if ($builder->getQuery()->columns === null) { $builder->select([$builder->getQuery()->from . '.*']); } }