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 Uri SnortApi { get; init; } = null!;
public Uri? DiscordLiveWebhook { get; init; }
}
public class VapidKeyDetails

View File

@ -120,6 +120,9 @@ internal static class Program
services.AddHostedService<PushSenderService>();
services.AddHostedService<EventStream>();
// webhooks
services.AddTransient<DiscordWebhook>();
// snort api
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.EntityFrameworkCore;
using Newtonsoft.Json;
using Nostr.Client.Identifiers;
using Nostr.Client.Json;
using Nostr.Client.Messages;
using Nostr.Client.Utils;
using NostrStreamer.Database;
using NostrStreamer.Services.Dvr;
@ -14,6 +17,8 @@ public class NostrStreamManager : IStreamManager
private readonly StreamEventBuilder _eventBuilder;
private readonly IDvrStore _dvrStore;
private readonly Config _config;
private readonly DiscordWebhook _webhook;
private readonly SnortApi _snortApi;
private readonly IDataProtectionProvider _dataProtectionProvider;
public NostrStreamManager(ILogger<NostrStreamManager> logger, StreamManagerContext context, IServiceProvider serviceProvider)
@ -24,6 +29,8 @@ public class NostrStreamManager : IStreamManager
_dvrStore = serviceProvider.GetRequiredService<IDvrStore>();
_config = serviceProvider.GetRequiredService<Config>();
_dataProtectionProvider = serviceProvider.GetRequiredService<IDataProtectionProvider>();
_webhook = serviceProvider.GetRequiredService<DiscordWebhook>();
_snortApi = serviceProvider.GetRequiredService<SnortApi>();
}
public UserStream GetStream()
@ -75,6 +82,21 @@ public class NostrStreamManager : IStreamManager
TestCanStream();
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()
@ -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;
_context.UserStream.State = state;
@ -209,5 +231,6 @@ public class NostrStreamManager : IStreamManager
.SetProperty(v => v.Ends, ends));
_eventBuilder.BroadcastEvent(ev);
return ev;
}
}