123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using InABox.Clients;
- using InABox.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InABox.Core
- {
- public class RepostedException : Exception
- {
- public RepostedException(): base("Cannot process an item twice.")
- {
- }
- }
- public interface IPosterEngine<TPostable>
- where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
- {
- bool Process(IEnumerable<TPostable> posts);
- }
- public interface IPosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable>
- where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
- where TPoster : IPoster<TPostable, TSettings>
- where TSettings : PosterSettings, new()
- {
- }
- public abstract class PosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable, TPoster, TSettings>
- where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
- where TPoster : IPoster<TPostable, TSettings>
- where TSettings : PosterSettings, new()
- {
- protected static TPoster Poster = GetPoster();
- private static Type[]? _posters;
- private static TPoster GetPoster()
- {
- _posters ??= CoreUtils.TypeList(
- AppDomain.CurrentDomain.GetAssemblies(),
- x => x.IsClass
- && !x.IsAbstract
- && !x.IsGenericType
- && x.HasInterface(typeof(IPoster<,>))
- ).ToArray();
- var type = _posters.Where(x => typeof(TPoster).IsAssignableFrom(x)).FirstOrDefault()
- ?? throw new Exception($"No poster of type {typeof(TPoster)}.");
- return (TPoster)Activator.CreateInstance(type);
- }
- protected static TSettings GetSettings()
- {
- return PosterUtils.LoadPosterSettings<TPostable, TSettings>();
- }
- protected static void SaveSettings(TSettings settings)
- {
- PosterUtils.SavePosterSettings<TPostable, TSettings>(settings);
- }
- /// <summary>
- /// Returns the <see cref="TSettings.Script"/>, if <see cref="TSettings.ScriptEnabled"/> is <see langword="true"/>;
- /// otherwise, returns <see langword="null"/>.
- /// </summary>
- protected static string? GetScript()
- {
- var settings = GetSettings();
- return settings.ScriptEnabled ? settings.Script : null;
- }
- protected abstract bool DoProcess(IEnumerable<TPostable> posts);
- public bool Process(IEnumerable<TPostable> posts)
- {
- var list = posts.ToList();
- if(list.Any(x => x.PostedStatus == PostedStatus.Posted))
- {
- throw new RepostedException();
- }
- try
- {
- var success = DoProcess(list);
- if (success)
- {
- foreach (var post in list)
- {
- post.Posted = DateTime.Now;
- post.PostedStatus = PostedStatus.Posted;
- }
- new Client<TPostable>().Save(list, "Posted by user.");
- }
- else
- {
- foreach (var post in list)
- {
- post.PostedStatus = PostedStatus.PostFailed;
- }
- new Client<TPostable>().Save(list, "Post failed by user.");
- }
- return success;
- }
- catch(Exception e)
- {
- Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
- foreach (var post in list)
- {
- post.PostedStatus = PostedStatus.PostFailed;
- }
- new Client<TPostable>().Save(list, "Post failed by user.");
- throw;
- }
- }
- }
- }
|