2012-03-12 04:32:15 +01:00
|
|
|
|
{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, RecordWildCards,
|
2012-03-12 12:52:48 +01:00
|
|
|
|
TemplateHaskell, TypeFamilies, OverloadedStrings, ScopedTypeVariables, BangPatterns #-}
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
import Control.Applicative ((<$>), optional)
|
|
|
|
|
import Control.Exception (bracket)
|
|
|
|
|
import Control.Monad (msum, mzero)
|
|
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
|
|
|
|
import Control.Monad.Reader (ask)
|
|
|
|
|
import Control.Monad.State (get, put)
|
|
|
|
|
import Control.Monad.Trans (liftIO)
|
|
|
|
|
import Data.Acid
|
|
|
|
|
import Data.Acid.Advanced
|
|
|
|
|
import Data.Acid.Local
|
|
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
|
import Data.Data (Data, Typeable)
|
|
|
|
|
import Data.IxSet (Indexable(..), IxSet(..), (@=), Proxy(..), getOne, ixFun, ixSet)
|
|
|
|
|
import Data.SafeCopy (SafeCopy, base, deriveSafeCopy)
|
|
|
|
|
import Data.Text (Text, pack)
|
|
|
|
|
import Data.Text.Lazy (toStrict)
|
|
|
|
|
import Data.Time
|
2012-03-13 05:31:13 +01:00
|
|
|
|
import System.Environment(getEnv)
|
|
|
|
|
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
import qualified Crypto.Hash.SHA512 as SHA (hash)
|
|
|
|
|
import qualified Data.ByteString.Char8 as B
|
|
|
|
|
import qualified Data.ByteString.Base64 as B64 (encode)
|
|
|
|
|
import qualified Data.IxSet as IxSet
|
|
|
|
|
import qualified Data.Text as Text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{-CouchDB imports-}
|
|
|
|
|
|
2012-03-12 09:47:30 +01:00
|
|
|
|
import Database.CouchDB hiding (runCouchDB')
|
2012-03-12 04:32:15 +01:00
|
|
|
|
import Database.CouchDB.JSON
|
|
|
|
|
import Text.JSON
|
2012-03-12 12:52:48 +01:00
|
|
|
|
import Data.List (intersperse, (\\))
|
2012-03-12 04:32:15 +01:00
|
|
|
|
import System.Locale (defaultTimeLocale)
|
|
|
|
|
|
|
|
|
|
-- data types and acid-state setup
|
|
|
|
|
|
|
|
|
|
newtype EntryId = EntryId { unEntryId :: Integer }
|
|
|
|
|
deriving (Eq, Ord, Data, Enum, Typeable, SafeCopy)
|
|
|
|
|
|
2012-03-12 12:52:48 +01:00
|
|
|
|
instance Show EntryId where
|
|
|
|
|
show = show . unEntryId
|
|
|
|
|
|
2012-03-12 04:32:15 +01:00
|
|
|
|
data BlogLang = EN | DE
|
2012-03-12 12:52:48 +01:00
|
|
|
|
deriving (Eq, Ord, Data, Typeable)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
instance Show BlogLang where
|
2012-03-12 12:52:48 +01:00
|
|
|
|
show DE = "de"
|
|
|
|
|
show EN = "en"
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
$(deriveSafeCopy 0 'base ''BlogLang)
|
|
|
|
|
|
|
|
|
|
data Comment = Comment {
|
2012-03-12 12:52:48 +01:00
|
|
|
|
cauthor :: Text,
|
|
|
|
|
ctext :: Text,
|
|
|
|
|
cdate :: UTCTime
|
|
|
|
|
} deriving (Eq, Ord, Show, Data, Typeable)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
$(deriveSafeCopy 0 'base ''Comment)
|
|
|
|
|
|
|
|
|
|
data Entry = Entry {
|
2012-03-12 12:52:48 +01:00
|
|
|
|
entryId :: EntryId,
|
|
|
|
|
lang :: BlogLang,
|
|
|
|
|
author :: Text,
|
|
|
|
|
title :: Text,
|
|
|
|
|
btext :: Text,
|
|
|
|
|
mtext :: Text,
|
|
|
|
|
edate :: UTCTime,
|
|
|
|
|
tags :: [Text],
|
|
|
|
|
comments :: [Comment]
|
|
|
|
|
} deriving (Eq, Ord, Show, Data, Typeable)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
$(deriveSafeCopy 0 'base ''Entry)
|
|
|
|
|
|
|
|
|
|
-- ixSet requires different datatypes for field indexes, so let's define some
|
|
|
|
|
newtype Author = Author Text deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
|
|
|
|
newtype Title = Title Text deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
|
|
|
|
newtype BText = BText Text deriving (Eq, Ord, Data, Typeable, SafeCopy) -- standard text
|
|
|
|
|
newtype MText = MText Text deriving (Eq, Ord, Data, Typeable, SafeCopy) -- "read more" text
|
2012-03-12 12:52:48 +01:00
|
|
|
|
newtype Tag = Tag Text deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
newtype EDate = EDate UTCTime deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
|
|
|
|
newtype SDate = SDate UTCTime deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
|
|
|
|
newtype Username = Username Text deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
|
|
|
|
newtype SessionID = SessionID Text deriving (Eq, Ord, Data, Typeable, SafeCopy)
|
|
|
|
|
|
|
|
|
|
instance Indexable Entry where
|
2012-03-12 12:52:48 +01:00
|
|
|
|
empty = ixSet [ ixFun $ \e -> [ entryId e]
|
|
|
|
|
, ixFun $ (:[]) . lang
|
|
|
|
|
, ixFun $ \e -> [ Author $ author e ]
|
|
|
|
|
, ixFun $ \e -> [ Title $ title e]
|
|
|
|
|
, ixFun $ \e -> [ BText $ btext e]
|
|
|
|
|
, ixFun $ \e -> [ MText $ mtext e]
|
|
|
|
|
, ixFun $ \e -> [ EDate $ edate e]
|
|
|
|
|
, ixFun $ \e -> map Tag (tags e)
|
|
|
|
|
, ixFun $ comments
|
|
|
|
|
]
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
data User = User {
|
2012-03-12 12:52:48 +01:00
|
|
|
|
username :: Text,
|
|
|
|
|
password :: ByteString
|
2012-03-12 04:32:15 +01:00
|
|
|
|
} deriving (Eq, Ord, Data, Typeable)
|
|
|
|
|
|
|
|
|
|
$(deriveSafeCopy 0 'base ''User)
|
|
|
|
|
|
|
|
|
|
data Session = Session {
|
2012-03-12 12:52:48 +01:00
|
|
|
|
sessionID :: Text,
|
|
|
|
|
user :: User,
|
|
|
|
|
sdate :: UTCTime
|
2012-03-12 04:32:15 +01:00
|
|
|
|
} deriving (Eq, Ord, Data, Typeable)
|
|
|
|
|
|
|
|
|
|
$(deriveSafeCopy 0 'base ''Session)
|
|
|
|
|
|
|
|
|
|
instance Indexable User where
|
2012-03-12 12:52:48 +01:00
|
|
|
|
empty = ixSet [ ixFun $ \u -> [Username $ username u]
|
|
|
|
|
, ixFun $ (:[]) . password
|
|
|
|
|
]
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
instance Indexable Session where
|
2012-03-12 12:52:48 +01:00
|
|
|
|
empty = ixSet [ ixFun $ \s -> [SessionID $ sessionID s]
|
|
|
|
|
, ixFun $ (:[]) . user
|
|
|
|
|
, ixFun $ \s -> [SDate $ sdate s]
|
|
|
|
|
]
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
data Blog = Blog {
|
2012-03-12 12:52:48 +01:00
|
|
|
|
blogSessions :: IxSet Session,
|
|
|
|
|
blogUsers :: IxSet User,
|
|
|
|
|
blogEntries :: IxSet Entry
|
2012-03-12 04:32:15 +01:00
|
|
|
|
} deriving (Data, Typeable)
|
|
|
|
|
|
|
|
|
|
$(deriveSafeCopy 0 'base ''Blog)
|
|
|
|
|
|
|
|
|
|
initialBlogState :: Blog
|
|
|
|
|
initialBlogState =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
Blog { blogSessions = empty
|
|
|
|
|
, blogUsers = empty
|
|
|
|
|
, blogEntries = empty }
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
-- acid-state database functions (purity is necessary!)
|
|
|
|
|
|
|
|
|
|
insertEntry :: Entry -> Update Blog Entry
|
|
|
|
|
insertEntry e =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
do b@Blog{..} <- get
|
|
|
|
|
put $ b { blogEntries = IxSet.insert e blogEntries }
|
|
|
|
|
return e
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
updateEntry :: Entry -> Update Blog Entry
|
|
|
|
|
updateEntry e =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
do b@Blog{..} <- get
|
|
|
|
|
put $ b { blogEntries = IxSet.updateIx (entryId e) e blogEntries}
|
|
|
|
|
return e
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
getPost :: EntryId -> Query Blog (Maybe Entry)
|
|
|
|
|
getPost eid =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
do b@Blog{..} <- ask
|
|
|
|
|
return $ getOne $ blogEntries @= eid
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
latestPosts :: Query Blog [Entry]
|
|
|
|
|
latestPosts =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
do b@Blog{..} <- ask
|
2012-03-12 15:12:39 +01:00
|
|
|
|
return $ IxSet.toDescList (Proxy :: Proxy EDate) $ blogEntries
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
addSession :: Text -> User -> UTCTime -> Update Blog Session
|
|
|
|
|
addSession sId u t =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
do b@Blog{..} <- get
|
|
|
|
|
let s = Session sId u t
|
|
|
|
|
put $ b { blogSessions = IxSet.insert s blogSessions}
|
|
|
|
|
return s
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
addUser :: Text -> String -> Update Blog User
|
|
|
|
|
addUser un pw =
|
2012-03-12 12:52:48 +01:00
|
|
|
|
do b@Blog{..} <- get
|
|
|
|
|
let u = User un $ hashString pw
|
|
|
|
|
put $ b { blogUsers = IxSet.insert u blogUsers}
|
|
|
|
|
return u
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
-- various functions
|
|
|
|
|
hashString :: String -> ByteString
|
|
|
|
|
hashString = B64.encode . SHA.hash . B.pack
|
|
|
|
|
|
|
|
|
|
$(makeAcidic ''Blog
|
2012-03-12 12:52:48 +01:00
|
|
|
|
[ 'insertEntry
|
|
|
|
|
, 'updateEntry
|
|
|
|
|
, 'getPost
|
|
|
|
|
, 'latestPosts
|
|
|
|
|
, 'addSession
|
|
|
|
|
, 'addUser
|
|
|
|
|
])
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
-- CouchDB database functions
|
2012-03-12 09:47:30 +01:00
|
|
|
|
|
|
|
|
|
runCouchDB' :: CouchMonad a -> IO a
|
2012-03-12 12:52:48 +01:00
|
|
|
|
runCouchDB' = runCouchDB "127.0.0.1" 5984
|
2012-03-12 09:47:30 +01:00
|
|
|
|
|
2012-03-12 04:32:15 +01:00
|
|
|
|
instance JSON Comment where
|
2012-03-12 12:52:48 +01:00
|
|
|
|
showJSON = undefined
|
|
|
|
|
readJSON val = do
|
|
|
|
|
obj <- jsonObject val
|
|
|
|
|
scauthor <- jsonField "cauthor" obj
|
2012-03-12 15:12:39 +01:00
|
|
|
|
jsscdate <- jsonField "cdate" obj :: Result JSValue
|
|
|
|
|
let rcdate = stripResult $ jsonInt jsscdate
|
|
|
|
|
sctext <- jsonField "ctext" obj
|
|
|
|
|
return $ Comment (pack scauthor) (pack sctext) (parseSeconds rcdate)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
instance JSON Entry where
|
2012-03-12 12:52:48 +01:00
|
|
|
|
showJSON = undefined
|
|
|
|
|
readJSON val = do
|
|
|
|
|
obj <- jsonObject val
|
|
|
|
|
sauthor <- jsonField "author" obj
|
|
|
|
|
stitle <- jsonField "title" obj
|
|
|
|
|
day <- jsonField "day" obj
|
|
|
|
|
month <- jsonField "month" obj
|
|
|
|
|
year <- jsonField "year" obj
|
|
|
|
|
stext <- jsonField "text" obj
|
2012-03-12 15:12:39 +01:00
|
|
|
|
comments <- jsonField "comments" obj
|
2012-03-12 12:52:48 +01:00
|
|
|
|
oldid <- jsonField "_id" obj
|
|
|
|
|
let leTime = parseShittyTime year month day oldid
|
|
|
|
|
return $ Entry (EntryId $ getUnixTime leTime) DE (pack sauthor) (pack $ stitle \\ "\n") (pack stext) (Text.empty)
|
2012-03-12 15:12:39 +01:00
|
|
|
|
leTime [] comments
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getUnixTime :: UTCTime -> Integer
|
|
|
|
|
getUnixTime t = read $ formatTime defaultTimeLocale "%s" t
|
|
|
|
|
|
|
|
|
|
parseSeconds :: Integer -> UTCTime
|
|
|
|
|
parseSeconds t = readTime defaultTimeLocale "%s" $ show t
|
|
|
|
|
|
|
|
|
|
parseShittyTime :: Int -> Int -> Int -> String -> UTCTime
|
|
|
|
|
parseShittyTime y m d i = readTime defaultTimeLocale "%Y %m %e %k:%M:%S" newPartTime
|
2012-03-12 12:52:48 +01:00
|
|
|
|
where
|
|
|
|
|
firstPart = take 2 i
|
|
|
|
|
secondPart = take 2 $ drop 2 i
|
|
|
|
|
thirdPart = drop 4 i
|
|
|
|
|
newPartTime = concat $ intersperse " " [show y, showMonth m, show d, " "] ++
|
|
|
|
|
intersperse ":" [firstPart, secondPart, thirdPart]
|
|
|
|
|
showMonth mn
|
|
|
|
|
| mn < 10 = "0" ++ show mn
|
|
|
|
|
| otherwise = show mn
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
getOldEntries = runCouchDB' $ queryView (db "tazblog") (doc "entries") (doc "latestDE") []
|
|
|
|
|
|
|
|
|
|
parseOldEntries :: IO [Entry]
|
|
|
|
|
parseOldEntries = do
|
2012-03-12 12:52:48 +01:00
|
|
|
|
queryResult <- getOldEntries
|
|
|
|
|
let entries = map (stripResult . readJSON . snd) queryResult
|
|
|
|
|
return entries
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
stripResult :: Result a -> a
|
|
|
|
|
stripResult (Ok z) = z
|
|
|
|
|
stripResult (Error s) = error $ "JSON error: " ++ s
|
|
|
|
|
|
|
|
|
|
pasteToDB :: AcidState Blog -> Entry -> IO (EventResult InsertEntry)
|
2012-03-12 12:52:48 +01:00
|
|
|
|
pasteToDB acid !e = update' acid (InsertEntry e)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
main :: IO()
|
|
|
|
|
main = do
|
2012-03-13 05:31:13 +01:00
|
|
|
|
tbDir <- getEnv "TAZBLOG"
|
|
|
|
|
bracket (openLocalStateFrom (tbDir ++ "/BlogState") initialBlogState)
|
2012-03-12 12:52:48 +01:00
|
|
|
|
(createCheckpointAndClose)
|
|
|
|
|
(\acid -> convertEntries acid)
|
2012-03-12 04:32:15 +01:00
|
|
|
|
|
|
|
|
|
convertEntries acid = do
|
2012-03-12 12:52:48 +01:00
|
|
|
|
entries <- parseOldEntries
|
2012-03-12 14:23:45 +01:00
|
|
|
|
let r = map forceHack entries
|
|
|
|
|
rs <- sequence r
|
|
|
|
|
putStrLn $ show rs
|
2012-03-12 12:52:48 +01:00
|
|
|
|
where
|
|
|
|
|
forceHack !x = do
|
2012-03-12 14:23:45 +01:00
|
|
|
|
xy <- pasteToDB acid x
|
|
|
|
|
return $ show xy
|
2012-03-12 15:12:39 +01:00
|
|
|
|
|
|
|
|
|
testThis :: IO ()
|
|
|
|
|
testThis = do
|
|
|
|
|
acid <- openLocalState initialBlogState
|
|
|
|
|
allE <- query' acid LatestPosts
|
|
|
|
|
putStrLn $ show allE
|