1
1
{-# LANGUAGE RecordWildCards, NamedFieldPuns, TypeApplications #-}
2
2
module Distribution.Server.Features.Browse.ApplyFilter (applyFilter ) where
3
3
4
+ import Control.Monad (filterM )
4
5
import Data.List (sortBy )
5
6
import Data.Ord (comparing )
6
7
import Data.Time.Clock (UTCTime (utctDay ), diffUTCTime )
@@ -13,18 +14,21 @@ import qualified Data.Set as S
13
14
import Distribution.Server.Features.Browse.Options (BrowseOptions (.. ), Direction (.. ), Column (.. ), Sort (.. ), NormalColumn (.. ), IsSearch (.. ))
14
15
import Distribution.Server.Features.Browse.Parsers (DeprecatedOption (.. ), Filter (.. ), operatorToFunction )
15
16
import Distribution.Server.Features.Core (CoreResource , corePackageNameUri )
17
+ import Distribution.Server.Features.Distro (DistroFeature (DistroFeature , queryPackageStatus ))
18
+ import Distribution.Server.Features.Distro.Types (DistroName (.. ))
16
19
import Distribution.Server.Features.PackageList (PackageItem (.. ))
17
20
import Distribution.Server.Features.Tags (Tag (.. ), TagsResource , tagUri )
18
21
import Distribution.Server.Features.Users (UserResource , userPageUri )
19
22
import Distribution.Server.Users.Types (UserName )
20
23
import Distribution.Text (display )
21
24
22
- applyFilter :: UTCTime -> IsSearch -> CoreResource -> UserResource -> TagsResource -> BrowseOptions -> [PackageItem ] -> [Value ]
23
- applyFilter now isSearch coreResource userResource tagsResource browseOptions items =
24
- map packageIndexInfoToValue $
25
- sort isSearch (boSort browseOptions) $
26
- filterPackages now (boFilters browseOptions) $
27
- items
25
+ applyFilter :: UTCTime -> IsSearch -> CoreResource -> UserResource -> TagsResource -> DistroFeature -> BrowseOptions -> [PackageItem ] -> IO [Value ]
26
+ applyFilter now isSearch coreResource userResource tagsResource DistroFeature {queryPackageStatus} browseOptions items = do
27
+ packages <- filterM filterForItem items
28
+ pure $
29
+ map packageIndexInfoToValue $
30
+ sort isSearch (boSort browseOptions) $
31
+ packages
28
32
where
29
33
packageIndexInfoToValue :: PackageItem -> Value
30
34
packageIndexInfoToValue PackageItem {.. } =
@@ -55,6 +59,33 @@ applyFilter now isSearch coreResource userResource tagsResource browseOptions it
55
59
, T. pack " display" .= display pkg
56
60
]
57
61
62
+ includeItem :: PackageItem -> Filter -> IO Bool
63
+ includeItem PackageItem { itemDownloads } (DownloadsFilter ( op, sndParam)) = pure $ operatorToFunction op (fromIntegral @ Int @ Word itemDownloads) sndParam
64
+ includeItem PackageItem { itemVotes } (RatingFilter (op, sndParam) ) = pure $ operatorToFunction op itemVotes sndParam
65
+ includeItem PackageItem { itemLastUpload } (LastUploadFilter (op, sndParam)) = pure $ operatorToFunction op (utctDay itemLastUpload) sndParam
66
+ includeItem PackageItem { itemTags } (TagFilter tagStr) = pure $ any (\ tag -> display tag == tagStr) itemTags
67
+ includeItem PackageItem { itemMaintainer } (MaintainerFilter maintainerStr) = pure $ any (\ user -> display user == maintainerStr) itemMaintainer
68
+ includeItem PackageItem { itemLastUpload } (AgeLastULFilter (op, sndParam)) = pure $ operatorToFunction op (diffUTCTime now itemLastUpload) sndParam
69
+ includeItem PackageItem { itemDeprecated } (DeprecatedFilter OnlyDeprecated ) = pure $ not (null itemDeprecated)
70
+ includeItem PackageItem { itemDeprecated } (DeprecatedFilter ExcludeDeprecated ) = pure $ null itemDeprecated
71
+ includeItem _ (DeprecatedFilter Don'tCareAboutDeprecated ) = pure True
72
+ includeItem PackageItem { itemName } (DistroFilter distroStr) = elem (DistroName distroStr) . map fst <$> queryPackageStatus itemName
73
+ includeItem packageItem (Not filt) = not <$> includeItem packageItem filt
74
+
75
+ filtersWithoutDefaults = boFilters browseOptions
76
+
77
+ filtersWithDefaults =
78
+ -- The lack of other filters means we don't care.
79
+ -- But deprecated packages are excluded by default.
80
+ -- So we check if the user has overriden the default filter.
81
+ case [ x | DeprecatedFilter x <- filtersWithoutDefaults ] of
82
+ [] -> DeprecatedFilter ExcludeDeprecated : filtersWithoutDefaults
83
+ _ -> filtersWithoutDefaults
84
+
85
+ filterForItem :: PackageItem -> IO Bool
86
+ filterForItem item =
87
+ all id <$> traverse (includeItem item) filtersWithDefaults
88
+
58
89
sort :: IsSearch -> Sort -> [PackageItem ] -> [PackageItem ]
59
90
sort isSearch Sort {sortColumn, sortDirection} =
60
91
case sortColumn of
@@ -79,28 +110,3 @@ sort isSearch Sort {sortColumn, sortDirection} =
79
110
case sortDirection of
80
111
Ascending -> id
81
112
Descending -> flip
82
-
83
- includeItem :: UTCTime -> PackageItem -> Filter -> Bool
84
- includeItem _ PackageItem { itemDownloads } (DownloadsFilter ( op, sndParam)) = operatorToFunction op (fromIntegral @ Int @ Word itemDownloads) sndParam
85
- includeItem _ PackageItem { itemVotes } (RatingFilter (op, sndParam) ) = operatorToFunction op itemVotes sndParam
86
- includeItem _ PackageItem { itemLastUpload } (LastUploadFilter (op, sndParam)) = operatorToFunction op (utctDay itemLastUpload) sndParam
87
- includeItem _ PackageItem { itemTags } (TagFilter tagStr) = any (\ tag -> display tag == tagStr) itemTags
88
- includeItem _ PackageItem { itemMaintainer } (MaintainerFilter maintainerStr) = any (\ user -> display user == maintainerStr) itemMaintainer
89
- includeItem now PackageItem { itemLastUpload } (AgeLastULFilter (op, sndParam)) = operatorToFunction op (diffUTCTime now itemLastUpload) sndParam
90
- includeItem _ PackageItem { itemDeprecated } (DeprecatedFilter OnlyDeprecated ) = not (null itemDeprecated)
91
- includeItem _ PackageItem { itemDeprecated } (DeprecatedFilter ExcludeDeprecated ) = null itemDeprecated
92
- includeItem _ _ (DeprecatedFilter Don'tCareAboutDeprecated ) = True
93
- includeItem now packageItem (Not filt) = not (includeItem now packageItem filt)
94
-
95
- filterPackages :: UTCTime -> [Filter ] -> [PackageItem ] -> [PackageItem ]
96
- filterPackages now filtersWithoutDefaults = filter filterForItem
97
- where
98
- filterForItem :: PackageItem -> Bool
99
- filterForItem item = all (includeItem now item) filtersWithDefaults
100
- filtersWithDefaults =
101
- -- The lack of other filters means we don't care.
102
- -- But deprecated packages are excluded by default.
103
- -- So we check if the user has overriden the default filter.
104
- case [ x | DeprecatedFilter x <- filtersWithoutDefaults ] of
105
- [] -> DeprecatedFilter ExcludeDeprecated : filtersWithoutDefaults
106
- _ -> filtersWithoutDefaults
0 commit comments