Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ids distinct types #27

Merged
merged 5 commits into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Hastodon.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: Hastodon
Version: 0.5.0
Version: 0.6.0
Synopsis: mastodon client module for Haskell
Category: Web
Description: mastodon client module for Haskell
Expand Down
20 changes: 2 additions & 18 deletions Web/Hastodon.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
module Web.Hastodon
( module Web.Hastodon.Option

, module Web.Hastodon.Streaming
, module Web.Hastodon.Types
, HastodonClient(..)
, Account(..)
, Application(..)
, Attachment(..)
, Card(..)
, Context(..)
, Instance(..)
, Mention(..)
, Notification(..)
, OAuthClient(..)
, Relationship(..)
, Report(..)
, Results(..)
, Status(..)
, StreamingPayload(..)
, StreamingResponse
, Tag(..)

, mkHastodonClient
, getAccountById
, getCurrentAccount
Expand Down
62 changes: 31 additions & 31 deletions Web/Hastodon/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mkHastodonClient clientId clientSecret username password host = do
Left err -> return $ Nothing
Right oauthData -> return $ Just $ HastodonClient host (accessToken oauthData)

getAccountById :: HastodonClient -> Int -> IO (Either JSONException Account)
getAccountById :: HastodonClient -> AccountId -> IO (Either JSONException Account)
getAccountById client id = do
res <- getHastodonResponseJSON (replace ":id" (show id) pAccountById) client
return (getResponseBody res :: Either JSONException Account)
Expand All @@ -182,40 +182,40 @@ getCurrentAccount client = do
res <- getHastodonResponseJSON pCurrentAccounts client
return (getResponseBody res :: Either JSONException Account)

getFollowers :: HastodonClient -> Int -> IO (Either JSONException [Account])
getFollowers :: HastodonClient -> AccountId -> IO (Either JSONException [Account])
getFollowers client = getFollowersWithOption client mempty

getFollowersWithOption :: HastodonClient -> RangeOption -> Int -> IO (Either JSONException [Account])
getFollowersWithOption :: HastodonClient -> RangeOption -> AccountId -> IO (Either JSONException [Account])
getFollowersWithOption client opt id = do
res <- getHastodonResponseJSONWithOption
(optionAsQuery opt)
(replace ":id" (show id) pFollowers)
client
return (getResponseBody res :: Either JSONException [Account])

getFollowing :: HastodonClient -> Int -> IO (Either JSONException [Account])
getFollowing :: HastodonClient -> AccountId -> IO (Either JSONException [Account])
getFollowing client = getFollowingWithOption client mempty

getFollowingWithOption :: HastodonClient -> RangeOption -> Int -> IO (Either JSONException [Account])
getFollowingWithOption :: HastodonClient -> RangeOption -> AccountId -> IO (Either JSONException [Account])
getFollowingWithOption client opt id = do
res <- getHastodonResponseJSONWithOption
(optionAsQuery opt)
(replace ":id" (show id) pFollowing)
client
return (getResponseBody res :: Either JSONException [Account])

getAccountStatusesWithOption :: HastodonClient -> GetAccountStatusesOption -> Int -> IO (Either JSONException [Status])
getAccountStatusesWithOption :: HastodonClient -> GetAccountStatusesOption -> AccountId -> IO (Either JSONException [Status])
getAccountStatusesWithOption client opt id = do
res <- getHastodonResponseJSONWithOption
(optionAsQuery opt)
(replace ":id" (show id) pAccountStatuses)
client
return (getResponseBody res :: Either JSONException [Status])

getAccountStatuses :: HastodonClient -> Int -> IO (Either JSONException [Status])
getAccountStatuses :: HastodonClient -> AccountId -> IO (Either JSONException [Status])
getAccountStatuses client = getAccountStatusesWithOption client mempty

getRelationships :: HastodonClient -> [Int] -> IO (Either JSONException [Relationship])
getRelationships :: HastodonClient -> [AccountId] -> IO (Either JSONException [Relationship])
getRelationships client ids = do
let intIds = map (show) ids
let params = foldl (\x y -> x ++ (if x == "" then "?" else "&") ++ "id%5b%5d=" ++ y) "" intIds
Expand All @@ -231,37 +231,37 @@ getSearchedAccountsWithOption client opt query = do
getSearchedAccounts :: HastodonClient -> String -> IO (Either JSONException [Account])
getSearchedAccounts client = getSearchedAccountsWithOption client mempty

postFollow :: HastodonClient -> Int -> IO (Either JSONException Relationship)
postFollow :: HastodonClient -> AccountId -> IO (Either JSONException Relationship)
postFollow client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pFollow) [] client
return (getResponseBody res :: Either JSONException Relationship)

postUnfollow :: HastodonClient -> Int -> IO (Either JSONException Relationship)
postUnfollow :: HastodonClient -> AccountId -> IO (Either JSONException Relationship)
postUnfollow client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pUnfollow) [] client
return (getResponseBody res :: Either JSONException Relationship)

