summaryrefslogtreecommitdiff
path: root/Services
diff options
context:
space:
mode:
Diffstat (limited to 'Services')
-rw-r--r--Services/FeedService.cs78
-rw-r--r--Services/PrincipalProvider.cs7
-rw-r--r--Services/SecurityService.cs10
3 files changed, 92 insertions, 3 deletions
diff --git a/Services/FeedService.cs b/Services/FeedService.cs
new file mode 100644
index 0000000..c66b9ee
--- /dev/null
+++ b/Services/FeedService.cs
@@ -0,0 +1,78 @@
+using Microsoft.EntityFrameworkCore;
+using System.Data.Common;
+
+namespace HyperBooru.Services;
+
+public enum FeedOrder {
+ Chronological,
+ Rating
+}
+
+public interface IFeedService {
+ public IEnumerable<Media> Feed { get; }
+
+ public void InitializeFeed(
+ FeedOrder order = FeedOrder.Chronological,
+ bool descending = true,
+ bool randomPosition = false);
+}
+
+public class FeedService : IFeedService {
+ private FeedConfiguration? feedConfig;
+
+ private IDbContextFactory<HBContext> dbFactory;
+
+ public FeedService(IDbContextFactory<HBContext> dbFactory) =>
+ this.dbFactory = dbFactory;
+
+ public void InitializeFeed(
+ FeedOrder order,
+ bool descending,
+ bool randomPosition) {
+
+ feedConfig = new() {
+ Order = order,
+ Descending = descending,
+ RandomPosition = randomPosition
+ };
+ }
+
+ public IEnumerable<Media> Feed {
+ get {
+ if(feedConfig is null)
+ throw new InvalidOperationException("Feed must be initialized first");
+
+ while(true) {
+ var db = dbFactory.CreateDbContext();
+
+ IOrderedQueryable<Media> media;
+
+ switch(feedConfig.Order) {
+ default:
+ case FeedOrder.Chronological:
+ if(feedConfig.Descending)
+ media = db.Media.OrderByDescending(m => m.ObjectId);
+ else
+ media = db.Media.OrderBy(m => m.ObjectId);
+ break;
+ }
+
+ Media[] mediaArray = media.Take(50).ToArray();
+
+ db.Dispose();
+
+ if(mediaArray.Count() == 0)
+ break;
+
+ foreach(var m in mediaArray)
+ yield return m;
+ }
+ }
+ }
+
+ private record FeedConfiguration {
+ public FeedOrder Order { get; set; }
+ public bool Descending { get; set; }
+ public bool RandomPosition { get; set; }
+ }
+}
diff --git a/Services/PrincipalProvider.cs b/Services/PrincipalProvider.cs
index 4b2cf42..7039379 100644
--- a/Services/PrincipalProvider.cs
+++ b/Services/PrincipalProvider.cs
@@ -5,6 +5,9 @@ public interface IPrincipalProvider {
public IUser? GetUser(string name);
public IGroup? GetGroup(string name);
+ public SecurityIdentifier? TranslateName(string name);
+ public string? TranslateName(SecurityIdentifier sid);
+
/// <summary>
/// Perform a search for any principals whose account name
/// matches the search term specified by <c>name</c>.
@@ -29,13 +32,15 @@ public abstract class PrincipalProvider : IPrincipalProvider {
public abstract IUser? GetUser(string name);
public abstract IGroup? GetGroup(string name);
+ public abstract SecurityIdentifier? TranslateName(string name);
+ public abstract string? TranslateName(SecurityIdentifier sid);
+
public abstract IPrincipal[]? SearchPrincipals(string name);
public IGroup[] GetGroups(IPrincipal principal) =>
GetGroups(principal.Sid, false);
public IGroup[] GetGroups(IPrincipal principal, bool recurse) =>
GetGroups(principal.Sid, recurse);
-
public IGroup[] GetGroups(SecurityIdentifier sid) => GetGroups(sid, false);
public abstract IGroup[] GetGroups(SecurityIdentifier sid, bool recurse);
diff --git a/Services/SecurityService.cs b/Services/SecurityService.cs
index 2d23a58..85513ec 100644
--- a/Services/SecurityService.cs
+++ b/Services/SecurityService.cs
@@ -80,15 +80,21 @@ public class SecurityService : ISecurityService {
if(wellKnownSid is not null)
return wellKnownSid;
- return sid.SddlForm;
+ return principalProvider.TranslateName(sid) ?? sid.SddlForm;
}
public SecurityIdentifier? TranslateName(string name) {
+ name = name.Trim().ToLower();
+
+ try {
+ return new SecurityIdentifier(name.ToUpper());
+ } catch(ArgumentException) {}
+
var wellKnownSid = WellKnownSid.TranslateName(name);
if(wellKnownSid is not null)
return wellKnownSid;
- throw new NotImplementedException();
+ return principalProvider.TranslateName(name);
}
public IEnumerable<HBObject> Filter(