💄 Optimize realm discovery
This commit is contained in:
		| @@ -29,7 +29,7 @@ public class ActivityService(AppDatabase db, PublisherService pub, RelationshipS | ||||
|  | ||||
|         if (cursor == null) | ||||
|         { | ||||
|             var realms = await ds.GetPublicRealmsAsync(null, null); | ||||
|             var realms = await ds.GetPublicRealmsAsync(null, null, 5, 0, true); | ||||
|             if (realms.Count > 0) | ||||
|             { | ||||
|                 activities.Add(new DiscoveryActivity("Explore Realms", realms.Cast<object>().ToList()).ToActivity()); | ||||
| @@ -89,7 +89,7 @@ public class ActivityService(AppDatabase db, PublisherService pub, RelationshipS | ||||
|  | ||||
|         if (cursor == null) | ||||
|         { | ||||
|             var realms = await ds.GetPublicRealmsAsync(null, null); | ||||
|             var realms = await ds.GetPublicRealmsAsync(null, null, 5, 0, true); | ||||
|             if (realms.Count > 0) | ||||
|             { | ||||
|                 activities.Add(new DiscoveryActivity("Explore Realms", realms.Cast<object>().ToList()).ToActivity()); | ||||
|   | ||||
| @@ -9,9 +9,16 @@ namespace DysonNetwork.Sphere.Discovery; | ||||
|  | ||||
| public class DiscoveryService(AppDatabase appDatabase) | ||||
| { | ||||
|     public Task<List<Realm.Realm>> GetPublicRealmsAsync(string? query, List<string>? tags) | ||||
|     public Task<List<Realm.Realm>> GetPublicRealmsAsync(string? query, | ||||
|         List<string>? tags, | ||||
|         int take = 10, | ||||
|         int offset = 0, | ||||
|         bool randomizer = false | ||||
|     ) | ||||
|     { | ||||
|         var realmsQuery = appDatabase.Realms | ||||
|             .Take(take) | ||||
|             .Skip(offset) | ||||
|             .Where(r => r.IsPublic); | ||||
|  | ||||
|         if (!string.IsNullOrEmpty(query)) | ||||
| @@ -19,11 +26,13 @@ public class DiscoveryService(AppDatabase appDatabase) | ||||
|             realmsQuery = realmsQuery.Where(r => r.Name.Contains(query) || r.Description.Contains(query)); | ||||
|         } | ||||
|  | ||||
|         if (tags != null && tags.Count > 0) | ||||
|         { | ||||
|         if (tags is { Count: > 0 }) | ||||
|             realmsQuery = realmsQuery.Where(r => r.RealmTags.Any(rt => tags.Contains(rt.Tag.Name))); | ||||
|         } | ||||
|         if (randomizer) | ||||
|             realmsQuery = realmsQuery.OrderBy(r => EF.Functions.Random()); | ||||
|         else | ||||
|             realmsQuery = realmsQuery.OrderByDescending(r => r.CreatedAt); | ||||
|  | ||||
|         return realmsQuery.ToListAsync(); | ||||
|         return realmsQuery.Skip(offset).Take(take).ToListAsync(); | ||||
|     } | ||||
| } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user