예제 #1
0
        public async Task <IActionResult> Index()
        {
            PageTitle = HttpContext.Items[ItemKey.SiteName].ToString();
            var site = await GetCurrentSiteAsync();

            if (AuthUser.Identity.IsAuthenticated)
            {
                if (TempData.ContainsKey(TempDataKey.UserJoined))
                {
                    if (UserHasPermission(Permission.AccessMissionControl))
                    {
                        if (GetSiteStage() == SiteStage.BeforeRegistration ||
                            GetSiteStage() == SiteStage.AccessClosed)
                        {
                            return(RedirectToAction(nameof(MissionControl.HomeController.Index),
                                                    HomeController.Name,
                                                    new { Area = nameof(MissionControl) }));
                        }
                    }
                    else if (UserHasPermission(Permission.AccessPerformerRegistration))
                    {
                        var dates = await _performerSchedulingService.GetSettingsAsync();

                        var schedulingStage
                            = _performerSchedulingService.GetSchedulingStage(dates);

                        if (schedulingStage != PsSchedulingStage.Unavailable)
                        {
                            TempData.Remove(TempDataKey.UserJoined);
                            return(RedirectToAction(
                                       nameof(PerformerRegistration.HomeController.Index),
                                       Name,
                                       new { Area = nameof(PerformerRegistration) }));
                        }
                    }
                }

                User user;
                // signed-in users can view the dashboard
                try
                {
                    user = await _userService.GetDetails(GetActiveUserId());
                }
                catch (GraException gex)
                {
                    // this likely means the currently authenticated user isn't valid anymore
                    _logger.LogError(gex,
                                     "Problem displaying index for user {GetActiveUserId}: {Message}",
                                     GetActiveUserId(),
                                     gex.Message);
                    return(RedirectToAction(nameof(SignOut)));
                }

                await _vendorCodeService.PopulateVendorCodeStatusAsync(user);

                var userLogs = await _userService.GetPaginatedUserHistoryAsync(user.Id,
                                                                               new UserLogFilter(take : BadgesToDisplay)
                {
                    HasBadge = true
                });

                foreach (var userLog in userLogs.Data)
                {
                    userLog.BadgeFilename
                        = _pathResolver.ResolveContentPath(userLog.BadgeFilename);
                }

                var pointTranslation = await _activityService.GetUserPointTranslationAsync();

                var viewModel = new DashboardViewModel
                {
                    User        = user,
                    SingleEvent = pointTranslation.IsSingleEvent,
                    ActivityDescriptionPlural = pointTranslation.ActivityDescriptionPlural,
                    UserLogs = userLogs.Data,
                    DisableSecretCode
                                    = await GetSiteSettingBoolAsync(SiteSettingKey.SecretCode.Disable),
                    UpcomingStreams = await _eventService
                                      .GetUpcomingStreamListAsync()
                };

                try
                {
                    viewModel.SiteStage = (SiteStage)HttpContext.Items[ItemKey.SiteStage];
                }
                catch (Exception)
                {
                    viewModel.SiteStage = SiteStage.Unknown;
                }

                var program = await _siteService.GetProgramByIdAsync(user.ProgramId);

                if (program.DailyLiteracyTipId.HasValue)
                {
                    var day = _siteLookupService.GetSiteDay(site);
                    if (day.HasValue)
                    {
                        var image = await _dailyLiteracyTipService.GetImageByDayAsync(
                            program.DailyLiteracyTipId.Value, day.Value);

                        if (image != null)
                        {
                            var imagePath = Path.Combine($"site{site.Id}",
                                                         "dailyimages",
                                                         $"dailyliteracytip{program.DailyLiteracyTipId}",
                                                         $"{image.Name}{image.Extension}");

                            if (System.IO.File.Exists(_pathResolver
                                                      .ResolveContentFilePath(imagePath)))
                            {
                                var dailyLiteracyTip = await _dailyLiteracyTipService
                                                       .GetByIdAsync(program.DailyLiteracyTipId.Value);

                                viewModel.DailyImageLarge   = dailyLiteracyTip.IsLarge;
                                viewModel.DailyImageMessage = dailyLiteracyTip.Message;
                                viewModel.DailyImagePath
                                    = _pathResolver.ResolveContentPath(imagePath);
                            }
                        }
                    }
                }

                if (TempData.ContainsKey(TempDataKey.UserJoined))
                {
                    TempData.Remove(TempDataKey.UserJoined);
                    viewModel.FirstTimeParticipant = user.IsFirstTime;
                    viewModel.ProgramName          = program.Name;
                    viewModel.UserJoined           = true;
                }

                var userAvatar = await _avatarService.GetUserAvatarAsync();

                if (userAvatar?.Count > 0)
                {
                    var avatarElements = userAvatar;
                    foreach (var element in avatarElements)
                    {
                        element.Filename = _pathResolver.ResolveContentPath(element.Filename);
                    }
                    viewModel.AvatarElements = avatarElements;
                }

                var dashboardPage = await _dashboardContentService.GetCurrentContentAsync();

                if (dashboardPage != null && !string.IsNullOrWhiteSpace(dashboardPage.Content))
                {
                    viewModel.DashboardPageContent = CommonMark.CommonMarkConverter
                                                     .Convert(dashboardPage.Content);
                }

                viewModel.Carousel = await _carouselService.GetCurrentForDashboardAsync();

                if (TempData.ContainsKey(ModelData))
                {
                    var model = Newtonsoft.Json.JsonConvert
                                .DeserializeObject <DashboardViewModel>((string)TempData[ModelData]);
                    viewModel.ActivityAmount = model.ActivityAmount;
                    viewModel.Title          = model.Title;
                    viewModel.Author         = model.Author;
                }
                if (TempData.ContainsKey(ActivityErrorMessage))
                {
                    ModelState.AddModelError("ActivityAmount",
                                             _sharedLocalizer[(string)TempData[ActivityErrorMessage]]);
                    viewModel.ActivityAmount = null;
                }
                if (TempData.ContainsKey(TitleErrorMessage))
                {
                    ModelState.AddModelError(DisplayNames.Title,
                                             _sharedLocalizer[(string)TempData[TitleErrorMessage]]);
                }
                if (TempData.ContainsKey(AuthorErrorMessage))
                {
                    ModelState.AddModelError(DisplayNames.Author,
                                             _sharedLocalizer[(string)TempData[AuthorErrorMessage]]);
                }
                if (TempData.ContainsKey(SecretCodeMessage))
                {
                    viewModel.SecretCodeMessage
                        = _sharedLocalizer[(string)TempData[SecretCodeMessage]];
                }

                if (user.DailyPersonalGoal.HasValue)
                {
                    var programDays = (int)Math.Ceiling((
                                                            site.ProgramEnds.Value - site.ProgramStarts.Value).TotalDays);
                    viewModel.TotalProgramGoal = programDays * user.DailyPersonalGoal.Value;
                    viewModel.ActivityEarned   = await _activityService.GetActivityEarnedAsync();
                }
                else
                {
                    viewModel.TotalProgramGoal = program.AchieverPointAmount;
                    viewModel.ActivityEarned   = user.PointsEarned;
                    viewModel.ProgressMessage
                        = _sharedLocalizer[Annotations.Info.Goal, program.AchieverPointAmount];
                }
                viewModel.PercentComplete = Math.Min(
                    (int)(viewModel.ActivityEarned * 100 / viewModel.TotalProgramGoal), 100);

                var userVendorCode = await _vendorCodeService.GetUserVendorCodeAsync(user.Id);

                if (userVendorCode?.CanBeDonated == true && userVendorCode.IsDonated == null &&
                    (!userVendorCode.ExpirationDate.HasValue ||
                     userVendorCode.ExpirationDate.Value > _dateTimeProvider.Now))
                {
                    viewModel.HasPendingVendorCodeQuestion = true;
                    viewModel.VendorCodeExpiration         = userVendorCode.ExpirationDate;
                }

                return(View(ViewTemplates.Dashboard, viewModel));
            }
            else
            {
                return(await ShowLandingPageAsync(site, GetSiteStage()));
            }
        }