Skip to content

Commit

Permalink
feat(notifications): ✨ Send new request email notifications to power …
Browse files Browse the repository at this point in the history
…users (#4462)

* Send new request email notifications to power users

* Send new request to power users as convention

Remove option to toggle it off

* Send New Request notification to power users in app

* Better wording + fix mobile notifications
  • Loading branch information
sephrat committed Jan 14, 2022
1 parent c5b7b67 commit 10cc0c0
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/Ombi.Notifications/Agents/DiscordNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Discord;
using Ombi.Api.Discord.Models;
Expand All @@ -21,8 +22,8 @@ public class DiscordNotification : BaseNotification<DiscordNotificationSettings>
public DiscordNotification(IDiscordApi api, ISettingsService<DiscordNotificationSettings> sn,
ILogger<DiscordNotification> log, INotificationTemplatesRepository r,
IMovieRequestRepository m, ITvRequestRepository t, ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref)
: base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um)
: base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
13 changes: 11 additions & 2 deletions src/Ombi.Notifications/Agents/EmailNotification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
Expand All @@ -22,7 +23,7 @@ public class EmailNotification : BaseNotification<EmailNotificationSettings>, IE
{
public EmailNotification(ISettingsService<EmailNotificationSettings> settings, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, IEmailProvider prov, ISettingsService<CustomizationSettings> c,
ILogger<EmailNotification> log, UserManager<OmbiUser> um, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(settings, r, m, t, c, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref) : base(settings, r, m, t, c, log, sub, music, userPref, um)
{
EmailProvider = prov;
Logger = log;
Expand Down Expand Up @@ -114,7 +115,15 @@ protected override async Task NewRequest(NotificationOptions model, EmailNotific
var plaintext = await LoadPlainTextMessage(NotificationType.NewRequest, model, settings);
message.Other.Add("PlainTextBody", plaintext);

await Send(message, settings);
foreach (var recipient in (await GetPrivilegedUsers()).DistinctBy(x => x.Email))
{
if (recipient.Email.IsNullOrEmpty())
{
continue;
}
message.To = recipient.Email;
await Send(message, settings);
}
}

protected override async Task NewIssue(NotificationOptions model, EmailNotificationSettings settings)
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/GotifyNotification.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Gotify;
using Ombi.Core.Settings;
Expand All @@ -17,7 +18,7 @@ public class GotifyNotification : BaseNotification<GotifySettings>, IGotifyNotif
{
public GotifyNotification(IGotifyApi api, ISettingsService<GotifySettings> sn, ILogger<GotifyNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
26 changes: 18 additions & 8 deletions src/Ombi.Notifications/Agents/LegacyMobileNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LegacyMobileNotification : BaseNotification<MobileNotificationSetti
public LegacyMobileNotification(IOneSignalApi api, ISettingsService<MobileNotificationSettings> sn, ILogger<LegacyMobileNotification> log, INotificationTemplatesRepository r,
IMovieRequestRepository m, ITvRequestRepository t, ISettingsService<CustomizationSettings> s, IRepository<NotificationUserId> notification,
UserManager<OmbiUser> um, IRepository<RequestSubscription> sub, IMusicRequestRepository music, IRepository<Issues> issueRepository,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
_api = api;
_logger = log;
Expand Down Expand Up @@ -59,7 +59,7 @@ protected override async Task NewRequest(NotificationOptions model, MobileNotifi
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.NewRequest);
var playerIds = await GetPrivilegedUsersPlayerIds();
await Send(playerIds, notification, settings, model, true);
}

Expand All @@ -77,7 +77,7 @@ protected override async Task NewIssue(NotificationOptions model, MobileNotifica
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.Issue);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model);
}

Expand Down Expand Up @@ -106,7 +106,7 @@ protected override async Task IssueComment(NotificationOptions model, MobileNoti
else
{
// Send to admin
var playerIds = await GetAdmins(NotificationType.IssueComment);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model);
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ protected override async Task AddedToRequestQueue(NotificationOptions model, Mob
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.Test);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model);
}

Expand Down Expand Up @@ -241,15 +241,25 @@ protected override async Task Test(NotificationOptions model, MobileNotification
await Send(playerIds, notification, settings, model);
}

private async Task<List<string>> GetAdmins(NotificationType type)
private async Task<List<string>> GetAdmins()
{
var adminUsers = (await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)).Select(x => x.Id).ToList();
return await GetNotificationRecipients(await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin));
}
private async Task<List<string>> GetPrivilegedUsersPlayerIds()
{
return await GetNotificationRecipients(await GetPrivilegedUsers());
}

