mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Split search sync and search manager to split lifetimes
This commit is contained in:
parent
72628519a5
commit
8fb46099d4
@ -66,7 +66,7 @@ namespace Kyoo.Core
|
||||
builder.RegisterRepository<PeopleRepository>();
|
||||
builder.RegisterRepository<StudioRepository>();
|
||||
builder.RegisterRepository<UserRepository>();
|
||||
builder.RegisterType<NewsRepository>().OwnedByLifetimeScope();
|
||||
builder.RegisterType<NewsRepository>().InstancePerLifetimeScope();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
79
back/src/Kyoo.Meilisearch/MeiliSync.cs
Normal file
79
back/src/Kyoo.Meilisearch/MeiliSync.cs
Normal file
@ -0,0 +1,79 @@
|
||||
// Kyoo - A portable and vast media library solution.
|
||||
// Copyright (c) Kyoo.
|
||||
//
|
||||
// See AUTHORS.md and LICENSE file in the project root for full license information.
|
||||
//
|
||||
// Kyoo is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// Kyoo is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Dynamic;
|
||||
using System.Reflection;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Meilisearch;
|
||||
using static System.Text.Json.JsonNamingPolicy;
|
||||
|
||||
namespace Kyoo.Meiliseach;
|
||||
|
||||
public class MeiliSync
|
||||
{
|
||||
private readonly MeilisearchClient _client;
|
||||
|
||||
public MeiliSync(MeilisearchClient client)
|
||||
{
|
||||
_client = client;
|
||||
|
||||
IRepository<Movie>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Movie));
|
||||
IRepository<Movie>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Movie));
|
||||
IRepository<Movie>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Movie));
|
||||
IRepository<Show>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Show));
|
||||
IRepository<Show>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Show));
|
||||
IRepository<Show>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Show));
|
||||
IRepository<Collection>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Collection));
|
||||
IRepository<Collection>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Collection));
|
||||
IRepository<Collection>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Collection));
|
||||
|
||||
IRepository<Episode>.OnCreated += (x) => CreateOrUpdate(nameof(Episode), x);
|
||||
IRepository<Episode>.OnEdited += (x) => CreateOrUpdate(nameof(Episode), x);
|
||||
IRepository<Episode>.OnDeleted += (x) => _Delete(nameof(Episode), x.Id);
|
||||
|
||||
IRepository<Studio>.OnCreated += (x) => CreateOrUpdate(nameof(Studio), x);
|
||||
IRepository<Studio>.OnEdited += (x) => CreateOrUpdate(nameof(Studio), x);
|
||||
IRepository<Studio>.OnDeleted += (x) => _Delete(nameof(Studio), x.Id);
|
||||
}
|
||||
|
||||
public Task CreateOrUpdate(string index, IResource item, string? kind = null)
|
||||
{
|
||||
if (kind != null)
|
||||
{
|
||||
dynamic expando = new ExpandoObject();
|
||||
var dictionary = (IDictionary<string, object?>)expando;
|
||||
|
||||
foreach (PropertyInfo property in item.GetType().GetProperties())
|
||||
dictionary.Add(CamelCase.ConvertName(property.Name), property.GetValue(item));
|
||||
dictionary.Add("ref", $"{kind}-{item.Id}");
|
||||
expando.kind = kind;
|
||||
return _client.Index(index).AddDocumentsAsync(new[] { expando });
|
||||
}
|
||||
return _client.Index(index).AddDocumentsAsync(new[] { item });
|
||||
}
|
||||
|
||||
private Task _Delete(string index, int id, string? kind = null)
|
||||
{
|
||||
if (kind != null)
|
||||
{
|
||||
return _client.Index(index).DeleteOneDocumentAsync($"{kind}/{id}");
|
||||
}
|
||||
return _client.Index(index).DeleteOneDocumentAsync(id);
|
||||
}
|
||||
}
|
@ -155,7 +155,7 @@ namespace Kyoo.Meiliseach
|
||||
if (info.NumberOfDocuments == 0)
|
||||
{
|
||||
ILibraryManager database = provider.GetRequiredService<ILibraryManager>();
|
||||
SearchManager search = provider.GetRequiredService<SearchManager>();
|
||||
MeiliSync search = provider.GetRequiredService<MeiliSync>();
|
||||
|
||||
// This is a naive implementation that absolutly does not care about performances.
|
||||
// This will run only once on users that already had a database when they upgrade.
|
||||
@ -186,9 +186,9 @@ namespace Kyoo.Meiliseach
|
||||
_configuration.GetValue("MEILI_HOST", "http://meilisearch:7700"),
|
||||
_configuration.GetValue<string?>("MEILI_MASTER_KEY")
|
||||
)).SingleInstance();
|
||||
builder.RegisterType<SearchManager>().AsSelf().As<ISearchManager>()
|
||||
.SingleInstance()
|
||||
builder.RegisterType<MeiliSync>().AsSelf().SingleInstance()
|
||||
.AutoActivate();
|
||||
builder.RegisterType<SearchManager>().As<ISearchManager>().InstancePerLifetimeScope();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,6 @@
|
||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Dynamic;
|
||||
using System.Reflection;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
@ -50,49 +48,6 @@ public class SearchManager : ISearchManager
|
||||
{
|
||||
_client = client;
|
||||
_libraryManager = libraryManager;
|
||||
|
||||
IRepository<Movie>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Movie));
|
||||
IRepository<Movie>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Movie));
|
||||
IRepository<Movie>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Movie));
|
||||
IRepository<Show>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Show));
|
||||
IRepository<Show>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Show));
|
||||
IRepository<Show>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Show));
|
||||
IRepository<Collection>.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Collection));
|
||||
IRepository<Collection>.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Collection));
|
||||
IRepository<Collection>.OnDeleted += (x) => _Delete("items", x.Id, nameof(Collection));
|
||||
|
||||
IRepository<Episode>.OnCreated += (x) => CreateOrUpdate(nameof(Episode), x);
|
||||
IRepository<Episode>.OnEdited += (x) => CreateOrUpdate(nameof(Episode), x);
|
||||
IRepository<Episode>.OnDeleted += (x) => _Delete(nameof(Episode), x.Id);
|
||||
|
||||
IRepository<Studio>.OnCreated += (x) => CreateOrUpdate(nameof(Studio), x);
|
||||
IRepository<Studio>.OnEdited += (x) => CreateOrUpdate(nameof(Studio), x);
|
||||
IRepository<Studio>.OnDeleted += (x) => _Delete(nameof(Studio), x.Id);
|
||||
}
|
||||
|
||||
public Task CreateOrUpdate(string index, IResource item, string? kind = null)
|
||||
{
|
||||
if (kind != null)
|
||||
{
|
||||
dynamic expando = new ExpandoObject();
|
||||
var dictionary = (IDictionary<string, object?>)expando;
|
||||
|
||||
foreach (PropertyInfo property in item.GetType().GetProperties())
|
||||
dictionary.Add(CamelCase.ConvertName(property.Name), property.GetValue(item));
|
||||
dictionary.Add("ref", $"{kind}-{item.Id}");
|
||||
expando.kind = kind;
|
||||
return _client.Index(index).AddDocumentsAsync(new[] { expando });
|
||||
}
|
||||
return _client.Index(index).AddDocumentsAsync(new[] { item });
|
||||
}
|
||||
|
||||
private Task _Delete(string index, int id, string? kind = null)
|
||||
{
|
||||
if (kind != null)
|
||||
{
|
||||
return _client.Index(index).DeleteOneDocumentAsync($"{kind}/{id}");
|
||||
}
|
||||
return _client.Index(index).DeleteOneDocumentAsync(id);
|
||||
}
|
||||
|
||||
private async Task<SearchPage<T>.SearchResult> _Search<T>(string index, string? query,
|
||||
|
Loading…
x
Reference in New Issue
Block a user