diff --git a/back/src/Kyoo.Core/CoreModule.cs b/back/src/Kyoo.Core/CoreModule.cs index fc9bda22..1c938909 100644 --- a/back/src/Kyoo.Core/CoreModule.cs +++ b/back/src/Kyoo.Core/CoreModule.cs @@ -66,7 +66,7 @@ namespace Kyoo.Core builder.RegisterRepository(); builder.RegisterRepository(); builder.RegisterRepository(); - builder.RegisterType().OwnedByLifetimeScope(); + builder.RegisterType().InstancePerLifetimeScope(); } /// diff --git a/back/src/Kyoo.Meilisearch/MeiliSync.cs b/back/src/Kyoo.Meilisearch/MeiliSync.cs new file mode 100644 index 00000000..6b121122 --- /dev/null +++ b/back/src/Kyoo.Meilisearch/MeiliSync.cs @@ -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 . + +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.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Movie)); + IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Movie)); + IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Movie)); + IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Show)); + IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Show)); + IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Show)); + IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Collection)); + IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Collection)); + IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Collection)); + + IRepository.OnCreated += (x) => CreateOrUpdate(nameof(Episode), x); + IRepository.OnEdited += (x) => CreateOrUpdate(nameof(Episode), x); + IRepository.OnDeleted += (x) => _Delete(nameof(Episode), x.Id); + + IRepository.OnCreated += (x) => CreateOrUpdate(nameof(Studio), x); + IRepository.OnEdited += (x) => CreateOrUpdate(nameof(Studio), x); + IRepository.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)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); + } +} diff --git a/back/src/Kyoo.Meilisearch/MeilisearchModule.cs b/back/src/Kyoo.Meilisearch/MeilisearchModule.cs index 1228b002..589fa55f 100644 --- a/back/src/Kyoo.Meilisearch/MeilisearchModule.cs +++ b/back/src/Kyoo.Meilisearch/MeilisearchModule.cs @@ -155,7 +155,7 @@ namespace Kyoo.Meiliseach if (info.NumberOfDocuments == 0) { ILibraryManager database = provider.GetRequiredService(); - SearchManager search = provider.GetRequiredService(); + MeiliSync search = provider.GetRequiredService(); // 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("MEILI_MASTER_KEY") )).SingleInstance(); - builder.RegisterType().AsSelf().As() - .SingleInstance() + builder.RegisterType().AsSelf().SingleInstance() .AutoActivate(); + builder.RegisterType().As().InstancePerLifetimeScope(); } } } diff --git a/back/src/Kyoo.Meilisearch/SearchManager.cs b/back/src/Kyoo.Meilisearch/SearchManager.cs index 23e0fb37..a3ec477d 100644 --- a/back/src/Kyoo.Meilisearch/SearchManager.cs +++ b/back/src/Kyoo.Meilisearch/SearchManager.cs @@ -17,8 +17,6 @@ // along with Kyoo. If not, see . 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.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Movie)); - IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Movie)); - IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Movie)); - IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Show)); - IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Show)); - IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Show)); - IRepository.OnCreated += (x) => CreateOrUpdate("items", x, nameof(Collection)); - IRepository.OnEdited += (x) => CreateOrUpdate("items", x, nameof(Collection)); - IRepository.OnDeleted += (x) => _Delete("items", x.Id, nameof(Collection)); - - IRepository.OnCreated += (x) => CreateOrUpdate(nameof(Episode), x); - IRepository.OnEdited += (x) => CreateOrUpdate(nameof(Episode), x); - IRepository.OnDeleted += (x) => _Delete(nameof(Episode), x.Id); - - IRepository.OnCreated += (x) => CreateOrUpdate(nameof(Studio), x); - IRepository.OnEdited += (x) => CreateOrUpdate(nameof(Studio), x); - IRepository.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)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.SearchResult> _Search(string index, string? query,