Files
ignacionelson 6e47d76ba6 ProjectSend 2.0.0
Client file sharing, rebuilt from the ground up: a private area per
client, resumable uploads, folders, groups and categories, sharing with
expiry dates and download limits, comments, file versions, an activity
log, a REST API, and sixteen languages.

This repository begins here. ProjectSend 2 was developed privately, and
that development history is not published — the previous generation
remains available, with its own history, at projectsend/legacy.

Free software under the GNU General Public License v2, or (at your
option) any later version.
2026-08-14 01:38:12 -03:00

71 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Comments;
use Illuminate\Http\Request;
/**
* Which comments this visitor wrote, remembered in their session.
*
* A visitor has no account, which is why their held comment used to
* disappear the moment they posted it: the thread reloads, the comment is
* not approved yet, and nothing could tell "the person who just wrote
* this" from any other stranger. Posting then looked like it had failed.
*
* The session is the identity. It is weak on purpose — it lasts as long
* as the browser session and no longer, and it is only ever used to widen
* what somebody sees of *their own* writing, never to grant anything.
* Losing it shows them one fewer pending comment, which is the harmless
* direction to fail in.
*
* Read by Access\VisibleCommentScope, written by the public controller —
* the only place a comment can be posted without an account.
*/
class GuestCommentIdentity
{
private const KEY = 'comments.own';
/**
* How many ids to keep. A visitor cannot post faster than the route's
* rate limit allows, so this is a bound on a long session rather than
* on a burst; the oldest are dropped, and dropping one only means that
* comment stops showing while it waits.
*/
private const LIMIT = 50;
public function __construct(
private readonly Request $request,
) {}
public function remember(int $commentId): void
{
if (! $this->request->hasSession()) {
return;
}
$ids = [...$this->ownCommentIds(), $commentId];
$this->request->session()->put(self::KEY, array_slice(array_unique($ids), -self::LIMIT));
}
/**
* @return list<int>
*/
public function ownCommentIds(): array
{
if (! $this->request->hasSession()) {
return [];
}
$stored = $this->request->session()->get(self::KEY, []);
if (! is_array($stored)) {
return [];
}
return array_values(array_map(intval(...), array_filter($stored, is_numeric(...))));
}
}