Introduce new `ChatService` for managing chat messages, including marking messages as read and checking read status. Add `WebSocketPacket` class for handling WebSocket communication and integrate it with `WebSocketService` to process chat-related packets. Enhance `ChatRoom` and `ChatMember` models with additional fields and relationships. Update `AppDatabase` to include new chat-related entities and adjust permissions for chat creation.
38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using DysonNetwork.Sphere.Chat;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DysonNetwork.Sphere.Realm;
|
|
|
|
[ApiController]
|
|
[Route("/realm/{slug}")]
|
|
public class RealmChatController(AppDatabase db) : ControllerBase
|
|
{
|
|
[HttpGet("chat")]
|
|
[Authorize]
|
|
public async Task<ActionResult<List<ChatRoom>>> ListRealmChat(string slug)
|
|
{
|
|
var currentUser = HttpContext.Items["CurrentUser"] as Account.Account;
|
|
|
|
var realm = await db.Realms
|
|
.Where(r => r.Slug == slug)
|
|
.FirstOrDefaultAsync();
|
|
if (realm is null) return NotFound();
|
|
if (!realm.IsPublic)
|
|
{
|
|
if (currentUser is null) return Unauthorized();
|
|
var member = await db.ChatMembers
|
|
.Where(m => m.ChatRoomId == realm.Id)
|
|
.Where(m => m.AccountId == currentUser.Id)
|
|
.FirstOrDefaultAsync();
|
|
if (member is null) return BadRequest("You need at least one member to view the realm's chat.");
|
|
}
|
|
|
|
var chatRooms = await db.ChatRooms
|
|
.Where(c => c.RealmId == realm.Id)
|
|
.ToListAsync();
|
|
|
|
return Ok(chatRooms);
|
|
}
|
|
} |