Update: Mutli Channel Support

This commit is contained in:
2026-03-22 01:54:52 -04:00
parent 69a4951579
commit caf020c393
10 changed files with 283 additions and 69 deletions

View File

@@ -0,0 +1,8 @@
namespace RelayServer.Models;
public class SocketChannelInfo
{
public required string ChannelId { get; set; }
public required string Name { get; set; }
public required DateTime CreatedAt { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace RelayServer.Models;
public class SocketChannelList
{
public required string Type { get; set; }
public required List<SocketChannelInfo> Channels { get; set; }
}

View File

@@ -4,7 +4,8 @@ public class SocketEncryptedMessage
{
public required string Type { get; set; }
public required string SenderUsername { get; set; }
public required string RecipientUsername { get; set; }
public string? RecipientUsername { get; set; }
public required string ChannelId { get; set; }
public required string CipherText { get; set; }
public required string Nonce { get; set; }
public required string Tag { get; set; }

View File

@@ -75,18 +75,29 @@ var channel = await db.Create("channels", new Channels
CreatedAt = DateTime.UtcNow
});
var channel2 = await db.Create("files", new Channels
var channel2 = await db.Create("channels", new Channels
{
Name = "files",
CreatedAt = DateTime.UtcNow.Subtract(new TimeSpan(0, 4, 0,0))
CreatedAt = DateTime.UtcNow.Subtract(new TimeSpan(0, 4, 0, 0))
});
var channel3 = await db.Create("channels", new Channels
{
Name = "welcome",
CreatedAt = DateTime.UtcNow.Subtract(new TimeSpan(1, 4, 4, 4))
});
Console.WriteLine($"Channel created: {ToJsonString(channel)}");
Console.WriteLine($"Channel created: {ToJsonString(channel2)}");
Console.WriteLine($"Channel created: {ToJsonString(channel3)}");
var channelId = GetRecordId(channel.Id);
var channelId2 = GetRecordId(channel2.Id);
var channelId3 = GetRecordId(channel3.Id);
Console.WriteLine($"Resolved channelId: {channelId}");
ChatTest.DefaultChannelId = channelId;
Console.WriteLine($"Resolved channelId: {channelId2}");
Console.WriteLine($"Resolved channelId: {channelId3}");
var keyBase64 = cryptoService.GenerateKey();
var serverKeys = E2EeHelper.GenerateRsaKeyPair();

View File

@@ -12,7 +12,6 @@ public class ChatTest : WebSocketBehavior
public static string? ServerPrivateKey { get; set; }
public static string? ChannelDbKey { get; set; }
public static SurrealDb.Net.SurrealDbClient? Db { get; set; }
public static string? DefaultChannelId { get; set; }
protected override void OnMessage(MessageEventArgs e)
{
@@ -31,6 +30,12 @@ public class ChatTest : WebSocketBehavior
return;
}
if (msg == "GET_CHANNELS")
{
HandleGetChannels();
return;
}
if (msg.StartsWith("GET_HISTORY|"))
{
HandleGetHistory(msg);
@@ -76,6 +81,35 @@ public class ChatTest : WebSocketBehavior
Send($"SERVER:REGISTERED_KEY:{username}");
}
private void HandleGetChannels()
{
if (Db is null)
{
Console.WriteLine("Db is not initialized.");
return;
}
var channels = Task.Run(async () => await Db.Select<Channels>("channels"))
.GetAwaiter()
.GetResult()
.OrderBy(c => c.CreatedAt)
.Select(c => new SocketChannelInfo
{
ChannelId = GetRecordId(c.Id),
Name = c.Name,
CreatedAt = c.CreatedAt
})
.ToList();
var payload = new SocketChannelList
{
Type = "channel_list",
Channels = channels
};
Send(JsonSerializer.Serialize(payload));
}
private void HandleGetServerKey()
{
if (string.IsNullOrWhiteSpace(ServerPublicKey))
@@ -113,8 +147,7 @@ public class ChatTest : WebSocketBehavior
if (ClientKeyService is null ||
Db is null ||
string.IsNullOrWhiteSpace(ServerPrivateKey) ||
string.IsNullOrWhiteSpace(ChannelDbKey) ||
string.IsNullOrWhiteSpace(DefaultChannelId))
string.IsNullOrWhiteSpace(ChannelDbKey))
{
Console.WriteLine("Server crypto/database dependencies are not initialized.");
return;
@@ -150,7 +183,7 @@ public class ChatTest : WebSocketBehavior
var savedMessage = Task.Run(async () =>
await Db.Create("channel_messages", new ChannelMessages
{
ChannelId = DefaultChannelId,
ChannelId = clientPayload.ChannelId,
SenderUserId = $"users:{clientPayload.SenderUsername.ToLower()}",
CipherText = dbEncrypted.cipherText,
Nonce = dbEncrypted.nonce,
@@ -182,6 +215,7 @@ public class ChatTest : WebSocketBehavior
Type = "encrypted_chat",
SenderUsername = clientPayload.SenderUsername,
RecipientUsername = client.Username,
ChannelId = clientPayload.ChannelId,
CipherText = encrypted.CipherText,
Nonce = encrypted.Nonce,
Tag = encrypted.Tag,
@@ -194,20 +228,20 @@ public class ChatTest : WebSocketBehavior
private void HandleGetHistory(string msg)
{
var parts = msg.Split('|', 2);
var parts = msg.Split('|', 3);
if (parts.Length < 2)
if (parts.Length < 3)
{
Console.WriteLine("Invalid GET_HISTORY payload.");
return;
}
var username = parts[1];
var channelId = parts[2];
if (ClientKeyService is null ||
Db is null ||
string.IsNullOrWhiteSpace(ChannelDbKey) ||
string.IsNullOrWhiteSpace(DefaultChannelId))
string.IsNullOrWhiteSpace(ChannelDbKey))
{
Console.WriteLine("History dependencies are not initialized.");
return;
@@ -228,7 +262,7 @@ public class ChatTest : WebSocketBehavior
.GetResult();
var channelMessages = allMessages
.Where(m => m.ChannelId == DefaultChannelId)
.Where(m => m.ChannelId == channelId)
.OrderBy(m => m.CreatedAt)
.ToList();
@@ -262,6 +296,7 @@ public class ChatTest : WebSocketBehavior
Type = "encrypted_chat",
SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId),
RecipientUsername = username,
ChannelId = channelId,
CipherText = encrypted.CipherText,
Nonce = encrypted.Nonce,
Tag = encrypted.Tag,
@@ -271,4 +306,21 @@ public class ChatTest : WebSocketBehavior
Send(JsonSerializer.Serialize(outbound));
}
}
private static string GetRecordId(object? id)
{
if (id is null)
return string.Empty;
var json = JsonSerializer.Serialize(id);
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var recordId = root.GetProperty("Id").GetString() ?? string.Empty;
var table = root.GetProperty("Table").GetString() ?? string.Empty;
return $"{table}:{recordId}";
}
}