Files
rekhoff 86c6d3b009 Updated C# quickstart-chat to pattern match variable assignment (#3173)
# Description of Changes

To address user issue
https://github.com/clockworklabs/SpacetimeDB/issues/2647 , the C#
implementation of quickstart-chat files and documentation have been
updated to pattern match the variable assignment of User.

This has the benefit of the variables never being perceived by the
analyzers as a nullable types.

# API and ABI breaking changes

Not API breaking

# Expected complexity level and risk

1

# Testing

- [X] Tested updated module code locally, following instructions from
the documentation.

Co-authored-by: John Detter <4099508+jdetter@users.noreply.github.com>
2025-08-26 21:19:01 +00:00

111 lines
3.0 KiB
C#

using SpacetimeDB;
public static partial class Module
{
[Table(Name = "user", Public = true)]
public partial class User
{
[PrimaryKey]
public Identity Identity;
public string? Name;
public bool Online;
}
[Table(Name = "message", Public = true)]
public partial class Message
{
public Identity Sender;
public Timestamp Sent;
public string Text = "";
}
[Reducer]
public static void SetName(ReducerContext ctx, string name)
{
name = ValidateName(name);
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
{
user.Name = name;
ctx.Db.user.Identity.Update(user);
}
}
/// Takes a name and checks if it's acceptable as a user's name.
private static string ValidateName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("Names must not be empty");
}
return name;
}
[Reducer]
public static void SendMessage(ReducerContext ctx, string text)
{
text = ValidateMessage(text);
Log.Info(text);
ctx.Db.message.Insert(
new Message
{
Sender = ctx.Sender,
Text = text,
Sent = ctx.Timestamp,
}
);
}
/// Takes a message's text and checks if it's acceptable to send.
private static string ValidateMessage(string text)
{
if (string.IsNullOrEmpty(text))
{
throw new ArgumentException("Messages must not be empty");
}
return text;
}
[Reducer(ReducerKind.ClientConnected)]
public static void ClientConnected(ReducerContext ctx)
{
Log.Info($"Connect {ctx.Sender}");
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
{
// If this is a returning user, i.e., we already have a `User` with this `Identity`,
// set `Online: true`, but leave `Name` and `Identity` unchanged.
user.Online = true;
ctx.Db.user.Identity.Update(user);
}
else
{
// If this is a new user, create a `User` object for the `Identity`,
// which is online, but hasn't set a name.
ctx.Db.user.Insert(
new User
{
Name = null,
Identity = ctx.Sender,
Online = true,
}
);
}
}
[Reducer(ReducerKind.ClientDisconnected)]
public static void ClientDisconnected(ReducerContext ctx)
{
if (ctx.Db.user.Identity.Find(ctx.Sender) is User user)
{
// This user should exist, so set `Online: false`.
user.Online = false;
ctx.Db.user.Identity.Update(user);
}
else
{
// User does not exist, log warning
Log.Warn("Warning: No user found for disconnected client.");
}
}
}