Пример #1
0
        public async Task <IActionResult> Index()
        {
            var showRead = false;

            if (Request.Query.ContainsKey("show-read"))
            {
                showRead = true;
            }

            if (UserContext.ApplicationUser is null)
            {
                throw new Exception("ApplicationUser is null or not authenticated.");
            }

            var hiddenTimeLimit = DateTime.Now.AddDays(-7);
            var recentTimeLimit = DateTime.Now.AddMinutes(-30);

            var notificationQuery = from n in DbContext.Notifications
                                    where n.UserId == UserContext.ApplicationUser.Id
                                    where n.Time > hiddenTimeLimit
                                    where showRead || n.Unread
                                    orderby n.Time descending
                                    select new ViewModels.Notifications.IndexItem {
                Id           = n.Id,
                Type         = n.Type,
                Recent       = n.Time > recentTimeLimit,
                Time         = n.Time,
                TargetUserId = n.TargetUserId
            };

            var notifications = notificationQuery.ToList();
            var users         = await AccountRepository.Records();

            foreach (var notification in notifications)
            {
                if (!string.IsNullOrEmpty(notification.TargetUserId))
                {
                    notification.TargetUser = users.FirstOrDefault(r => r.Id == notification.TargetUserId)?.DecoratedName ?? "User";
                }

                notification.Text = notification.Type switch
                {
                    ENotificationType.Quote => $"{notification.TargetUser} quoted you.",
                    ENotificationType.Reply => $"{notification.TargetUser} replied to your topic.",
                    ENotificationType.Thought => $"{notification.TargetUser} thought about your post.",
                    ENotificationType.Mention => $"{notification.TargetUser} mentioned you.",
                    _ => $"Unknown notification type. {notification.Id} | {notification.Type}"
                };
            }

            var viewModel = new ViewModels.Notifications.IndexPage {
                Notifications = notifications
            };

            return(View(viewModel));
        }
Пример #2
0
        public async Task <IActionResult> Index()
        {
            var showRead = false;

            if (Request.Query.ContainsKey("show-read"))
            {
                showRead = true;
            }

            var notifications = await NotificationRepository.Index(showRead);

            var viewModel = new ViewModels.Notifications.IndexPage {
                Notifications = notifications
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }