示例#1
0
        public async Task <ICommandResult <Answer> > SendAsync(INotificationContext <Answer> context)
        {
            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.NewAnswer.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Answer>();

            // Get the entity for the reply
            var entity = await _entityStore.GetByIdAsync(context.Model.EntityId);

            if (entity == null)
            {
                return(result.Failed($"No entity could be found with the Id of {context.Model.EntityId} when sending the follow notification '{WebNotifications.NewAnswer.Name}'."));
            }

            // Get base Uri
            var baseUri = await _capturedRouterUrlHelper.GetBaseUrlAsync();

            // Build user notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = entity.Title,
                Message          = S["An answer has been posted within a question your following"],
                CreatedUserId    = context.Model.CreatedUserId,
                Url = _capturedRouterUrlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
                {
                    ["area"]         = "Plato.Questions",
                    ["controller"]   = "Home",
                    ["action"]       = "Reply",
                    ["opts.id"]      = entity.Id,
                    ["opts.alias"]   = entity.Alias,
                    ["opts.replyId"] = context.Model.Id
                })
            };

            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#2
0
        public async Task <ICommandResult <Idea> > SendAsync(INotificationContext <Idea> context)
        {
            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.NewMention.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // We always need a model
            if (context.Model == null)
            {
                return(null);
            }

            // The entity should be visible
            if (context.Model.IsHidden())
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Idea>();

            // Build user notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = S["New Mention"].Value,
                Message          = S["You've been mentioned by "].Value + context.Model.CreatedBy.DisplayName,
                CreatedUserId    = context.Model.CreatedUserId,
                Url = _contextFacade.GetRouteUrl(new RouteValueDictionary()
                {
                    ["area"]       = "Plato.Ideas",
                    ["controller"] = "Home",
                    ["action"]     = "Display",
                    ["opts.id"]    = context.Model.Id,
                    ["opts.alias"] = context.Model.Alias
                })
            };

            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#3
0
        public async Task <ICommandResult <Article> > SendAsync(INotificationContext <Article> context)
        {
            // Create result
            var result = new CommandResult <Article>();

            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.NewLabel.Name, StringComparison.Ordinal))
            {
                return(result.Failed($"Skipping notification '{WebNotifications.NewLabel.Name}' as this does not match '{context.Notification.Type.Name}'."));
            }

            // Build user notification
            var baseUri = await _capturedRouterUrlHelper.GetBaseUrlAsync();

            // Build user notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = context.Model.Title,
                Message          = S["An article has been posted with a label your following"],
                CreatedUserId    = context.Model.CreatedUserId,
                Url = _capturedRouterUrlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
                {
                    ["area"]       = "Plato.Articles",
                    ["controller"] = "Home",
                    ["action"]     = "Display",
                    ["opts.id"]    = context.Model.Id,
                    ["opts.alias"] = context.Model.Alias
                })
            };

            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#4
0
        public async Task <ICommandResult <Doc> > SendAsync(INotificationContext <Doc> context)
        {
            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.EntityUpdate.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Doc>();

            // Get base Uri
            var baseUri = await _capturedRouterUrlHelper.GetBaseUrlAsync();

            // Build user notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = context.Model.Title,
                Message          = S["I've just updated a doc your following"],
                CreatedUserId    = context.Model.ModifiedUserId,
                Url = _capturedRouterUrlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
                {
                    ["area"]       = "Plato.Docs",
                    ["controller"] = "Home",
                    ["action"]     = "Display",
                    ["opts.id"]    = context.Model.Id,
                    ["opts.alias"] = context.Model.Alias
                })
            };

            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#5
0
        public async Task <ICommandResult <ReportSubmission <Question> > > SendAsync(INotificationContext <ReportSubmission <Question> > context)
        {
            // Validate
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Notification == null)
            {
                throw new ArgumentNullException(nameof(context.Notification));
            }

            if (context.Notification.Type == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.Type));
            }

            if (context.Notification.To == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.To));
            }

            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.QuestionReport.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <ReportSubmission <Question> >();

            var baseUri = await _urlHelper.GetBaseUrlAsync();

            var url = _urlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
            {
                ["area"]       = "Plato.Questions",
                ["controller"] = "Home",
                ["action"]     = "Display",
                ["opts.id"]    = context.Model.What.Id,
                ["opts.alias"] = context.Model.What.Alias
            });

            // Get reason given text
            var reasonText = S["Question Reported"];

            if (ReportReasons.Reasons.ContainsKey(context.Model.Why))
            {
                reasonText = S[ReportReasons.Reasons[context.Model.Why]];
            }

            //// Build notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = reasonText.Value,
                Message          = S["A question has been reported!"],
                Url           = url,
                CreatedUserId = context.Notification.From?.Id ?? 0,
                CreatedDate   = DateTimeOffset.UtcNow
            };

            // Create notification
            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#6
