FIXME SR is private

This commit is contained in:
Paul Schneider
2023-03-19 17:57:55 +00:00
parent dac93a6206
commit 8b607e2606
576 changed files with 76023 additions and 13743 deletions

View File

@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Yavsc.Abstract.Workflow;
using Yavsc.Models;
using Microsoft.Data.Entity;
namespace Yavsc.Services
{

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.Options;
using Yavsc;
using Yavsc.Models;
using Yavsc.Services;

View File

@ -1,24 +1,20 @@
// // EMailer.cs
using System.Text;
// // EMailer.cs
// /*
// paul 26/06/2018 12:18 20182018 6 26
// */
using System;
using Microsoft.AspNet.Razor;
using Yavsc.Templates;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Yavsc.Models;
using Yavsc.Services;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection;
using Yavsc.Abstract.Templates;
using Microsoft.AspNetCore.Identity;
using RazorEngine.Configuration;
namespace Yavsc.Lib
{
@ -26,16 +22,20 @@ namespace Yavsc.Lib
{
const string DefaultBaseClassName = "ATemplate";
const string DefaultBaseClass = nameof(UserOrientedTemplate);
const string DefaultNamespace = "CompiledRazorTemplates";
readonly RazorTemplateEngine razorEngine;
ISet<string> Namespaces = new System.Collections.Generic.HashSet<string> {
"System",
"Yavsc.Templates" ,
"Yavsc.Models",
"Yavsc.Models.Identity"};
readonly IStringLocalizer<EMailer> stringLocalizer;
readonly ApplicationDbContext dbContext;
readonly IEmailSender mailSender;
readonly RazorEngineHost host;
readonly ILogger logger;
public EMailer(ApplicationDbContext context, IEmailSender sender,
IStringLocalizer<EMailer> localizer,
public EMailer(ApplicationDbContext context, IEmailSender sender,
IStringLocalizer<EMailer> localizer,
ILoggerFactory loggerFactory)
{
stringLocalizer = localizer;
@ -43,24 +43,15 @@ namespace Yavsc.Lib
logger = loggerFactory.CreateLogger<EMailer>();
var language = new CSharpRazorCodeLanguage();
host = new RazorEngineHost(language)
var templateServiceConfig = new TemplateServiceConfiguration()
{
DefaultBaseClass = DefaultBaseClass,
DefaultClassName = DefaultBaseClassName,
DefaultNamespace = DefaultNamespace
BaseTemplateType = typeof(UserOrientedTemplate),
Language = RazorEngine.Language.CSharp,
Namespaces = Namespaces
};
host.NamespaceImports.Add("System");
host.NamespaceImports.Add("Yavsc.Templates");
host.NamespaceImports.Add("Yavsc.Models");
host.NamespaceImports.Add("Yavsc.Models.Identity");
host.NamespaceImports.Add("Microsoft.AspNet.Identity.EntityFramework");
host.InstrumentedSourceFilePath = ".";
host.StaticHelpers = true;
dbContext = context;
razorEngine = new RazorTemplateEngine(host);
}
public void SendMonthlyEmail(string templateCode, string baseclassName = DefaultBaseClassName)
@ -70,9 +61,11 @@ namespace Yavsc.Lib
string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value;
logger.LogInformation($"Generating {subtemp}[{className}]");
var templateInfo = dbContext.MailingTemplate.FirstOrDefault(t => t.Id == templateCode);
var templatekey = RazorEngine.Engine.Razor.GetKey(templateInfo.Id);
logger.LogInformation($"Using code: {templateCode}, subject: {subtemp} ");
logger.LogInformation("And body:\n" + templateInfo.Body);
@ -80,16 +73,20 @@ namespace Yavsc.Lib
{
// Generate code for the template
var razorResult = razorEngine.GenerateCode(reader, className, DefaultNamespace, DefaultNamespace + ".cs");
using (var rzcode = new MemoryStream())
{
using (var writter = new StreamWriter(rzcode))
{
RazorEngine.Engine.Razor.Run(templatekey, writter);
rzcode.Seek(0, SeekOrigin.Begin);
logger.LogInformation("Razor exited " + (razorResult.Success ? "Ok" : "Ko") + ".");
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(Encoding.Default.GetString(rzcode.ToArray()));
logger.LogInformation(razorResult.GeneratedCode);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(razorResult.GeneratedCode);
logger.LogInformation("CSharp parsed");
List<MetadataReference> references = new List<MetadataReference>();
foreach (var type in new Type[] {
logger.LogInformation("CSharp parsed");
List<MetadataReference> references = new List<MetadataReference>();
foreach (var type in new Type[] {
typeof(object),
typeof(Enumerable),
typeof(IdentityUser),
@ -98,78 +95,80 @@ namespace Yavsc.Lib
typeof(UserOrientedTemplate),
typeof(System.Threading.Tasks.TaskExtensions)
})
{
var location = type.Assembly.Location;
if (!string.IsNullOrWhiteSpace(location))
{
references.Add(
MetadataReference.CreateFromFile(location)
);
logger.LogInformation($"Assembly for {type.Name} found at {location}");
}
else logger.LogWarning($"Assembly Not found for {type.Name}");
}
logger.LogInformation("Compilation creation ...");
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithAllowUnsafe(true).WithOptimizationLevel(OptimizationLevel.Debug)
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu)
.WithUsings("Yavsc.Templates")
;
string assemblyName = DefaultNamespace;
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { syntaxTree },
references: references,
options: compilationOptions
);
using (var ms = new MemoryStream())
{
logger.LogInformation("Emitting result ...");
EmitResult result = compilation.Emit(ms);
foreach (Diagnostic diagnostic in result.Diagnostics.Where(diagnostic =>
diagnostic.Severity < DiagnosticSeverity.Error && !diagnostic.IsWarningAsError))
{
logger.LogWarning("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
logger.LogWarning("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
}
if (!result.Success)
{
logger.LogInformation(razorResult.GeneratedCode);
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
}
}
else
{
logger.LogInformation(razorResult.GeneratedCode);
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(ms.ToArray());
Type type = assembly.GetType(DefaultNamespace + "." + className);
var generatedtemplate = (UserOrientedTemplate)Activator.CreateInstance(type);
foreach (var user in dbContext.ApplicationUser.Where(
u => u.AllowMonthlyEmail
))
{
logger.LogInformation("Generation for " + user.UserName);
generatedtemplate.Init();
generatedtemplate.User = user;
generatedtemplate.ExecuteAsync();
logger.LogInformation(generatedtemplate.GeneratedText);
var location = type.Assembly.Location;
if (!string.IsNullOrWhiteSpace(location))
{
references.Add(
MetadataReference.CreateFromFile(location)
);
logger.LogInformation($"Assembly for {type.Name} found at {location}");
}
else logger.LogWarning($"Assembly Not found for {type.Name}");
}
logger.LogInformation("Compilation creation ...");
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithAllowUnsafe(true).WithOptimizationLevel(OptimizationLevel.Debug)
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary).WithPlatform(Platform.AnyCpu)
.WithUsings("Yavsc.Templates")
;
string assemblyName = "EMailSenderTemplate";
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { syntaxTree },
references: references,
options: compilationOptions
);
using (var ms = new MemoryStream())
{
logger.LogInformation("Emitting result ...");
EmitResult result = compilation.Emit(ms);
foreach (Diagnostic diagnostic in result.Diagnostics.Where(diagnostic =>
diagnostic.Severity < DiagnosticSeverity.Error && !diagnostic.IsWarningAsError))
{
logger.LogWarning("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
logger.LogWarning("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
}
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
logger.LogCritical("{0}: {1}", diagnostic.Id, diagnostic.Location.GetLineSpan());
}
}
else
{
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = Assembly.Load(ms.ToArray());
Type type = assembly.GetType(Namespaces + "." + className);
var generatedtemplate = (UserOrientedTemplate)Activator.CreateInstance(type);
foreach (var user in dbContext.ApplicationUser.Where(
u => u.AllowMonthlyEmail
))
{
logger.LogInformation("Generation for " + user.UserName);
generatedtemplate.Init();
generatedtemplate.User = user;
generatedtemplate.ExecuteAsync();
logger.LogInformation(generatedtemplate.GeneratedText);
}
}
}
}
}
}
}
}
}

View File

@ -1,14 +1,10 @@
using System;
using System.Linq;
using System.Security.Principal;
using System.Security.Claims;
using Yavsc.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using rules;
using Microsoft.Data.Entity;
using Microsoft.AspNet.FileProviders;
using Yavsc.Helpers;
using Yavsc.Models;
namespace Yavsc.Services
{

View File

@ -19,23 +19,14 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using System.Collections.Generic;
using System.Linq;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
namespace Yavsc.Services
{
using Yavsc.Models;
using Yavsc.Models.Calendar;
using Yavsc.Server.Helpers;
using Yavsc.ViewModels.Calendar;

View File

@ -1,7 +1,6 @@
using System;
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.AspNet.FileProviders;
using Microsoft.Extensions.FileProviders;
namespace Yavsc.Services
{

View File

@ -1,21 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.SignalR;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Logging;
using Yavsc.Helpers;
using Yavsc.Models;
using Yavsc.ViewModels.Streaming;
using Yavsc.Models.Messaging;
using Yavsc.Models.FileSystem;
using Newtonsoft.Json;
namespace Yavsc.Services

View File

@ -4,7 +4,7 @@ using System.Threading.Tasks;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.Options;
using MimeKit;
using Yavsc.Abstract.Manage;
@ -44,7 +44,7 @@ namespace Yavsc.Services
EmailSentViewModel model = new EmailSentViewModel{ EMail = email };
try
{
logger.LogInformation($"SendEmailAsync for {email}: {message}");
logger.LogInformation($"sendEmail for {email} : {message}");
MimeMessage msg = new MimeMessage();
msg.From.Add(new MailboxAddress(
siteSettings.Owner.Name,

View File

@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Yavsc.Interfaces.Workflow;
using Yavsc.Models;
@ -19,22 +14,23 @@ namespace Yavsc.Services
private readonly ILogger _logger;
readonly IEmailSender _emailSender;
readonly SiteSettings siteSettings;
private readonly IHubContext hubContext;
readonly ApplicationDbContext _dbContext;
readonly IConnexionManager _cxManager;
private readonly IHubContext<ChatHub> hubContext;
public YavscMessageSender(
ILoggerFactory loggerFactory,
IOptions<SiteSettings> sitesOptions,
IEmailSender emailSender,
ApplicationDbContext dbContext,
IConnexionManager cxManager
IConnexionManager cxManager,
IHubContext<ChatHub> hubContext
)
{
_logger = loggerFactory.CreateLogger<MailSender>();
_emailSender = emailSender;
siteSettings = sitesOptions?.Value;
hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
this.hubContext = hubContext;
_dbContext = dbContext;
_cxManager = cxManager;
}
@ -129,7 +125,7 @@ namespace Yavsc.Services
{
["event"] = JsonConvert.SerializeObject(ev)
};
hubClient.push(ev.Topic, JsonConvert.SerializeObject(data));
await hubClient.SendAsync("push", ev.Topic, JsonConvert.SerializeObject(data));
}
result.message_id = MimeKit.Utils.MimeUtils.GenerateMessageId(