<?php
require_once __DIR__ . '/config.php';
$me = must_auth();
$b  = json_decode(file_get_contents('php://input'),true) ?: array();
$to = isset($b['to']) ? trim($b['to']) : '';
$ty = !empty($b['typing']);

if ($ty && $to !== '') {
    db()->prepare("UPDATE presence SET is_typing=1, typing_to=?, typing_at=UTC_TIMESTAMP(), last_seen=UTC_TIMESTAMP() WHERE username=?")
       ->execute(array($to, $me));
} else {
    db()->prepare("UPDATE presence SET is_typing=0, typing_to='', typing_at=NULL, last_seen=UTC_TIMESTAMP() WHERE username=?")
       ->execute(array($me));
}
ok();
