Also added new notification variables and a new notification when someone adds a comment on an issue.
pull/1959/head
tidusjar 6 years ago
parent b223306ee8
commit 480a107fa6

@ -12,5 +12,6 @@
ItemAddedToFaultQueue = 7,
WelcomeEmail = 8,
IssueResolved = 9,
IssueComment = 10,
}
}

@ -87,6 +87,22 @@ namespace Ombi.Notifications.Agents
await Send(notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, DiscordNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Discord}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
notification.Other.Add("image", parsed.Image);
await Send(notification, settings);
}
protected override async Task IssueResolved(NotificationOptions model, DiscordNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Discord, NotificationType.IssueResolved, model);

@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using MimeKit;
using Ombi.Core.Settings;
@ -19,14 +21,16 @@ namespace Ombi.Notifications.Agents
public class EmailNotification : BaseNotification<EmailNotificationSettings>, IEmailNotification
{
public EmailNotification(ISettingsService<EmailNotificationSettings> settings, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, IEmailProvider prov, ISettingsService<CustomizationSettings> c,
ILogger<EmailNotification> log) : base(settings, r, m, t, c,log)
ILogger<EmailNotification> log, UserManager<OmbiUser> um) : base(settings, r, m, t, c)
{
EmailProvider = prov;
Logger = log;
_userManager = um;
}
private IEmailProvider EmailProvider { get; }
private ILogger<EmailNotification> Logger { get; }
public override string NotificationName => nameof(EmailNotification);
private readonly UserManager<OmbiUser> _userManager;
protected override bool ValidateConfiguration(EmailNotificationSettings settings)
{
@ -65,9 +69,24 @@ namespace Ombi.Notifications.Agents
{
Message = html,
Subject = parsed.Subject,
To = model.Recipient.HasValue() ? model.Recipient : settings.AdminEmail,
};
if (model.Substitutes.TryGetValue("AdminComment", out var isAdminString))
{
var isAdmin = bool.Parse(isAdminString);
if (isAdmin)
{
var user = _userManager.Users.FirstOrDefault(x => x.Id == model.UserId);
// Send to user
message.To = user.Email;
}
else
{
// Send to admin
message.To = settings.AdminEmail;
}
}
return message;
}
@ -109,6 +128,27 @@ namespace Ombi.Notifications.Agents
await Send(message, settings);
}
protected override async Task IssueComment(NotificationOptions model, EmailNotificationSettings settings)
{
var message = await LoadTemplate(NotificationType.IssueComment, model, settings);
if (message == null)
{
return;
}
var plaintext = await LoadPlainTextMessage(NotificationType.IssueComment, model, settings);
message.Other.Add("PlainTextBody", plaintext);
if (model.Substitutes.TryGetValue("AdminComment", out var isAdminString))
{
var isAdmin = bool.Parse(isAdminString);
message.To = isAdmin ? model.Recipient : settings.AdminEmail;
}
await Send(message, settings);
}
protected override async Task IssueResolved(NotificationOptions model, EmailNotificationSettings settings)
{
var message = await LoadTemplate(NotificationType.IssueResolved, model, settings);

@ -79,6 +79,22 @@ namespace Ombi.Notifications.Agents
await Send(notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, MattermostNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Mattermost}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
notification.Other.Add("image", parsed.Image);
await Send(notification, settings);
}
protected override async Task IssueResolved(NotificationOptions model, MattermostNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Mattermost, NotificationType.IssueResolved, model);

