packages feed

panda-2008.9.19.1: Panda/Model/Comment.hs

module Panda.Model.Comment where

-- env
import Panda.Helper.Env hiding (match, title, body)
import Prelude hiding ((.), (/), (^), id, readFile, writeFile)
import qualified Panda.Config.Global as G
import qualified Data.Map as M
import Panda.Type.Reader
import qualified Panda.Model.Post as Post
import System.Time
import System.Directory

data Comment = Comment
  { uid    :: String     -- blog/08-09-04 blog title
  , author :: String
  , url    :: String
  , body   :: String
  }
  deriving (Show, Eq)

list_for post_id = do
  has_comments <- doesDirectoryExist d
  if has_comments
    then ls d ^ map from_utf8 ^ rsort ^ map (G.comment_id / r /) >>= mapM get
    else return []
  where 
    d = (G.comment_uri / r)
    r = post_id.id_to_resource.to_utf8


get id = do
  xs <- (G.flat_uri / id) .readFile ^ lines
  let author = xs.first
  let url = xs.drop 2.first
  let body = xs.drop 4.unlines
  return $ Comment id author url body


create h = do
  let author  = at "author"
  let url     = at "url"
  let body    = at "comment"
  let post_id = at "post_id" 

  timestamp <- ( getClockTime >>= toCalendarTime ) ^ format_date
  let comment_path = G.flat_uri / G.comment_id / (post_id.split "/".last.to_utf8) 
  createDirectoryIfMissing True comment_path
  
  let uid = comment_path / timestamp
  let content = [author, url, body].join "\n\n"
  writeFile uid content
  
  where at s = h.M.lookup s .fromJust

format_date x = formatCalendarTime defaultTimeLocale "%y-%m-%d %T" x
parse_date = parseCalendarTime defaultTimeLocale "%Y-%m-%d %T"
date x     = x.uid.split "/".last.("20"++).parse_date.fromMaybe (parse_date "2000-1-1 00:00:00".fromJust)

fill_comment_size x = do
  size <- x.Post.uid.list_for ^ length 
  return $ x { Post.comment_size = size }

markup x  = render_to_html Markdown (x.body)