module Facebook.Object.User
( User(..)
, Gender(..)
, getUser
, searchUsers
, getUserCheckins
, Friend(..)
, getUserFriends
) where
import Control.Applicative
import Control.Monad (mzero)
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Aeson ((.:), (.:?))
import Data.Text (Text)
import Data.Typeable (Typeable)
import qualified Data.Aeson as A
import qualified Data.Conduit as C
import Facebook.Types
import Facebook.Monad
import Facebook.Graph
import Facebook.Pager
import Facebook.Object.Checkin
data User =
User { userId :: UserId
, userName :: Maybe Text
, userFirstName :: Maybe Text
, userMiddleName :: Maybe Text
, userLastName :: Maybe Text
, userGender :: Maybe Gender
, userLocale :: Maybe Text
, userUsername :: Maybe Text
, userVerified :: Maybe Bool
, userEmail :: Maybe Text
, userLocation :: Maybe Place
}
deriving (Eq, Ord, Show, Read, Typeable)
instance A.FromJSON User where
parseJSON (A.Object v) =
User <$> v .: "id"
<*> v .:? "name"
<*> v .:? "first_name"
<*> v .:? "middle_name"
<*> v .:? "last_name"
<*> v .:? "gender"
<*> v .:? "locale"
<*> v .:? "username"
<*> v .:? "verified"
<*> v .:? "email"
<*> v .:? "location"
parseJSON _ = mzero
data Gender = Male | Female deriving (Eq, Ord, Show, Read, Enum, Typeable)
instance A.FromJSON Gender where
parseJSON (A.String "male") = return Male
parseJSON (A.String "female") = return Female
parseJSON _ = mzero
instance A.ToJSON Gender where
toJSON = A.toJSON . toText
where
toText :: Gender -> Text
toText Male = "male"
toText Female = "female"
getUser :: (C.MonadResource m, MonadBaseControl IO m) =>
UserId
-> [Argument]
-> Maybe UserAccessToken
-> FacebookT anyAuth m User
getUser id_ query mtoken = getObject ("/" <> idCode id_) query mtoken
searchUsers :: (C.MonadResource m, MonadBaseControl IO m)
=> Text
-> [Argument]
-> Maybe UserAccessToken
-> FacebookT anyAuth m (Pager User)
searchUsers = searchObjects "user"
getUserCheckins ::
(C.MonadResource m, MonadBaseControl IO m) =>
UserId
-> [Argument]
-> UserAccessToken
-> FacebookT anyAuth m (Pager Checkin)
getUserCheckins id_ query token =
getObject ("/" <> idCode id_ <> "/checkins") query (Just token)
data Friend =
Friend { friendId :: UserId
, friendName :: Text
}
deriving (Eq, Ord, Show, Read, Typeable)
instance A.FromJSON Friend where
parseJSON (A.Object v) =
Friend <$> v .: "id"
<*> v .: "name"
parseJSON _ = mzero
getUserFriends ::
(C.MonadResource m, MonadBaseControl IO m) =>
UserId
-> [Argument]
-> UserAccessToken
-> FacebookT anyAuth m (Pager Friend)
getUserFriends id_ query token =
getObject ("/" <> idCode id_ <> "/friends") query (Just token)