// 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.Text; using System.Text.Json; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; using RabbitMQ.Client; namespace Kyoo.RabbitMq; public class RabbitProducer { private readonly IModel _channel; public RabbitProducer(IConnection rabbitConnection) { _channel = rabbitConnection.CreateModel(); _channel.ExchangeDeclare(exchange: "events.resource.collection", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.collection", "created"); IRepository.OnEdited += _Publish("events.resource.collection", "edited"); IRepository.OnDeleted += _Publish("events.resource.collection", "deleted"); _channel.ExchangeDeclare(exchange: "events.resource.movie", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.movie", "created"); IRepository.OnEdited += _Publish("events.resource.movie", "edited"); IRepository.OnDeleted += _Publish("events.resource.movie", "deleted"); _channel.ExchangeDeclare(exchange: "events.resource.show", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.show", "created"); IRepository.OnEdited += _Publish("events.resource.show", "edited"); IRepository.OnDeleted += _Publish("events.resource.show", "deleted"); _channel.ExchangeDeclare(exchange: "events.resource.season", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.season", "created"); IRepository.OnEdited += _Publish("events.resource.season", "edited"); IRepository.OnDeleted += _Publish("events.resource.season", "deleted"); _channel.ExchangeDeclare(exchange: "events.resource.episode", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.episode", "created"); IRepository.OnEdited += _Publish("events.resource.episode", "edited"); IRepository.OnDeleted += _Publish("events.resource.episode", "deleted"); _channel.ExchangeDeclare(exchange: "events.resource.studio", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.studio", "created"); IRepository.OnEdited += _Publish("events.resource.studio", "edited"); IRepository.OnDeleted += _Publish("events.resource.studio", "deleted"); _channel.ExchangeDeclare(exchange: "events.resource.user", type: ExchangeType.Fanout); IRepository.OnCreated += _Publish("events.resource.user", "created"); IRepository.OnEdited += _Publish("events.resource.user", "edited"); IRepository.OnDeleted += _Publish("events.resource.user", "deleted"); } private IRepository.ResourceEventHandler _Publish(string exchange, string action) where T : IResource, IQuery { return (T resource) => { var message = new { Action = action, Type = typeof(T).Name.ToLowerInvariant(), Value = resource, }; _channel.BasicPublish( exchange, routingKey: string.Empty, body: Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message)) ); return Task.CompletedTask; }; } }