From 834ff3ce35ba2dcac12b9880c7e97e382f94a4eb Mon Sep 17 00:00:00 2001 From: Martin Mitrevski Date: Tue, 11 Aug 2026 14:28:25 +0200 Subject: [PATCH 1/2] Add Translation and Slow Mode docs code samples These back two docs pages that previously told Unity readers the features were not available in the SDK, when the client-side APIs already ship: - TranslationCodeSamples - reading `message.I18n`, enabling channel-level auto-translation via `channel.UpdatePartialAsync`, and setting a user's language via `Client.UpsertUsersAsync`. Only the translate endpoint and app-level auto-translation remain server-side. - SlowModeCodeSamples - setting and clearing the channel `cooldown` field, and computing the remaining cooldown from `channel.Cooldown` plus the local user's last message so the send UI can be gated. Keeping them here means the snippets are compiled by StreamChat.Samples and stay in sync with the SDK, matching the existing *CodeSamples.cs files. Docs side: GetStream/docs-content#1497 Co-Authored-By: Claude Opus 5 (1M context) --- .../StreamChat/Samples/SlowModeCodeSamples.cs | 94 +++++++++++++++++++ .../Samples/SlowModeCodeSamples.cs.meta | 3 + .../Samples/TranslationCodeSamples.cs | 69 ++++++++++++++ .../Samples/TranslationCodeSamples.cs.meta | 3 + 4 files changed, 169 insertions(+) create mode 100644 Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs create mode 100644 Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs.meta create mode 100644 Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs create mode 100644 Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs.meta diff --git a/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs b/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs new file mode 100644 index 00000000..d41ee9b4 --- /dev/null +++ b/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using StreamChat.Core; +using StreamChat.Core.StatefulModels; +using UnityEngine; + +namespace StreamChat.Samples +{ + /// + /// Code examples for the features/advanced/slow-mode/ docs page + /// + internal sealed class SlowModeCodeSamples + { + public async Task EnableDisableSlowMode() + { + var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "channel-id"); + +// The Unity SDK has no dedicated slow mode method - set the channel's +// `cooldown` field, in seconds, with a partial channel update + +// Enable slow mode with a 1s cooldown + await channel.UpdatePartialAsync(new Dictionary + { + { "cooldown", 1 } + }); + +// Increase cooldown to 30s + await channel.UpdatePartialAsync(new Dictionary + { + { "cooldown", 30 } + }); + +// Disable slow mode by setting the cooldown back to 0 + await channel.UpdatePartialAsync(new Dictionary + { + { "cooldown", 0 } + }); + +// Read the current cooldown. Null or 0 means slow mode is off + Debug.Log(channel.Cooldown); + } + + public async Task GateSendingUiOnRemainingCooldown() + { + var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "channel-id"); + +// Disable/enable the send button based on the remaining cooldown + var remaining = GetRemainingCooldown(channel); + if (remaining > 0) + { + // disable/enable UI is app-specific + DisableMessageSendingUi(remaining); + } + } + +// The Unity SDK exposes the configured cooldown via `channel.Cooldown`, but does +// not track the remaining time for you. Compute it from the local user's last +// message in the channel + + private int GetRemainingCooldown(IStreamChannel channel) + { + var cooldown = channel.Cooldown ?? 0; + if (cooldown <= 0) + { + return 0; + } + + var localUserId = Client.LocalUserData.UserId; + + var lastOwnMessage = channel.Messages + .Where(m => m.User != null && m.User.Id == localUserId) + .OrderByDescending(m => m.CreatedAt) + .FirstOrDefault(); + + if (lastOwnMessage == null) + { + return 0; + } + + var elapsed = (DateTimeOffset.UtcNow - lastOwnMessage.CreatedAt).TotalSeconds; + var remaining = cooldown - (int)elapsed; + + return remaining > 0 ? remaining : 0; + } + + private void DisableMessageSendingUi(int forSeconds) + { + } + + private IStreamChatClient Client { get; } = StreamChatClient.CreateDefaultClient(); + } +} diff --git a/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs.meta b/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs.meta new file mode 100644 index 00000000..0e6224b7 --- /dev/null +++ b/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e7409c84722c44348688345448c3aea4 +timeCreated: 1754913600 diff --git a/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs b/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs new file mode 100644 index 00000000..b8062fbb --- /dev/null +++ b/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using StreamChat.Core; +using StreamChat.Core.Requests; +using StreamChat.Core.StatefulModels; +using UnityEngine; + +namespace StreamChat.Samples +{ + /// + /// Code examples for the features/translation/ docs page + /// + internal sealed class TranslationCodeSamples + { + public void ReadMessageTranslations() + { + IStreamMessage message = null; + +// Translations are exposed on the message as the I18n dictionary + foreach (var pair in message.I18n) + { + Debug.Log($"{pair.Key} = {pair.Value}"); // e.g. "fr_text = Bonjour, ..." + } + +// The original language is under the "language" key + if (message.I18n.TryGetValue("language", out var originalLanguage)) + { + Debug.Log(originalLanguage); // "en" + } + +// Read a specific translation, falling back to the original text + var text = message.I18n.TryGetValue("fr_text", out var french) ? french : message.Text; + } + + public async Task EnableChannelAutoTranslation() + { + var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "channel-id"); + +// Enable auto-translation for a single channel + await channel.UpdatePartialAsync(new Dictionary + { + { "auto_translation_enabled", true }, + { "auto_translation_language", "en" } + }); + +// Read the current settings back from the channel + Debug.Log(channel.AutoTranslationEnabled); + Debug.Log(channel.AutoTranslationLanguage); + +// Enabling auto-translation for the whole app is a server-side operation. +// Use one of our server-side SDKs or the Stream Dashboard for that. + } + + public async Task SetUserLanguage() + { +// Set the language used to translate messages for a user + await Client.UpsertUsersAsync(new[] + { + new StreamUserUpsertRequest + { + Id = "user-id", + Language = "fr" + } + }); + } + + private IStreamChatClient Client { get; } = StreamChatClient.CreateDefaultClient(); + } +} diff --git a/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs.meta b/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs.meta new file mode 100644 index 00000000..15a97837 --- /dev/null +++ b/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: aba271a3dfd6486e9e5638f64c45af94 +timeCreated: 1754913600 From 536d9ddeb74fe3278cfb58f9c120e5fc38c9208a Mon Sep 17 00:00:00 2001 From: Martin Mitrevski Date: Tue, 11 Aug 2026 14:50:14 +0200 Subject: [PATCH 2/2] Use full docs URLs in sample summaries Matches the convention in ClientAndUsersCodeSamples.cs: a per-method holding the full ?language=unity URL with a section anchor, so you can jump straight from the sample to the page it backs. Co-Authored-By: Claude Opus 5 (1M context) --- .../StreamChat/Samples/SlowModeCodeSamples.cs | 9 ++++++--- .../StreamChat/Samples/TranslationCodeSamples.cs | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs b/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs index d41ee9b4..702f2640 100644 --- a/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs +++ b/Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs @@ -8,11 +8,11 @@ namespace StreamChat.Samples { - /// - /// Code examples for the features/advanced/slow-mode/ docs page - /// internal sealed class SlowModeCodeSamples { + /// + /// https://getstream.io/chat/docs/unity/slow-mode/?language=unity#channel-slow-mode + /// public async Task EnableDisableSlowMode() { var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "channel-id"); @@ -42,6 +42,9 @@ await channel.UpdatePartialAsync(new Dictionary Debug.Log(channel.Cooldown); } + /// + /// https://getstream.io/chat/docs/unity/slow-mode/?language=unity#channel-slow-mode + /// public async Task GateSendingUiOnRemainingCooldown() { var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "channel-id"); diff --git a/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs b/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs index b8062fbb..a450c98e 100644 --- a/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs +++ b/Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs @@ -7,11 +7,11 @@ namespace StreamChat.Samples { - /// - /// Code examples for the features/translation/ docs page - /// internal sealed class TranslationCodeSamples { + /// + /// https://getstream.io/chat/docs/unity/translation/?language=unity#i18n-data + /// public void ReadMessageTranslations() { IStreamMessage message = null; @@ -32,6 +32,9 @@ public void ReadMessageTranslations() var text = message.I18n.TryGetValue("fr_text", out var french) ? french : message.Text; } + /// + /// https://getstream.io/chat/docs/unity/translation/?language=unity#enabling-automatic-translation + /// public async Task EnableChannelAutoTranslation() { var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "channel-id"); @@ -51,6 +54,9 @@ await channel.UpdatePartialAsync(new Dictionary // Use one of our server-side SDKs or the Stream Dashboard for that. } + /// + /// https://getstream.io/chat/docs/unity/translation/?language=unity#set-user-language + /// public async Task SetUserLanguage() { // Set the language used to translate messages for a user