module Reddit.Actions.Comment
( getNewComments
, getNewComments'
, getMoreChildren
, getCommentInfo
, getCommentsInfo
, editComment
, deleteComment
, removeComment ) where
import Reddit.Types.Comment
import Reddit.Types.Empty
import Reddit.Types.Error
import Reddit.Types.Listing
import Reddit.Types.Options
import Reddit.Types.Post
import Reddit.Types.Reddit
import Reddit.Types.Subreddit
import qualified Reddit.Routes as Route
import Data.Default.Class
import Data.Text (Text)
import Network.API.Builder (APIError(..))
getNewComments :: Monad m => Maybe SubredditName -> RedditT m CommentListing
getNewComments = getNewComments' def
getNewComments' :: Monad m => Options CommentID -> Maybe SubredditName -> RedditT m CommentListing
getNewComments' opts r = runRoute $ Route.newComments opts r
getMoreChildren :: Monad m
=> PostID
-> [CommentID]
-> RedditT m [CommentReference]
getMoreChildren _ [] = return []
getMoreChildren p cs = do
let (now, next) = splitAt 20 cs
POSTWrapped rs <- runRoute $ Route.moreChildren p now
more <- getMoreChildren p next
return $ rs ++ more
getCommentInfo :: Monad m => CommentID -> RedditT m Comment
getCommentInfo c = do
res <- getCommentsInfo [c]
case res of
Listing _ _ [comment] -> return comment
_ -> failWith $ APIError InvalidResponseError
getCommentsInfo :: Monad m => [CommentID] -> RedditT m CommentListing
getCommentsInfo cs =
if null $ drop 100 cs
then do
res <- runRoute $ Route.commentsInfo cs
case res of
Listing _ _ comments | sameLength comments cs ->
return res
_ -> failWith $ APIError InvalidResponseError
else failWith $ APIError TooManyRequests
where
sameLength (_:xs) (_:ys) = sameLength xs ys
sameLength [] [] = True
sameLength _ _ = False
editComment :: Monad m
=> CommentID
-> Text
-> RedditT m Comment
editComment thing text = do
POSTWrapped res <- runRoute $ Route.edit thing text
return res
deleteComment :: Monad m => CommentID -> RedditT m ()
deleteComment = nothing . runRoute . Route.delete
removeComment :: Monad m => CommentID -> RedditT m ()
removeComment = nothing . runRoute . Route.removePost False