LAN_Portal/chat/features/replies.php
2026-10-16 16:22:59 -04:00

33 lines
1.5 KiB
PHP
Executable file

<?php
if (!defined('PORTAL')) { http_response_code(403); exit; }
// Replies: a message can quote an earlier one. Pairs with replies.js, which adds the "Reply" buttons.
// The quote (name + first 80 characters) is saved with the message, so it survives if the original is deleted.
$replySnippet = function ($text) {
return preg_match('/^.{1,80}/us', (string)$text, $x) ? $x[0] : '';
};
// The send form carries the id of the message being replied to in chat_reply (empty if none).
chat_on('before_send', function ($msg) use ($replySnippet) {
$id = (int)($_POST['chat_reply'] ?? 0);
if ($id > 0) {
foreach (chat_read() ?: [] as $m) {
if (($m['id'] ?? 0) === $id) {
$msg['reply'] = ['id' => $id, 'name' => (string)($m['name'] ?? ''), 'text' => $replySnippet($m['text'] ?? '')];
}
}
}
return $msg;
});
chat_on('msg_top', function ($html, $msg) {
$r = $msg['reply'] ?? null;
if (!is_array($r)) return $html;
return $html . '<div class="border-start border-3 ps-2 small text-body-secondary text-truncate">↩ <strong>'
. e($r['name'] ?? '') . '</strong> ' . e($r['text'] ?? '') . '</div>';
});
chat_on('msg_bottom', function ($html, $msg) use ($replySnippet) {
return $html . '<button type="button" class="btn btn-sm btn-link p-0 chat-reply" data-id="' . (int)($msg['id'] ?? 0)
. '" data-name="' . e($msg['name'] ?? '') . '" data-text="' . e($replySnippet($msg['text'] ?? '')) . '">↩ Reply</button>';
});