Add embeddable slides and pause mode
Every slide gets an embed URL /e/{key}/{id} for presentation software
like Canva. The page goes live while it is visible, keeps itself live
with heartbeats and pauses when it disappears; stale embeds are paused
lazily. The embed key is separate from the admin token and only allows
showing, activating, pausing and voting controls of that slide.
Participants see a pause page while no slide is live. The presenter
can pause (P) and resume the paused slide with the arrow keys; the
dashboard has a pause button.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58145e9753
commit
07a292d109
19 files changed
+1116
-68
No files matched your search
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model\Entity;
|
||||
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
/**
|
||||
@@ -12,7 +13,10 @@ use Cake\ORM\Entity;
|
||||
* @property string $admin_token_hash
|
||||
* @property string $admin_token_encrypted
|
||||
* @property string $join_code
|
||||
* @property string|null $embed_key
|
||||
* @property int|null $current_slide_id
|
||||
* @property \Cake\I18n\DateTime|null $embed_seen_at
|
||||
* @property int|null $paused_slide_id
|
||||
* @property bool $is_active
|
||||
* @property bool $show_results_to_participants
|
||||
* @property \Cake\I18n\DateTime|null $last_admin_access
|
||||
@@ -44,6 +48,7 @@ class Poll extends Entity
|
||||
'admin_token_hash',
|
||||
'admin_token_encrypted',
|
||||
'email',
|
||||
'embed_key',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -55,4 +60,52 @@ class Poll extends Entity
|
||||
{
|
||||
return trim(chunk_split($this->join_code, 3, ' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a slide to the participants (from Plenario itself: no embed timeout).
|
||||
*
|
||||
* @param int $slideId Slide id (must belong to this poll, checked by the caller)
|
||||
* @return void
|
||||
*/
|
||||
public function showSlide(int $slideId): void
|
||||
{
|
||||
$this->current_slide_id = $slideId;
|
||||
$this->paused_slide_id = null;
|
||||
$this->embed_seen_at = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a slide because its embedded page (e.g. in Canva) became visible.
|
||||
* It stays live only as long as the page keeps sending heartbeats.
|
||||
*
|
||||
* @param int $slideId Slide id
|
||||
* @return void
|
||||
*/
|
||||
public function showEmbeddedSlide(int $slideId): void
|
||||
{
|
||||
$this->showSlide($slideId);
|
||||
$this->embed_seen_at = DateTime::now();
|
||||
}
|
||||
|
||||
/**
|
||||
* No slide live: participants see the pause page. Idempotent.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function pause(): void
|
||||
{
|
||||
if ($this->current_slide_id !== null) {
|
||||
$this->paused_slide_id = $this->current_slide_id;
|
||||
}
|
||||
$this->current_slide_id = null;
|
||||
$this->embed_seen_at = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Paused after a slide had been live (not: not started yet)
|
||||
*/
|
||||
public function isPaused(): bool
|
||||
{
|
||||
return $this->current_slide_id === null && $this->paused_slide_id !== null;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace App\Model\Table;
|
||||
|
||||
use App\Model\Entity\Poll;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\ORM\Query\SelectQuery;
|
||||
use Cake\ORM\RulesChecker;
|
||||
use Cake\ORM\Table;
|
||||
@@ -230,4 +231,82 @@ class PollsTable extends Table
|
||||
{
|
||||
return $query->where(['Polls.join_code' => static::normalizeJoinCode($code)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @param string $key Embed key
|
||||
* @return \Cake\ORM\Query\SelectQuery
|
||||
*/
|
||||
public function findByEmbedKey(SelectQuery $query, string $key): SelectQuery
|
||||
{
|
||||
// Never match polls without key (null) and never compare against an empty string.
|
||||
return $query->where(['Polls.embed_key' => $key === '' ? '-' : $key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the embed key on first use; keeps an existing one (idempotent).
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return string
|
||||
*/
|
||||
public function ensureEmbedKey(Poll $poll): string
|
||||
{
|
||||
if ($poll->embed_key === null || $poll->embed_key === '') {
|
||||
return $this->rotateEmbedKey($poll);
|
||||
}
|
||||
|
||||
return $poll->embed_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* New embed key, all previous embed URLs stop working.
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return string
|
||||
*/
|
||||
public function rotateEmbedKey(Poll $poll): string
|
||||
{
|
||||
// Only the key column, so the rest of the entity (e.g. live slide) is not overwritten.
|
||||
$key = static::generateToken();
|
||||
$this->updateAll(['embed_key' => $key], ['id' => $poll->id]);
|
||||
$poll->embed_key = $key;
|
||||
$poll->setDirty('embed_key', false);
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses the poll when the embedded slide that made itself live has stopped reporting
|
||||
* (presentation moved on to a non-Plenario slide, or was closed without a pause beacon).
|
||||
* Called lazily whenever the state is read, like SlidesTable::closeIfExpired().
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return \App\Model\Entity\Poll
|
||||
*/
|
||||
public function pauseIfEmbedStale(Poll $poll): Poll
|
||||
{
|
||||
$timeout = max(1, (int)Configure::read('Plenario.embed.timeoutSeconds', 20));
|
||||
if (
|
||||
$poll->embed_seen_at === null
|
||||
|| $poll->current_slide_id === null
|
||||
|| $poll->embed_seen_at->wasWithinLast($timeout . ' seconds')
|
||||
) {
|
||||
return $poll;
|
||||
}
|
||||
|
||||
// Conditional update: a concurrent heartbeat or slide change wins.
|
||||
$this->updateAll(
|
||||
['current_slide_id' => null, 'paused_slide_id' => $poll->current_slide_id, 'embed_seen_at' => null],
|
||||
['id' => $poll->id, 'current_slide_id' => $poll->current_slide_id, 'embed_seen_at' => $poll->embed_seen_at],
|
||||
);
|
||||
|
||||
/** @var \App\Model\Entity\Poll $fresh */
|
||||
$fresh = $this->get($poll->id);
|
||||
foreach (['current_slide_id', 'paused_slide_id', 'embed_seen_at'] as $field) {
|
||||
$poll->set($field, $fresh->get($field));
|
||||
$poll->setDirty($field, false);
|
||||
}
|
||||
|
||||
return $poll;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user