diff --git a/config/models b/config/models index ee4023d..d32d5b3 100644 --- a/config/models +++ b/config/models @@ -21,3 +21,11 @@ Profile userId UserId siteId SiteId username Text sqltype=varchar(255) + +Entry + kind EntryKind + name Text maxlen=255 + content Text sqltype=longtext + published UTCTime + updated UTCTime + authorId UserId diff --git a/config/routes b/config/routes index def52b9..6d3980c 100644 --- a/config/routes +++ b/config/routes @@ -7,3 +7,5 @@ /sitemap.xml SitemapR GET / HomeR GET + +!/#EntryKind EntriesR GET diff --git a/src/Application.hs b/src/Application.hs index bdbce5b..1433fc8 100644 --- a/src/Application.hs +++ b/src/Application.hs @@ -6,6 +6,7 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Application ( getApplicationDev @@ -48,6 +49,7 @@ import System.Log.FastLogger (defaultBufSize, newStdoutLoggerSet, -- Import all relevant handler modules here. -- Don't forget to add new modules to your cabal file! import Handler.Common +import Handler.Entries import Handler.Home -- This line actually creates our YesodDispatch instance. It is the second half diff --git a/src/Entry/Kind.hs b/src/Entry/Kind.hs new file mode 100644 index 0000000..c11427a --- /dev/null +++ b/src/Entry/Kind.hs @@ -0,0 +1,29 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskell #-} +module Entry.Kind where + +import Database.Persist.TH ( derivePersistField ) +import Yesod.Core.Dispatch ( PathPiece, toPathPiece, fromPathPiece ) + +import qualified Data.Text as T +import Text.Read ( readMaybe ) + +data EntryKind = Note | Article | Photo | Video | Audio + | Reply | Repost | Like | Favourite | Bookmark | Quote | Rsvp + | Listen | Jam | Watch | Play | Read + deriving (Eq, Read, Show) +derivePersistField "EntryKind" + +singularise :: T.Text -> Maybe EntryKind +singularise "replies" = Just Reply +singularise "watches" = Just Watch +singularise k = readMaybe . T.unpack . T.toTitle . T.init $ k + +pluralise :: EntryKind -> T.Text +pluralise Reply = "replies" +pluralise Watch = "watches" +pluralise k = T.toLower . flip T.snoc 's' . T.pack . show $ k + +instance PathPiece EntryKind where + toPathPiece = pluralise + fromPathPiece = singularise diff --git a/src/Foundation.hs b/src/Foundation.hs index a7fd3db..ca56b0e 100644 --- a/src/Foundation.hs +++ b/src/Foundation.hs @@ -20,6 +20,7 @@ import Yesod.Core.Types (Logger) import qualified Yesod.Core.Unsafe as Unsafe import Package +import Entry.Kind ( EntryKind ) -- | The foundation datatype for your application. This can be a good place to -- keep settings and values requiring initialization before your application diff --git a/src/Handler/Entries.hs b/src/Handler/Entries.hs new file mode 100644 index 0000000..0439ab2 --- /dev/null +++ b/src/Handler/Entries.hs @@ -0,0 +1,12 @@ +{-# LANGUAGE TemplateHaskell #-} +module Handler.Entries where + +import Import + +import qualified Entry.Kind as K +import Widget.Feed ( hFeed ) + +getEntriesR :: K.EntryKind -> Handler Html +getEntriesR kind = do + entries <- runDB $ selectList [EntryKind ==. kind] [Desc EntryPublished] + defaultLayout $(widgetFile "entries") diff --git a/src/Model.hs b/src/Model.hs index 5a9da96..a48d670 100644 --- a/src/Model.hs +++ b/src/Model.hs @@ -13,6 +13,8 @@ import ClassyPrelude.Yesod import Database.Persist.Quasi import Yesod.Auth.HashDB ( HashDBUser(..) ) +import Entry.Kind ( EntryKind ) + -- You can define all of your database entities in the entities file. -- You can find more information on persistent and how to declare entities -- at: diff --git a/src/Widget/Entry.hs b/src/Widget/Entry.hs new file mode 100644 index 0000000..ecf731c --- /dev/null +++ b/src/Widget/Entry.hs @@ -0,0 +1,11 @@ +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskell #-} +module Widget.Entry ( hEntry ) where + +import Import + +hEntry :: Entity Entry -> Widget +hEntry (Entity entryId entry) = do + maybeAuthor <- handlerToWidget . runDB . get . entryAuthorId $ entry + $(widgetFile "mf2/h-entry") diff --git a/src/Widget/Feed.hs b/src/Widget/Feed.hs new file mode 100644 index 0000000..b707dd8 --- /dev/null +++ b/src/Widget/Feed.hs @@ -0,0 +1,8 @@ +{-# LANGUAGE TemplateHaskell #-} +module Widget.Feed ( hFeed ) where + +import Import +import Widget.Entry ( hEntry ) + +hFeed :: [Entity Entry] -> Widget +hFeed entries = $(widgetFile "mf2/h-feed") diff --git a/templates/entries.hamlet b/templates/entries.hamlet new file mode 100644 index 0000000..da892e3 --- /dev/null +++ b/templates/entries.hamlet @@ -0,0 +1 @@ +^{hFeed entries} diff --git a/templates/mf2/h-entry.hamlet b/templates/mf2/h-entry.hamlet new file mode 100644 index 0000000..d5edbca --- /dev/null +++ b/templates/mf2/h-entry.hamlet @@ -0,0 +1,12 @@ +
+ #{entryName entry} +

+ Published + $maybe author <- maybeAuthor + \ by + + #{userFullName author} + on +

+ #{entryContent entry} diff --git a/templates/mf2/h-feed.hamlet b/templates/mf2/h-feed.hamlet new file mode 100644 index 0000000..aad1ce0 --- /dev/null +++ b/templates/mf2/h-feed.hamlet @@ -0,0 +1,3 @@ +
    + $forall entry <- entries +
  1. ^{hEntry entry}