Exemplo n.º 1
0
        public DisplayPackageViewModel(Package package, User currentUser, PackageDeprecation deprecation)
            : this(package, currentUser, (string)null)
        {
            HasSemVer2Version    = NuGetVersion.IsSemVer2;
            HasSemVer2Dependency = package.Dependencies.ToList()
                                   .Where(pd => !string.IsNullOrEmpty(pd.VersionSpec))
                                   .Select(pd => VersionRange.Parse(pd.VersionSpec))
                                   .Any(p => (p.HasUpperBound && p.MaxVersion.IsSemVer2) || (p.HasLowerBound && p.MinVersion.IsSemVer2));

            Dependencies = new DependencySetsViewModel(package.Dependencies);

            var packageHistory = package
                                 .PackageRegistration
                                 .Packages
                                 .OrderByDescending(p => new NuGetVersion(p.Version))
                                 .ToList();

            PackageVersions = packageHistory.Select(p => new DisplayPackageViewModel(p, currentUser, GetPushedBy(p, currentUser))).ToList();

            PushedBy        = GetPushedBy(package, currentUser);
            PackageFileSize = package.PackageFileSize;

            LatestSymbolsPackage          = package.LatestSymbolPackage();
            LatestAvailableSymbolsPackage = LatestSymbolsPackage != null && LatestSymbolsPackage.StatusKey == PackageStatus.Available
                ? LatestSymbolsPackage
                : package.LatestAvailableSymbolPackage();

            if (packageHistory.Any())
            {
                // calculate the number of days since the package registration was created
                // round to the nearest integer, with a min value of 1
                // divide the total download count by this number
                TotalDaysSinceCreated   = Convert.ToInt32(Math.Max(1, Math.Round((DateTime.UtcNow - packageHistory.Min(p => p.Created)).TotalDays)));
                DownloadsPerDay         = TotalDownloadCount / TotalDaysSinceCreated; // for the package
                DownloadsPerDayLabel    = DownloadsPerDay < 1 ? "<1" : DownloadsPerDay.ToNuGetNumberString();
                IsDotnetToolPackageType = package.PackageTypes.Any(e => e.Name.Equals("DotnetTool", StringComparison.OrdinalIgnoreCase));
            }

            if (deprecation != null)
            {
                AlternatePackageId = deprecation.AlternatePackageRegistration?.Id;

                var alternatePackage = deprecation.AlternatePackage;
                if (alternatePackage != null)
                {
                    // A deprecation should not have both an alternate package registration and an alternate package.
                    // In case a deprecation does have both, we will hide the alternate package registration's ID in this model.
                    AlternatePackageId      = alternatePackage?.Id;
                    AlternatePackageVersion = alternatePackage?.Version;
                }

                CustomMessage = deprecation.CustomMessage;
            }
        }
        private DisplayPackageViewModel SetupInternal(
            DisplayPackageViewModel viewModel,
            Package package,
            User currentUser,
            PackageDeprecation deprecation,
            string readMeHtml)
        {
            viewModel.HasSemVer2Version    = viewModel.NuGetVersion.IsSemVer2;
            viewModel.HasSemVer2Dependency = package.Dependencies.ToList()
                                             .Where(pd => !string.IsNullOrEmpty(pd.VersionSpec))
                                             .Select(pd => VersionRange.Parse(pd.VersionSpec))
                                             .Any(p => (p.HasUpperBound && p.MaxVersion.IsSemVer2) || (p.HasLowerBound && p.MinVersion.IsSemVer2));

            viewModel.Dependencies = new DependencySetsViewModel(package.Dependencies);

            var packageHistory = package
                                 .PackageRegistration
                                 .Packages
                                 .OrderByDescending(p => new NuGetVersion(p.Version))
                                 .ToList();
            var pushedByCache = new Dictionary <User, string>();

            viewModel.PackageVersions = packageHistory
                                        .Select(
                p =>
            {
                var vm = new DisplayPackageViewModel();
                _listPackageItemViewModelFactory.Setup(vm, p, currentUser);
                return(SetupCommon(vm, p, GetPushedBy(p, currentUser, pushedByCache)));
            })
                                        .ToList();

            viewModel.PushedBy        = GetPushedBy(package, currentUser, pushedByCache);
            viewModel.PackageFileSize = package.PackageFileSize;

            viewModel.LatestSymbolsPackage          = package.LatestSymbolPackage();
            viewModel.LatestAvailableSymbolsPackage = viewModel.LatestSymbolsPackage != null && viewModel.LatestSymbolsPackage.StatusKey == PackageStatus.Available
                ? viewModel.LatestSymbolsPackage
                : package.LatestAvailableSymbolPackage();

            if (packageHistory.Any())
            {
                // calculate the number of days since the package registration was created
                // round to the nearest integer, with a min value of 1
                // divide the total download count by this number
                viewModel.TotalDaysSinceCreated   = Convert.ToInt32(Math.Max(1, Math.Round((DateTime.UtcNow - packageHistory.Min(p => p.Created)).TotalDays)));
                viewModel.DownloadsPerDay         = viewModel.TotalDownloadCount / viewModel.TotalDaysSinceCreated; // for the package
                viewModel.DownloadsPerDayLabel    = viewModel.DownloadsPerDay < 1 ? "<1" : viewModel.DownloadsPerDay.ToNuGetNumberString();
                viewModel.IsDotnetToolPackageType = package.PackageTypes.Any(e => e.Name.Equals("DotnetTool", StringComparison.OrdinalIgnoreCase));
            }

            if (deprecation != null)
            {
                viewModel.AlternatePackageId = deprecation.AlternatePackageRegistration?.Id;

                var alternatePackage = deprecation.AlternatePackage;
                if (alternatePackage != null)
                {
                    // A deprecation should not have both an alternate package registration and an alternate package.
                    // In case a deprecation does have both, we will hide the alternate package registration's ID in this model.
                    viewModel.AlternatePackageId      = alternatePackage?.Id;
                    viewModel.AlternatePackageVersion = alternatePackage?.Version;
                }

                viewModel.CustomMessage = deprecation.CustomMessage;
            }

            viewModel.ReadMeHtml      = readMeHtml;
            viewModel.HasEmbeddedIcon = package.HasEmbeddedIcon;

            return(viewModel);
        }
