Skip to content

Commit

Permalink
Add TTS rate limit (space-syndicate#2515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morb0 authored Aug 23, 2024
1 parent 6a722ba commit a2b5413
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Content.Server/Corvax/TTS/TTSSystem.RateLimit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Content.Server.Chat.Managers;
using Content.Server.Players.RateLimiting;
using Content.Shared.Corvax.CCCVars;
using Robust.Shared.Player;

namespace Content.Server.Corvax.TTS;

public sealed partial class TTSSystem
{
[Dependency] private readonly PlayerRateLimitManager _rateLimitManager = default!;
[Dependency] private readonly IChatManager _chat = default!;

private const string RateLimitKey = "TTS";

private void RegisterRateLimits()
{
_rateLimitManager.Register(RateLimitKey,
new RateLimitRegistration
{
CVarLimitPeriodLength = CCCVars.TTSRateLimitPeriod,
CVarLimitCount = CCCVars.TTSRateLimitCount,
PlayerLimitedAction = RateLimitPlayerLimited,
});
}

private void RateLimitPlayerLimited(ICommonSession player)
{
_chat.DispatchServerMessage(player, Loc.GetString("tts-rate-limited"), suppressLog: true);
}

private RateLimitStatus HandleRateLimit(ICommonSession player)
{
return _rateLimitManager.CountAction(player, RateLimitKey);
}
}
6 changes: 6 additions & 0 deletions Content.Server/Corvax/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Content.Server.Chat.Systems;
using Content.Server.Players.RateLimiting;
using Content.Shared.Corvax.CCCVars;
using Content.Shared.Corvax.TTS;
using Content.Shared.GameTicking;
Expand Down Expand Up @@ -48,6 +49,8 @@ public override void Initialize()
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestartCleanup);

SubscribeNetworkEvent<RequestPreviewTTSEvent>(OnRequestPreviewTTS);

RegisterRateLimits();
}

private void OnRoundRestartCleanup(RoundRestartCleanupEvent ev)
Expand All @@ -61,6 +64,9 @@ private async void OnRequestPreviewTTS(RequestPreviewTTSEvent ev, EntitySessionE
!_prototypeManager.TryIndex<TTSVoicePrototype>(ev.VoiceId, out var protoVoice))
return;

if (HandleRateLimit(args.SenderSession) != RateLimitStatus.Allowed)
return;

var previewText = _rng.Pick(_sampleText);
var soundData = await GenerateTTS(previewText, protoVoice.Speaker);
if (soundData is null)
Expand Down
13 changes: 13 additions & 0 deletions Content.Shared/Corvax/CCCVars/CCCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public sealed class CCCVars
public static readonly CVarDef<int> TTSMaxCache =
CVarDef.Create("tts.max_cache", 250, CVar.SERVERONLY | CVar.ARCHIVE);

/// <summary>
/// Tts rate limit values are accounted in periods of this size (seconds).
/// After the period has passed, the count resets.
/// </summary>
public static readonly CVarDef<int> TTSRateLimitPeriod =
CVarDef.Create("tts.rate_limit_period", 2, CVar.SERVERONLY);

/// <summary>
/// How many tts preview messages are allowed in a single rate limit period.
/// </summary>
public static readonly CVarDef<int> TTSRateLimitCount =
CVarDef.Create("tts.rate_limit_count", 3, CVar.SERVERONLY);

/*
* Peaceful Round End
*/
Expand Down
1 change: 1 addition & 0 deletions Resources/Locale/ru-RU/corvax/tts/tts-ui.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ui-options-tts-volume = Громкость TTS:
credits-window-tts-title = Функция TTS (Text-To-Speech)
humanoid-profile-editor-voice-label = Голос:
humanoid-profile-editor-voice-play =
tts-rate-limited = Вы генерируете TTS слишком быстро!

0 comments on commit a2b5413

Please sign in to comment.