postBlock :: HastodonClient -> Int -> IO (Either JSONException Relationship)
postBlock :: HastodonClient -> AccountId -> IO (Either JSONException Relationship)
postBlock client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pBlock) [] client
return (getResponseBody res :: Either JSONException Relationship)

postUnblock :: HastodonClient -> Int -> IO (Either JSONException Relationship)
postUnblock :: HastodonClient -> AccountId -> IO (Either JSONException Relationship)
postUnblock client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pUnblock) [] client
return (getResponseBody res :: Either JSONException Relationship)

postMuteWithOption ::
HastodonClient -> PostMuteOption -> Int -> IO (Either JSONException Relationship)
HastodonClient -> PostMuteOption -> AccountId -> IO (Either JSONException Relationship)
postMuteWithOption client opt id = do
let prms = optionAsForm opt
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pMute) prms client
return (getResponseBody res :: Either JSONException Relationship)

postMute :: HastodonClient -> Int -> IO (Either JSONException Relationship)
postMute :: HastodonClient -> AccountId -> IO (Either JSONException Relationship)
postMute client = postMuteWithOption client mempty

postUnmute :: HastodonClient -> Int -> IO (Either JSONException Relationship)
postUnmute :: HastodonClient -> AccountId -> IO (Either JSONException Relationship)
postUnmute client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pUnmute) [] client
return (getResponseBody res :: Either JSONException Relationship)
Expand Down Expand Up @@ -300,10 +300,10 @@ getFollowRequestsWithOption client opt = do
getFollowRequests :: HastodonClient -> IO (Either JSONException [Account])
getFollowRequests client = getFollowRequestsWithOption client mempty

postAuthorizeRequest :: HastodonClient -> Int -> IO Bool
postAuthorizeRequest :: HastodonClient -> AccountId -> IO Bool
postAuthorizeRequest client id = postAndGetHastodonResult (replace ":id" (show id) pAuthorizeRequest) [] client

postRejectRequest :: HastodonClient -> Int -> IO Bool
postRejectRequest :: HastodonClient -> AccountId -> IO Bool
postRejectRequest client id = postAndGetHastodonResult (replace ":id" (show id) pRejectRequest) [] client

getInstance :: HastodonClient -> IO (Either JSONException Instance)
Expand Down Expand Up @@ -338,7 +338,7 @@ getNotificationsWithOption client opt = do
res <- getHastodonResponseJSONWithOption (optionAsQuery opt) pNotifications client
return (getResponseBody res :: Either JSONException [Notification])

getNotificationById :: HastodonClient -> Int -> IO (Either JSONException Notification)
getNotificationById :: HastodonClient -> NotificationId -> IO (Either JSONException Notification)
getNotificationById client id = do
res <- getHastodonResponseJSON (replace ":id" (show id) pNotificationById) client
return (getResponseBody res :: Either JSONException Notification)
Expand All @@ -360,35 +360,35 @@ getSearchedResultsWithOption client opt query = do
res <- getHastodonResponseJSONWithOption (optionAsQuery opt) (pSearch ++ "?q=" ++ query) client
return (getResponseBody res :: Either JSONException [Results])

getStatus :: HastodonClient -> Int -> IO (Either JSONException Status)
getStatus :: HastodonClient -> StatusId -> IO (Either JSONException Status)
getStatus client id = do
res <- getHastodonResponseJSON (replace ":id" (show id) pStatus) client
return (getResponseBody res :: Either JSONException Status)

getCard :: HastodonClient -> Int -> IO (Either JSONException Card)
getCard :: HastodonClient -> StatusId -> IO (Either JSONException Card)
getCard client id = do
res <- getHastodonResponseJSON (replace ":id" (show id) pCard) client
return (getResponseBody res :: Either JSONException Card)

getContext :: HastodonClient -> Int -> IO (Either JSONException Context)
getContext :: HastodonClient -> StatusId -> IO (Either JSONException Context)
getContext client id = do
res <- getHastodonResponseJSON (replace ":id" (show id) pContext) client
return (getResponseBody res :: Either JSONException Context)

getRebloggedByWithOption :: HastodonClient -> RangeOption -> Int -> IO (Either JSONException [Account])
getRebloggedByWithOption :: HastodonClient -> RangeOption -> StatusId -> IO (Either JSONException [Account])
getRebloggedByWithOption client opt id = do
res <- getHastodonResponseJSONWithOption (optionAsQuery opt) (replace ":id" (show id) pRebloggedBy) client
return (getResponseBody res :: Either JSONException [Account])

getRebloggedBy :: HastodonClient -> Int -> IO (Either JSONException [Account])
getRebloggedBy :: HastodonClient -> StatusId -> IO (Either JSONException [Account])
getRebloggedBy client = getRebloggedByWithOption client mempty

