Skip to content

Commit

Permalink
Merge pull request #291 from sixwaaaay/fixpatch
Browse files Browse the repository at this point in the history
fix: fix recommendation for anonymous
  • Loading branch information
sixwaaaay authored Feb 28, 2025
2 parents 40f67cc + 917ab43 commit cb10f12
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sharp/content/domainservice/HistoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<bool> AddHistory(long userId, long videoId)
public async Task<Pagination<VideoDto>> Recommendation(long userId, ulong page = 0, ulong size = 10)
{
var historyList = await history.GetHistorys(userId, 0, 0);
var positiveVectors = historyList.Count != 0 ? new ReadOnlyMemory<Vector>(historyList.Select(x => new Vector(Enumerable.Range(1, 1024).Select(_ => (float)random.NextDouble()).ToArray())).ToArray()) : null;
var positiveVectors = historyList.Count == 0 ? new Vector[] { new(Enumerable.Range(1, 1024).Select(_ => (float)random.NextDouble()).ToArray()) } : null;
var result = await client.RecommendAsync("videos", positive: historyList.Select(x => (ulong)x).ToList(), /* history */
positiveVectors: positiveVectors, /* if empty positive point */
limit: size, offset: page * size
Expand Down
5 changes: 1 addition & 4 deletions sharp/content/endpoints/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,15 @@ public static Task AddHistory(HistoryService service,ClaimsPrincipal user , AddV
public static void MapEndpoints(this IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/users/{userId:long}/videos", UserVideos).WithName("getUserVideos");
// endpoints.MapGet("/users/{userId:long}/likes", Likes).RequireAuthorization().WithName("getUserLikes");
endpoints.MapGet("/users/{userId:long}/likes", Likes).WithName("getUserLikes");
endpoints.MapGet("/users/{userId:long}/likes", Likes).RequireAuthorization().WithName("getUserLikes");
endpoints.MapGet("/videos", Videos).WithName("getVideos");
// todo: user id
endpoints.MapGet("/videos/recommend", RecommendVideos).WithName("getRecommendVideos");
endpoints.MapGet("/videos/{id:long}", FindVideoById).WithName("getVideo");
endpoints.MapGet("/videos/{id:long}/similar", SimilarVideos).WithName("getSimilarVideos");
endpoints.MapPost("/videos/popular", DailyPopularVideos).WithName("getDailyPopularVideos");
endpoints.MapPost("/videos", CreateVideo).RequireAuthorization().WithName("createVideo");
endpoints.MapPost("/videos/history", AddHistory).RequireAuthorization().WithName("addVideoHistory");
endpoints.MapGet("/videos/history", HistoryVideos).RequireAuthorization().WithName("getVideoHistory");

}


Expand Down
13 changes: 7 additions & 6 deletions sharp/content/repository/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public async Task<IReadOnlyList<long>> VotedOfVideos(List<long> videoIds)
/// <summary> Scan voted videos, which means paging through all voted videos. </summary>
public async Task<(long?, IReadOnlyList<long>)> VotedVideos(long page, int size)
{
using var req = new HttpRequestMessage(HttpMethod.Get, $"/graph/videos?page={page}&size={size}");
var url = $"/graph/videos?page={page}&size={size}";
using var req = new HttpRequestMessage(HttpMethod.Get, url);
if (!string.IsNullOrEmpty(Token) && AuthenticationHeaderValue.TryParse(Token, out var auth))
{
req.Headers.Authorization = auth;
Expand Down Expand Up @@ -212,8 +213,8 @@ public static string GetConnString(this IServiceProvider sp, string name)
=> sp.GetRequiredService<IConfiguration>().ConnString(name);

public static IServiceCollection AddVoteRepository(this IServiceCollection services) => services
.AddHttpClient<IVoteRepository, VoteRepository>("Vote",
(sp, client) => client.BaseAddress = new Uri(sp.GetConnString("Vote"))).Services;
.AddScoped<IVoteRepository, VoteRepository>(sp => new VoteRepository(sp.GetRequiredService<IHttpClientFactory>().CreateClient("Vote")))
.AddHttpClient("Vote", (sp, client) => client.BaseAddress = new Uri(sp.GetConnString("Vote"))).AddAsKeyed(ServiceLifetime.Scoped).Services;

public static IServiceCollection AddSearchClient(this IServiceCollection services) => services
.AddSingleton<IConnectionMultiplexer>(sp => ConnectionMultiplexer.Connect(sp.GetRequiredService<IConfiguration>().GetConnectionString("Redis").EnsureNotNull("Redis connection string is null")))
Expand All @@ -225,9 +226,9 @@ public static IServiceCollection AddSearchClient(this IServiceCollection service
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}).Services;

public static IServiceCollection AddUserRepository(this IServiceCollection services) =>
services.AddHttpClient<IUserRepository, UserRepository>("User",
(sp, client) => client.BaseAddress = new Uri(sp.GetConnString("User"))).Services;
public static IServiceCollection AddUserRepository(this IServiceCollection services) => services.
AddScoped<IUserRepository, UserRepository>(sp => new UserRepository(sp.GetRequiredService<IHttpClientFactory>().CreateClient("User"))).
AddHttpClient("User", (sp, client) => client.BaseAddress = new Uri(sp.GetConnString("User"))).Services;

public static IApplicationBuilder UseToken(this IApplicationBuilder app) =>
app.Use(async (context, next) =>
Expand Down

0 comments on commit cb10f12

Please sign in to comment.