*/ protected $guarded = []; protected $dateFormat = false; /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Release, $this> */ public function release(): BelongsTo { return $this->belongsTo(Release::class, 'releases_id'); } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User, $this> */ public function user(): BelongsTo { return $this->belongsTo(User::class, 'users_id'); } /** * Get a comment by id. * * * @return Model|null|static */ public static function getCommentById(mixed $id) { return self::query()->where('id', $id)->first(); } /** * @return array */ public static function getComments(mixed $id): array { return self::query()->where('releases_id', $id)->orderByDesc('created_at')->get()->toArray(); } public static function getCommentCount(): int { return self::query()->count('id'); } /** * Delete single comment on the site. */ public static function deleteComment(mixed $id): void { $res = self::getCommentById($id); if ($res) { self::query()->where('id', $id)->delete(); self::updateReleaseCommentCount($res['gid']); } } /** * Add a release_comments row. * * * * @throws \Exception */ public static function addComment(mixed $id, mixed $gid, mixed $text, mixed $userid, mixed $host): int { if (config('nntmux:settings.store_user_ip') === false) { $host = ''; } $username = User::query()->where('id', $userid)->first(['username']); $username = ($username === null ? 'ANON' : $username['username']); $comid = self::query() ->insertGetId( [ 'releases_id' => $id, 'gid' => $gid, 'text' => $text, 'users_id' => $userid, 'created_at' => now(), 'updated_at' => now(), 'host' => $host, 'username' => $username, ] ); self::updateReleaseCommentCount($id); return $comid; } /** * Get release_comments rows by limit. */ public static function getCommentsRange(): LengthAwarePaginator // @phpstan-ignore missingType.generics { $range = self::query() ->select(['release_comments.*', 'releases.guid']) ->leftJoin('releases', 'releases.id', '=', 'release_comments.releases_id') ->orderByDesc('release_comments.created_at'); return $range->paginate(config('nntmux.items_per_page')); } /** * Update the denormalised count of comments for a release. */ public static function updateReleaseCommentCount(mixed $gid): void { $commentCount = self::query()->where('gid', '=', 'releases.gid')->where('isvisible', '=', 1)->count('id'); Release::query()->where('gid', $gid)->update(['comments' => $commentCount]); } /** * Get a count of all comments for a user. */ public static function getCommentCountForUser(mixed $uid): int { $res = self::query()->where(['users_id' => $uid, 'isvisible' => 1])->count('id'); return $res; } public static function getCommentsForUserRange(mixed $uid): LengthAwarePaginator // @phpstan-ignore missingType.generics { return self::query() ->select(['release_comments.*', 'r.guid', 'r.searchname', 'u.username']) ->join('releases as r', 'r.id', '=', 'release_comments.releases_id') ->leftJoin('users as u', 'u.id', '=', 'release_comments.users_id') ->where('users_id', $uid) ->orderByDesc('created_at') ->paginate(config('nntmux.items_per_page')); } }