Пример #1
0
        /// <summary>
        /// Returns a list of All friends who have added the facebook app
        /// regardless of whether they are online
        /// </summary>
        /// <param name="fbAccountId"></param>
        /// <param name="fbSessionKey"></param>
        /// <returns></returns>
        public SimpleResponse GetAllFriendsWhoAreAppUsers(long fbAccountId, string fbSessionKey, string getMockData)
        {
            mServiceLog.Log.InfoFormat("GetAllFriendsWhoAreAppUsers: fbAccountId={0}, fbSessionKey={1}, getMockData={2}", fbAccountId, fbSessionKey, getMockData);
            bool isMockRequest = false;

            if (getMockData == "1" || getMockData == "true")
            {
                isMockRequest = true;
            }
            StringBuilder xmlBuilder = new StringBuilder();

            //instantiate connection to facebookApi
            FacebookSession facebookSession = new HangoutFacebookSession();

            facebookSession.ApplicationKey    = WebConfig.FacebookAPIKey;
            facebookSession.ApplicationSecret = WebConfig.FacebookSecret;
            facebookSession.SessionKey        = fbSessionKey;
            Api facebookApi = new Api(facebookSession);


            Dictionary <long, string> accountMapping = new Dictionary <long, string>();
            List <long> fbAppUserFriendIds;

            try
            {
                fbAppUserFriendIds = (List <long>)facebookApi.Friends.GetAppUsers();
            }
            catch (FacebookException fbEx)
            {
                mServiceLog.Log.Error("Facebook Exception: errorcode=" + fbEx.ErrorCode + ", errorType=" + fbEx.ErrorType + ", message=" + fbEx.Message);
                HangoutException hangoutException = new HangoutException("FACEBOOK EXCEPTION: " + fbEx.Message + ", Facebook ErrorCode: " + fbEx.ErrorCode + ", FbErrorType: " + fbEx.ErrorType);


                hangoutException.Source = fbEx.Source;
                throw hangoutException;
            }
            catch (System.Exception ex)
            {
                throw new Exception("Facebook did not return any useful information or invalid xml " + ex.Message, ex.InnerException);
            }

            accountMapping = ListHangoutAccountsForFbAccounts(fbAppUserFriendIds);

            //For each friend who is an app user,
            XmlDocument friendsXml = GetAllFriendsWhoAreAppUsersXml(fbAccountId, facebookApi);

            string friendString = "";

            if (getMockData != null && isMockRequest)
            {
                friendString = GetMockFriendDataForFacebook();
            }
            else
            {
                friendString = ConstructFriendData(friendsXml, accountMapping);
            }


            return(new SimpleResponse("Friends", friendString));
        }
Пример #2
0
        public SimpleResponse GetBannedUsers()
        {
            FacebookSession facebookSession = new HangoutFacebookSession();

            facebookSession.ApplicationKey    = WebConfig.FacebookAPIKey;
            facebookSession.ApplicationSecret = WebConfig.FacebookSecret;
            Api facebookApi = new Api(facebookSession);

            IList <long> bannedUsers      = facebookApi.Admin.GetBannedUsers();
            string       bannedUserString = "";
            string       delimiter        = "";

            foreach (long bUser in bannedUsers)
            {
                bannedUserString += delimiter + bUser;
                delimiter         = ",";
            }

            AccountService accounts       = new AccountService();
            string         response       = "";
            XmlDocument    accountsXmlDoc = accounts.GetAccounts(null, null, null, null, bannedUserString, "1");

            if (accountsXmlDoc != null)
            {
                response = accountsXmlDoc.OuterXml;
            }

            return(new SimpleResponse("BannedUsers", response));
        }
Пример #3
0
        public SimpleResponse LoginFacebook(long fbAccountId, string fbSessionKey)
        {
            mServiceLog.Log.InfoFormat("LoginFacebook: fbAccountId={0}, fbSessionKey={1}", fbAccountId, fbSessionKey);

            FacebookSession facebookSession = new HangoutFacebookSession();

            facebookSession.ApplicationKey    = WebConfig.FacebookAPIKey;
            facebookSession.ApplicationSecret = WebConfig.FacebookSecret;
            facebookSession.SessionKey        = fbSessionKey;

            Api facebookApi = new Api(facebookSession);

            long currentFbUserId;

            Facebook.Schema.user fbUser;

            try
            {
                currentFbUserId = facebookApi.Users.GetLoggedInUser();
                fbUser          = facebookApi.Users.GetInfo(currentFbUserId);
            }
            catch (FacebookException fbEx)
            {
                mServiceLog.Log.Error("Facebook Exception: errorcode=" + fbEx.ErrorCode + ", errorType=" + fbEx.ErrorType + ", message=" + fbEx.Message);
                HangoutException hangoutException = new HangoutException("FACEBOOK EXCEPTION: " + fbEx.Message + ", Facebook ErrorCode: " + fbEx.ErrorCode + ", FbErrorType: " + fbEx.ErrorType);


                hangoutException.Source = fbEx.Source;
                throw hangoutException;
            }
            catch (System.Exception ex)
            {
                throw new Exception("Facebook did not return any useful information or invalid xml " + ex.Message, ex.InnerException);
            }

            if (!fbUser.is_app_user.GetValueOrDefault())
            {
                throw new Exception("is_app_user returned false");
            }

            if (fbAccountId != fbUser.uid)
            {
                throw new Exception("logged in facebook UID does not match fbAccountId parameter");
            }
            SetUserMetricsData(fbAccountId.ToString());
            return(GetAccounts(fbAccountId.ToString(), null, null, null, null, null));
        }
