Update: Mutli Channel Support
This commit is contained in:
@@ -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}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user