Notify when live

This commit is contained in:
2024-01-10 14:02:03 +00:00
parent 5b5f011df0
commit f571579141
4 changed files with 50 additions and 1 deletions

View File

@ -54,6 +54,8 @@ public class Config
public string Redis { get; init; } = null!; public string Redis { get; init; } = null!;
public Uri SnortApi { get; init; } = null!; public Uri SnortApi { get; init; } = null!;
public Uri? DiscordLiveWebhook { get; init; }
} }
public class VapidKeyDetails public class VapidKeyDetails

View File

@ -120,6 +120,9 @@ internal static class Program
services.AddHostedService<PushSenderService>(); services.AddHostedService<PushSenderService>();
services.AddHostedService<EventStream>(); services.AddHostedService<EventStream>();
// webhooks
services.AddTransient<DiscordWebhook>();
// snort api // snort api
services.AddTransient<SnortApi>(); services.AddTransient<SnortApi>();

View File

@ -0,0 +1,21 @@
using System.Net.Http.Formatting;
namespace NostrStreamer.Services;
public class DiscordWebhook
{
private readonly HttpClient _client;
public DiscordWebhook(HttpClient client)
{
_client = client;
}
public async Task SendMessage(Uri webhook, string msg)
{
await _client.PostAsync(webhook, new
{
content = msg
}, new JsonMediaTypeFormatter());
}
}

View File

@ -1,7 +1,10 @@
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json; using Newtonsoft.Json;
using Nostr.Client.Identifiers;
using Nostr.Client.Json; using Nostr.Client.Json;
using Nostr.Client.Messages;
using Nostr.Client.Utils;
using NostrStreamer.Database; using NostrStreamer.Database;
using NostrStreamer.Services.Dvr; using NostrStreamer.Services.Dvr;
@ -14,6 +17,8 @@ public class NostrStreamManager : IStreamManager
private readonly StreamEventBuilder _eventBuilder; private readonly StreamEventBuilder _eventBuilder;
private readonly IDvrStore _dvrStore; private readonly IDvrStore _dvrStore;
private readonly Config _config; private readonly Config _config;
private readonly DiscordWebhook _webhook;
private readonly SnortApi _snortApi;
private readonly IDataProtectionProvider _dataProtectionProvider; private readonly IDataProtectionProvider _dataProtectionProvider;
public NostrStreamManager(ILogger<NostrStreamManager> logger, StreamManagerContext context, IServiceProvider serviceProvider) public NostrStreamManager(ILogger<NostrStreamManager> logger, StreamManagerContext context, IServiceProvider serviceProvider)
@ -24,6 +29,8 @@ public class NostrStreamManager : IStreamManager
_dvrStore = serviceProvider.GetRequiredService<IDvrStore>(); _dvrStore = serviceProvider.GetRequiredService<IDvrStore>();
_config = serviceProvider.GetRequiredService<Config>(); _config = serviceProvider.GetRequiredService<Config>();
_dataProtectionProvider = serviceProvider.GetRequiredService<IDataProtectionProvider>(); _dataProtectionProvider = serviceProvider.GetRequiredService<IDataProtectionProvider>();
_webhook = serviceProvider.GetRequiredService<DiscordWebhook>();
_snortApi = serviceProvider.GetRequiredService<SnortApi>();
} }
public UserStream GetStream() public UserStream GetStream()
@ -75,6 +82,21 @@ public class NostrStreamManager : IStreamManager
TestCanStream(); TestCanStream();
await UpdateStreamState(UserStreamState.Live); await UpdateStreamState(UserStreamState.Live);
if (_config.DiscordLiveWebhook != default)
{
try
{
var profile = await _snortApi.Profile(_context.User.PubKey);
var name = profile?.Name ?? NostrConverter.ToBech32(_context.User.PubKey, "npub");
var id = new NostrAddressIdentifier(_context.UserStream.Id.ToString(), _context.User.PubKey, null, NostrKind.LiveEvent);
await _webhook.SendMessage(_config.DiscordLiveWebhook, $"{name} went live!\nhttps://zap.stream/{id.ToBech32()}");
}
catch (Exception ex)
{
_logger.LogWarning("Failed to send notification {msg}", ex.Message);
}
}
} }
public async Task StreamStopped() public async Task StreamStopped()
@ -196,7 +218,7 @@ public class NostrStreamManager : IStreamManager
} }
} }
private async Task UpdateStreamState(UserStreamState state) private async Task<NostrEvent> UpdateStreamState(UserStreamState state)
{ {
DateTime? ends = state == UserStreamState.Ended ? DateTime.UtcNow : null; DateTime? ends = state == UserStreamState.Ended ? DateTime.UtcNow : null;
_context.UserStream.State = state; _context.UserStream.State = state;
@ -209,5 +231,6 @@ public class NostrStreamManager : IStreamManager
.SetProperty(v => v.Ends, ends)); .SetProperty(v => v.Ends, ends));
_eventBuilder.BroadcastEvent(ev); _eventBuilder.BroadcastEvent(ev);
return ev;
} }
} }