Store notification delivery settings in database
This commit is contained in:
1 parent
ce8e7021a9
commit
0231c468a4
14 files changed
+13255
-13
No files matched your search
+2
-1
@@ -87,7 +87,8 @@ Operations__MySqlClientPath=mysql
|
||||
# Operations__MySqlAdditionalArguments__0=--ssl-mode=VERIFY_IDENTITY
|
||||
# Operations__MySqlAdditionalArguments__1=--ssl-ca=/etc/jiaowu/mysql-ca.pem
|
||||
|
||||
# 外部通知默认关闭;站内消息不受这些配置影响。启用邮件前应使用专用 SMTP 账号。
|
||||
# 外部通知默认关闭;站内消息不受影响。邮件启用状态、发件人和重试次数首次启动后在后台管理。
|
||||
# SMTP 主机及凭据仍由环境配置;启用邮件前应使用专用 SMTP 账号。
|
||||
NotificationDelivery__Email__Enabled=false
|
||||
# NotificationDelivery__Email__Host=smtp.example.edu.cn
|
||||
# NotificationDelivery__Email__Port=587
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Net.Mail;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Notifications;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Jiaowu.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize(Roles = SystemRoles.SuperAdmin)]
|
||||
[Route("api/notification-settings")]
|
||||
public sealed class NotificationSettingsController(AppDbContext db, NotificationSettings settings) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get(CancellationToken ct) => Ok(await settings.ReadAsync(ct));
|
||||
|
||||
[HttpPut]
|
||||
public async Task<IActionResult> Put(NotificationSettingsRequest request, CancellationToken ct)
|
||||
{
|
||||
var address = request.EmailFromAddress?.Trim() ?? "";
|
||||
var name = request.EmailFromName?.Trim() ?? "";
|
||||
if (request.EmailEnabled && (!MailAddress.TryCreate(address, out _) || name.Length == 0 ||
|
||||
string.IsNullOrWhiteSpace(settings.Defaults.Email.Host) || settings.Defaults.Email.Port is < 1 or > 65535))
|
||||
return BadRequest(new { message = "启用邮件前请配置 SMTP 主机、端口及有效的发件地址和名称。" });
|
||||
if (address.Length > 0 && !MailAddress.TryCreate(address, out _))
|
||||
return BadRequest(new { message = "发件地址无效。" });
|
||||
var item = await db.NotificationSystemSettings.SingleOrDefaultAsync(x => x.Id == NotificationSettings.SettingId, ct);
|
||||
if (item is null) { item = settings.CreateDefault(); db.NotificationSystemSettings.Add(item); }
|
||||
item.EmailEnabled = request.EmailEnabled;
|
||||
item.EmailFromAddress = address;
|
||||
item.EmailFromName = name;
|
||||
item.MaxAttempts = request.MaxAttempts;
|
||||
await db.SaveChangesAsync(ct);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record NotificationSettingsRequest(bool EmailEnabled,
|
||||
[StringLength(320)] string? EmailFromAddress,
|
||||
[StringLength(100)] string? EmailFromName,
|
||||
[Range(1, 20)] int MaxAttempts);
|
||||
@@ -143,6 +143,9 @@ public sealed class NotificationsController(
|
||||
.OrderByDescending(x => x.LastSeenAt)
|
||||
.Select(x => new { x.Id, x.UserAgent, x.LastSeenAt, x.ExpiresAt })
|
||||
.ToListAsync(cancellationToken);
|
||||
var emailProviderEnabled = await db.NotificationSystemSettings.AsNoTracking()
|
||||
.Where(x => x.Id == NotificationSettings.SettingId).Select(x => (bool?)x.EmailEnabled)
|
||||
.SingleOrDefaultAsync(cancellationToken) ?? deliverySettings.Email.Enabled;
|
||||
return Ok(new
|
||||
{
|
||||
EmailEnabled = preference?.EmailEnabled ?? false,
|
||||
@@ -153,7 +156,7 @@ public sealed class NotificationsController(
|
||||
TimeZoneId = preference?.TimeZoneId ?? "Asia/Shanghai",
|
||||
user.Email,
|
||||
user.EmailConfirmed,
|
||||
EmailProviderEnabled = deliverySettings.Email.Enabled,
|
||||
EmailProviderEnabled = emailProviderEnabled,
|
||||
WebPushProviderEnabled = deliverySettings.WebPush.Enabled,
|
||||
WebPushPublicKey = deliverySettings.WebPush.Enabled
|
||||
? deliverySettings.WebPush.PublicKey
|
||||
|
||||
Loaded 3 of 14 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user