浏览代码

Fixed PosterEngine postedstatus

Kenric Nugteren 1 年之前
父节点
当前提交
991bbde37a
共有 3 个文件被更改,包括 45 次插入7 次删除
  1. 27 1
      InABox.Core/CoreUtils.cs
  2. 1 0
      InABox.Core/Postable/IPoster.cs
  3. 17 6
      InABox.Core/Postable/PosterEngine.cs

+ 27 - 1
InABox.Core/CoreUtils.cs

@@ -2627,7 +2627,7 @@ namespace InABox.Core
             return string.Format("{0:X8}", hash);
         }
 
-        public static string StripHTML(this string html)
+        public static string StripHTML(this string? html)
         {
             if (string.IsNullOrWhiteSpace(html))
                 return "";
@@ -2703,6 +2703,32 @@ namespace InABox.Core
             return enumerable.ToList<T>();
         }
 
+        public static IEnumerable<T> AnyOr<T>(this IEnumerable<T> enumerable, IEnumerable<T> or)
+        {
+            var any = false;
+            foreach(var item in enumerable)
+            {
+                any = true;
+                yield return item;
+            }
+            if (!any)
+            {
+                foreach(var item in or)
+                {
+                    yield return item;
+                }
+            }
+        }
+
+        public static IEnumerable<T> One<T>(T item)
+        {
+            yield return item;
+        }
+        public static IEnumerable<T> One<T>(Func<T> fnc)
+        {
+            yield return fnc();
+        }
+
         #endregion
 
     }

+ 1 - 0
InABox.Core/Postable/IPoster.cs

@@ -16,5 +16,6 @@ namespace InABox.Core
         where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
         where TSettings : PosterSettings
     {
+        TSettings Settings { get; set; }
     }
 }

+ 17 - 6
InABox.Core/Postable/PosterEngine.cs

@@ -69,24 +69,29 @@ namespace InABox.Core
 
         protected virtual TPoster CreatePoster()
         {
-            return (Activator.CreateInstance(PosterType!) as TPoster)!;
+            var poster = (Activator.CreateInstance(PosterType!) as TPoster)!;
+            poster.Settings = GetSettings();
+            return poster;
         }
 
-        protected static TSettings GetSettings()
+        private TSettings _settings;
+        protected TSettings GetSettings()
         {
-            return PosterUtils.LoadPosterSettings<TPostable, TSettings>();
+            _settings ??= PosterUtils.LoadPosterSettings<TPostable, TSettings>();
+            return _settings;
         }
 
-        protected static void SaveSettings(TSettings settings)
+        protected void SaveSettings(TSettings settings)
         {
-            PosterUtils.SavePosterSettings<TPostable, TSettings>(settings);
+            _settings = 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()
+        protected string? GetScript()
         {
             var settings = GetSettings();
             return settings.ScriptEnabled ? settings.Script : null;
@@ -135,6 +140,12 @@ namespace InABox.Core
             {
                 return false;
             }
+
+            // Add posted flags; if the columns is null, then we don't need to worry, because it will be loaded.
+            model.GetColumns<TPostable>()?.Add(x => x.PostedStatus)
+                .Add(x => x.Posted)
+                .Add(x => x.PostedNote);
+
             model.LoadModel();
 
             var data = model.GetTable<TPostable>();