diff --git a/RelayServer/Endpoints/RtcEndpoints.cs b/RelayServer/Endpoints/RtcEndpoints.cs index 602e6d3..d7d6891 100644 --- a/RelayServer/Endpoints/RtcEndpoints.cs +++ b/RelayServer/Endpoints/RtcEndpoints.cs @@ -6,30 +6,23 @@ namespace RelayServer.Endpoints; public static class RtcEndpoints { /// - /// Maps all RTC-related HTTP endpoints used for joining calls, storing offers and answers, - /// writing ICE candidates, and leaving active calls. + /// Maps all RTC-related HTTP endpoints used for storing offers and answers, + /// writing ICE candidates, checking active calls, and leaving active calls. /// /// The web application to map endpoints onto. 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. app.MapPost("/api/rtc/offer", async (RtcOffer request, RtcCallService rtcCallService) => { await rtcCallService.WriteOfferAsync(request.ChannelId, request.Username, request.Sdp); return Results.Ok(); }); + // List all offers. app.MapGet("/api/rtc/offers", async (RtcCallService rtcCallService) => { return Results.Ok(await rtcCallService.GetOffersAsync()); - }); // Return whether the specified channel currently has an active call. diff --git a/RelayServer/Services/Rtc/RtcCallService.cs b/RelayServer/Services/Rtc/RtcCallService.cs index b0ca22b..d57768f 100644 --- a/RelayServer/Services/Rtc/RtcCallService.cs +++ b/RelayServer/Services/Rtc/RtcCallService.cs @@ -25,62 +25,10 @@ public sealed class RtcCallService return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive); } - /// - /// Joins a user to a channel call and determines whether they should become the offerer - /// or join an already active call. - /// - /// The channel being joined. - /// The user joining the call. - /// - /// A join response describing whether a call already exists, who the offer user is, - /// and whether the caller should act as the offerer. - /// - public async Task JoinCallAsync(string channelId, string username) - { - //TODO: move active call creation logic to WriteOfferAsync Function - var activeCalls = await _db.Select("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("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 - }; - } - /// /// 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. /// /// The channel the offer belongs to. /// The user creating the offer. @@ -110,7 +58,19 @@ public sealed class RtcCallService var activeCalls = await _db.Select("rtc_active_calls"); 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; await _db.Merge(activeCall); @@ -281,9 +241,11 @@ public sealed class RtcCallService } } - public async Task GetOffersAsync() + public async Task> GetOffersAsync() { var offers = await _db.Select("rtc_offers"); - return offers; + return offers + .OrderByDescending(x => x.CreatedAt) + .ToList(); } } \ No newline at end of file