Repositories | Polyback | Log in to see private repositories


polyback-website / db9a7ac7345e94204336a2a121c1338c5462f5f6 / api/chat/history.php

1,052 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// A conversation with a friend: the latest messages, or older ones (before=<message id>).
// Opening a conversation marks the friend's messages to you as read.
//   POST friendId, before (optional)
// Response: {"success":true,"messages":[{id,sender,message,time,timeText}...],"more":bool}

use pizzaboxer\ProjectPolygon\API;
use pizzaboxer\ProjectPolygon\Chat;

API::initialize(["method" => "POST", "logged_in" => true, "secure" => true]);

$friendId = API::GetParameter("POST", "friendId", "int");
$before = API::GetParameter("POST", "before", "int", 0);

if (!Chat::AreFriends(SESSION["user"]["id"], $friendId)) API::respond(200, false, "You can only chat with your friends");

$messages = Chat::GetMessages(SESSION["user"]["id"], $friendId, null, $before);
if (!$before) Chat::MarkRead(SESSION["user"]["id"], $friendId);

API::respondCustom([
	"status" => 200,
	"success" => true,
	"message" => "OK",
	"messages" => $messages,
	"more" => count($messages) >= Chat::HISTORY_PAGE
]);