@ -78,6 +78,36 @@ namespace Ombi.Notifications.Agents
await Send(playerIds, notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, MobileNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
_logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Mobile}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
if (model.Substitutes.TryGetValue("AdminComment", out var isAdminString))
{
var isAdmin = bool.Parse(isAdminString);
if (isAdmin)
{
// Send to user
var playerIds = GetUsers(model, NotificationType.IssueComment);
await Send(playerIds, notification, settings);
}
else
{
// Send to admin
var playerIds = await GetAdmins(NotificationType.IssueComment);
await Send(playerIds, notification, settings);
}
}
}
protected override async Task IssueResolved(NotificationOptions model, MobileNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Mobile, NotificationType.IssueResolved, model);
@ -216,9 +246,19 @@ namespace Ombi.Notifications.Agents
}
private List<string> GetUsers(NotificationOptions model, NotificationType type)
{
var notificationIds = model.RequestType == RequestType.Movie
? MovieRequest.RequestedUser.NotificationUserIds
: TvRequest.RequestedUser.NotificationUserIds;
var notificationIds = new List<NotificationUserId>();
if (MovieRequest != null || TvRequest != null)
{
notificationIds = model.RequestType == RequestType.Movie
? MovieRequest?.RequestedUser?.NotificationUserIds
: TvRequest?.RequestedUser?.NotificationUserIds;
}
if (model.UserId.HasValue() && !notificationIds.Any())
{
var user= _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefault(x => x.Id == model.UserId);
notificationIds = user.NotificationUserIds;
}
if (!notificationIds.Any())
{
_logger.LogInformation(

@ -73,6 +73,21 @@ namespace Ombi.Notifications.Agents
await Send(notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, PushbulletSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Pushbullet}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
await Send(notification, settings);
}
protected override async Task IssueResolved(NotificationOptions model, PushbulletSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Pushbullet, NotificationType.IssueResolved, model);

@ -74,6 +74,21 @@ namespace Ombi.Notifications.Agents
await Send(notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, PushoverSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Pushover}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
await Send(notification, settings);
}
protected override async Task IssueResolved(NotificationOptions model, PushoverSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Pushover, NotificationType.IssueResolved, model);

@ -85,6 +85,22 @@ namespace Ombi.Notifications.Agents
await Send(notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, SlackNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Slack}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
notification.Other.Add("image", parsed.Image);
await Send(notification, settings);
}
protected override async Task IssueResolved(NotificationOptions model, SlackNotificationSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Slack, NotificationType.IssueResolved, model);