Exemplo n.º 3
0
        private DisplayPackageViewModel SetupInternal(
            DisplayPackageViewModel viewModel,
            Package package,
            IReadOnlyCollection <Package> allVersions,
            User currentUser,
            IReadOnlyDictionary <int, PackageDeprecation> packageKeyToDeprecation,
            IReadOnlyList <PackageRename> packageRenames,
            RenderedReadMeResult readmeResult)
        {
            var dependencies = package.Dependencies.ToList();

            viewModel.Dependencies = new DependencySetsViewModel(dependencies);

            var packageHistory = allVersions
                                 .OrderByDescending(p => new NuGetVersion(p.Version))
                                 .ToList();
            var pushedByCache = new Dictionary <User, string>();

            viewModel.PackageVersions = packageHistory
                                        .Select(
                p =>
            {
                var vm = new DisplayPackageViewModel();
                _listPackageItemViewModelFactory.Setup(vm, p, currentUser);
                return(SetupCommon(vm, p, GetPushedBy(p, currentUser, pushedByCache), packageKeyToDeprecation));
            })
                                        .ToList();

            viewModel.PushedBy        = GetPushedBy(package, currentUser, pushedByCache);
            viewModel.PackageFileSize = package.PackageFileSize;

            viewModel.LatestSymbolsPackage          = package.LatestSymbolPackage();
            viewModel.LatestAvailableSymbolsPackage = viewModel.LatestSymbolsPackage != null && viewModel.LatestSymbolsPackage.StatusKey == PackageStatus.Available
                ? viewModel.LatestSymbolsPackage
                : package.LatestAvailableSymbolPackage();

            if (packageHistory.Any())
            {
                // calculate the number of days since the package registration was created
                // round to the nearest integer, with a min value of 1
                // divide the total download count by this number
                viewModel.TotalDaysSinceCreated = Convert.ToInt32(Math.Max(1, Math.Round((DateTime.UtcNow - packageHistory.Min(p => p.Created)).TotalDays)));
                viewModel.DownloadsPerDay       = viewModel.TotalDownloadCount / viewModel.TotalDaysSinceCreated; // for the package
                viewModel.DownloadsPerDayLabel  = viewModel.DownloadsPerDay < 1 ? "<1" : viewModel.DownloadsPerDay.ToNuGetNumberString();

                // Lazily load the package types from the database.
                viewModel.IsDotnetToolPackageType        = package.PackageTypes.Any(e => e.Name.Equals("DotnetTool", StringComparison.OrdinalIgnoreCase));
                viewModel.IsDotnetNewTemplatePackageType = package.PackageTypes.Any(e => e.Name.Equals("Template", StringComparison.OrdinalIgnoreCase));
            }

            if (packageKeyToDeprecation != null && packageKeyToDeprecation.TryGetValue(package.Key, out var deprecation))
            {
                viewModel.AlternatePackageId = deprecation.AlternatePackageRegistration?.Id;

                var alternatePackage = deprecation.AlternatePackage;
                if (alternatePackage != null)
                {
                    // A deprecation should not have both an alternate package registration and an alternate package.
                    // In case a deprecation does have both, we will hide the alternate package registration's ID in this model.
                    viewModel.AlternatePackageId      = alternatePackage?.Id;
                    viewModel.AlternatePackageVersion = alternatePackage?.Version;
                }

                viewModel.CustomMessage = deprecation.CustomMessage;
            }

            if (packageRenames != null && packageRenames.Count != 0)
            {
                viewModel.PackageRenames = packageRenames;
                if (package.PackageRegistration?.RenamedMessage != null)
                {
                    viewModel.RenamedMessage = package.PackageRegistration.RenamedMessage;
                }
            }

            viewModel.ReadMeHtml            = readmeResult?.Content;
            viewModel.ReadMeImagesRewritten = readmeResult != null ? readmeResult.ImagesRewritten : false;
            viewModel.HasEmbeddedIcon       = package.HasEmbeddedIcon;

            return(viewModel);
        }