<?php
require_once __DIR__ . '/config.php';
$me = must_auth();
$b  = json_decode(file_get_contents('php://input'),true) ?: array();
$id = isset($b['messageId']) ? (int)$b['messageId'] : 0;
$em = isset($b['emoji'])     ? trim($b['emoji'])     : '';
if (!$id) fail('Missing messageId');
if ($em) db()->prepare("INSERT INTO reactions (message_id,username,emoji) VALUES (?,?,?) ON DUPLICATE KEY UPDATE emoji=?,created_at=NOW()")->execute(array($id,$me,$em,$em));
else     db()->prepare("DELETE FROM reactions WHERE message_id=? AND username=?")->execute(array($id,$me));
ok();
