mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Save patreon id to database, use transaction when saving feed
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -62,8 +63,12 @@ namespace Podsync.Controllers
|
||||
};
|
||||
|
||||
// Check if user eligible for Patreon features
|
||||
var enablePatreonFeatures = User.EnablePatreonFeatures();
|
||||
if (!enablePatreonFeatures)
|
||||
var allowFeatures = User.EnablePatreonFeatures();
|
||||
if (allowFeatures)
|
||||
{
|
||||
feed.PatreonId = User.GetClaim(ClaimTypes.NameIdentifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
feed.Quality = ResolveFormat.VideoHigh;
|
||||
}
|
||||
|
@@ -69,7 +69,7 @@ namespace Podsync.Helpers
|
||||
?? "noname :(";
|
||||
}
|
||||
|
||||
private static string GetClaim(this ClaimsPrincipal claimsPrincipal, string type)
|
||||
public static string GetClaim(this ClaimsPrincipal claimsPrincipal, string type)
|
||||
{
|
||||
return claimsPrincipal.Claims.FirstOrDefault(x => x.Type == type)?.Value;
|
||||
}
|
||||
|
@@ -15,6 +15,11 @@ namespace Podsync.Services.Storage
|
||||
|
||||
public int PageSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// User id on Patreon
|
||||
/// </summary>
|
||||
public string PatreonId { get; set; }
|
||||
|
||||
public override string ToString() => $"{Provider} ({LinkType}) {Id}";
|
||||
|
||||
// Workaround for backward compatibility
|
||||
|
@@ -78,25 +78,22 @@ namespace Podsync.Services.Storage
|
||||
{
|
||||
var id = await MakeId();
|
||||
|
||||
if (await Db.KeyExistsAsync(id))
|
||||
var t = Db.CreateTransaction();
|
||||
t.AddCondition(Condition.KeyNotExists(id));
|
||||
|
||||
#pragma warning disable 4014
|
||||
// We should not await here because of transaction
|
||||
// See http://stackoverflow.com/questions/25976231/stackexchange-redis-transaction-methods-freezes
|
||||
t.HashSetAsync(id, BuildSet(metadata).ToArray());
|
||||
t.KeyExpireAsync(id, TimeSpan.FromDays(1));
|
||||
#pragma warning restore 4014
|
||||
|
||||
var succeeded = await t.ExecuteAsync();
|
||||
if (!succeeded)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to generate feed id");
|
||||
throw new InvalidOperationException("Failed to save feed");
|
||||
}
|
||||
|
||||
await Db.HashSetAsync(id, new[]
|
||||
{
|
||||
// V1
|
||||
new HashEntry(nameof(metadata.Provider), metadata.Provider.ToString()),
|
||||
new HashEntry(nameof(metadata.Type), metadata.Type.ToString()),
|
||||
new HashEntry(nameof(metadata.Id), metadata.Id),
|
||||
|
||||
// V2
|
||||
new HashEntry(nameof(metadata.Quality), metadata.Quality.ToString()),
|
||||
new HashEntry(nameof(metadata.PageSize), metadata.PageSize),
|
||||
});
|
||||
|
||||
await Db.KeyExpireAsync(id, TimeSpan.FromDays(1));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -120,13 +117,14 @@ namespace Podsync.Services.Storage
|
||||
var metadata = new FeedMetadata();
|
||||
|
||||
// V1
|
||||
SetProperty(metadata, x => x.Id, entries);
|
||||
SetProperty(metadata, x => x.Type, entries);
|
||||
SetProperty(metadata, x => x.Provider, entries);
|
||||
UnpackProperty(metadata, x => x.Id, entries);
|
||||
UnpackProperty(metadata, x => x.Type, entries);
|
||||
UnpackProperty(metadata, x => x.Provider, entries);
|
||||
|
||||
// V2
|
||||
SetProperty(metadata, x => x.Quality, entries, Constants.DefaultFormat);
|
||||
SetProperty(metadata, x => x.PageSize, entries, Constants.DefaultPageSize);
|
||||
UnpackProperty(metadata, x => x.Quality, entries, Constants.DefaultFormat);
|
||||
UnpackProperty(metadata, x => x.PageSize, entries, Constants.DefaultPageSize);
|
||||
UnpackProperty(metadata, x => x.PatreonId, entries, null);
|
||||
|
||||
return metadata;
|
||||
}
|
||||
@@ -156,12 +154,12 @@ namespace Podsync.Services.Storage
|
||||
return key;
|
||||
}
|
||||
|
||||
private static void SetProperty<T, P>(T target, Expression<Func<T, P>> memberLamda, HashEntry[] entries)
|
||||
private static void UnpackProperty<T, P>(T target, Expression<Func<T, P>> memberLamda, HashEntry[] entries)
|
||||
{
|
||||
SetProperty(target, memberLamda, entries, default(P), true);
|
||||
UnpackProperty(target, memberLamda, entries, default(P), true);
|
||||
}
|
||||
|
||||
private static void SetProperty<T, P>(T target, Expression<Func<T, P>> memberLamda, HashEntry[] entries, P fallback, bool throwIfMissing = false)
|
||||
private static void UnpackProperty<T, P>(T target, Expression<Func<T, P>> memberLamda, HashEntry[] entries, P fallback, bool throwIfMissing = false)
|
||||
{
|
||||
var memberExpression = memberLamda.Body as MemberExpression;
|
||||
|
||||
@@ -202,5 +200,23 @@ namespace Podsync.Services.Storage
|
||||
var property = memberExpression.Member as PropertyInfo;
|
||||
property?.SetValue(target, value);
|
||||
}
|
||||
|
||||
private IEnumerable<HashEntry> BuildSet(FeedMetadata metadata)
|
||||
{
|
||||
// V1.0
|
||||
yield return new HashEntry(nameof(metadata.Provider), metadata.Provider.ToString());
|
||||
yield return new HashEntry(nameof(metadata.Type), metadata.Type.ToString());
|
||||
yield return new HashEntry(nameof(metadata.Id), metadata.Id);
|
||||
|
||||
// V2.0
|
||||
yield return new HashEntry(nameof(metadata.Quality), metadata.Quality.ToString());
|
||||
yield return new HashEntry(nameof(metadata.PageSize), metadata.PageSize);
|
||||
|
||||
// V2.1
|
||||
if (!string.IsNullOrEmpty(metadata.PatreonId))
|
||||
{
|
||||
yield return new HashEntry(nameof(metadata.PatreonId), metadata.PatreonId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user