♻️ Remove the discovery service in the Sphere.
This commit is contained in:
37
DysonNetwork.Pass/Realm/RealmDiscoveryController.cs
Normal file
37
DysonNetwork.Pass/Realm/RealmDiscoveryController.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using DysonNetwork.Shared.Models;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DysonNetwork.Pass.Realm;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/realms/discovery")]
|
||||||
|
public class RealmDiscoveryController(AppDatabase db) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<ActionResult<List<SnRealm>>> GetRealmDiscovery(
|
||||||
|
[FromQuery] string? query,
|
||||||
|
[FromQuery] int take = 10,
|
||||||
|
[FromQuery] int offset = 0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var realmsQuery = db.Realms
|
||||||
|
.Where(r => r.IsCommunity)
|
||||||
|
.OrderByDescending(r => r.Members.Count)
|
||||||
|
.AsQueryable();
|
||||||
|
if (!string.IsNullOrWhiteSpace(query))
|
||||||
|
realmsQuery = realmsQuery.Where(r =>
|
||||||
|
EF.Functions.ILike(r.Name, $"%{query}%") ||
|
||||||
|
EF.Functions.ILike(r.Description, $"%{query}%")
|
||||||
|
);
|
||||||
|
|
||||||
|
var totalCount = await realmsQuery.CountAsync();
|
||||||
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
|
||||||
|
var realms = await realmsQuery
|
||||||
|
.Take(take)
|
||||||
|
.Skip(offset)
|
||||||
|
.ToListAsync();
|
||||||
|
return Ok(realms);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,24 +68,30 @@ public class RealmServiceGrpc(
|
|||||||
return new GetUserRealmsResponse { RealmIds = { realms.Select(g => g.ToString()) } };
|
return new GetUserRealmsResponse { RealmIds = { realms.Select(g => g.ToString()) } };
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<GetPublicRealmsResponse> GetPublicRealms(
|
public override async Task<GetPublicRealmsResponse> GetPublicRealms(
|
||||||
GetPublicRealmsRequest request,
|
GetPublicRealmsRequest request,
|
||||||
ServerCallContext context
|
ServerCallContext context
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var realmsQueryable = db.Realms.Where(r => r.IsPublic).AsQueryable();
|
var realmsQuery = db.Realms.AsQueryable();
|
||||||
|
|
||||||
realmsQueryable = request.OrderBy switch
|
realmsQuery = request.IsCommunity ?
|
||||||
|
realmsQuery.Where(r => r.IsCommunity) :
|
||||||
|
realmsQuery.Where(r => r.IsPublic);
|
||||||
|
|
||||||
|
realmsQuery = request.OrderBy switch
|
||||||
{
|
{
|
||||||
"random" => realmsQueryable.OrderBy(_ => EF.Functions.Random()),
|
"random" => realmsQuery.OrderBy(_ => EF.Functions.Random()),
|
||||||
"name" => realmsQueryable.OrderBy(r => r.Name),
|
"name" => realmsQuery.OrderBy(r => r.Name),
|
||||||
"popularity" => realmsQueryable.OrderByDescending(r => r.Members.Count),
|
"popularity" => realmsQuery.OrderByDescending(r => r.Members.Count),
|
||||||
_ => realmsQueryable.OrderByDescending(r => r.CreatedAt)
|
_ => realmsQuery.OrderByDescending(r => r.CreatedAt)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var realms = await realmsQuery.Take(request.Take).ToListAsync();
|
||||||
|
|
||||||
var response = new GetPublicRealmsResponse();
|
var response = new GetPublicRealmsResponse();
|
||||||
response.Realms.AddRange(realmsQueryable.Select(r => r.ToProtoValue()));
|
response.Realms.AddRange(realms.Select(r => r.ToProtoValue()));
|
||||||
return Task.FromResult(response);
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<GetPublicRealmsResponse> SearchRealms(SearchRealmsRequest request,
|
public override async Task<GetPublicRealmsResponse> SearchRealms(SearchRealmsRequest request,
|
||||||
|
|||||||
@@ -86,6 +86,8 @@ message GetUserRealmsResponse {
|
|||||||
|
|
||||||
message GetPublicRealmsRequest {
|
message GetPublicRealmsRequest {
|
||||||
optional string order_by = 1;
|
optional string order_by = 1;
|
||||||
|
int32 take = 2;
|
||||||
|
bool is_community = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message GetPublicRealmsResponse {
|
message GetPublicRealmsResponse {
|
||||||
|
|||||||
@@ -27,11 +27,12 @@ public class RemoteRealmService(RealmService.RealmServiceClient realms)
|
|||||||
return response.RealmIds.Select(Guid.Parse).ToList();
|
return response.RealmIds.Select(Guid.Parse).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<SnRealm>> GetPublicRealms(string orderBy = "date")
|
public async Task<List<SnRealm>> GetPublicRealms(string orderBy = "date", int take = 20)
|
||||||
{
|
{
|
||||||
var response = await realms.GetPublicRealmsAsync(new GetPublicRealmsRequest
|
var response = await realms.GetPublicRealmsAsync(new GetPublicRealmsRequest
|
||||||
{
|
{
|
||||||
OrderBy = orderBy
|
OrderBy = orderBy,
|
||||||
|
Take = take
|
||||||
});
|
});
|
||||||
return response.Realms.Select(SnRealm.FromProtoValue).ToList();
|
return response.Realms.Select(SnRealm.FromProtoValue).ToList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace DysonNetwork.Sphere.Discovery;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("/api/discovery")]
|
|
||||||
public class DiscoveryController(DiscoveryService discoveryService) : ControllerBase
|
|
||||||
{
|
|
||||||
[HttpGet("realms")]
|
|
||||||
public Task<List<Shared.Models.SnRealm>> GetPublicRealms(
|
|
||||||
[FromQuery] string? query,
|
|
||||||
[FromQuery] int take = 10,
|
|
||||||
[FromQuery] int offset = 0
|
|
||||||
)
|
|
||||||
{
|
|
||||||
return discoveryService.GetCommunityRealmAsync(query, take, offset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using DysonNetwork.Shared.Registry;
|
|
||||||
|
|
||||||
namespace DysonNetwork.Sphere.Discovery;
|
|
||||||
|
|
||||||
public class DiscoveryService(RemoteRealmService remoteRealmService)
|
|
||||||
{
|
|
||||||
public async Task<List<Shared.Models.SnRealm>> GetCommunityRealmAsync(
|
|
||||||
string? query,
|
|
||||||
int take = 10,
|
|
||||||
int offset = 0,
|
|
||||||
bool random = false
|
|
||||||
)
|
|
||||||
{
|
|
||||||
var allRealms = await remoteRealmService.GetPublicRealms(random ? "random" : "popularity");
|
|
||||||
return allRealms.Where(r => r.IsCommunity).Skip(offset).Take(take).ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -46,10 +46,6 @@
|
|||||||
</Content>
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Discovery\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Update="Resources\SharedResource.resx">
|
<EmbeddedResource Update="Resources\SharedResource.resx">
|
||||||
<Generator>ResXFileCodeGenerator</Generator>
|
<Generator>ResXFileCodeGenerator</Generator>
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ using DysonNetwork.Shared.Cache;
|
|||||||
using DysonNetwork.Shared.Geometry;
|
using DysonNetwork.Shared.Geometry;
|
||||||
using DysonNetwork.Sphere.ActivityPub;
|
using DysonNetwork.Sphere.ActivityPub;
|
||||||
using DysonNetwork.Sphere.Autocompletion;
|
using DysonNetwork.Sphere.Autocompletion;
|
||||||
using DysonNetwork.Sphere.Discovery;
|
|
||||||
using DysonNetwork.Sphere.Localization;
|
using DysonNetwork.Sphere.Localization;
|
||||||
using DysonNetwork.Sphere.Poll;
|
using DysonNetwork.Sphere.Poll;
|
||||||
using DysonNetwork.Sphere.Post;
|
using DysonNetwork.Sphere.Post;
|
||||||
@@ -94,7 +93,6 @@ public static class ServiceCollectionExtensions
|
|||||||
services.AddScoped<PublisherSubscriptionService>();
|
services.AddScoped<PublisherSubscriptionService>();
|
||||||
services.AddScoped<TimelineService>();
|
services.AddScoped<TimelineService>();
|
||||||
services.AddScoped<PostService>();
|
services.AddScoped<PostService>();
|
||||||
services.AddScoped<DiscoveryService>();
|
|
||||||
services.AddScoped<PollService>();
|
services.AddScoped<PollService>();
|
||||||
services.AddScoped<StickerService>();
|
services.AddScoped<StickerService>();
|
||||||
services.AddScoped<AutocompletionService>();
|
services.AddScoped<AutocompletionService>();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using DysonNetwork.Shared.Models;
|
using DysonNetwork.Shared.Models;
|
||||||
using DysonNetwork.Shared.Proto;
|
using DysonNetwork.Shared.Proto;
|
||||||
using DysonNetwork.Shared.Registry;
|
using DysonNetwork.Shared.Registry;
|
||||||
using DysonNetwork.Sphere.Discovery;
|
|
||||||
using DysonNetwork.Sphere.Post;
|
using DysonNetwork.Sphere.Post;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
@@ -13,7 +12,6 @@ public class TimelineService(
|
|||||||
Publisher.PublisherService pub,
|
Publisher.PublisherService pub,
|
||||||
Post.PostService ps,
|
Post.PostService ps,
|
||||||
RemoteRealmService rs,
|
RemoteRealmService rs,
|
||||||
DiscoveryService ds,
|
|
||||||
AccountService.AccountServiceClient accounts,
|
AccountService.AccountServiceClient accounts,
|
||||||
RemoteWebArticleService webArticles
|
RemoteWebArticleService webArticles
|
||||||
)
|
)
|
||||||
@@ -194,7 +192,7 @@ public class TimelineService(
|
|||||||
|
|
||||||
private async Task<SnTimelineEvent?> GetRealmDiscoveryActivity(int count = 5)
|
private async Task<SnTimelineEvent?> GetRealmDiscoveryActivity(int count = 5)
|
||||||
{
|
{
|
||||||
var realms = await ds.GetCommunityRealmAsync(null, count, 0, true);
|
var realms = await rs.GetPublicRealms("random", count);
|
||||||
return realms.Count > 0
|
return realms.Count > 0
|
||||||
? new TimelineDiscoveryEvent(
|
? new TimelineDiscoveryEvent(
|
||||||
realms.Select(x => new DiscoveryItem("realm", x)).ToList()
|
realms.Select(x => new DiscoveryItem("realm", x)).ToList()
|
||||||
|
|||||||
Reference in New Issue
Block a user