chat.js 969 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. async function getUserList(q, token) {
  2. if (!token) {
  3. return "not_init";
  4. }
  5. const response = await fetch(`/api/chat/users` + (q ? `?q=${encodeURIComponent(q)}` : ""), {
  6. method: "GET",
  7. headers: {
  8. "Authorization": `Bearer ${token}`
  9. }
  10. });
  11. if (!response.ok) {
  12. const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
  13. throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  14. }
  15. const data = await response.json();
  16. return data.users || [];
  17. }
  18. async function getOnlineUserCount(token) {
  19. if (!token) {
  20. return "not_init";
  21. }
  22. const response = await fetch(`/api/chat/onlines`, {
  23. method: "GET",
  24. headers: {
  25. "Authorization": `Bearer ${token}`
  26. }
  27. });
  28. const data = await response.json();
  29. if (!data.success){
  30. throw new Error(data.error.message);
  31. }
  32. return data.data.count || 0;
  33. }
  34. export { getUserList, getOnlineUserCount };