mirror of
https://github.com/commercialhaskell/stackage-server.git
synced 2026-01-21 08:21:55 +01:00
Added like button functionality (#15)
This commit is contained in:
parent
3d8852dfcd
commit
9c229d2d4e
@ -10,6 +10,7 @@ import qualified Data.Text.Encoding as T
|
|||||||
import Data.Time (addUTCTime)
|
import Data.Time (addUTCTime)
|
||||||
import Database.Esqueleto ((^.), (&&.), Value (Value))
|
import Database.Esqueleto ((^.), (&&.), Value (Value))
|
||||||
import qualified Database.Esqueleto as E
|
import qualified Database.Esqueleto as E
|
||||||
|
import qualified Database.Persist as P
|
||||||
import Formatting
|
import Formatting
|
||||||
import Import
|
import Import
|
||||||
import Text.Email.Validate
|
import Text.Email.Validate
|
||||||
@ -22,7 +23,8 @@ getPackageR pn = do
|
|||||||
asInt = id
|
asInt = id
|
||||||
haddocksLink ident version =
|
haddocksLink ident version =
|
||||||
HaddockR ident [concat [toPathPiece pn, "-", toPathPiece version]]
|
HaddockR ident [concat [toPathPiece pn, "-", toPathPiece version]]
|
||||||
(packages, downloads, recentDownloads, Entity _ metadata) <- runDB $ do
|
muid <- maybeAuthId
|
||||||
|
(packages, downloads, recentDownloads, nLikes, liked, Entity _ metadata) <- runDB $ do
|
||||||
packages <- fmap (map reformat) $ E.select $ E.from $ \(p, s) -> do
|
packages <- fmap (map reformat) $ E.select $ E.from $ \(p, s) -> do
|
||||||
E.where_ $ (p ^. PackageStackage E.==. s ^. StackageId)
|
E.where_ $ (p ^. PackageStackage E.==. s ^. StackageId)
|
||||||
&&. (p ^. PackageName' E.==. E.val pn)
|
&&. (p ^. PackageName' E.==. E.val pn)
|
||||||
@ -31,12 +33,19 @@ getPackageR pn = do
|
|||||||
E.limit maxSnaps
|
E.limit maxSnaps
|
||||||
--selectList [PackageName' ==. pn] [LimitTo 10, Desc PackageStackage]
|
--selectList [PackageName' ==. pn] [LimitTo 10, Desc PackageStackage]
|
||||||
return (p ^. PackageVersion, s ^. StackageTitle, s ^. StackageIdent, s ^. StackageHasHaddocks)
|
return (p ^. PackageVersion, s ^. StackageTitle, s ^. StackageIdent, s ^. StackageHasHaddocks)
|
||||||
|
nLikes <- count [LikePackage ==. pn]
|
||||||
|
let getLiked uid = (>0) <$> count [LikePackage ==. pn, LikeVoter ==. uid]
|
||||||
|
liked <- maybe (return False) getLiked muid
|
||||||
downloads <- count [DownloadPackage ==. pn]
|
downloads <- count [DownloadPackage ==. pn]
|
||||||
now <- liftIO getCurrentTime
|
now <- liftIO getCurrentTime
|
||||||
let nowMinus30 = addUTCTime (-30 * 24 * 60 * 60) now
|
let nowMinus30 = addUTCTime (-30 * 24 * 60 * 60) now
|
||||||
recentDownloads <- count [DownloadPackage ==. pn, DownloadTimestamp >=. nowMinus30]
|
recentDownloads <- count [DownloadPackage ==. pn, DownloadTimestamp >=. nowMinus30]
|
||||||
metadata <- getBy404 (UniqueMetadata pn)
|
metadata <- getBy404 (UniqueMetadata pn)
|
||||||
return (packages, downloads, recentDownloads, metadata)
|
|
||||||
|
return (packages, downloads, recentDownloads, nLikes, liked, metadata)
|
||||||
|
|
||||||
|
let likedClass = if liked then "fa-thumbs-up" else "fa-thumbs-o-up" :: Text
|
||||||
|
|
||||||
let deps = enumerate (metadataDeps metadata)
|
let deps = enumerate (metadataDeps metadata)
|
||||||
authors = enumerate (parseIdentitiesLiberally (metadataAuthor metadata))
|
authors = enumerate (parseIdentitiesLiberally (metadataAuthor metadata))
|
||||||
maintainers = let ms = enumerate (parseIdentitiesLiberally (metadataMaintainer metadata))
|
maintainers = let ms = enumerate (parseIdentitiesLiberally (metadataMaintainer metadata))
|
||||||
@ -151,3 +160,19 @@ renderEmail = T.decodeUtf8 . toByteString
|
|||||||
-- | Format a number with commas nicely.
|
-- | Format a number with commas nicely.
|
||||||
formatNum :: Int -> Text
|
formatNum :: Int -> Text
|
||||||
formatNum = sformat commas
|
formatNum = sformat commas
|
||||||
|
|
||||||
|
postPackageLikeR :: PackageName -> Handler ()
|
||||||
|
postPackageLikeR packageName = maybeAuthId >>= \muid -> case muid of
|
||||||
|
Nothing -> return ()
|
||||||
|
Just uid -> do
|
||||||
|
runDB $ P.insert $ Like packageName uid
|
||||||
|
return ()
|
||||||
|
|
||||||
|
postPackageUnlikeR :: PackageName -> Handler ()
|
||||||
|
postPackageUnlikeR name = maybeAuthId >>= \muid -> case muid of
|
||||||
|
Nothing -> return ()
|
||||||
|
Just uid -> do
|
||||||
|
runDB $ E.delete $ E.from $ \like ->
|
||||||
|
E.where_ $ like ^. LikePackage E.==. E.val name
|
||||||
|
&&. like ^. LikeVoter E.==. E.val uid
|
||||||
|
return ()
|
||||||
|
|||||||
@ -47,6 +47,10 @@ Tag
|
|||||||
tag Slug
|
tag Slug
|
||||||
voter UserId
|
voter UserId
|
||||||
|
|
||||||
|
Like
|
||||||
|
package PackageName
|
||||||
|
voter UserId
|
||||||
|
|
||||||
Download
|
Download
|
||||||
ident PackageSetIdent Maybe
|
ident PackageSetIdent Maybe
|
||||||
view HackageView Maybe
|
view HackageView Maybe
|
||||||
|
|||||||
@ -27,3 +27,5 @@
|
|||||||
/package/#PackageName PackageR GET
|
/package/#PackageName PackageR GET
|
||||||
/package PackageListR GET
|
/package PackageListR GET
|
||||||
/compressor-status CompressorStatusR GET
|
/compressor-status CompressorStatusR GET
|
||||||
|
/package/#PackageName/like PackageLikeR POST
|
||||||
|
/package/#PackageName/unlike PackageUnlikeR POST
|
||||||
|
|||||||
@ -32,9 +32,11 @@ $newline never
|
|||||||
<button .btn>Add
|
<button .btn>Add
|
||||||
<div .social>
|
<div .social>
|
||||||
<span #likes>
|
<span #likes>
|
||||||
#{formatNum 234}
|
#{nLikes}
|
||||||
\ likes #
|
\ likes #
|
||||||
<i class="fa fa-thumbs-o-up" onclick="this.className = 'fa fa-thumbs-up'; document.getElementById('likes').innerText = '235'"></i>
|
$if isJust muid
|
||||||
|
<i .fa.#{likedClass}
|
||||||
|
onclick="toggleLike(this)"></i>
|
||||||
$if downloads /= 0
|
$if downloads /= 0
|
||||||
<span .downloads>
|
<span .downloads>
|
||||||
$if downloads /= recentDownloads
|
$if downloads /= recentDownloads
|
||||||
|
|||||||
@ -9,3 +9,25 @@ $(function(){
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function toggleLike(el) {
|
||||||
|
var $this = $(el);
|
||||||
|
var action = 'like';
|
||||||
|
if ($this.hasClass('fa-thumbs-up')) {
|
||||||
|
action = 'unlike';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this.toggleClass('fa-thumbs-up');
|
||||||
|
$this.toggleClass('fa-thumbs-o-up');
|
||||||
|
|
||||||
|
$likes = $('#likes');
|
||||||
|
nLikes = parseInt($likes.text(), 10);
|
||||||
|
|
||||||
|
if (action == 'like') {
|
||||||
|
$likes.text(nLikes + 1);
|
||||||
|
$.post("@{PackageLikeR pn}");
|
||||||
|
} else {
|
||||||
|
$likes.text(nLikes - 1);
|
||||||
|
$.post("@{PackageUnlikeR pn}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -49,6 +49,9 @@ h3 {
|
|||||||
.fa-thumbs-o-up:hover {
|
.fa-thumbs-o-up:hover {
|
||||||
color: #3f4549
|
color: #3f4549
|
||||||
}
|
}
|
||||||
|
.fa-thumbs-up {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.dependencies {
|
.dependencies {
|
||||||
margin-top: 0.5em;
|
margin-top: 0.5em;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user