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
= Options CommentID
-> Maybe SubredditName -> RedditT m CommentListing
forall (m :: * -> *).
Monad m =>
Options CommentID
-> Maybe SubredditName -> RedditT m CommentListing
getNewComments' Options CommentID
forall a. Default a => a
def
getNewComments' :: Monad m => Options CommentID -> Maybe SubredditName -> RedditT m CommentListing
Options CommentID
opts Maybe SubredditName
r = Route -> RedditT m CommentListing
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m CommentListing)
-> Route -> RedditT m CommentListing
forall a b. (a -> b) -> a -> b
$ Options CommentID -> Maybe SubredditName -> Route
Route.newComments Options CommentID
opts Maybe SubredditName
r
getMoreChildren :: Monad m
=> PostID
-> [CommentID]
-> RedditT m [CommentReference]
getMoreChildren :: PostID -> [CommentID] -> RedditT m [CommentReference]
getMoreChildren PostID
_ [] = [CommentReference] -> RedditT m [CommentReference]
forall (m :: * -> *) a. Monad m => a -> m a
return []
getMoreChildren PostID
p [CommentID]
cs = do
let ([CommentID]
now, [CommentID]
next) = Int -> [CommentID] -> ([CommentID], [CommentID])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
20 [CommentID]
cs
POSTWrapped [CommentReference]
rs <- Route -> RedditT m (POSTWrapped [CommentReference])
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m (POSTWrapped [CommentReference]))
-> Route -> RedditT m (POSTWrapped [CommentReference])
forall a b. (a -> b) -> a -> b
$ PostID -> [CommentID] -> Route
Route.moreChildren PostID
p [CommentID]
now
[CommentReference]
more <- PostID -> [CommentID] -> RedditT m [CommentReference]
forall (m :: * -> *).
Monad m =>
PostID -> [CommentID] -> RedditT m [CommentReference]
getMoreChildren PostID
p [CommentID]
next
[CommentReference] -> RedditT m [CommentReference]
forall (m :: * -> *) a. Monad m => a -> m a
return ([CommentReference] -> RedditT m [CommentReference])
-> [CommentReference] -> RedditT m [CommentReference]
forall a b. (a -> b) -> a -> b
$ [CommentReference]
rs [CommentReference] -> [CommentReference] -> [CommentReference]
forall a. [a] -> [a] -> [a]
++ [CommentReference]
more
getCommentInfo :: Monad m => CommentID -> RedditT m Comment
CommentID
c = do
CommentListing
res <- [CommentID] -> RedditT m CommentListing
forall (m :: * -> *).
Monad m =>
[CommentID] -> RedditT m CommentListing
getCommentsInfo [CommentID
c]
case CommentListing
res of
Listing Maybe CommentID
_ Maybe CommentID
_ [Comment
comment] -> Comment -> RedditT m Comment
forall (m :: * -> *) a. Monad m => a -> m a
return Comment
comment
CommentListing
_ -> APIError RedditError -> RedditT m Comment
forall (m :: * -> *) a.
Monad m =>
APIError RedditError -> RedditT m a
failWith (APIError RedditError -> RedditT m Comment)
-> APIError RedditError -> RedditT m Comment
forall a b. (a -> b) -> a -> b
$ RedditError -> APIError RedditError
forall a. a -> APIError a
APIError RedditError
InvalidResponseError
getCommentsInfo :: Monad m => [CommentID] -> RedditT m CommentListing
[CommentID]
cs =
if [CommentID] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([CommentID] -> Bool) -> [CommentID] -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> [CommentID] -> [CommentID]
forall a. Int -> [a] -> [a]
drop Int
100 [CommentID]
cs
then do
CommentListing
res <- Route -> RedditT m CommentListing
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m CommentListing)
-> Route -> RedditT m CommentListing
forall a b. (a -> b) -> a -> b
$ [CommentID] -> Route
Route.commentsInfo [CommentID]
cs
case CommentListing
res of
Listing Maybe CommentID
_ Maybe CommentID
_ [Comment]
comments | [Comment] -> [CommentID] -> Bool
forall a a. [a] -> [a] -> Bool
sameLength [Comment]
comments [CommentID]
cs ->
CommentListing -> RedditT m CommentListing
forall (m :: * -> *) a. Monad m => a -> m a
return CommentListing
res
CommentListing
_ -> APIError RedditError -> RedditT m CommentListing
forall (m :: * -> *) a.
Monad m =>
APIError RedditError -> RedditT m a
failWith (APIError RedditError -> RedditT m CommentListing)
-> APIError RedditError -> RedditT m CommentListing
forall a b. (a -> b) -> a -> b
$ RedditError -> APIError RedditError
forall a. a -> APIError a
APIError RedditError
InvalidResponseError
else APIError RedditError -> RedditT m CommentListing
forall (m :: * -> *) a.
Monad m =>
APIError RedditError -> RedditT m a
failWith (APIError RedditError -> RedditT m CommentListing)
-> APIError RedditError -> RedditT m CommentListing
forall a b. (a -> b) -> a -> b
$ RedditError -> APIError RedditError
forall a. a -> APIError a
APIError RedditError
TooManyRequests
where
sameLength :: [a] -> [a] -> Bool
sameLength (a
_:[a]
xs) (a
_:[a]
ys) = [a] -> [a] -> Bool
sameLength [a]
xs [a]
ys
sameLength [] [] = Bool
True
sameLength [a]
_ [a]
_ = Bool
False
editComment :: Monad m
=> CommentID
-> Text
-> RedditT m Comment
CommentID
thing Text
text = do
POSTWrapped Comment
res <- Route -> RedditT m (POSTWrapped Comment)
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m (POSTWrapped Comment))
-> Route -> RedditT m (POSTWrapped Comment)
forall a b. (a -> b) -> a -> b
$ CommentID -> Text -> Route
forall a. Thing a => a -> Text -> Route
Route.edit CommentID
thing Text
text
Comment -> RedditT m Comment
forall (m :: * -> *) a. Monad m => a -> m a
return Comment
res
deleteComment :: Monad m => CommentID -> RedditT m ()
= RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (CommentID -> RedditT m Empty) -> CommentID -> RedditT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Route -> RedditT m Empty
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m Empty)
-> (CommentID -> Route) -> CommentID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommentID -> Route
forall a. Thing a => a -> Route
Route.delete
removeComment :: Monad m => CommentID -> RedditT m ()
= RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (CommentID -> RedditT m Empty) -> CommentID -> RedditT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Route -> RedditT m Empty
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m Empty)
-> (CommentID -> Route) -> CommentID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> CommentID -> Route
forall a. (ToQuery a, Thing a) => Bool -> a -> Route
Route.removePost Bool
False