{-# LANGUAGE FlexibleContexts #-}moduleFacebook.TestUsers(TestUser(..),CreateTestUser(..),CreateTestUserInstalled(..),getTestUsers,removeTestUser,createTestUser,makeFriendConn,incompleteTestUserAccessToken)whereimportControl.Applicative((<$>),(<*>))importControl.Monad(unless,mzero)importControl.Monad.Trans.Control(MonadBaseControl)importData.DefaultimportData.TextimportData.Time(UTCTime(..),Day(..))importData.Typeable(Typeable)importqualifiedControl.Exception.LiftedasEimportqualifiedData.AesonasAimportqualifiedData.ByteString.Char8asBimportqualifiedData.ConduitasCimportFacebook.AuthimportFacebook.BaseimportFacebook.GraphimportFacebook.MonadimportFacebook.Types-- | A Facebook test user.dataTestUser=TestUser{tuId::UserId,tuAccessToken::MaybeAccessTokenData,tuLoginUrl::MaybeText,tuEmail::MaybeText,tuPassword::MaybeText}deriving(Eq,Ord,Show,Read,Typeable)instanceA.FromJSONTestUserwhereparseJSON(A.Objectv)=TestUser<$>vA..:"id"<*>vA..:?"access_token"<*>vA..:?"login_url"<*>vA..:?"email"<*>vA..:?"password"parseJSON_=mzero-- | Data type used to hold information of a new test user. This type-- also accepts a Data.Default value.dataCreateTestUser=CreateTestUser{ctuInstalled::CreateTestUserInstalled,ctuName::MaybeText,ctuLocale::MaybeText}-- | Specify if the app is to be installed on the new test user. If-- it is, then you must tell what permissions should be given.dataCreateTestUserInstalled=CreateTestUserNotInstalled|CreateTestUserInstalled{ctuiPermissions::[Permission]}|CreateTestUserFbDefault-- ^ Uses Facebook's default. It seems that this is equivalent to-- @CreateTestUserInstalled []@, but Facebook's documentation is-- not clear about it.-- | Default instance for 'CreateTestUser'.instanceDefaultCreateTestUserwheredef=CreateTestUserdefdefdef-- | Default instance for 'CreateTestUserInstalled'.instanceDefaultCreateTestUserInstalledwheredef=CreateTestUserFbDefault-- | Construct a query from a 'CreateTestUser'.createTestUserQueryArgs::CreateTestUser->[Argument]createTestUserQueryArgs(CreateTestUserinstallednamelocale)=forInstinstalled++forField"name"name++forField"locale"localewhereforInst(CreateTestUserInstalledp)=["installed"#=True,"permissions"#=p]forInstCreateTestUserNotInstalled=["installed"#=False]forInstCreateTestUserFbDefault=[]forField_Nothing=[]forFieldfieldName(Justf)=[fieldName#=f]-- | Create a new test user.createTestUser::(C.MonadResourcem,MonadBaseControlIOm)=>CreateTestUser-- ^ How the test user should be-- created.->AppAccessToken-- ^ Access token for your app.->FacebookTAuthmTestUsercreateTestUseruserInfotoken=docreds<-getCredsletquery=("method","post"):createTestUserQueryArgsuserInfogetObject("/"<>appIdcreds<>"/accounts/test-users")query(Justtoken)-- | Get a list of test users.getTestUsers::(C.MonadResourcem,MonadBaseControlIOm)=>AppAccessToken-- ^ Access token for your app.->FacebookTAuthm(PagerTestUser)getTestUserstoken=docreds<-getCredsgetObject("/"<>appIdcreds<>"/accounts/test-users")[](Justtoken)-- | Remove an existing test user.removeTestUser::(C.MonadResourcem,MonadBaseControlIOm)=>TestUser-- ^ The TestUser to be removed.->AppAccessToken-- ^ Access token for your app.->FacebookTAuthmBoolremoveTestUsertestUsertoken=getObjectBool("/"<>idCode(tuIdtestUser))[("method","delete")](Justtoken)-- | Make a friend connection between two test users.---- This is how Facebook's API work: two calls must be made. The first-- call has the format: \"\/userA_id\/friends\/userB_id\" with the-- access token of user A as query parameter. The second call has the-- format: \"\/userB_id\/friends\/userA_id\" with the access token of-- user B as query parameter. The first call creates a friend request-- and the second call accepts the friend request.makeFriendConn::(C.MonadResourcem,MonadBaseControlIOm)=>TestUser->TestUser->FacebookTAuthm()makeFriendConn(TestUser{tuAccessToken=Nothing})_=E.throw$FbLibraryException"The test user passed on the first argument doesn't have\
\ a token. Both users must have a token."makeFriendConn_(TestUser{tuAccessToken=Nothing})=E.throw$FbLibraryException"The test user passed on the second argument doesn't have\
\ a token. Both users must have a token."makeFriendConn(TestUser{tuId=id1,tuAccessToken=(Justtoken1)})(TestUser{tuId=id2,tuAccessToken=(Justtoken2)})=doletfriendRequserId1userId2token=getObjectBool("/"<>idCodeuserId1<>"/friends/"<>idCodeuserId2)["method"#=("post"::B.ByteString),"access_token"#=token]Nothingr1<-friendReqid1id2token1r2<-friendReqid2id1token2unlessr1$E.throw$FbLibraryException"Couldn't make friend request."unlessr2$E.throw$FbLibraryException"Couldn't accept friend request."return()-- | Create an 'UserAccessToken' from a 'TestUser'. It's incomplete-- because it will not have the right expiration time.incompleteTestUserAccessToken::TestUser->MaybeUserAccessTokenincompleteTestUserAccessTokent=dotokenData<-tuAccessTokentletfarFuture=UTCTime(ModifiedJulianDay100000)0return(UserAccessToken(tuIdt)tokenDatafarFuture)-- | Same as 'getObject', but instead of parsing the result-- as a JSON, it tries to parse either as "true" or "false".-- Used only by the Test User API bindings.getObjectBool::(C.MonadResourcem,MonadBaseControlIOm)=>Text-- ^ Path (should begin with a slash @\/@).->[Argument]-- ^ Arguments to be passed to Facebook.->Maybe(AccessTokenanyKind)-- ^ Optional access token.->FacebookTanyAuthmBoolgetObjectBoolpathquerymtoken=runResourceInFb$dobs<-asBS=<<fbhttp=<<fbreqpathmtokenqueryreturn(bs=="true")