Skip to content

Commit

Permalink
Merge pull request #48 from argon-chat/feature/fluid_and_email_forms
Browse files Browse the repository at this point in the history
Feature/fluid and email forms
  • Loading branch information
0xF6 authored Nov 16, 2024
2 parents 8916011 + 33fd695 commit 31bf9ca
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 74 deletions.
5 changes: 3 additions & 2 deletions src/Argon.Api/Argon.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1"/>
<PackageReference Include="Aspire.StackExchange.Redis" Version="9.0.0"/>
<PackageReference Include="Fluid.Core" Version="2.12.0"/>
<PackageReference Include="Flurl.Http" Version="4.0.2"/>
<PackageReference Include="Flurl.Http.Newtonsoft" Version="0.9.1"/>
<PackageReference Include="Genbox.SimpleS3.Core" Version="3.2.4"/>
Expand All @@ -23,7 +24,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10"/>
<PackageReference Include="ActualLab.Fusion" Version="9.5.59"/>
<PackageReference Include="ActualLab.Fusion" Version="9.5.64"/>
<PackageReference Include="Argon.Sfu.Protocol" Version="1.26.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10">
Expand All @@ -44,7 +45,7 @@
<PackageReference Include="R3" Version="1.2.9"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.9.0"/>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.0"/>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1"/>
</ItemGroup>

<ItemGroup>
Expand Down
55 changes: 0 additions & 55 deletions src/Argon.Api/Features/EmailForms/EMailFormStorage.cs

This file was deleted.

65 changes: 65 additions & 0 deletions src/Argon.Api/Features/Template/TemplateFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace Argon.Api.Features.Template;

using System.Collections.Concurrent;
using Fluid;

public static class TemplateFeature
{
public static IServiceCollection AddTemplateEngine(this WebApplicationBuilder builder)
{
builder.Services.AddSingleton<FluidParser>();
builder.Services.AddHostedService<EMailFormLoader>();
builder.Services.AddSingleton<EMailFormStorage>();
return builder.Services;
}
}

public class EMailFormStorage
{
private readonly ConcurrentDictionary<string, IFluidTemplate> htmlForms = new();

public void Load(string name, IFluidTemplate template) => htmlForms.TryAdd(name, template);

public IFluidTemplate GetContentFor(string formKey)
{
if (htmlForms.TryGetValue(formKey, out var form))
return form;
throw new InvalidOperationException($"No '{formKey}' form found");
}

public string Render(string formKey, Dictionary<string, string> values)
{
var template = GetContentFor(formKey);

var context = new TemplateContext();

foreach (var (key, value) in values)
context.SetValue(key, value);

return template.Render(context);
}
}

public class EMailFormLoader(EMailFormStorage storage, ILogger<EMailFormLoader> logger, FluidParser engine) : BackgroundService
{
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
var formFiles = Directory.EnumerateFiles("./Resources", "*.html").ToList();

logger.LogInformation("Found '{count}' email forms", formFiles.Count);

foreach (var file in formFiles)
{
var content = await File.ReadAllTextAsync(file, stoppingToken);
var name = Path.GetFileNameWithoutExtension(file);

if (engine.TryParse(content, out var result, out var error))
{
storage.Load(name, result);
logger.LogInformation("Loaded '{name}' email form", name);
}
else
logger.LogError("Failed load '{name}' email form, error: {error}", name, error);
}
}
}
4 changes: 2 additions & 2 deletions src/Argon.Api/Grains/EmailManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Argon.Api.Grains;

using System.Net;
using System.Net.Mail;
using Features.EmailForms;
using Features.Template;
using Interfaces;
using Microsoft.Extensions.Options;

Expand All @@ -26,7 +26,7 @@ public Task SendEmailAsync(string email, string subject, string message, string

public async Task SendOtpCodeAsync(string email, string otpCode, TimeSpan validity)
{
var form = formStorage.CompileAndGetForm("otp", new Dictionary<string, string>
var form = formStorage.Render("otp", new Dictionary<string, string>
{
{
"otp", otpCode
Expand Down
4 changes: 2 additions & 2 deletions src/Argon.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
using Argon.Api.Extensions;
using Argon.Api.Features;
using Argon.Api.Features.Captcha;
using Argon.Api.Features.EmailForms;
using Argon.Api.Features.Env;
using Argon.Api.Features.Jwt;
using Argon.Api.Features.MediaStorage;
using Argon.Api.Features.Otp;
using Argon.Api.Features.Pex;
using Argon.Api.Features.Template;
using Argon.Api.Grains.Interfaces;
using Argon.Api.Migrations;
using Argon.Api.Services;
Expand Down Expand Up @@ -49,7 +49,7 @@
builder.Services.AddSingleton<IFusionContext, FusionContext>();
builder.AddOtpCodes();
builder.AddOrleans();
builder.AddEMailForms();
builder.AddTemplateEngine();
builder.AddKubeResources();
builder.AddCaptchaFeature();
builder.Services.AddDataProtection();
Expand Down
84 changes: 84 additions & 0 deletions src/Argon.Api/Resources/new_session.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Login to Your Account</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #1a1a1a;
margin: 0;
padding: 0;
color: #e0e0e0;
}

.container {
max-width: 600px;
margin: 0 auto;
background-color: #2a2a2a;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

.header {
font-size: 24px;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
color: #bb86fc;
}

.message {
font-size: 16px;
color: #cccccc;
margin-bottom: 20px;
line-height: 1.5;
}

.button {
display: inline-block;
font-size: 18px;
font-weight: bold;
padding: 10px 20px;
color: #ffffff;
background-color: #6200ea;
border-radius: 4px;
text-decoration: none;
margin-bottom: 20px;
}

.footer {
font-size: 14px;
color: #888888;
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">Login to Argon</div>
<div class="message">
Hello,<br><br>
You have successfully logged in to your account. Your session details are as follows:
</div>
<div style="text-align: center;">
<div class="message">
IP: <strong>{{ user_ip }}</strong><br>
Region: <strong>{{ user_region }}</strong><br>
Host: <strong>{{ user_host }}</strong><br>
Login Time: <strong>{{ login_time }}</strong>
</div>
</div>
<div class="message">
If this login was not made by you, please contact our support team immediately.
</div>
<div class="footer">
Thank you,<br>
The Argon Team
</div>
</div>
</body>
</html>
31 changes: 22 additions & 9 deletions src/Argon.Api/Resources/otp.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,63 @@
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
background-color: #1a1a1a;
margin: 0;
padding: 0;
color: #e0e0e0;
}

.container {
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
background-color: #2a2a2a;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

.header {
font-size: 24px;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
color: #333333;
color: #bb86fc;
}

.message {
font-size: 16px;
color: #555555;
color: #cccccc;
margin-bottom: 20px;
line-height: 1.5;
}

.otp-code {
.otp-code {
display: inline-block;
font-size: 28px;
font-weight: bold;
padding: 10px 20px;
color: #ffffff;
background-color: #007bff;
background-color: #6200ea;
border-radius: 4px;
text-decoration: none;
margin-bottom: 20px;
}

.button {
display: inline-block;
font-size: 18px;
font-weight: bold;
padding: 10px 20px;
color: #ffffff;
background-color: #6200ea;
border-radius: 4px;
text-decoration: none;
margin-bottom: 20px;
}

.footer {
font-size: 14px;
color: #777777;
color: #888888;
text-align: center;
margin-top: 20px;
}
Expand Down
Loading

0 comments on commit 31bf9ca

Please sign in to comment.