0
        public async Task <ICommandResult <Topic> > SendAsync(INotificationContext <Topic> context)
        {
            // Validate
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Notification == null)
            {
                throw new ArgumentNullException(nameof(context.Notification));
            }

            if (context.Notification.Type == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.Type));
            }

            if (context.Notification.To == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.To));
            }

            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.TopicSpam.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Topic>();

            var baseUri = await _urlHelper.GetBaseUrlAsync();

            var url = _urlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
            {
                ["area"]       = "Plato.Discuss",
                ["controller"] = "Home",
                ["action"]     = "Display",
                ["opts.id"]    = context.Model.Id,
                ["opts.alias"] = context.Model.Alias
            });

            //// Build notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = S["Possible SPAM"].Value,
                Message          = S["A topic has been detected as SPAM!"],
                Url           = url,
                CreatedUserId = context.Notification.From?.Id ?? 0,
                CreatedDate   = DateTimeOffset.UtcNow
            };

            // Create notification
            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#7
0
        public async Task <ICommandResult <Answer> > SendAsync(INotificationContext <Answer> context)
        {
            // Validate
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Notification == null)
            {
                throw new ArgumentNullException(nameof(context.Notification));
            }

            if (context.Notification.Type == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.Type));
            }

            if (context.Notification.To == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.To));
            }

            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.AnswerSpam.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Answer>();

            // Get entity for reply
            var entity = await _entityStore.GetByIdAsync(context.Model.EntityId);

            // Ensure we found the entity
            if (entity == null)
            {
                return(result.Failed(
                           $"No entity with id '{context.Model.EntityId}' exists. Failed to send reply spam web notification."));
            }

            var baseUri = await _urlHelper.GetBaseUrlAsync();

            var url = _urlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
            {
                ["area"]         = "Plato.Questions",
                ["controller"]   = "Home",
                ["action"]       = "Reply",
                ["opts.id"]      = entity.Id,
                ["opts.alias"]   = entity.Alias,
                ["opts.replyId"] = context.Model.Id
            });

            //// Build notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = S["Possible SPAM"].Value,
                Message          = S["A question answer has been detected as SPAM!"],
                Url           = url,
                CreatedUserId = context.Notification.From?.Id ?? 0,
                CreatedDate   = DateTimeOffset.UtcNow
            };

            // Create notification
            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }
示例#8
0
        public async Task <ICommandResult <Badge> > SendAsync(INotificationContext <Badge> context)
        {
            // Validate
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Notification == null)
            {
                throw new ArgumentNullException(nameof(context.Notification));
            }

            if (context.Notification.Type == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.Type));
            }

            if (context.Notification.To == null)
            {
                throw new ArgumentNullException(nameof(context.Notification.To));
            }

            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(WebNotifications.NewBadge.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Badge>();

            var baseUri = await _urlHelper.GetBaseUrlAsync();

            var url = _urlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
            {
                ["area"]       = "Plato.Users.Badges",
                ["controller"] = "Profile",
                ["action"]     = "Index",
                ["opts.id"]    = context.Notification.To.Id,
                ["opts.alias"] = context.Notification.To.Alias
            });

            //// Build notification
            var userNotification = new UserNotification()
            {
                NotificationName = context.Notification.Type.Name,
                UserId           = context.Notification.To.Id,
                Title            = context.Model.Title,
                Message          = $"{S["You've earned the"].Value} '{context.Model.Title}' {S["badge"].Value}",
                Url           = url,
                CreatedUserId = context.Notification.From?.Id ?? 0,
                CreatedDate   = DateTimeOffset.UtcNow
            };

            // Create notification
            var userNotificationResult = await _userNotificationManager.CreateAsync(userNotification);

            if (userNotificationResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(userNotificationResult.Errors?.ToArray()));
        }