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
FetchPumpAsync(ChatPumpQuery, CancellationToken)
Reads the chat rows one scene server should relay: new, relevant to someone it hosts, and not its own echo.
Task<DatabaseResult<ChatPumpPage>> FetchPumpAsync(ChatPumpQuery query, CancellationToken cancellationToken = default)
Parameters
queryChatPumpQueryWhere to start, what to skip, and what this server hosts.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ChatPumpPage>>
The rows in
(time_created, id)order, the database clock at the start of the read, and whether the read caught up.
Remarks
This replaces a strict (time_created, id) cursor that lost rows for good whenever
two writers committed out of stamp order, and read one 20-row page of the whole shard's
chat per call however far behind it was (hot-path audit H5, H6).
The caller reads from a start held PumpCommitWindowSeconds behind what it has settled, and passes the IDs it has already handled in that window as ExcludeIds. Pages are read while they come back full, up to MaxPages, so a backlog is worked through instead of growing.
FetchRelayAsync(ChatRelayQuery, CancellationToken)
Reads the chat rows the Discord relay has not handled yet: every game row, never a row bridged in from Discord.
Task<DatabaseResult<ChatPumpPage>> FetchRelayAsync(ChatRelayQuery query, CancellationToken cancellationToken = default)
Parameters
queryChatRelayQueryWhere to start and what to skip.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ChatPumpPage>>
The rows in
(time_created, id)order, the database clock at the start of the read, and whether the read caught up.
Remarks
The same window as FetchPumpAsync(ChatPumpQuery, CancellationToken), read by a ChatReadWindow.
The relay used to page by id > last id seen, and IDs are taken at the INSERT,
not at the commit: a row that committed after a higher ID had been read was skipped for
good. It also read everything past that ID in one unbounded query (hot-path audit H5).
A query with no FromUtc starts at the database's "now", so a relay that starts does not republish history.
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 ONE transaction: every message lands, or none does.
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).
maxBatchSizeintMessages per INSERT statement inside the transaction (clamped to 500–2500). It does not change atomicity.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.
Remarks
All-or-nothing is what makes a failed call safe to retry. The rows are the chat audit log and other scene servers deliver whatever new rows they find, so a call that had committed part of a list before failing would write — and deliver — that part twice when the caller retried it.
PersistBridgedAsync(long, long, string, string, DateTime, CancellationToken)
Persists one line bridged in from Discord, as a Discord row the
scene servers relay to every player in worldServerId.
Task<DatabaseResult> PersistBridgedAsync(long worldServerId, long sceneServerId, string authorName, string message, DateTime serverReceivedTime, CancellationToken cancellationToken = default)
Parameters
worldServerIdlongWorld the Discord channel is bridged to.
sceneServerIdlongScene server the Discord channel is bridged to.
authorNamestringThe Discord author's (already sanitised) display name, for the audit columns.
messagestringThe line as players will see it.
serverReceivedTimeDateTimeWhen the bridge received it, UTC.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.
Remarks
Through the same INSERT as every other chat row so it is stamped by the same clock: the
bridge used to stamp time_created from its own host's clock, and the scene servers
page the table by that column.
SearchAdminAsync(ChatAdminQuery, CancellationToken)
Searches persisted chat for an operator, newest message first.
Task<DatabaseResult<ChatAdminPage>> SearchAdminAsync(ChatAdminQuery query, CancellationToken cancellationToken = default)
Parameters
queryChatAdminQueryThe filter to apply. Null is treated as an unfiltered query.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ChatAdminPage>>
A DatabaseResult<T> containing one page of ChatAdminData and the total row count on success, or a DatabaseException on failure.
Remarks
This is the read behind a harassment or abuse report, so it is separate from FetchPumpAsync(ChatPumpQuery, CancellationToken) in every respect: that one is a window the scene servers pull forward and it deliberately hides a server's own echo, while this one hides nothing and orders backwards from now.
A message-text filter supplied with no character name, no account name and no lower time bound is REFUSED with ValidationError rather than served. The reason is in the implementation; callers should surface the message rather than retrying, because a retry of the same query is refused identically.
Read-only: it uses AsNoTracking and writes nothing, including no audit row. Recording reads in the audit log would mean every listing of the log wrote a row of its own.