getFavoritedByWithOption :: HastodonClient -> RangeOption -> Int -> IO (Either JSONException [Account])
getFavoritedByWithOption :: HastodonClient -> RangeOption -> StatusId -> IO (Either JSONException [Account])
getFavoritedByWithOption client opt id = do
res <- getHastodonResponseJSONWithOption (optionAsQuery opt) (replace ":id" (show id) pFavoritedBy) client
return (getResponseBody res :: Either JSONException [Account])

getFavoritedBy :: HastodonClient -> Int -> IO (Either JSONException [Account])
getFavoritedBy :: HastodonClient -> StatusId -> IO (Either JSONException [Account])
getFavoritedBy client = getFavoritedByWithOption client mempty

postStatus :: HastodonClient -> String -> IO (Either JSONException Status)
Expand All @@ -401,29 +401,29 @@ postStatusWithOption client opt status = do
res <- postAndGetHastodonResponseJSON pStatuses prms client
return (getResponseBody res :: Either JSONException Status)

postStatusWithMediaIds :: HastodonClient -> String -> [HastodonId] -> IO (Either JSONException Status)
postStatusWithMediaIds :: HastodonClient -> String -> [MediaId] -> IO (Either JSONException Status)
postStatusWithMediaIds client status mediaIds = do
let unpackedMediaIds = [(Char8.pack "media_ids[]",utf8ToChar8 media)|media <- mediaIds] -- Rails array parameter convention?
let unpackedMediaIds = [(Char8.pack "media_ids[]", (utf8ToChar8 . unMediaId) media) | media <- mediaIds] -- Rails array parameter convention?
let body = (Char8.pack "status",utf8ToChar8 status):unpackedMediaIds
res <- postAndGetHastodonResponseJSON pStatuses body client
return (getResponseBody res :: Either JSONException Status)

postReblog :: HastodonClient -> Int -> IO (Either JSONException Status)
postReblog :: HastodonClient -> StatusId -> IO (Either JSONException Status)
postReblog client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pReblog) [] client
return (getResponseBody res :: Either JSONException Status)

postUnreblog :: HastodonClient -> Int -> IO (Either JSONException Status)
postUnreblog :: HastodonClient -> StatusId -> IO (Either JSONException Status)
postUnreblog client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pUnreblog) [] client
return (getResponseBody res :: Either JSONException Status)

postFavorite :: HastodonClient -> Int -> IO (Either JSONException Status)
postFavorite :: HastodonClient -> StatusId -> IO (Either JSONException Status)
postFavorite client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pFavorite) [] client
return (getResponseBody res :: Either JSONException Status)

postUnfavorite :: HastodonClient -> Int -> IO (Either JSONException Status)
postUnfavorite :: HastodonClient -> StatusId -> IO (Either JSONException Status)
postUnfavorite client id = do
res <- postAndGetHastodonResponseJSON (replace ":id" (show id) pUnfavorite) [] client
return (getResponseBody res :: Either JSONException Status)
Expand Down
15 changes: 6 additions & 9 deletions Web/Hastodon/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ class IsOption a => IsRangeOption a where {}
instance IsLimitOption RangeOption where {}
instance IsRangeOption RangeOption where {}

sinceId :: IsRangeOption a => String -> a
sinceId i = mkOption "since_id" $ Just i
sinceId :: IsRangeOption a => StatusId -> a
sinceId i = mkOption "since_id" $ Just $ show i

maxId :: IsRangeOption a => String -> a
maxId i = mkOption "max_id" $ Just i
maxId :: IsRangeOption a => StatusId -> a
maxId i = mkOption "max_id" $ Just $ show i

--
-- Timeline options
Expand Down Expand Up @@ -280,10 +280,10 @@ formatVis VisibilityPrivate = "private"
formatVis VisibilityUnlisted = "unlisted"
formatVis VisibilityPublic = "public"

inReplyToId :: IsPostStatusOption a => Int -> a
inReplyToId :: IsPostStatusOption a => AccountId -> a
inReplyToId i = mkOption "in_reply_to_id" (Just $ show i)

mediaIds :: IsPostStatusOption a => [Int] -> a
mediaIds :: IsPostStatusOption a => [MediaId] -> a
mediaIds l = mkArrayOption "media_ids" $ show <$> l

sensitive :: IsPostStatusOption a => a
Expand Down Expand Up @@ -318,6 +318,3 @@ instance IsPostMuteOption PostMuteOption where {}

muteNotifications :: IsPostMuteOption a => a
muteNotifications = mkOption "notifications" $ Nothing



4 changes: 2 additions & 2 deletions Web/Hastodon/Streaming.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type StreamingResponse m =

data StreamingPayload = SUpdate Status |
SNotification Notification |
SDelete HastodonId |
SDelete StatusId |
Thump
deriving (Show)

Expand Down Expand Up @@ -97,7 +97,7 @@ pd = string "data: "
parseDelete :: Parser StreamingPayload
parseDelete = do
pd
i <- many C8.digit
i <- StatusId <$> many C8.digit
return $ SDelete i


Expand Down
Loading