private async Task<List<string>> GetNotificationRecipients(IEnumerable<OmbiUser> users)
{

var adminUsers = users.Select(x => x.Id).ToList();
var notificationUsers = _notifications.GetAll().Include(x => x.User).Where(x => adminUsers.Contains(x.UserId));
var playerIds = await notificationUsers.Select(x => x.PlayerId).ToListAsync();
if (!playerIds.Any())
{
_logger.LogInformation(
$"there are no admins to send a notification for {type}, for agent {NotificationAgent.Mobile}");
$"there are no users to send a notification for agent {NotificationAgent.Mobile}");
return null;
}
return playerIds;
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/MattermostNotification.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Mattermost;
using Ombi.Api.Mattermost.Models;
Expand All @@ -19,7 +20,7 @@ public class MattermostNotification : BaseNotification<MattermostNotificationSet
{
public MattermostNotification(IMattermostApi api, ISettingsService<MattermostNotificationSettings> sn, ILogger<MattermostNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
28 changes: 19 additions & 9 deletions src/Ombi.Notifications/Agents/MobileNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MobileNotification : BaseNotification<MobileNotificationSettings>,
public MobileNotification(ICloudMobileNotification api, ISettingsService<MobileNotificationSettings> sn, ILogger<MobileNotification> log, INotificationTemplatesRepository r,
IMovieRequestRepository m, ITvRequestRepository t, ISettingsService<CustomizationSettings> s, IRepository<MobileDevices> notification,
UserManager<OmbiUser> um, IRepository<RequestSubscription> sub, IMusicRequestRepository music, IRepository<Issues> issueRepository,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
_api = api;
_logger = log;
Expand Down Expand Up @@ -62,7 +62,7 @@ protected override async Task NewRequest(NotificationOptions model, MobileNotifi
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.NewRequest);
var playerIds = await GetPrivilegedUsersPlayerIds();
await Send(playerIds, notification, settings, model, true);
}

Expand All @@ -82,7 +82,7 @@ protected override async Task NewIssue(NotificationOptions model, MobileNotifica
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.Issue);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model);
}

Expand Down Expand Up @@ -112,7 +112,7 @@ protected override async Task IssueComment(NotificationOptions model, MobileNoti
else
{
// Send to admin
var playerIds = await GetAdmins(NotificationType.IssueComment);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model);
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ protected override async Task AddedToRequestQueue(NotificationOptions model, Mob
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.Test);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model);
}

Expand Down Expand Up @@ -279,15 +279,25 @@ protected override async Task Test(NotificationOptions model, MobileNotification
await Send(playerIds, notification, settings, model);
}

private async Task<List<string>> GetAdmins(NotificationType type)
private async Task<List<string>> GetAdmins()
{
var adminUsers = (await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin)).Select(x => x.Id).ToList();
return await GetNotificationRecipients(await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin));
}
private async Task<List<string>> GetPrivilegedUsersPlayerIds()
{
return await GetNotificationRecipients(await GetPrivilegedUsers());
}

private async Task<List<string>> GetNotificationRecipients(IEnumerable<OmbiUser> users)
{

var adminUsers = users.Select(x => x.Id).ToList();
var notificationUsers = _notifications.GetAll().Include(x => x.User).Where(x => adminUsers.Contains(x.UserId));
var playerIds = await notificationUsers.Select(x => x.Token).ToListAsync();
if (!playerIds.Any())
{
_logger.LogInformation(
$"there are no admins to send a notification for {type}, for agent {NotificationAgent.Mobile}");
$"there are no users to send a notification for agent {NotificationAgent.Mobile}");
return null;
}
return playerIds;
Expand Down Expand Up @@ -377,7 +387,7 @@ protected override async Task PartiallyAvailable(NotificationOptions model, Mobi
};

// Get admin devices
var playerIds = await GetAdmins(NotificationType.PartiallyAvailable);
var playerIds = await GetAdmins();
await Send(playerIds, notification, settings, model, true);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/PushbulletNotification.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Pushbullet;
using Ombi.Core.Settings;
Expand All @@ -17,7 +18,7 @@ public class PushbulletNotification : BaseNotification<PushbulletSettings>, IPus
{
public PushbulletNotification(IPushbulletApi api, ISettingsService<PushbulletSettings> sn, ILogger<PushbulletNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/PushoverNotification.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Pushbullet;
using Ombi.Api.Pushover;
Expand All @@ -18,7 +19,7 @@ public class PushoverNotification : BaseNotification<PushoverSettings>, IPushove
{
public PushoverNotification(IPushoverApi api, ISettingsService<PushoverSettings> sn, ILogger<PushoverNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/SlackNotification.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Slack;
using Ombi.Api.Slack.Models;
Expand All @@ -18,7 +19,7 @@ public class SlackNotification : BaseNotification<SlackNotificationSettings>, IS
{
public SlackNotification(ISlackApi api, ISettingsService<SlackNotificationSettings> sn, ILogger<SlackNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/TelegramNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
using Ombi.Api.Telegram;
using Microsoft.AspNetCore.Identity;

namespace Ombi.Notifications.Agents
{
Expand All @@ -19,7 +20,7 @@ public class TelegramNotification : BaseNotification<TelegramSettings>, ITelegra
INotificationTemplatesRepository r, IMovieRequestRepository m,
ITvRequestRepository t, ISettingsService<CustomizationSettings> s
, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t,s,log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t,s,log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/WebhookNotification.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Ombi.Api.Webhook;
using Ombi.Core.Settings;
Expand All @@ -18,7 +19,7 @@ public class WebhookNotification : BaseNotification<WebhookSettings>, IWebhookNo
{
public WebhookNotification(IWebhookApi api, ISettingsService<WebhookSettings> sn, ILogger<WebhookNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down
3 changes: 2 additions & 1 deletion src/Ombi.Notifications/Agents/WhatsAppNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
using Ombi.Api.Twilio;
using Microsoft.AspNetCore.Identity;

namespace Ombi.Notifications.Agents
{
Expand All @@ -19,7 +20,7 @@ public class WhatsAppNotification : BaseNotification<TwilioSettings>
INotificationTemplatesRepository r, IMovieRequestRepository m,
ITvRequestRepository t, ISettingsService<CustomizationSettings> s
, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t,s,log, sub, music, userPref)
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t,s,log, sub, music, userPref, um)
{
Api = api;
Logger = log;
Expand Down

0 comments on commit 10cc0c0

Please sign in to comment.