Skip to content

Commit cb10f12

Browse files
authored
Merge pull request #291 from sixwaaaay/fixpatch
fix: fix recommendation for anonymous
2 parents 40f67cc + 917ab43 commit cb10f12

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

sharp/content/domainservice/HistoryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<bool> AddHistory(long userId, long videoId)
2727
public async Task<Pagination<VideoDto>> Recommendation(long userId, ulong page = 0, ulong size = 10)
2828
{
2929
var historyList = await history.GetHistorys(userId, 0, 0);
30-
var positiveVectors = historyList.Count != 0 ? new ReadOnlyMemory<Vector>(historyList.Select(x => new Vector(Enumerable.Range(1, 1024).Select(_ => (float)random.NextDouble()).ToArray())).ToArray()) : null;
30+
var positiveVectors = historyList.Count == 0 ? new Vector[] { new(Enumerable.Range(1, 1024).Select(_ => (float)random.NextDouble()).ToArray()) } : null;
3131
var result = await client.RecommendAsync("videos", positive: historyList.Select(x => (ulong)x).ToList(), /* history */
3232
positiveVectors: positiveVectors, /* if empty positive point */
3333
limit: size, offset: page * size

sharp/content/endpoints/Endpoints.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,15 @@ public static Task AddHistory(HistoryService service,ClaimsPrincipal user , AddV
106106
public static void MapEndpoints(this IEndpointRouteBuilder endpoints)
107107
{
108108
endpoints.MapGet("/users/{userId:long}/videos", UserVideos).WithName("getUserVideos");
109-
// endpoints.MapGet("/users/{userId:long}/likes", Likes).RequireAuthorization().WithName("getUserLikes");
110-
endpoints.MapGet("/users/{userId:long}/likes", Likes).WithName("getUserLikes");
109+
endpoints.MapGet("/users/{userId:long}/likes", Likes).RequireAuthorization().WithName("getUserLikes");
111110
endpoints.MapGet("/videos", Videos).WithName("getVideos");
112-
// todo: user id
113111
endpoints.MapGet("/videos/recommend", RecommendVideos).WithName("getRecommendVideos");
114112
endpoints.MapGet("/videos/{id:long}", FindVideoById).WithName("getVideo");
115113
endpoints.MapGet("/videos/{id:long}/similar", SimilarVideos).WithName("getSimilarVideos");
116114
endpoints.MapPost("/videos/popular", DailyPopularVideos).WithName("getDailyPopularVideos");
117115
endpoints.MapPost("/videos", CreateVideo).RequireAuthorization().WithName("createVideo");
118116
endpoints.MapPost("/videos/history", AddHistory).RequireAuthorization().WithName("addVideoHistory");
119117
endpoints.MapGet("/videos/history", HistoryVideos).RequireAuthorization().WithName("getVideoHistory");
120-
121118
}
122119

123120

sharp/content/repository/Client.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ public async Task<IReadOnlyList<long>> VotedOfVideos(List<long> videoIds)
129129
/// <summary> Scan voted videos, which means paging through all voted videos. </summary>
130130
public async Task<(long?, IReadOnlyList<long>)> VotedVideos(long page, int size)
131131
{
132-
using var req = new HttpRequestMessage(HttpMethod.Get, $"/graph/videos?page={page}&size={size}");
132+
var url = $"/graph/videos?page={page}&size={size}";
133+
using var req = new HttpRequestMessage(HttpMethod.Get, url);
133134
if (!string.IsNullOrEmpty(Token) && AuthenticationHeaderValue.TryParse(Token, out var auth))
134135
{
135136
req.Headers.Authorization = auth;
@@ -212,8 +213,8 @@ public static string GetConnString(this IServiceProvider sp, string name)
212213
=> sp.GetRequiredService<IConfiguration>().ConnString(name);
213214

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

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

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

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

0 commit comments

Comments
 (0)