Interface IChatService
- Namespace
- FishMMO.Database.Npgsql.Services.Interfaces
- Assembly
- FishMMO-DB.dll
Service interface for chat message operations. Provides async methods for persisting and fetching chat messages.
public interface IChatService
Remarks
Write operations (Persist*) in this service use execution strategies to ensure transient database failures are automatically retried according to the retry policy configured on the DbContext. This is critical because retry handling is done by BaseService, not by provider-level retry. BaseService uses explicit transactions only when a write requires multiple database statements. This interface describes behavior; the implementation uses BaseService execution wrappers.
All methods return DatabaseResult or DatabaseResult<T> to provide structured error information through the DatabaseException system, helping distinguish between: - Validation failures (invalid parameters) - Database errors (connection issues, constraint violations, timeouts) - Entity not found errors - Unexpected runtime errors
Methods
FetchAsync(DateTime, long, int, long, CancellationToken)
Fetches paginated chat messages excluding local messages for the specified scene server.
Task<DatabaseResult<List<ChatData>>> FetchAsync(DateTime lastFetch, long lastPosition, int amount, long sceneServerId, CancellationToken cancellationToken = default)
Parameters
lastFetchDateTimeTimestamp to compare messages against.
lastPositionlongLast message ID fetched (for pagination).
amountintMaximum number of messages to fetch.
sceneServerIdlongScene server ID to filter out local messages.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<List<ChatData>>>
A DatabaseResult<T> containing the list of chat message data on success, or a DatabaseException on failure.
Remarks
This method uses LINQ with AsNoTracking for optimal read performance and automatically benefits from the retry policy configured on the DbContext without requiring explicit execution strategy wrapping. Filters out local channel messages (Tell, Guild, Party, World, Trade) from the specified scene server. Returns empty list for invalid amount.
PersistAsync(long, string, string, long, long, ChatChannel, string, DateTime, CancellationToken)
Persists a chat message with denormalized audit fields.
Task<DatabaseResult> PersistAsync(long characterId, string characterName, string accountName, long worldServerId, long sceneServerId, ChatChannel channel, string message, DateTime serverReceivedTime, CancellationToken cancellationToken = default)
Parameters
characterIdlongCharacter ID sending the message.
characterNamestringCharacter name (denormalized for audit retention).
accountNamestringAccount name (denormalized for audit retention).
worldServerIdlongWorld server ID.
sceneServerIdlongScene server ID.
channelChatChannelChat channel.
messagestringMessage content.
serverReceivedTimeDateTimeTimestamp when server received the message (for legal audit trail).
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or containing a DatabaseException on failure.
Remarks
Chat audit fields are denormalized so logs can survive character deletion. Passing the names avoids a race where the character row is deleted between lookup and insert. Uses BaseService.ExecuteWriteAsync for:
- Automatic transient failure retry
- Centralized exception handling and mapping
- Consistent DatabaseResult pattern
PersistBatchAsync(List<(long characterId, string characterName, string accountName, long worldServerId, long sceneServerId, ChatChannel channel, string message, DateTime serverReceivedTime)>, int, CancellationToken)
Persists multiple chat messages in batches.
Task<DatabaseResult> PersistBatchAsync(List<(long characterId, string characterName, string accountName, long worldServerId, long sceneServerId, ChatChannel channel, string message, DateTime serverReceivedTime)> messages, int maxBatchSize = 1000, CancellationToken cancellationToken = default)
Parameters
messagesList<(long characterId, string characterName, string accountName, long worldServerId, long sceneServerId, ChatChannel channel, string message, DateTime serverReceivedTime)>List of chat messages to persist. Each tuple contains: (characterId, characterName, accountName, worldServerId, sceneServerId, channel, message, serverReceivedTime).
maxBatchSizeintMaximum number of messages per database round-trip (500–2500).
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.