Fixed the issue where the new mobile app was not recieving any notifications

This commit is contained in:
tidusjar 2021-03-08 10:13:24 +00:00
commit a61594dcd6
2 changed files with 25 additions and 20 deletions

View file

@ -131,7 +131,7 @@ namespace Ombi.Notifications.Agents
}; };
// Send to user // Send to user
var playerIds = GetUsers(model, NotificationType.IssueResolved); var playerIds = await GetUsers(model, NotificationType.IssueResolved);
await Send(playerIds, notification, settings, model); await Send(playerIds, notification, settings, model);
} }
@ -172,7 +172,7 @@ namespace Ombi.Notifications.Agents
}; };
// Send to user // Send to user
var playerIds = GetUsers(model, NotificationType.RequestDeclined); var playerIds = await GetUsers(model, NotificationType.RequestDeclined);
await AddSubscribedUsers(playerIds); await AddSubscribedUsers(playerIds);
await Send(playerIds, notification, settings, model); await Send(playerIds, notification, settings, model);
} }
@ -192,7 +192,7 @@ namespace Ombi.Notifications.Agents
}; };
// Send to user // Send to user
var playerIds = GetUsers(model, NotificationType.RequestApproved); var playerIds = await GetUsers(model, NotificationType.RequestApproved);
await AddSubscribedUsers(playerIds); await AddSubscribedUsers(playerIds);
await Send(playerIds, notification, settings, model); await Send(playerIds, notification, settings, model);
@ -215,7 +215,7 @@ namespace Ombi.Notifications.Agents
Data = data Data = data
}; };
// Send to user // Send to user
var playerIds = GetUsers(model, NotificationType.RequestAvailable); var playerIds = await GetUsers(model, NotificationType.RequestAvailable);
await AddSubscribedUsers(playerIds); await AddSubscribedUsers(playerIds);
await Send(playerIds, notification, settings, model); await Send(playerIds, notification, settings, model);
@ -285,19 +285,23 @@ namespace Ombi.Notifications.Agents
return playerIds; return playerIds;
} }
private List<string> GetUsers(NotificationOptions model, NotificationType type) private async Task<List<string>> GetUsers(NotificationOptions model, NotificationType type)
{ {
var notificationIds = new List<NotificationUserId>(); var notificationIds = new List<MobileDevices>();
if (MovieRequest != null || TvRequest != null) if (MovieRequest != null || TvRequest != null)
{ {
notificationIds = model.RequestType == RequestType.Movie var userId = model.RequestType == RequestType.Movie
? MovieRequest?.RequestedUser?.NotificationUserIds ? MovieRequest?.RequestedUser?.Id
: TvRequest?.RequestedUser?.NotificationUserIds; : TvRequest?.RequestedUser?.Id;
var userNotificationIds = await _notifications.GetAll().Where(x => x.UserId == userId).ToListAsync();
notificationIds.AddRange(userNotificationIds);
} }
if (model.UserId.HasValue() && (!notificationIds?.Any() ?? true)) if (model.UserId.HasValue() && (!notificationIds?.Any() ?? true))
{ {
var user = _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefault(x => x.Id == model.UserId); var user = _userManager.Users.FirstOrDefault(x => x.Id == model.UserId);
notificationIds = user.NotificationUserIds; var userNotificationIds = await _notifications.GetAll().Where(x => x.UserId == model.UserId).ToListAsync();
notificationIds.AddRange(userNotificationIds);
} }
if (!notificationIds?.Any() ?? true) if (!notificationIds?.Any() ?? true)
@ -306,21 +310,21 @@ namespace Ombi.Notifications.Agents
$"there are no users to send a notification for {type}, for agent {NotificationAgent.Mobile}"); $"there are no users to send a notification for {type}, for agent {NotificationAgent.Mobile}");
return null; return null;
} }
var playerIds = notificationIds.Select(x => x.PlayerId).ToList(); var playerIds = notificationIds.Select(x => x.Token).ToList();
return playerIds; return playerIds;
} }
private async Task<List<string>> GetUsersForIssue(NotificationOptions model, int issueId, NotificationType type) private async Task<List<string>> GetUsersForIssue(NotificationOptions model, int issueId, NotificationType type)
{ {
var notificationIds = new List<NotificationUserId>(); var notificationIds = new List<MobileDevices>();
var issue = await _issueRepository.GetAll() var issue = await _issueRepository.GetAll()
.FirstOrDefaultAsync(x => x.Id == issueId); .FirstOrDefaultAsync(x => x.Id == issueId);
// Get the user that raised the issue to send the notification to // Get the user that raised the issue to send the notification to
var userRaised = await _userManager.Users.Include(x => x.NotificationUserIds).FirstOrDefaultAsync(x => x.Id == issue.UserReportedId); var userRaised = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == issue.UserReportedId);
notificationIds = userRaised.NotificationUserIds; notificationIds = await _notifications.GetAll().Where(x => x.UserId == userRaised.Id).ToListAsync();
if (!notificationIds?.Any() ?? true) if (!notificationIds?.Any() ?? true)
{ {
@ -328,7 +332,7 @@ namespace Ombi.Notifications.Agents
$"there are no users to send a notification for {type}, for agent {NotificationAgent.Mobile}"); $"there are no users to send a notification for {type}, for agent {NotificationAgent.Mobile}");
return null; return null;
} }
var playerIds = notificationIds.Select(x => x.PlayerId).ToList(); var playerIds = notificationIds.Select(x => x.Token).ToList();
return playerIds; return playerIds;
} }
@ -338,10 +342,11 @@ namespace Ombi.Notifications.Agents
{ {
foreach (var user in SubsribedUsers) foreach (var user in SubsribedUsers)
{ {
var notificationId = user.NotificationUserIds; var notificationIds = await _notifications.GetAll().Where(x => x.UserId == user.Id).ToListAsync();
if (notificationId.Any())
if (notificationIds.Any())
{ {
playerIds.AddRange(notificationId.Select(x => x.PlayerId)); playerIds.AddRange(notificationIds.Select(x => x.Token));
} }
} }
} }

View file

@ -191,7 +191,7 @@ namespace Ombi.Notifications
protected IQueryable<OmbiUser> GetSubscriptions(int requestId, RequestType type) protected IQueryable<OmbiUser> GetSubscriptions(int requestId, RequestType type)
{ {
var subs = RequestSubscription.GetAll().Include(x => x.User).ThenInclude(x => x.NotificationUserIds).Where(x => x.RequestId == requestId && type == x.RequestType); var subs = RequestSubscription.GetAll().Include(x => x.User).Where(x => x.RequestId == requestId && type == x.RequestType);
return subs.Select(x => x.User); return subs.Select(x => x.User);
} }