Stream forwarding

This commit is contained in:
2023-12-07 22:46:36 +00:00
parent 1ad5186aff
commit cef1b845bc
17 changed files with 594 additions and 10 deletions

View File

@ -1,5 +1,6 @@
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore;
using Nostr.Client.Utils;
using NostrStreamer.ApiModel;
@ -11,11 +12,13 @@ public class UserService
{
private readonly StreamerContext _db;
private readonly LndNode _lnd;
private readonly IDataProtectionProvider _dataProtector;
public UserService(StreamerContext db, LndNode lnd)
public UserService(StreamerContext db, LndNode lnd, IDataProtectionProvider dataProtector)
{
_db = db;
_lnd = lnd;
_dataProtector = dataProtector;
}
/// <summary>
@ -41,7 +44,7 @@ public class UserService
Amount = (ulong)user.Balance / 1000,
PaymentHash = SHA256.HashData(Encoding.UTF8.GetBytes($"{pubkey}-init-credit")).ToHex()
});
await _db.SaveChangesAsync();
return user;
}
@ -79,6 +82,7 @@ public class UserService
public async Task<User?> GetUser(string pubkey)
{
return await _db.Users.AsNoTracking()
.Include(a => a.Forwards)
.SingleOrDefaultAsync(a => a.PubKey.Equals(pubkey));
}
@ -90,6 +94,25 @@ public class UserService
if (change != 1) throw new Exception($"Failed to accept TOS, {change} rows updated.");
}
public async Task AddForward(string pubkey, string name, string dest)
{
var protector = _dataProtector.CreateProtector("forward-targets");
_db.Forwards.Add(new()
{
UserPubkey = pubkey,
Name = name,
Target = protector.Protect(dest)
});
await _db.SaveChangesAsync();
}
public async Task RemoveForward(string pubkey, Guid id)
{
await _db.Forwards.Where(a => a.UserPubkey.Equals(pubkey) && a.Id == id)
.ExecuteDeleteAsync();
}
public async Task UpdateStreamInfo(string pubkey, PatchEvent req)
{
await _db.Users