massive/server/module/MessageTable.cs
2025-05-23 12:27:23 -04:00

37 lines
890 B
C#

using SpacetimeDB;
public static partial class Module
{
[Table(Name = "Message", Public = true)]
public partial class Message
{
public Identity Sender;
public Timestamp Sent;
public string Text = "";
}
/// 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]
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,
}
);
}
}