Пример #4
0
        public SimpleResponse GetAllFacebookFriends(long fbAccountId, string fbSessionKey)
        {
            mServiceLog.Log.InfoFormat("GetAllFacebookFriends: fbAccountId={0}, fbSessionKey={1}", fbAccountId, fbSessionKey);
            StringBuilder xmlBuilder = new StringBuilder();

            FacebookSession facebookSession = new HangoutFacebookSession();

            facebookSession.ApplicationKey    = WebConfig.FacebookAPIKey;
            facebookSession.ApplicationSecret = WebConfig.FacebookSecret;
            facebookSession.SessionKey        = fbSessionKey;

            Api facebookApi = new Api(facebookSession);

            //IList<facebook.Schema.user> fbFriends = facebookApi.friends.getUserObjects(fbAccountId);//get(Convert.ToInt64(fbAccountId));
            mServiceLog.Log.InfoFormat("GetAllFacebookFriends: got fb response");

            Dictionary <long, string> accountMapping = new Dictionary <long, string>();

            List <long> fbAppUserFriendIds;

            try
            {
                fbAppUserFriendIds = (List <long>)facebookApi.Friends.GetAppUsers();
            }
            catch (FacebookException fbEx)
            {
                mServiceLog.Log.Error("Facebook Exception: errorcode=" + fbEx.ErrorCode + ", errorType=" + fbEx.ErrorType + ", message=" + fbEx.Message);
                HangoutException hangoutException = new HangoutException("FACEBOOK EXCEPTION: " + fbEx.Message + ", Facebook ErrorCode: " + fbEx.ErrorCode + ", FbErrorType: " + fbEx.ErrorType);


                hangoutException.Source = fbEx.Source;
                throw hangoutException;
            }
            catch (System.Exception ex)
            {
                throw new Exception("Facebook did not return any useful information or invalid xml " + ex.Message, ex.InnerException);
            }
            accountMapping = ListHangoutAccountsForFbAccounts(fbAppUserFriendIds);

            XmlDocument friendsXml = GetAllFriendsXml(fbAccountId, facebookApi);

            xmlBuilder.Append(ConstructFriendData(friendsXml, accountMapping));

            return(new SimpleResponse("Friends", xmlBuilder.ToString()));
        }
