-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathModel.hs
198 lines (179 loc) · 5.57 KB
/
Model.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{-# LANGUAGE FlexibleInstances, DeriveDataTypeable #-}
module Model (
-- Model
infoHashByName,
infoHashExists,
Torrent (..),
torrentByName,
purgeTorrent,
ActiveUser (..),
getActiveUsers,
DirectoryEntry (..),
getDirectory,
-- Model.Query
QueryPage (..),
-- Model.Stats
StatsValue (..),
getCounter,
getDownloadCounter,
addCounter,
getGauge,
setGauge,
-- Model.Download
InfoHash (..),
infoHashToHex,
Download (..),
recentDownloads,
mostDownloaded,
userDownloads,
enclosureDownloads,
guidDownloads,
torrentGuids,
torrentDownloads,
feedDownloads,
searchDownloads,
-- Model.Item
Item (..),
groupDownloads,
userItemImage,
-- Model.User
UserName (..),
UserDetails (..),
userDetailsByName,
setUserDetails,
UserSalt (..),
userSalt,
setUserSalted,
registerUser,
userByEmail,
-- Model.Feed
FeedXml (..),
feedXml,
feedEnclosures,
enclosureErrors,
FeedInfo (..),
userFeed,
userFeeds,
userFeedInfo,
addUserFeed,
deleteUserFeed,
feedByUrl,
FeedDetails (..),
userFeedDetails,
setUserFeedDetails,
searchFeeds,
-- Model.Token
Token (..),
generateToken,
validateToken,
peekToken
) where
import Prelude
import Data.Convertible
import Data.Text (Text)
import qualified Data.Text as T
import Data.Data (Typeable)
import qualified Data.ByteString as B
import Database.PostgreSQL.LibPQ (Connection)
import Utils
import Model.SqlValue
import Model.Query
import Model.Download
import Model.Item
import Model.Feed
import Model.User
import Model.Token
import Model.Stats
import PathPieces (DirectoryPage (..))
infoHashByName :: UserName -> Text -> Text -> Query InfoHash
infoHashByName user slug name =
query "SELECT \"info_hash\" FROM user_feeds JOIN enclosures USING (feed) JOIN enclosure_torrents USING (url) JOIN torrents USING (info_hash) WHERE user_feeds.\"user\"=? AND user_feeds.\"slug\"=? AND torrents.\"name\"=?" [convert user, convert slug, convert name]
data Torrent = Torrent {
torrentInfoHash :: InfoHash,
torrentName :: Text,
torrentSize :: Integer,
torrentTorrent :: B.ByteString
} deriving (Show, Typeable)
instance Convertible [SqlValue] Torrent where
safeConvert [info_hash, name, size, torrent] =
Torrent <$>
safeConvert info_hash <*>
safeConvert name <*>
safeConvert size <*>
safeConvert torrent
safeConvert vals = convError "Torrent" vals
newtype InfoHashExists = InfoHashExists Bool
instance Convertible [SqlValue] InfoHashExists where
safeConvert (value:_) =
InfoHashExists <$>
safeConvert value
safeConvert _ =
Right $
InfoHashExists False
infoHashExists :: InfoHash -> Connection -> IO Bool
infoHashExists infoHash conn = do
results <- infoHashExists' infoHash conn
case results of
(InfoHashExists True : _) -> return True
_ -> return False
infoHashExists' :: InfoHash -> Query InfoHashExists
infoHashExists' infoHash =
query "SELECT EXISTS (SELECT \"info_hash\" FROM torrents WHERE \"info_hash\"=?)" [convert infoHash]
torrentByName :: UserName -> Text -> Text -> Query Torrent
torrentByName user slug name =
query "SELECT \"info_hash\", \"name\", \"size\", \"torrent\" FROM user_feeds JOIN enclosures USING (feed) JOIN enclosure_torrents USING (url) JOIN torrents USING (info_hash) WHERE user_feeds.\"user\"=? AND user_feeds.\"slug\"=? AND torrents.\"name\"=?" [convert user, convert slug, convert name]
purgeTorrent :: UserName -> Text -> Text -> Connection -> IO Int
purgeTorrent user slug name db =
head <$>
query1 "SELECT * FROM purge_download(?, ?, ?)"
[convert user, convert slug, convert name]
db
data ActiveUser = ActiveUser
{ activeUser :: UserName
, activeFeeds :: Int
, activeFeedLangs :: Text
, activeFeedTypes :: Text
} deriving (Show, Typeable)
instance Convertible [SqlValue] ActiveUser where
safeConvert [userVal, feedsVal, langsVal, typesVal] =
ActiveUser <$>
safeConvert userVal <*>
safeConvert feedsVal <*>
safeConvert langsVal <*>
safeConvert typesVal
safeConvert vals = convError "ActiveUser" vals
getActiveUsers :: Query ActiveUser
getActiveUsers =
query "SELECT \"user\", \"feeds\", array_to_string(\"langs\", ','), array_to_string(\"types\", ',') FROM active_users" []
data DirectoryEntry = DirectoryEntry
{ dirUser :: UserName
, dirUserTitle :: Text
, dirUserImage :: Text
, dirFeedSlug :: Text
, dirFeedTitle :: Text
, dirFeedLang :: Text
, dirFeedTypes :: Text
} deriving (Show, Typeable)
instance Convertible [SqlValue] DirectoryEntry where
safeConvert [userVal, userTitleVal, userImageVal,
feedSlugVal, feedTitleVal, feedLangVal, feedTypesVal] =
DirectoryEntry <$>
safeConvert userVal <*>
safeConvert userTitleVal <*>
(fixUrl <$> safeConvert userImageVal) <*>
safeConvert feedSlugVal <*>
safeConvert feedTitleVal <*>
safeConvert feedLangVal <*>
safeConvert feedTypesVal
safeConvert vals = convError "DirectoryEntry" vals
getDirectory :: Maybe DirectoryPage -> Query DirectoryEntry
getDirectory mPage =
let (cond, params) =
case mPage of
Nothing ->
("", [])
Just DirectoryDigit ->
(" WHERE \"user\" ~* E'^[^a-z]'", [])
Just (DirectoryLetter c) ->
(" WHERE LOWER(LEFT(\"user\", 1)) = ?", [convert $ T.pack [c]])
in query ("SELECT \"user\", COALESCE(\"title\", \"user\"), COALESCE(\"image\", ''), \"slug\", COALESCE(\"feed_title\", \"slug\"), COALESCE(\"lang\", ''), array_to_string(\"types\", ',') FROM directory" ++ cond) params