Skip to content

Commit

Permalink
Merge pull request #1065 from sbwalker/dev
Browse files Browse the repository at this point in the history
notification improvements
  • Loading branch information
sbwalker authored Jan 18, 2021
2 parents 350d2ce + 82a118b commit 892d9c1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 94 deletions.
20 changes: 3 additions & 17 deletions Oqtane.Client/Modules/Admin/UserProfile/Add.razor
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,12 @@

private async Task Send()
{
var notification = new Notification();
try
{
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
if (user != null)
{
notification.SiteId = PageState.Site.SiteId;
notification.FromUserId = PageState.User.UserId;
notification.FromDisplayName = PageState.User.DisplayName;
notification.FromEmail = PageState.User.Email;
notification.ToUserId = user.UserId;
notification.ToDisplayName = user.DisplayName;
notification.ToEmail = user.Email;
notification.Subject = subject;
notification.Body = body;
notification.ParentId = null;
notification.CreatedOn = DateTime.UtcNow;
notification.IsDelivered = false;
notification.DeliveredOn = null;
notification.SendOn = DateTime.UtcNow;
{
var notification = new Notification(PageState.Site.SiteId, PageState.User, user, subject, body, null);
notification = await NotificationService.AddNotificationAsync(notification);
await logger.LogInformation("Notification Created {Notification}", notification);
NavigationManager.NavigateTo(NavigateUrl());
Expand All @@ -79,7 +65,7 @@
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Adding Notification {Notification} {Error}", notification, ex.Message);
await logger.LogError(ex, "Error Adding Notification {Error}", ex.Message);
AddModuleMessage(Localizer["Error Adding Notification"], MessageType.Error);
}
}
Expand Down
20 changes: 3 additions & 17 deletions Oqtane.Client/Modules/Admin/UserProfile/View.razor
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,12 @@

private async Task Send()
{
var notification = new Notification();
try
{
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
if (user != null)
{
notification.SiteId = PageState.Site.SiteId;
notification.FromUserId = PageState.User.UserId;
notification.FromDisplayName = PageState.User.DisplayName;
notification.FromEmail = PageState.User.Email;
notification.ToUserId = user.UserId;
notification.ToDisplayName = user.DisplayName;
notification.ToEmail = user.Email;
notification.Subject = subject;
notification.Body = body;
notification.ParentId = notificationid;
notification.CreatedOn = DateTime.UtcNow;
notification.IsDelivered = false;
notification.DeliveredOn = null;
notification.SendOn = DateTime.UtcNow;
{
var notification = new Notification(PageState.Site.SiteId, PageState.User, user, subject, body, notificationid);
notification = await NotificationService.AddNotificationAsync(notification);
await logger.LogInformation("Notification Created {Notification}", notification);
NavigationManager.NavigateTo(NavigateUrl());
Expand All @@ -207,7 +193,7 @@
}
catch (Exception ex)
{
await logger.LogError(ex, "Error Adding Notification {Notification} {Error}", notification, ex.Message);
await logger.LogError(ex, "Error Adding Notification {Error}", ex.Message);
AddModuleMessage(Localizer["Error Adding Notification"], MessageType.Error);
}
}
Expand Down
30 changes: 5 additions & 25 deletions Oqtane.Server/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -146,20 +146,10 @@ private async Task<User> CreateUser(User user)
newUser = _users.AddUser(user);
if (!verified)
{
Notification notification = new Notification();
notification.SiteId = user.SiteId;
notification.FromUserId = null;
notification.ToUserId = newUser.UserId;
notification.ToEmail = newUser.Email;
notification.Subject = "User Account Verification";
string token = await _identityUserManager.GenerateEmailConfirmationTokenAsync(identityuser);
string url = HttpContext.Request.Scheme + "://" + _tenants.GetAlias().Name + "/login?name=" + user.Username + "&token=" + WebUtility.UrlEncode(token);
notification.Body = "Dear " + user.DisplayName + ",\n\nIn Order To Complete The Registration Of Your User Account Please Click The Link Displayed Below:\n\n" + url + "\n\nThank You!";
notification.ParentId = null;
notification.CreatedOn = DateTime.UtcNow;
notification.IsDelivered = false;
notification.DeliveredOn = null;
notification.SendOn = DateTime.UtcNow;
string body = "Dear " + user.DisplayName + ",\n\nIn Order To Complete The Registration Of Your User Account Please Click The Link Displayed Below:\n\n" + url + "\n\nThank You!";
var notification = new Notification(user.SiteId, null, newUser, "User Account Verification", body, null);
_notifications.AddNotification(notification);
}

Expand Down Expand Up @@ -379,20 +369,10 @@ public async Task Forgot([FromBody] User user)
IdentityUser identityuser = await _identityUserManager.FindByNameAsync(user.Username);
if (identityuser != null)
{
Notification notification = new Notification();
notification.SiteId = user.SiteId;
notification.FromUserId = null;
notification.ToUserId = user.UserId;
notification.ToEmail = "";
notification.Subject = "User Password Reset";
string token = await _identityUserManager.GeneratePasswordResetTokenAsync(identityuser);
string url = HttpContext.Request.Scheme + "://" + _tenants.GetAlias().Name + "/reset?name=" + user.Username + "&token=" + WebUtility.UrlEncode(token);
notification.Body = "Dear " + user.DisplayName + ",\n\nPlease Click The Link Displayed Below To Reset Your Password:\n\n" + url + "\n\nThank You!";
notification.ParentId = null;
notification.CreatedOn = DateTime.UtcNow;
notification.IsDelivered = false;
notification.DeliveredOn = null;
notification.SendOn = DateTime.UtcNow;
string body = "Dear " + user.DisplayName + ",\n\nPlease Click The Link Displayed Below To Reset Your Password:\n\n" + url + "\n\nThank You!";
var notification = new Notification(user.SiteId, null, user, "User Password Reset", body, null);
_notifications.AddNotification(notification);
_logger.Log(LogLevel.Information, this, LogFunction.Security, "Password Reset Notification Sent For {Username}", user.Username);
}
Expand Down
96 changes: 64 additions & 32 deletions Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ public NotificationJob(IServiceScopeFactory serviceScopeFactory) : base(serviceS
IsEnabled = false;
}

// job is executed for each tenant in installation
public override string ExecuteJob(IServiceProvider provider)
{
string log = "";

// get services
var siteRepository = provider.GetRequiredService<ISiteRepository>();
var userRepository = provider.GetRequiredService<IUserRepository>();
var settingRepository = provider.GetRequiredService<ISettingRepository>();
var notificationRepository = provider.GetRequiredService<INotificationRepository>();

// iterate through sites for this tenant
// iterate through sites for current tenant
List<Site> sites = siteRepository.GetSites().ToList();
foreach (Site site in sites)
{
Expand Down Expand Up @@ -59,49 +61,79 @@ public override string ExecuteJob(IServiceProvider provider)
client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
}

// iterate through notifications
// iterate through undelivered notifications
int sent = 0;
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
foreach (Notification notification in notifications)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name);
mailMessage.Subject = notification.Subject;
if (notification.FromUserId != null)
// get sender and receiver information if not provided
if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null)
{
mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n";
var user = userRepository.GetUser(notification.FromUserId.Value);
if (user != null)
{
notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail;
notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName;
}
}
else
{
mailMessage.Body = "From: " + site.Name + "\n";
}
mailMessage.Body += "Sent: " + notification.CreatedOn + "\n";
if (notification.ToUserId != null)
if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null)
{
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n";
var user = userRepository.GetUser(notification.ToUserId.Value);
if (user != null)
{
notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail;
notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName;
}
}
else
{
mailMessage.To.Add(new MailAddress(notification.ToEmail));
mailMessage.Body += "To: " + notification.ToEmail + "\n";
}
mailMessage.Body += "Subject: " + notification.Subject + "\n\n";
mailMessage.Body += notification.Body;

// send mail
try

// validate recipient
if (string.IsNullOrEmpty(notification.ToEmail))
{
client.Send(mailMessage);
sent = sent++;
notification.IsDelivered = true;
notification.DeliveredOn = DateTime.UtcNow;
log += "Recipient Missing For NotificationId: " + notification.NotificationId + "<br />";
notification.IsDeleted = true;
notificationRepository.UpdateNotification(notification);
}
catch (Exception ex)
else
{
// error
log += ex.Message + "<br />";
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name);
mailMessage.Subject = notification.Subject;
if (!string.IsNullOrEmpty(notification.FromEmail) && !string.IsNullOrEmpty(notification.FromDisplayName))
{
mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n";
}
else
{
mailMessage.Body = "From: " + site.Name + "\n";
}
mailMessage.Body += "Sent: " + notification.CreatedOn + "\n";
if (!string.IsNullOrEmpty(notification.ToEmail) && !string.IsNullOrEmpty(notification.ToDisplayName))
{
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n";
}
else
{
mailMessage.To.Add(new MailAddress(notification.ToEmail));
mailMessage.Body += "To: " + notification.ToEmail + "\n";
}
mailMessage.Body += "Subject: " + notification.Subject + "\n\n";
mailMessage.Body += notification.Body;

// send mail
try
{
client.Send(mailMessage);
sent = sent++;
notification.IsDelivered = true;
notification.DeliveredOn = DateTime.UtcNow;
notificationRepository.UpdateNotification(notification);
}
catch (Exception ex)
{
// error
log += ex.Message + "<br />";
}
}
}
log += "Notifications Delivered: " + sent + "<br />";
Expand Down
4 changes: 2 additions & 2 deletions Oqtane.Server/Repository/NotificationRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Oqtane.Models;
Expand All @@ -20,7 +20,7 @@ public IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, in
{
return _db.Notification
.Where(item => item.SiteId == siteId)
.Where(item => item.IsDelivered == false)
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
.ToList();
}
Expand Down
46 changes: 45 additions & 1 deletion Oqtane.Shared/Models/Notification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace Oqtane.Models
Expand All @@ -23,6 +23,50 @@ public class Notification : IDeletable
public DateTime? DeletedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime? SendOn { get; set; }

public Notification() {}

public Notification(int siteId, User from, User to, string subject, string body, int? parentId)
{
SiteId = siteId;
if (from != null)
{
FromUserId = from.UserId;
FromDisplayName = from.DisplayName;
FromEmail = from.Email;
}
if (to != null)
{
ToUserId = to.UserId;
ToDisplayName = to.DisplayName;
ToEmail = to.Email;
}
Subject = subject;
Body = body;
ParentId = parentId;
CreatedOn = DateTime.UtcNow;
IsDelivered = false;
DeliveredOn = null;
SendOn = DateTime.UtcNow;
}

public Notification(int siteId, string fromDisplayName, string fromEmail, string toDisplayName, string toEmail, string subject, string body)
{
SiteId = siteId;
FromUserId = null;
FromDisplayName = fromDisplayName;
FromEmail = fromEmail;
ToUserId = null;
ToDisplayName = toDisplayName;
ToEmail = toEmail;
Subject = subject;
Body = body;
ParentId = null;
CreatedOn = DateTime.UtcNow;
IsDelivered = false;
DeliveredOn = null;
SendOn = DateTime.UtcNow;
}
}

}

0 comments on commit 892d9c1

Please sign in to comment.