Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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
{
internal sealed class SlowModeCodeSamples
{
/// <summary>
/// https://getstream.io/chat/docs/unity/slow-mode/?language=unity#channel-slow-mode
/// </summary>
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<string, object>
{
{ "cooldown", 1 }
});

// Increase cooldown to 30s
await channel.UpdatePartialAsync(new Dictionary<string, object>
{
{ "cooldown", 30 }
});

// Disable slow mode by setting the cooldown back to 0
await channel.UpdatePartialAsync(new Dictionary<string, object>
{
{ "cooldown", 0 }
});

// Read the current cooldown. Null or 0 means slow mode is off
Debug.Log(channel.Cooldown);
}

/// <summary>
/// https://getstream.io/chat/docs/unity/slow-mode/?language=unity#channel-slow-mode
/// </summary>
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();
}
}
3 changes: 3 additions & 0 deletions Assets/Plugins/StreamChat/Samples/SlowModeCodeSamples.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions Assets/Plugins/StreamChat/Samples/TranslationCodeSamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using StreamChat.Core;
using StreamChat.Core.Requests;
using StreamChat.Core.StatefulModels;
using UnityEngine;

namespace StreamChat.Samples
{
internal sealed class TranslationCodeSamples
{
/// <summary>
/// https://getstream.io/chat/docs/unity/translation/?language=unity#i18n-data
/// </summary>
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;
}

/// <summary>
/// https://getstream.io/chat/docs/unity/translation/?language=unity#enabling-automatic-translation
/// </summary>
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<string, object>
{
{ "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.
}

/// <summary>
/// https://getstream.io/chat/docs/unity/translation/?language=unity#set-user-language
/// </summary>
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();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading