Some orgnization, and cleanup to come.
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
using RelayServer.Models;
|
||||
using SurrealDb.Net;
|
||||
|
||||
namespace RelayServer.Services;
|
||||
|
||||
public sealed class ClientKeyService
|
||||
{
|
||||
private readonly SurrealDbClient _db;
|
||||
|
||||
public ClientKeyService(SurrealDbClient db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task RegisterOrUpdateKeyAsync(string username, string publicKey)
|
||||
{
|
||||
var allKeys = await _db.Select<ClientPublicKeys>("client_public_keys");
|
||||
|
||||
var existing = allKeys.FirstOrDefault(x => x.Username == username);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
await _db.Create("client_public_keys", new ClientPublicKeys
|
||||
{
|
||||
Username = username,
|
||||
PublicKey = publicKey,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
Console.WriteLine($"Stored public key for {username}");
|
||||
return;
|
||||
}
|
||||
|
||||
existing.PublicKey = publicKey;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<ClientPublicKeys, ClientPublicKeys>(new ClientPublicKeys
|
||||
{
|
||||
Id = existing.Id,
|
||||
Username = existing.Username,
|
||||
PublicKey = existing.PublicKey,
|
||||
CreatedAt = existing.CreatedAt,
|
||||
UpdatedAt = existing.UpdatedAt
|
||||
});
|
||||
|
||||
Console.WriteLine($"Updated public key for {username}");
|
||||
}
|
||||
|
||||
public async Task<ClientPublicKeys?> GetByUsernameAsync(string username)
|
||||
{
|
||||
var allKeys = await _db.Select<ClientPublicKeys>("client_public_keys");
|
||||
return allKeys.FirstOrDefault(x => x.Username == username);
|
||||
}
|
||||
|
||||
public async Task<List<ClientPublicKeys>> GetAllAsync()
|
||||
{
|
||||
var allKeys = await _db.Select<ClientPublicKeys>("client_public_keys");
|
||||
return allKeys.ToList();
|
||||
}
|
||||
}
|
||||
148
RelayServer/Services/Rtc/RtcCallService.cs
Normal file
148
RelayServer/Services/Rtc/RtcCallService.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using RelayServer.Models.Rtc;
|
||||
using SurrealDb.Net;
|
||||
|
||||
namespace RelayServer.Services.Rtc;
|
||||
|
||||
public sealed class RtcCallService
|
||||
{
|
||||
private readonly SurrealDbClient _db;
|
||||
|
||||
public RtcCallService(SurrealDbClient db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<RtcJoinResponse> JoinCallAsync(string channelId, string username)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
if (activeCall is null)
|
||||
{
|
||||
await _db.Create("rtc_active_calls", new RtcActiveCall
|
||||
{
|
||||
ChannelId = channelId,
|
||||
OfferUser = username,
|
||||
IsActive = true,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
return new RtcJoinResponse
|
||||
{
|
||||
ChannelId = channelId,
|
||||
HasActiveCall = false,
|
||||
IsOfferer = true,
|
||||
OfferUser = username,
|
||||
OfferSdp = null
|
||||
};
|
||||
}
|
||||
|
||||
var offers = await _db.Select<RtcOffer>("rtc_offers");
|
||||
var offer = offers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
|
||||
return new RtcJoinResponse
|
||||
{
|
||||
ChannelId = channelId,
|
||||
HasActiveCall = true,
|
||||
IsOfferer = false,
|
||||
OfferUser = activeCall.OfferUser,
|
||||
OfferSdp = offer?.Sdp
|
||||
};
|
||||
}
|
||||
|
||||
public async Task WriteOfferAsync(string channelId, string username, string sdp)
|
||||
{
|
||||
var offers = await _db.Select<RtcOffer>("rtc_offers");
|
||||
var existing = offers.FirstOrDefault(x => x.ChannelId == channelId && x.Username == username);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
await _db.Create("rtc_offers", new RtcOffer
|
||||
{
|
||||
ChannelId = channelId,
|
||||
Username = username,
|
||||
Sdp = sdp,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
existing.Sdp = sdp;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcOffer, RtcOffer>(existing);
|
||||
}
|
||||
|
||||
public async Task<RtcOffer?> GetOfferAsync(string channelId)
|
||||
{
|
||||
var offers = await _db.Select<RtcOffer>("rtc_offers");
|
||||
return offers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task WriteAnswerAsync(string channelId, string offerUser, string answerUser, string sdp)
|
||||
{
|
||||
await _db.Create("rtc_answers", new RtcAnswer
|
||||
{
|
||||
ChannelId = channelId,
|
||||
OfferUser = offerUser,
|
||||
AnswerUser = answerUser,
|
||||
Sdp = sdp,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<RtcAnswer>> GetAnswersAsync(string channelId)
|
||||
{
|
||||
var answers = await _db.Select<RtcAnswer>("rtc_answers");
|
||||
return answers
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task WriteIceCandidateAsync(string channelId, string username, string candidate, string? sdpMid, int? sdpMLineIndex, string direction)
|
||||
{
|
||||
await _db.Create("rtc_ice_candidates", new RtcIceCandidate
|
||||
{
|
||||
ChannelId = channelId,
|
||||
Username = username,
|
||||
Candidate = candidate,
|
||||
SdpMid = sdpMid,
|
||||
SdpMLineIndex = sdpMLineIndex,
|
||||
Direction = direction,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<List<RtcIceCandidate>> GetIceCandidatesAsync(string channelId)
|
||||
{
|
||||
var candidates = await _db.Select<RtcIceCandidate>("rtc_ice_candidates");
|
||||
return candidates
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task LeaveCallAsync(string channelId, string username)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
if (activeCall is null)
|
||||
return;
|
||||
|
||||
if (activeCall.OfferUser == username)
|
||||
{
|
||||
activeCall.IsActive = false;
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using SurrealDb.Net;
|
||||
using SurrealDb.Net.Models.Auth;
|
||||
|
||||
namespace RelayServer.Services;
|
||||
|
||||
public sealed class SurrealService
|
||||
{
|
||||
public async Task<SurrealDbClient> ConnectAsync()
|
||||
{
|
||||
var db = new SurrealDbClient("ws://127.0.0.1:8000/rpc");
|
||||
|
||||
await db.SignIn(new RootAuth
|
||||
{
|
||||
Username = "root",
|
||||
Password = "secret"
|
||||
});
|
||||
|
||||
await db.Use("test", "test");
|
||||
return db;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user