ChatRoomKeys.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. namespace Application.Abstractions.Chat;
  2. /// <summary>
  3. /// 채팅 룸 키 규약 (개미투자 D2) — roomKey = main | stock:{code}.
  4. /// channel:{sid} 는 기존 채널 채팅(비활성) 호환용 레거시 키 — JoinRoom 공개 계약에는 포함하지 않는다.
  5. /// </summary>
  6. public static class ChatRoomKeys
  7. {
  8. public const string Main = "main";
  9. public const string StockPrefix = "stock:";
  10. public const string ChannelPrefix = "channel:";
  11. public const int MaxLength = 20;
  12. public static bool IsStockRoom(string roomKey) => roomKey.StartsWith(StockPrefix, StringComparison.Ordinal);
  13. public static bool IsChannelRoom(string roomKey) => roomKey.StartsWith(ChannelPrefix, StringComparison.Ordinal);
  14. /// <summary>공개 계약 유효성 — main 또는 stock:{6자리 숫자} 만 허용</summary>
  15. public static bool IsValid(string? roomKey)
  16. {
  17. if (string.IsNullOrWhiteSpace(roomKey) || roomKey.Length > MaxLength)
  18. {
  19. return false;
  20. }
  21. if (roomKey == Main)
  22. {
  23. return true;
  24. }
  25. if (IsStockRoom(roomKey))
  26. {
  27. var code = roomKey[StockPrefix.Length..];
  28. return code is { Length: 6 } && code.All(char.IsAsciiDigit);
  29. }
  30. return false;
  31. }
  32. public static string ForStock(string code) => $"{StockPrefix}{code}";
  33. public static string ForChannel(string channelSID) => $"{ChannelPrefix}{channelSID}";
  34. }