added basic sorting and score

This commit is contained in:
Yann Esposito 2012-09-24 16:23:08 +02:00
parent 2ab1401f10
commit 1496ee08a8
3 changed files with 24 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import Import
import Handler.Helper
import Yesod.Auth
import Data.Maybe
import Data.List (sortBy)
-- |The `EntryRequest` correspond to the data needed
-- to create a new entry.
@ -55,6 +56,23 @@ currentCreator :: EntryGeneric backend -- ^ The entry
-> Bool
currentCreator entry userId = entryCreator entry == userId
score :: UTCTime -> Entry -> Double
score currentTime entry =
3600 * (log (max (abs votesdiff) 1)) / age
where
age :: Double
age = (realToFrac $ diffUTCTime currentTime (entryCreated entry))
votesdiff = fromIntegral (entryYeah entry - entryNeah entry)
sortWith :: Ord b => (a -> b) -> [a] -> [a]
sortWith f xs =
map fst $ sortBy cmpByFst $ map (\x -> (x, f x)) xs
where
cmpByFst (_,v1) (_,v2) = compare v1 v2
fromEntity (Entity _ x) = x
-- |the name `getHomeR` is for
-- |handle the request `GET` on the resource HomeR
getHomeR :: Handler RepHtml
@ -91,7 +109,8 @@ getHomeR = do
creators <- selectList [UserId ==. entryCreator entry] [LimitTo 1]
return (entryId,creators)
mapM getCreatorOfEntry entries
return (entries, votes, creators)
return (reverse $ sortWith ((score currentTime).fromEntity) entries, votes, creators)
-- We return some HTML (not full)
defaultLayout $ do

View file

@ -33,7 +33,8 @@ $else
<a href=#{fromJust $ entryUrl entry}>
#{entryTitle entry} <span .light>»</span>
<div .secondline>
Submitted #{humanReadableRelativeTime currentTime (entryCreated entry)} by #{creatorOfEntry entryId creators}
<span .score>score: #{show $ floor $ score currentTime entry}
<span .age>submitted #{humanReadableRelativeTime currentTime (entryCreated entry)} by #{creatorOfEntry entryId creators}
<div .thirdline .actions>
<a class="comment" href="@{EntryR entryId}?action=comment#commentbutton">discuss</a>
$maybe userId <- currentUserId

View file

@ -194,8 +194,8 @@ form {
overflow: hidden;
line-height: 1em; }
.firstline { line-height: 1.3em; height: 1.3em; }
.secondline { height: 1em; }
.secondline { font-size: .6em; color: #{base1}; margin: 0.3em 0 .4em; }
.secondline { height: 1.4em; }
.secondline { font-size: .6em; color: #{base1}; margin: 0.3em 0 0; }
.thirdline { font-size: .8em; height: 1em }
#loginmessage,#popin {