@ -69,6 +69,21 @@ namespace Ombi.Notifications.Agents
await Send(notification, settings);
}
protected override async Task IssueComment(NotificationOptions model, TelegramSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.IssueComment, model);
if (parsed.Disabled)
{
Logger.LogInformation($"Template {NotificationType.IssueComment} is disabled for {NotificationAgent.Telegram}");
return;
}
var notification = new NotificationMessage
{
Message = parsed.Message,
};
await Send(notification, settings);
}
protected override async Task IssueResolved(NotificationOptions model, TelegramSettings settings)
{
var parsed = await LoadTemplate(NotificationAgent.Telegram, NotificationType.IssueResolved, model);

@ -99,6 +99,9 @@ namespace Ombi.Notifications.Interfaces
case NotificationType.IssueResolved:
await IssueResolved(model, notificationSettings);
break;
case NotificationType.IssueComment:
await IssueComment(model, notificationSettings);
break;
default:
throw new ArgumentOutOfRangeException();
}
@ -180,6 +183,7 @@ namespace Ombi.Notifications.Interfaces
protected abstract bool ValidateConfiguration(T settings);
protected abstract Task NewRequest(NotificationOptions model, T settings);
protected abstract Task NewIssue(NotificationOptions model, T settings);
protected abstract Task IssueComment(NotificationOptions model, T settings);
protected abstract Task IssueResolved(NotificationOptions model, T settings);
protected abstract Task AddedToRequestQueue(NotificationOptions model, T settings);
protected abstract Task RequestDeclined(NotificationOptions model, T settings);

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Ombi.Helpers;
using Ombi.Store;
using Ombi.Store.Entities;
@ -13,6 +14,9 @@ namespace Ombi.Notifications.Models
public RequestType RequestType { get; set; }
public string Recipient { get; set; }
public string AdditionalInformation { get; set; }
public string UserId { get; set; }
public Dictionary<string,string> Substitutes { get; set; } = new Dictionary<string, string>();
}
}

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Ombi.Helpers;
using Ombi.Notifications.Models;
using Ombi.Settings.Settings.Models;
@ -11,38 +10,58 @@ namespace Ombi.Notifications
{
public class NotificationMessageCurlys
{
public void Setup(NotificationOptions opts, FullBaseRequest req, CustomizationSettings s)
{
LoadIssues(opts);
string title;
if (req == null)
{
opts.Substitutes.TryGetValue("Title", out title);
}
else
{
title = req?.Title;
}
ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty;
ApplicationName = string.IsNullOrEmpty(s?.ApplicationName) ? "Ombi" : s?.ApplicationName;
RequestedUser = string.IsNullOrEmpty(req.RequestedUser.Alias)
? req.RequestedUser.UserName
: req.RequestedUser.Alias;
Title = req.Title;
RequestedDate = req.RequestedDate.ToString("D");
Type = req.RequestType.ToString();
Overview = req.Overview;
Year = req.ReleaseDate.Year.ToString();
PosterImage = req.RequestType == RequestType.Movie ?
$"https://image.tmdb.org/t/p/w300{req.PosterPath}" : req.PosterPath;
RequestedUser = string.IsNullOrEmpty(req?.RequestedUser?.Alias)
? req?.RequestedUser?.UserName
: req?.RequestedUser?.Alias;
Title = title;
RequestedDate = req?.RequestedDate.ToString("D");
Type = req?.RequestType.ToString();
Overview = req?.Overview;
Year = req?.ReleaseDate.Year.ToString();
PosterImage = req?.RequestType == RequestType.Movie ?
string.Format("https://image.tmdb.org/t/p/w300{0}", req?.PosterPath) : req?.PosterPath;
AdditionalInformation = opts?.AdditionalInformation ?? string.Empty;
}
public void Setup(NotificationOptions opts, ChildRequests req, CustomizationSettings s)
{
ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty;
LoadIssues(opts);
string title;
if (req == null)
{
opts.Substitutes.TryGetValue("Title", out title);
}
else
{
title = req?.ParentRequest.Title;
}
ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty;
ApplicationName = string.IsNullOrEmpty(s?.ApplicationName) ? "Ombi" : s?.ApplicationName;
RequestedUser = string.IsNullOrEmpty(req.RequestedUser.Alias)
? req.RequestedUser.UserName
: req.RequestedUser.Alias;
Title = req.ParentRequest.Title;
RequestedDate = req.RequestedDate.ToString("D");
Type = req.RequestType.ToString();
Overview = req.ParentRequest.Overview;
Year = req.ParentRequest.ReleaseDate.Year.ToString();
PosterImage = req.RequestType == RequestType.Movie ?
$"https://image.tmdb.org/t/p/w300{req.ParentRequest.PosterPath}" : req.ParentRequest.PosterPath;
RequestedUser = string.IsNullOrEmpty(req?.RequestedUser.Alias)
? req?.RequestedUser.UserName
: req?.RequestedUser.Alias;
Title = title;
RequestedDate = req?.RequestedDate.ToString("D");
Type = req?.RequestType.ToString();
Overview = req?.ParentRequest.Overview;
Year = req?.ParentRequest.ReleaseDate.Year.ToString();
PosterImage = req?.RequestType == RequestType.Movie ?
$"https://image.tmdb.org/t/p/w300{req?.ParentRequest.PosterPath}" : req?.ParentRequest.PosterPath;
AdditionalInformation = opts.AdditionalInformation;
// DO Episode and Season Lists
}
@ -54,6 +73,16 @@ namespace Ombi.Notifications
RequestedUser = user.UserName;
}
private void LoadIssues(NotificationOptions opts)
{
var val = string.Empty;
IssueDescription = opts.Substitutes.TryGetValue("IssueDescription", out val) ? val : string.Empty;
IssueCategory = opts.Substitutes.TryGetValue("IssueCategory", out val) ? val : string.Empty;
IssueStatus = opts.Substitutes.TryGetValue("IssueStatus", out val) ? val : string.Empty;
IssueSubject = opts.Substitutes.TryGetValue("IssueSubject", out val) ? val : string.Empty;
NewIssueComment = opts.Substitutes.TryGetValue("NewIssueComment", out val) ? val : string.Empty;
}
// User Defined
public string RequestedUser { get; set; }
public string Title { get; set; }
@ -67,6 +96,11 @@ namespace Ombi.Notifications
public string PosterImage { get; set; }
public string ApplicationName { get; set; }
public string ApplicationUrl { get; set; }
public string IssueDescription { get; set; }
public string IssueCategory { get; set; }
public string IssueStatus { get; set; }
public string IssueSubject { get; set; }
public string NewIssueComment { get; set; }
// System Defined
private string LongDate => DateTime.Now.ToString("D");
@ -92,6 +126,11 @@ namespace Ombi.Notifications
{nameof(PosterImage),PosterImage},
{nameof(ApplicationName),ApplicationName},
{nameof(ApplicationUrl),ApplicationUrl},
{nameof(IssueDescription),IssueDescription},
{nameof(IssueCategory),IssueCategory},
{nameof(IssueStatus),IssueStatus},
{nameof(IssueSubject),IssueSubject},
{nameof(NewIssueComment),NewIssueComment},
};
}
}

@ -205,6 +205,17 @@ namespace Ombi.Store.Context
Enabled = true,
};
break;
case NotificationType.IssueComment:
notificationToAdd = new NotificationTemplates
{
NotificationType = notificationType,
Message = "Hello, There is a new comment on your issue {IssueSubject}, The comment is: {NewIssueComment}",
Subject = "{ApplicationName}: New comment on issue {IssueSubject}!",
Agent = agent,
Enabled = true,
};
break;
case NotificationType.AdminNote:
continue;
default:

