module Reddit.Actions.Post
( getPosts
, getPosts'
, getPostComments
, getPostSubComments
, getComments
, getPostInfo
, getPostsInfo
, submitLink
, submitLinkWithCaptcha
, submitSelfPost
, submitSelfPostWithCaptcha
, setInboxReplies
, savePost
, unsavePost
, editPost
, deletePost
, setPostFlair
, removePost
, markPostSpam
, stickyPost
, unstickyPost
, setContestMode ) where
import qualified Reddit.Routes as Route
import Reddit.Types
import Reddit.Types.Captcha
import Reddit.Types.Comment
import Reddit.Types.Empty
import Reddit.Types.Listing
import Reddit.Types.Reddit
import Data.Default.Class
import Data.Text (Text)
import Network.API.Builder.Error (APIError(..))
import qualified Data.Char as Char
import qualified Data.Text as Text
getPostInfo :: Monad m => PostID -> RedditT m Post
getPostInfo :: PostID -> RedditT m Post
getPostInfo PostID
p = do
PostListing
res <- [PostID] -> RedditT m PostListing
forall (m :: * -> *). Monad m => [PostID] -> RedditT m PostListing
getPostsInfo [PostID
p]
case PostListing
res of
Listing Maybe PostID
_ Maybe PostID
_ [Post
post] -> Post -> RedditT m Post
forall (m :: * -> *) a. Monad m => a -> m a
return Post
post
PostListing
_ -> APIError RedditError -> RedditT m Post
forall (m :: * -> *) a.
Monad m =>
APIError RedditError -> RedditT m a
failWith (APIError RedditError -> RedditT m Post)
-> APIError RedditError -> RedditT m Post
forall a b. (a -> b) -> a -> b
$ RedditError -> APIError RedditError
forall a. a -> APIError a
APIError RedditError
InvalidResponseError
getPostsInfo :: Monad m => [PostID] -> RedditT m PostListing
getPostsInfo :: [PostID] -> RedditT m PostListing
getPostsInfo [PostID]
ps =
if [PostID] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([PostID] -> Bool) -> [PostID] -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> [PostID] -> [PostID]
forall a. Int -> [a] -> [a]
drop Int
100 [PostID]
ps
then do
PostListing
res <- Route -> RedditT m PostListing
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m PostListing) -> Route -> RedditT m PostListing
forall a b. (a -> b) -> a -> b
$ [PostID] -> Route
Route.aboutPosts [PostID]
ps
case PostListing
res of
Listing Maybe PostID
_ Maybe PostID
_ [Post]
posts | [Post] -> [PostID] -> Bool
forall a a. [a] -> [a] -> Bool
sameLength [Post]
posts [PostID]
ps ->
PostListing -> RedditT m PostListing
forall (m :: * -> *) a. Monad m => a -> m a
return PostListing
res
PostListing
_ -> APIError RedditError -> RedditT m PostListing
forall (m :: * -> *) a.
Monad m =>
APIError RedditError -> RedditT m a
failWith (APIError RedditError -> RedditT m PostListing)
-> APIError RedditError -> RedditT m PostListing
forall a b. (a -> b) -> a -> b
$ RedditError -> APIError RedditError
forall a. a -> APIError a
APIError RedditError
InvalidResponseError
else APIError RedditError -> RedditT m PostListing
forall (m :: * -> *) a.
Monad m =>
APIError RedditError -> RedditT m a
failWith (APIError RedditError -> RedditT m PostListing)
-> APIError RedditError -> RedditT m PostListing
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
getPosts :: Monad m => RedditT m PostListing
getPosts :: RedditT m PostListing
getPosts = Options PostID
-> ListingType -> Maybe SubredditName -> RedditT m PostListing
forall (m :: * -> *).
Monad m =>
Options PostID
-> ListingType -> Maybe SubredditName -> RedditT m PostListing
getPosts' Options PostID
forall a. Default a => a
def ListingType
Hot Maybe SubredditName
forall a. Maybe a
Nothing
getPosts' :: Monad m => Options PostID -> ListingType -> Maybe SubredditName -> RedditT m PostListing
getPosts' :: Options PostID
-> ListingType -> Maybe SubredditName -> RedditT m PostListing
getPosts' Options PostID
o ListingType
l Maybe SubredditName
r = Route -> RedditT m PostListing
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m PostListing) -> Route -> RedditT m PostListing
forall a b. (a -> b) -> a -> b
$ Options PostID -> Maybe SubredditName -> Text -> Route
Route.postsListing Options PostID
o Maybe SubredditName
r (String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String -> String
lower (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ ListingType -> String
forall a. Show a => a -> String
show ListingType
l)
where lower :: String -> String
lower = (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
Char.toLower
savePost :: Monad m => PostID -> RedditT m ()
savePost :: PostID -> RedditT m ()
savePost = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PostID -> Route
Route.savePost
unsavePost :: Monad m => PostID -> RedditT m ()
unsavePost :: PostID -> RedditT m ()
unsavePost = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PostID -> Route
Route.unsavePost
submitLink :: Monad m
=> SubredditName
-> Text
-> Text
-> RedditT m PostID
submitLink :: SubredditName -> Text -> Text -> RedditT m PostID
submitLink SubredditName
r Text
title Text
url = do
POSTWrapped PostID
res <- Route -> RedditT m (POSTWrapped PostID)
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m (POSTWrapped PostID))
-> Route -> RedditT m (POSTWrapped PostID)
forall a b. (a -> b) -> a -> b
$ SubredditName -> Text -> Text -> Route
Route.submitLink SubredditName
r Text
title Text
url
PostID -> RedditT m PostID
forall (m :: * -> *) a. Monad m => a -> m a
return PostID
res
submitLinkWithCaptcha :: Monad m
=> SubredditName
-> Text
-> Text
-> CaptchaID
-> Text
-> RedditT m PostID
submitLinkWithCaptcha :: SubredditName
-> Text -> Text -> CaptchaID -> Text -> RedditT m PostID
submitLinkWithCaptcha SubredditName
r Text
title Text
url CaptchaID
iden Text
captcha = do
POSTWrapped PostID
res <- Route -> RedditT m (POSTWrapped PostID)
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m (POSTWrapped PostID))
-> Route -> RedditT m (POSTWrapped PostID)
forall a b. (a -> b) -> a -> b
$ SubredditName -> Text -> Text -> Route
Route.submitLink SubredditName
r Text
title Text
url Route -> (CaptchaID, Text) -> Route
`withCaptcha` (CaptchaID
iden, Text
captcha)
PostID -> RedditT m PostID
forall (m :: * -> *) a. Monad m => a -> m a
return PostID
res
submitSelfPost :: Monad m
=> SubredditName
-> Text
-> Text
-> RedditT m PostID
submitSelfPost :: SubredditName -> Text -> Text -> RedditT m PostID
submitSelfPost SubredditName
r Text
title Text
postBody = do
POSTWrapped PostID
res <- Route -> RedditT m (POSTWrapped PostID)
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m (POSTWrapped PostID))
-> Route -> RedditT m (POSTWrapped PostID)
forall a b. (a -> b) -> a -> b
$ SubredditName -> Text -> Text -> Route
Route.submitSelfPost SubredditName
r Text
title Text
postBody
PostID -> RedditT m PostID
forall (m :: * -> *) a. Monad m => a -> m a
return PostID
res
submitSelfPostWithCaptcha :: Monad m
=> SubredditName
-> Text
-> Text
-> CaptchaID
-> Text
-> RedditT m PostID
submitSelfPostWithCaptcha :: SubredditName
-> Text -> Text -> CaptchaID -> Text -> RedditT m PostID
submitSelfPostWithCaptcha SubredditName
r Text
title Text
postBody CaptchaID
iden Text
captcha = do
POSTWrapped PostID
res <- Route -> RedditT m (POSTWrapped PostID)
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m (POSTWrapped PostID))
-> Route -> RedditT m (POSTWrapped PostID)
forall a b. (a -> b) -> a -> b
$ SubredditName -> Text -> Text -> Route
Route.submitSelfPost SubredditName
r Text
title Text
postBody Route -> (CaptchaID, Text) -> Route
`withCaptcha` (CaptchaID
iden, Text
captcha)
PostID -> RedditT m PostID
forall (m :: * -> *) a. Monad m => a -> m a
return PostID
res
deletePost :: Monad m => PostID -> RedditT m ()
deletePost :: PostID -> RedditT m ()
deletePost = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PostID -> Route
forall a. Thing a => a -> Route
Route.delete
setPostFlair :: Monad m
=> SubredditName
-> PostID
-> Text
-> Text
-> RedditT m ()
setPostFlair :: SubredditName -> PostID -> Text -> Text -> RedditT m ()
setPostFlair SubredditName
r PostID
p Text
text Text
css = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> RedditT m Empty -> RedditT m ()
forall a b. (a -> b) -> a -> b
$ Route -> RedditT m Empty
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m Empty) -> Route -> RedditT m Empty
forall a b. (a -> b) -> a -> b
$ SubredditName -> PostID -> Text -> Text -> Route
Route.postFlair SubredditName
r PostID
p Text
text Text
css
editPost :: Monad m => PostID -> Text -> RedditT m ()
editPost :: PostID -> Text -> RedditT m ()
editPost PostID
thing Text
text = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> RedditT m Empty -> RedditT m ()
forall a b. (a -> b) -> a -> b
$ Route -> RedditT m Empty
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m Empty) -> Route -> RedditT m Empty
forall a b. (a -> b) -> a -> b
$ PostID -> Text -> Route
forall a. Thing a => a -> Text -> Route
Route.edit PostID
thing Text
text
getPostComments :: Monad m => PostID -> RedditT m PostComments
PostID
p = Route -> RedditT m PostComments
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m PostComments)
-> Route -> RedditT m PostComments
forall a b. (a -> b) -> a -> b
$ PostID -> Maybe CommentID -> Route
Route.getComments PostID
p Maybe CommentID
forall a. Maybe a
Nothing
getPostSubComments :: Monad m => PostID -> CommentID -> RedditT m PostComments
PostID
p CommentID
c = Route -> RedditT m PostComments
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m PostComments)
-> Route -> RedditT m PostComments
forall a b. (a -> b) -> a -> b
$ PostID -> Maybe CommentID -> Route
Route.getComments PostID
p (CommentID -> Maybe CommentID
forall a. a -> Maybe a
Just CommentID
c)
getComments :: Monad m => PostID -> RedditT m [CommentReference]
PostID
p = do
PostComments Post
_ [CommentReference]
c <- PostID -> RedditT m PostComments
forall (m :: * -> *). Monad m => PostID -> RedditT m PostComments
getPostComments PostID
p
[CommentReference] -> RedditT m [CommentReference]
forall (m :: * -> *) a. Monad m => a -> m a
return [CommentReference]
c
setInboxReplies :: Monad m => Bool -> PostID -> RedditT m ()
setInboxReplies :: Bool -> PostID -> RedditT m ()
setInboxReplies Bool
enabled = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> PostID -> Route
Route.sendReplies Bool
enabled
setContestMode :: Monad m => Bool -> PostID -> RedditT m ()
setContestMode :: Bool -> PostID -> RedditT m ()
setContestMode Bool
enabled = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> PostID -> Route
Route.setContestMode Bool
enabled
removePost :: Monad m => PostID -> RedditT m ()
removePost :: PostID -> RedditT m ()
removePost = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> PostID -> Route
forall a. (ToQuery a, Thing a) => Bool -> a -> Route
Route.removePost Bool
False
markPostSpam :: Monad m => PostID -> RedditT m ()
markPostSpam :: PostID -> RedditT m ()
markPostSpam = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> (PostID -> RedditT m Empty) -> PostID -> 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)
-> (PostID -> Route) -> PostID -> RedditT m Empty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> PostID -> Route
forall a. (ToQuery a, Thing a) => Bool -> a -> Route
Route.removePost Bool
True
stickyPost :: Monad m
=> PostID
-> Maybe Integer
-> RedditT m ()
stickyPost :: PostID -> Maybe Integer -> RedditT m ()
stickyPost PostID
p Maybe Integer
n = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> RedditT m Empty -> RedditT m ()
forall a b. (a -> b) -> a -> b
$ Route -> RedditT m Empty
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m Empty) -> Route -> RedditT m Empty
forall a b. (a -> b) -> a -> b
$ Bool -> PostID -> Maybe Integer -> Route
Route.stickyPost Bool
True PostID
p Maybe Integer
n
unstickyPost :: Monad m
=> PostID
-> Maybe Integer
-> RedditT m ()
unstickyPost :: PostID -> Maybe Integer -> RedditT m ()
unstickyPost PostID
p Maybe Integer
n = RedditT m Empty -> RedditT m ()
forall (m :: * -> *). Monad m => m Empty -> m ()
nothing (RedditT m Empty -> RedditT m ())
-> RedditT m Empty -> RedditT m ()
forall a b. (a -> b) -> a -> b
$ Route -> RedditT m Empty
forall a (m :: * -> *).
(FromJSON a, Monad m) =>
Route -> RedditT m a
runRoute (Route -> RedditT m Empty) -> Route -> RedditT m Empty
forall a b. (a -> b) -> a -> b
$ Bool -> PostID -> Maybe Integer -> Route
Route.stickyPost Bool
False PostID
p Maybe Integer
n