--------------------------------------------------------------------
-- |
-- Module      : FriendFeed.Publish
-- Description : Posting / publishing to FriendFeed
-- Copyright   : (c) Sigbjorn Finne, 2008
-- License     : BSD3
--
-- Maintainer: Sigbjorn Finne <sof@forkIO.com>
-- Stability : provisional
-- Portability: portable
--
-- Actions for publishing entries, comments, likes etc. to FriendFeed.
-- 
--------------------------------------------------------------------
module FriendFeed.Publish where

import FriendFeed.Types
import FriendFeed.Types.Import ()
import FriendFeed.Monad

import Data.Maybe

-- | Publish a new entry on the authenticated user's feed.
publishLink :: String
            -> URLString
	    -> Maybe String
	    -> FFm ()
publishLink title l mbComment = postMethod $ authCall $
  ffeedCall_ ["share"]
             [ ("title", title)
	     , ("link",  l)
	     , ("comment", fromMaybe "" mbComment)
	     ]

-- | Add a comment or edit an existing comment on a FriendFeed entry.
addComment :: EntryID -> String -> FFm CommentID
addComment entry body = postMethod $
 ffeedTranslate $
  ffeedCall ["comment"]
            [ ("entry", entry)
	    , ("body",  body)
	    ]

-- | add a comment or edit an existing comment on a FriendFeed entry.
editComment :: EntryID -> String -> CommentID -> FFm ()
editComment entry body c = postMethod $
  ffeedCall_ ["comment"]
             [ ("entry",   entry)
	     , ("body",    body)
	     , ("comment", c)
	     ]

-- | delete an existing comment.
deleteComment :: EntryID -> CommentID -> FFm ()
deleteComment entry c = postMethod $
  ffeedCall_ ["comment","delete"]
             [ ("entry",   entry)
	     , ("comment", c)
	     ]

-- | add a "Like" to a FriendFeed entry for the authenticated user. 
addLike :: EntryID -> FFm ()
addLike e = authCall $ postMethod $ 
 ffeedCall_ ["like"]
            [("entry",e)]

-- | delete an existing @"Like"@.
deleteLike :: EntryID -> FFm ()
deleteLike e = authCall $ postMethod $ 
 ffeedCall_ ["like","delete"]
            [("entry",e)]

-- | Delete an existing entry, but un-delete if 2nd arg is @True@.
deleteEntry :: EntryID -> Bool -> FFm ()
deleteEntry e unDel = authCall $ 
  ffeedCall_ ["entry","delete"]
             ((if unDel then (("undelete","1"):) else id) 
	        [("entry",e)])

-- | Hide an entry, but un-hide/expose if 2nd arg is @True@.
hideEntry :: EntryID -> Bool -> FFm ()
hideEntry e unDel = authCall $ 
  ffeedCall_ ["entry","hide"]
             ((if unDel then (("unhide","1"):) else id) 
	        [("entry",e)])