@ -66,7 +66,11 @@ namespace Ombi.Store.Repository
var item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.ImdbId == providerId);
if (item == null)
{
item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TheMovieDbId == providerId) ?? await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TvDbId == providerId);
item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TheMovieDbId == providerId);
if (item == null)
{
item = await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TvDbId == providerId);
}
}
return item;
}

@ -136,14 +136,17 @@ namespace Ombi.Controllers
var notificationModel = new NotificationOptions
{
RequestId = 0,
RequestId = i.RequestId ?? 0,
DateTime = DateTime.Now,
NotificationType = NotificationType.Issue,
RequestType = i.RequestType,
Recipient = string.Empty,
AdditionalInformation = $"{i.Subject} | {i.Description}"
AdditionalInformation = $"{i.Subject} | {i.Description}",
UserId = i.UserReportedId
};
AddIssueNotificationSubstitutes(notificationModel, i);
BackgroundJob.Enqueue(() => _notification.Publish(notificationModel));
return i.Id;
@ -190,21 +193,46 @@ namespace Ombi.Controllers
[HttpPost("comments")]
public async Task<IssueComments> AddComment([FromBody] NewIssueCommentViewModel comment)
{
var userId = await _userManager.Users.Where(x => User.Identity.Name == x.UserName).Select(x => x.Id)
var user = await _userManager.Users.Where(x => User.Identity.Name == x.UserName)
.FirstOrDefaultAsync();
var issue = await _issues.Find(comment.IssueId ?? 0);
if (issue == null)
{
return null;
}
var newComment = new IssueComments
{
Comment = comment.Comment,
Date = DateTime.UtcNow,
UserId = userId,
UserId = user.Id,
IssuesId = comment.IssueId,
};
var notificationModel = new NotificationOptions
{
RequestId = issue.RequestId ?? 0,
DateTime = DateTime.Now,
NotificationType = NotificationType.IssueComment,
RequestType = issue.RequestType,
Recipient = user.Email,
UserId = user.Id
};
var isAdmin = await _userManager.IsInRoleAsync(user, OmbiRoles.Admin);
AddIssueNotificationSubstitutes(notificationModel, issue);
notificationModel.Substitutes.Add("NewIssueComment", comment.Comment);
notificationModel.Substitutes.Add("AdminComment", isAdmin.ToString());
BackgroundJob.Enqueue(() => _notification.Publish(notificationModel));
return await _issueComments.Add(newComment);
}
[HttpPost("status")]
public async Task<bool> UpdateStatus([FromBody] IssueStateViewModel model)
{
var user = await _userManager.Users.Where(x => User.Identity.Name == x.UserName)
.FirstOrDefaultAsync();
var issue = await _issues.Find(model.IssueId);
if (issue == null)
{
@ -214,20 +242,36 @@ namespace Ombi.Controllers
issue.Status = model.Status;
await _issues.SaveChangesAsync();
var notificationModel = new NotificationOptions
if (issue.Status == IssueStatus.Resolved)
{
RequestId = 0,
DateTime = DateTime.Now,
NotificationType = NotificationType.Issue,
RequestType = issue.RequestType,
Recipient = !string.IsNullOrEmpty(issue.UserReported?.Email) ? issue.UserReported.Email : string.Empty,
AdditionalInformation = $"{issue.Subject} | {issue.Description}"
};
var notificationModel = new NotificationOptions
{
RequestId = 0,
DateTime = DateTime.Now,
NotificationType = NotificationType.IssueResolved,
RequestType = issue.RequestType,
Recipient = !string.IsNullOrEmpty(issue.UserReported?.Email)
? issue.UserReported.Email
: string.Empty,
AdditionalInformation = $"{issue.Subject} | {issue.Description}",
UserId = user.Id
};
AddIssueNotificationSubstitutes(notificationModel, issue);
BackgroundJob.Enqueue(() => _notification.Publish(notificationModel));
BackgroundJob.Enqueue(() => _notification.Publish(notificationModel));
}
return true;
}
private static void AddIssueNotificationSubstitutes(NotificationOptions notificationModel, Issues issue)
{
notificationModel.Substitutes.Add("Title", issue.Title);
notificationModel.Substitutes.Add("IssueDescription", issue.Description);
notificationModel.Substitutes.Add("IssueCategory", issue.IssueCategory?.Value);
notificationModel.Substitutes.Add("IssueStatus", issue.Status.ToString());
notificationModel.Substitutes.Add("IssueSubject", issue.Subject);
}
}
}
Loading…
Cancel
Save