Update: Combined Endpoints to do correct jobs.

This commit is contained in:
2026-04-03 09:35:07 -04:00
parent 701e30c31b
commit 776889932e
2 changed files with 22 additions and 67 deletions

View File

@@ -6,30 +6,23 @@ namespace RelayServer.Endpoints;
public static class RtcEndpoints public static class RtcEndpoints
{ {
/// <summary> /// <summary>
/// Maps all RTC-related HTTP endpoints used for joining calls, storing offers and answers, /// Maps all RTC-related HTTP endpoints used for storing offers and answers,
/// writing ICE candidates, and leaving active calls. /// writing ICE candidates, checking active calls, and leaving active calls.
/// </summary> /// </summary>
/// <param name="app">The web application to map endpoints onto.</param> /// <param name="app">The web application to map endpoints onto.</param>
public static void MapRtcEndpoints(this WebApplication app) public static void MapRtcEndpoints(this WebApplication app)
{ {
// Join a channel call and determine whether the caller should become the offerer.
//TODO: Remove join endpoint and redo its logic in correct locations
app.MapPost("/api/rtc/join", async (RtcJoinRequest request, RtcCallService rtcCallService) =>
{
return Results.Ok(await rtcCallService.JoinCallAsync(request.ChannelId, request.Username));
});
// Store or update the current SDP offer for a channel call. // Store or update the current SDP offer for a channel call.
app.MapPost("/api/rtc/offer", async (RtcOffer request, RtcCallService rtcCallService) => app.MapPost("/api/rtc/offer", async (RtcOffer request, RtcCallService rtcCallService) =>
{ {
await rtcCallService.WriteOfferAsync(request.ChannelId, request.Username, request.Sdp); await rtcCallService.WriteOfferAsync(request.ChannelId, request.Username, request.Sdp);
return Results.Ok(); return Results.Ok();
}); });
// List all offers. // List all offers.
app.MapGet("/api/rtc/offers", async (RtcCallService rtcCallService) => app.MapGet("/api/rtc/offers", async (RtcCallService rtcCallService) =>
{ {
return Results.Ok(await rtcCallService.GetOffersAsync()); return Results.Ok(await rtcCallService.GetOffersAsync());
}); });
// Return whether the specified channel currently has an active call. // Return whether the specified channel currently has an active call.

View File

@@ -25,62 +25,10 @@ public sealed class RtcCallService
return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive); return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive);
} }
/// <summary>
/// Joins a user to a channel call and determines whether they should become the offerer
/// or join an already active call.
/// </summary>
/// <param name="channelId">The channel being joined.</param>
/// <param name="username">The user joining the call.</param>
/// <returns>
/// A join response describing whether a call already exists, who the offer user is,
/// and whether the caller should act as the offerer.
/// </returns>
public async Task<RtcJoinResponse> JoinCallAsync(string channelId, string username)
{
//TODO: move active call creation logic to WriteOfferAsync Function
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 //TODO: Remove offer creation in C#
.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
};
}
/// <summary> /// <summary>
/// Creates or updates the current SDP offer for a user in the specified channel. /// Creates or updates the current SDP offer for a user in the specified channel.
/// Also refreshes the active call timestamp when a matching active call exists. /// If no active call exists for the channel, a new active call is created.
/// Otherwise, the existing active call timestamp is refreshed.
/// </summary> /// </summary>
/// <param name="channelId">The channel the offer belongs to.</param> /// <param name="channelId">The channel the offer belongs to.</param>
/// <param name="username">The user creating the offer.</param> /// <param name="username">The user creating the offer.</param>
@@ -110,7 +58,19 @@ public sealed class RtcCallService
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls"); var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive); var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
if (activeCall is not null)
if (activeCall is null)
{
await _db.Create("rtc_active_calls", new RtcActiveCall
{
ChannelId = channelId,
OfferUser = username,
IsActive = true,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
});
}
else
{ {
activeCall.UpdatedAt = DateTime.UtcNow; activeCall.UpdatedAt = DateTime.UtcNow;
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall); await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
@@ -281,9 +241,11 @@ public sealed class RtcCallService
} }
} }
public async Task<object?> GetOffersAsync() public async Task<List<RtcOffer>> GetOffersAsync()
{ {
var offers = await _db.Select<RtcOffer>("rtc_offers"); var offers = await _db.Select<RtcOffer>("rtc_offers");
return offers; return offers
.OrderByDescending(x => x.CreatedAt)
.ToList();
} }
} }