Skip to content

Commit

Permalink
🎥 Update stream middleware to handle new WebRTC service (#37)
Browse files Browse the repository at this point in the history
FTL is dead. Long live WebRTC.
This change adds announcement middleware for a new service built on
pion/webrtc: https://github.com/haydenmc/WarmItUpTv

Also included is a small bug fix for a new Matrix change that was
causing JSON parse errors.
  • Loading branch information
haydenmc authored Aug 2, 2024
1 parent bbfbf46 commit 8e84175
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

namespace Theorem.Middleware
{
public class EzFtlStreamAnnouncementMiddleware : IMiddleware
public class StreamAnnouncementMiddleware : IMiddleware
{
/// <summary>
/// Logging instance
/// </summary>
private ILogger<EzFtlStreamAnnouncementMiddleware> _logger;
private ILogger<StreamAnnouncementMiddleware> _logger;

/// <summary>
/// Configuration object for retrieving configuration values
Expand Down Expand Up @@ -62,18 +62,18 @@ private string _webSocketUrl
}
}

public EzFtlStreamAnnouncementMiddleware(ILogger<EzFtlStreamAnnouncementMiddleware> logger,
public StreamAnnouncementMiddleware(ILogger<StreamAnnouncementMiddleware> logger,
ConfigurationSection configuration,
IEnumerable<IChatServiceConnection> chatServiceConnections)
{
_logger = logger;
_configuration = configuration;
_chatServiceConnections = chatServiceConnections;

_logger.LogInformation("Starting EzFtlStreamAnnouncement Middleware...");
_logger.LogInformation("Starting StreamAnnouncement Middleware...");
subscribeToChatServiceConnectedEvents();
TaskUtilities
.ExpontentialRetryAsync(startEzFtlConnection, onConnectionInterrupted)
.ExpontentialRetryAsync(startStreamConnection, onConnectionInterrupted)
.FireAndForget();
}

Expand All @@ -93,7 +93,7 @@ private void subscribeToChatServiceConnectedEvents()
private void onConnectionInterrupted(Exception exception,
(uint retryNumber, uint nextRetrySeconds) retries)
{
_logger.LogError("EzFtl connection threw exception. " +
_logger.LogError("Stream connection threw exception. " +
"retry {n} in {s} seconds. Exception: {e}",
retries.retryNumber,
retries.nextRetrySeconds,
Expand All @@ -118,12 +118,13 @@ private async void onChatServiceConnected(object sender, EventArgs e)
}
}

private async Task startEzFtlConnection()
private async Task startStreamConnection()
{
_logger.LogInformation("Connecting to EzFtl service...");
_logger.LogInformation("Connecting to Stream service...");
var webSocketClient = new ClientWebSocket();
webSocketClient.Options.AddSubProtocol("stream-updates");
await webSocketClient.ConnectAsync(new Uri(_webSocketUrl), CancellationToken.None);
_logger.LogInformation("Connected to EzFtl!");
_logger.LogInformation("Connected to Stream server!");
await receive(webSocketClient);
}

Expand Down Expand Up @@ -157,7 +158,7 @@ await webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure,
try
{
var channel = JsonConvert.DeserializeObject<ChannelUpdateModel>(messageString);
if (channel.HasActiveStream)
if (channel.IsLive)
{
await sendStreamingAnnouncement(channel);
}
Expand All @@ -179,26 +180,23 @@ await connection.Key.SendMessageToChannelIdAsync(
connection.Value,
new ChatMessageModel()
{
Body = $"🎥 '{channel.ChannelName}' has started! " +
$"{_baseUrl}/{channel.ChannelId}",
Body = $"🎥 '{channel.Name}' has started! " +
$"{_baseUrl}/{channel.Id}",
});
}
}

/* Private Types */
private class ChannelUpdateModel
{
[JsonProperty("channel_id")]
public int ChannelId { get; set; }
[JsonProperty("Id")]
public string Id { get; set; }

[JsonProperty("channel_name")]
public string ChannelName { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }

[JsonProperty("has_active_stream")]
public bool HasActiveStream { get; set; }

[JsonProperty("active_stream_id")]
public int ActiveStreamId { get; set; }
[JsonProperty("IsLive")]
public bool IsLive { get; set; }
}
}
}
7 changes: 5 additions & 2 deletions src/Theorem/Models/Matrix/MatrixRoomMessageEventModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public override MatrixRoomMessageEventContent Read(ref Utf8JsonReader reader, Ty
_ => new MatrixRoomMessageEventContent() { MessageType = typeValue },
};
}
throw new JsonException(
"Failed to extract type property from MatrixRoomMessageEventContent");
// TODO: Probably a better way to handle this, but this workaround
// Handles empty "content" fields in case of redacted messages.
return new MatrixRoomMessageEventContent() { MessageType = "" };
// throw new JsonException(
// "Failed to extract type property from MatrixRoomMessageEventContent");
}
throw new JsonException("Failed to parse MatrixRoomMessageEventContent");
}
Expand Down

0 comments on commit 8e84175

Please sign in to comment.