42 lines
2 KiB
PHP
Executable file
42 lines
2 KiB
PHP
Executable file
<?php
|
|
if (!defined('PORTAL')) { http_response_code(403); exit; }
|
|
|
|
// Reactions: emoji buttons under each message; click again to remove yours.
|
|
// Stored on the message as "reactions": {"👍": ["<device id>", ...]}. One per emoji per device.
|
|
$reactEmoji = ['👍', '❤️', '😂'];
|
|
|
|
// Handle a click (a plain POST, so it works with or without JavaScript).
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['chat_action'] ?? '') === 'react') {
|
|
$reactId = (int)($_POST['chat_id'] ?? 0);
|
|
$reactPick = (string)($_POST['chat_emoji'] ?? '');
|
|
if ($reactId > 0 && in_array($reactPick, $reactEmoji, true)) {
|
|
chat_update(function (&$msgs) use ($reactId, $reactPick) {
|
|
foreach ($msgs as &$m) {
|
|
if (($m['id'] ?? 0) !== $reactId) continue;
|
|
$list = $m['reactions'][$reactPick] ?? [];
|
|
if (!is_array($list)) $list = [];
|
|
$k = array_search(chat_who(), $list, true);
|
|
if ($k === false) $list[] = chat_who(); else array_splice($list, $k, 1);
|
|
if ($list) $m['reactions'][$reactPick] = $list; else unset($m['reactions'][$reactPick]);
|
|
if (empty($m['reactions'])) unset($m['reactions']);
|
|
}
|
|
unset($m);
|
|
return null;
|
|
});
|
|
}
|
|
}
|
|
|
|
chat_on('msg_bottom', function ($html, $msg) use ($reactEmoji) {
|
|
foreach ($reactEmoji as $emoji) {
|
|
$list = $msg['reactions'][$emoji] ?? [];
|
|
if (!is_array($list)) $list = [];
|
|
$mine = in_array(chat_who(), $list, true);
|
|
$html .= '<form method="post" class="d-inline">'
|
|
. '<input type="hidden" name="chat_action" value="react">'
|
|
. '<input type="hidden" name="chat_id" value="' . (int)($msg['id'] ?? 0) . '">'
|
|
. '<input type="hidden" name="chat_emoji" value="' . e($emoji) . '">'
|
|
. '<button class="btn btn-sm py-0 px-1 ' . ($mine ? 'btn-accent' : 'btn-outline-secondary') . '">'
|
|
. e($emoji) . ($list ? ' ' . count($list) : '') . '</button></form>';
|
|
}
|
|
return $html;
|
|
});
|