using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
namespace IDCGames.API
{
///
/// Friends direct-message chat: REST against /api/v1/chat.
/// Real-time: use GET realtime-config + Pusher-compatible client (see docs/CHAT_API.md).
///
public class IDCChatManager : MonoBehaviour
{
[Header("API")]
public string apiBaseUrl = "http://localhost:8000";
public event Action OnChatError;
private string _accessToken;
public void SetAccessToken(string token) => _accessToken = token;
public async Task GetRealtimeConfigAsync()
{
return await GetAsync("/api/v1/chat/realtime-config");
}
public async Task OpenConversationAsync(int friendUserId)
{
var body = JsonConvert.SerializeObject(new { friend_user_id = friendUserId });
return await PostAsync("/api/v1/chat/conversations", body);
}
public async Task> ListConversationsAsync(int perPage = 20)
{
return await GetAsync>(
$"/api/v1/chat/conversations?per_page={perPage}");
}
public async Task> GetMessagesAsync(int conversationId, int perPage = 50, int? beforeId = null)
{
var url = $"/api/v1/chat/conversations/{conversationId}/messages?per_page={perPage}";
if (beforeId.HasValue)
url += $"&before_id={beforeId.Value}";
return await GetAsync>(url);
}
public async Task SendMessageAsync(int conversationId, string body, string clientMessageId = null)
{
var payload = new Dictionary { { "body", body } };
if (!string.IsNullOrEmpty(clientMessageId))
payload["client_message_id"] = clientMessageId;
var json = JsonConvert.SerializeObject(payload);
return await PostAsync($"/api/v1/chat/conversations/{conversationId}/messages", json, expectCreated: true);
}
public async Task MarkReadAsync(int conversationId, int messageId)
{
var body = JsonConvert.SerializeObject(new { message_id = messageId });
await PostAsync