Browse Source

Merge branch 'master' of https://github.com/unaio/una

AntonLV 9 years ago
parent
commit
f017ccd148

+ 15 - 5
modules/base/connect/classes/BxBaseModConnectDb.php

@@ -21,19 +21,29 @@ class BxBaseModConnectDb extends BxBaseModGeneralDb
     }
     
     /**
-     * Check remote profile id
+     * Get local profile id by remote profile id
      *
-     * @param $iRemoteId integer
+     * @param $iRemoteId remote profile id
      * @return local profile id
      */
     function getProfileId($iRemoteId)
     {
-        $iRemoteId = (int) $iRemoteId;
-
         $sQuery = $this->prepare ("SELECT `local_profile` FROM `{$this -> sTablePrefix}accounts` WHERE `remote_profile` = ? LIMIT 1", $iRemoteId);
-        return $this -> getOne($sQuery);
+        return $this->getOne($sQuery);
     }
 
+    /**
+     * Get remote profile id by local profile id
+     *
+     * @param $iLocalId local profile id
+     * @return remote profile id
+     */
+    function getRemoteProfileId($iLocalId)
+    {
+        $sQuery = $this->prepare ("SELECT `remote_profile` FROM `{$this -> sTablePrefix}accounts` WHERE `local_profile` = ? LIMIT 1", (int)$iLocalId);
+        return $this->getOne($sQuery);
+    }
+    
     /**
      * Save new remote ID
      *

+ 43 - 0
modules/boonex/dolphin_connect/classes/BxDolConModule.php

@@ -86,6 +86,9 @@ class BxDolConModule extends BxBaseModConnectModule
         $sExpiresAt = new \DateTime('+' . $sExpiresIn . ' seconds');
         $sRefreshToken = $aResponse['refresh_token'];
 
+        $oSession = BxDolSession::getInstance();
+        $oSession->setValue($this->getName() . '_access_token', $sAccessToken);
+
         // request info about profile
         $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'api/me', array(), 'get', array(
             'Authorization: Bearer ' . $sAccessToken,
@@ -119,6 +122,46 @@ class BxDolConModule extends BxBaseModConnectModule
         }
     }
 
+    /**
+     * Make friends
+     *
+     * @param $iProfileId integer
+     * @return void
+     */
+    protected function _makeFriends($iProfileId)
+    {
+        if (!$this->_oConfig->bAutoFriends)
+            return;
+
+        $oConnFrinds = BxDolConnection::getObjectInstance('sys_profiles_friends');
+        if (!$oConnFrinds)
+            return;
+
+        // request info about profile
+        if (!($iRemoteProfileId = $this->_oDb->getRemoteProfileId($iProfileId)))
+            return;
+        $oSession = BxDolSession::getInstance();
+        if (!($sAccessToken = $oSession->getValue($this->getName() . '_access_token')))
+            return;
+        $s = bx_file_get_contents($this->_oConfig->sApiUrl . 'api/friends?id=' . $iRemoteProfileId, array(), 'get', array(
+            'Authorization: Bearer ' . $sAccessToken,
+        ));
+
+        // handle error
+        if (!$s || NULL === ($aResponse = json_decode($s, true)) || !$aResponse || isset($aResponse['error']) || !isset($aResponse['friends'])) {
+            $sErrorDescription = isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
+            return;
+        }
+
+        // add friends & followers
+        foreach ($aResponse['friends'] as $iRemoteProfileId => $a) {
+            if (!($iLocalProfileId = $this->_oDb->getProfileId($iRemoteProfileId)))
+                continue;
+            $oConnFrinds->actionAdd($iProfileId, $iLocalProfileId);
+            $oConnFrinds->actionAdd($iLocalProfileId, $iProfileId);
+        }
+    }
+    
     /**
      * @param $aProfileInfo - remote profile info
      * @param $sAlternativeName - suffix to add to NickName to make it unique

+ 10 - 6
modules/boonex/facebook_connect/classes/BxFaceBookConnectModule.php

@@ -166,10 +166,13 @@ class BxFaceBookConnectModule extends BxBaseModConnectModule
      */
     protected function _makeFriends($iProfileId)
     {
-        if (!$this->_oConfig->bAutoFriends) {
+        if (!$this->_oConfig->bAutoFriends)
             return;
-        }
 
+        $oConnFrinds = BxDolConnection::getObjectInstance('sys_profiles_friends');
+        if (!$oConnFrinds)
+            return;
+        
         try {
             //get friends from facebook
             $oFriendsResponse = $this -> oFacebook -> get('/me/friends?limit=50');
@@ -184,11 +187,12 @@ class BxFaceBookConnectModule extends BxBaseModConnectModule
         do {
             foreach ($oPagesEdge as $oPage) {
                 $aFriend = $oPage->asArray();
-                $iFriendId = $this -> _oDb -> getProfileId($aFriend['id']);
-
-                // TODO:
+                if (!($iLocalProfileId = $this->_oDb->getProfileId($aFriend['id'])))
+                    continue;
+                $oConnFrinds->actionAdd($iProfileId, $iLocalProfileId);
+                $oConnFrinds->actionAdd($iLocalProfileId, $iProfileId);
             }
-        } while ($oPagesEdge = $this -> oFacebook -> next($oPagesEdge));
+        } while ($oPagesEdge = $this->oFacebook->next($oPagesEdge));
     }
 
     /**