Пример #5
0
        /// <summary>
        /// CreateAccountFromFacebook checks first to see if there is an existing account for this fbUser.
        /// If a user exists it simply returns that user, otherwise it inserts a new user into the db
        /// and returns their information
        /// </summary>
        /// <param name="fbAccountId"></param>
        /// <param name="fbSessionKey"></param>
        /// <param name="nickName"></param>
        /// <param name="campaign"></param>
        /// ONLY ONE of the following should be sent.
        /// <param name="referringAccountId"></param>
        /// <param name="referringFbAccountId"></param>
        /// <param name="campaign"></param>
        /// <returns></returns>
        public SimpleResponse CreateAccountFromFacebook(long fbAccountId, string fbSessionKey, string nickName, string referringAccountId, string referringFbAccountId, string campaign)
        {
            mServiceLog.Log.InfoFormat("CreateAccountFromFacebook: fbAccountId={0}, fbSessionKey={1}, nickName={2}, referringAccountId={3}, referringFbAccountId={4}, campaign={5}", fbAccountId, fbSessionKey, nickName, referringAccountId, referringFbAccountId, campaign);

            FacebookSession facebookSession = new HangoutFacebookSession();

            facebookSession.ApplicationKey    = WebConfig.FacebookAPIKey;
            facebookSession.ApplicationSecret = WebConfig.FacebookSecret;
            facebookSession.SessionKey        = fbSessionKey;

            Api facebookApi = new Api(facebookSession);

            long currentFbUserId;

            Facebook.Schema.user fbUser;

            try
            {
                currentFbUserId = facebookApi.Users.GetLoggedInUser();
                fbUser          = facebookApi.Users.GetInfo(currentFbUserId);
            }
            catch (FacebookException fbEx)
            {
                mServiceLog.Log.Error("Facebook Exception: errorcode=" + fbEx.ErrorCode + ", errorType=" + fbEx.ErrorType + ", message=" + fbEx.Message);
                HangoutException hangoutException = new HangoutException("FACEBOOK EXCEPTION: " + fbEx.Message + ", Facebook ErrorCode: " + fbEx.ErrorCode + ", FbErrorType: " + fbEx.ErrorType);

                hangoutException.Source = fbEx.Source;
                throw hangoutException;
            }
            catch (System.Exception ex)
            {
                throw new Exception("Facebook did not return any useful information or invalid xml " + ex.Message, ex.InnerException);
            }



            if (!fbUser.is_app_user.GetValueOrDefault())
            {
                throw new Exception("is_app_user returned false");
            }

            if (fbAccountId != fbUser.uid)
            {
                throw new Exception("logged in facebook UID does not match fbAccountId parameter");
            }

            SimpleResponse selectResponse = GetAccounts(fbAccountId.ToString(), null, null, null, null, null);

            XmlNode accountNode = selectResponse.SelectSingleNode("//Account");

            if (accountNode != null)
            {
                XmlAttribute accountIdAttr = accountNode.Attributes["AccountId"];
                if (accountIdAttr != null && !String.IsNullOrEmpty(accountIdAttr.Value))
                {
                    return(selectResponse);
                }
            }
            //pass through and insert a new user... because the old user don't EXIST!!

            uint accountId = 0;

            using (MySqlConnection mysqlConnection = new MySqlConnection(WebConfig.AccountsDBConnectionString))
            {
                mysqlConnection.Open();

                string createNewHangoutUserQuery = "INSERT INTO LocalAccountInfo " +
                                                   " (FacebookAccountId, HangoutNickName, FirstName, LastName, Birthdate, Gender, CreatedDate, LastLoggedIn) " +
                                                   " VALUES (@FBAccountId, @HangoutNickName, @FirstName, @LastName, @Birthdate, @Gender, UTC_TIMESTAMP(), UTC_TIMESTAMP()); SELECT LAST_INSERT_ID();";

                using (MySqlCommand createAccountCommand = mysqlConnection.CreateCommand())
                {
                    createAccountCommand.CommandText = createNewHangoutUserQuery;
                    createAccountCommand.Parameters.AddWithValue("@FBAccountId", fbAccountId);
                    createAccountCommand.Parameters.AddWithValue("@HangoutNickName", fbUser.first_name);
                    createAccountCommand.Parameters.AddWithValue("@FirstName", fbUser.first_name);
                    createAccountCommand.Parameters.AddWithValue("@LastName", fbUser.last_name);
                    createAccountCommand.Parameters.AddWithValue("@Birthdate", fbUser.birthday_date.ToShortDateString());
                    createAccountCommand.Parameters.AddWithValue("@Gender", fbUser.sex);


                    accountId = Convert.ToUInt32(createAccountCommand.ExecuteScalar());
                    if (accountId == 0)
                    {
                        throw new Exception("AccountId returned 0");
                    }
                }

                InsertReferralData(accountId.ToString(), referringAccountId, referringFbAccountId, campaign);
            }

            return(GetAccounts(null, accountId.ToString(), null, null, null, null));
        }
Пример #6
0
        public SimpleResponse BanUsers(string fbAccountId, string accountId, string fbAccountIdCsvList, string accountIdCsvList)
        {
            mServiceLog.Log.InfoFormat("BanUser: fbAccountId={0}, fbSessionKey={1}", fbAccountId, fbAccountIdCsvList);

            List <long> uids = new List <long>();

            if (!String.IsNullOrEmpty(fbAccountId))
            {
                uids.Add(Convert.ToInt64(fbAccountId));
            }
            if (fbAccountIdCsvList != null)
            {
                if (fbAccountIdCsvList != "")
                {
                    string[] fbAccountIds = fbAccountIdCsvList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string s in fbAccountIds)
                    {
                        long sUint = Convert.ToInt64(s);
                        uids.Add(sUint);
                    }
                }
            }
            if (!String.IsNullOrEmpty(accountId))
            {
                if (!String.IsNullOrEmpty(accountIdCsvList))
                {
                    accountIdCsvList += "," + accountId;
                }
                else
                {
                    accountIdCsvList += accountId;
                }
            }
            if (accountIdCsvList != null)
            {
                if (accountIdCsvList != "")
                {
                    AccountService accounts           = new AccountService();
                    XmlDocument    accountXmlNodeList = accounts.GetAccounts(null, null, null, accountIdCsvList, null, null);
                    foreach (XmlNode accountXmlNode in accountXmlNodeList)
                    {
                        if (accountXmlNode != null)
                        {
                            if (accountXmlNode.Attributes["FbAccountId"] != null)
                            {
                                long uInt = Convert.ToInt64(accountXmlNode.Attributes["FbAccountId"].Value);
                                uids.Add(uInt);
                            }
                        }
                    }
                }
            }

            FacebookSession facebookSession = new HangoutFacebookSession();

            facebookSession.ApplicationKey    = WebConfig.FacebookAPIKey;
            facebookSession.ApplicationSecret = WebConfig.FacebookSecret;
            Api facebookApi = new Api(facebookSession);

            if (uids.Count == 0)
            {
                throw new Exception("You must include at least one fbAccountId to ban;");
            }
            bool response = facebookApi.Admin.BanUsers(uids);


            return(new SimpleResponse("Success", response.ToString()));
        }