Просмотр исходного кода

#4148 - Build-in API support - timeline

roman.lesnikov@gmail.com 3 лет назад
Родитель
Сommit
13dc8c9170

+ 10 - 0
inc/classes/BxDolProfile.php

@@ -130,6 +130,16 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
         return $GLOBALS['bxDolClasses'][$sClass];
     }
 
+    public static function getData($mixedProfileId = false)
+    {
+        $oProfile = BxDolProfile::getInstanceMagic($mixedProfileId);
+        return [
+            'display_name' => $oProfile->getDisplayName(),
+            'url' => $oProfile->getUrl(),
+            'url_avatar' => $oProfile->getAvatar(),
+        ];
+    }
+    
     /**
      * Get profile id
      */

+ 7 - 6
inc/utils.inc.php

@@ -2326,11 +2326,6 @@ function bx_absolute_url($sUrl, $sPrefix = BX_DOL_URL_ROOT)
 
 function bx_is_api()
 {
-    if (bx_get('api-acc')){
-        $iCookieTime = true ? time() + 60 * getParam('sys_session_lifetime_in_min') : 0;
-        bx_setcookie("memberID", 1, $iCookieTime, 'auto');
-        check_logged();
-    }
     return defined('BX_API') || bx_get('api') ? true : false;
 }
 
@@ -2351,8 +2346,14 @@ function bx_api_check_origins()
             header('Access-Control-Allow-Methods: POST, GET');
             header('Access-Control-Allow-Headers: Accept-Encoding, Authorization, Cache-Control, Connection, Host, Origin, Pragma, Referer, User-Agent, X-Custom-Header, X-Requested-With');                    
             exit;
-        }            
+        } 
+        
+      
     }
+    //TODO: Temporatery for use logged state 
+    bx_login(1);
+    check_logged();
+    //echoDbgLog($_SERVER);
 }
 
 /** @} */

+ 4 - 3
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -3458,12 +3458,13 @@ class BxBaseModGeneralModule extends BxDolModule
             return false;
 
         
-        if (bx_is_api())
+        if (bx_is_api()){
+            $aData = $oCmts->getFormPost(0);
             return [
                 ['id' => 1, 'type' => 'browse', 'data' => $oCmts->getCommentsBlockAPI(array(), array('in_designbox' => false, 'show_empty' => false))],
-                ['id' => 2, 'type' => 'form', 'data' => $oCmts->getFormObject()->getCodeAPI(), 'request' => ['url' => '/cmts.php', 'immutable' => true]]
+                ['id' => 2, 'type' => 'form', 'data' => $aData['frm']->getCodeAPI(), 'request' => ['url' => '/cmts.php?api=1', 'immutable' => true]]
             ];
-
+        }
         return $oCmts->getCommentsBlock(array(), array('in_designbox' => false, 'show_empty' => false));
     }
 

+ 3 - 13
modules/base/general/classes/BxBaseModGeneralSearchResult.php

@@ -214,10 +214,11 @@ class BxBaseModGeneralSearchResult extends BxTemplSearchResult
     function decodeData ($a)
     {
         $oContentInfo = $this->getContentInfoObject();
-
+        $CNF = $this->oModule->_oConfig->CNF;
+        
         foreach ($a as $i => $r) {
             if (isset($r['author']))
-                $a[$i]['author_data'] = $this->decodeDataAuthor($oContentInfo, $r);
+                $a[$i]['author_data'] = BxDolProfile::getData($r[$CNF['FIELD_AUTHOR']]);
 
             $a[$i]['url'] = $this->decodeDataUrl($oContentInfo, $r);
             $a[$i]['url_thumb'] = $oContentInfo->getContentThumb($r['id']);
@@ -232,17 +233,6 @@ class BxBaseModGeneralSearchResult extends BxTemplSearchResult
         return bx_ltrim_str($oContentInfo->getContentLink($r['id']), BX_DOL_URL_ROOT);
     }
 
-    function decodeDataAuthor ($oContentInfo, $r) 
-    {
-        $CNF = $this->oModule->_oConfig->CNF;
-        $oProfile = BxDolProfile::getInstanceMagic($r[$CNF['FIELD_AUTHOR']]);
-        return [
-            'display_name' => $oProfile->getDisplayName(),
-            'url' => $oProfile->getUrl(),
-            'url_avatar' => $oProfile->getAvatar(),
-        ];
-    }
-
     function decodeDataSummaryPlain ($oContentInfo, $r)
     {
         $CNF = $this->oModule->_oConfig->CNF;

+ 1 - 3
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -4901,9 +4901,7 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
 
         $this->_iOwnerId = $aParams['owner_id'];
 
-        return array(
-            'content' => $this->_oTemplate->getViewBlock($aParams)
-        );
+        return bx_is_api() ? [['id' => 1, 'type' => 'browse', 'data' => ['unit' => 'feed', 'data' => $this->_oTemplate->getViewBlock($aParams)]]] : ['content' => $this->_oTemplate->getViewBlock($aParams)];
     }
 
     protected function _getBlockPost($iProfileId, $aParams = array())

+ 9 - 0
modules/boonex/timeline/classes/BxTimelineTemplate.php

@@ -331,6 +331,15 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
         $oModule = $this->getModule();
 
         list($sContent, $sLoadMore, $sBack, $sEmpty, $iEvent, $bEventsToLoad) = $this->getPosts($aParams);
+        
+        if (bx_is_api()){
+            $aPosts = $this->getPosts(array_merge($aParams, ['return_data_type' => 'array']));
+            foreach($aPosts as &$aPost)  {
+                $aPost['author_data'] = BxDolProfile::getData($aPost['object_owner_id']);
+                $aPost['url'] = bx_ltrim_str($aPost['content']['url'], BX_DOL_URL_ROOT);
+            }
+            return $aPosts;
+        }
 
         //--- Add live update
         $oModule->actionResumeLiveUpdate($aParams['type'], $aParams['owner_id']);

+ 12 - 23
template/scripts/BxBaseCmts.php

@@ -155,22 +155,12 @@ class BxBaseCmts extends BxDolCmts
     {
         foreach ($a as $i => $r) {
             if (isset($r['cmt_author_id']))
-                $a[$i]['author_data'] = $this->decodeDataAuthor($r);
+                $a[$i]['author_data'] = BxDolProfile::getData($r['cmt_author_id']);
         }
 
         return $a;
     }
 
-    function decodeDataAuthor ($r) // TODO: get rid of duplicate code here and in BxBaseModGeneralSearchResult
-    {
-        $oProfile = BxDolProfile::getInstanceMagic($r['cmt_author_id']);
-        return [
-            'display_name' => $oProfile->getDisplayName(),
-            'url' => $oProfile->getUrl(),
-            'url_avatar' => $oProfile->getAvatar(),
-        ];
-    }
-
     /**
      * get full comments block with initializations
      */
@@ -1135,23 +1125,22 @@ class BxBaseCmts extends BxDolCmts
     protected function _getFormPost($iCmtParentId = 0, $aDp = [])
     {
         $bCmtParentId = !empty($iCmtParentId);
-        if (!bx_is_api()){
-            if(!$bCmtParentId && !$this->isPostAllowed()) {
-                $sMsg = '';
-                if(!isLogged()) {
-                    $oPermalink = BxDolPermalinks::getInstance();
-                    $sMsg = _t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account'));
-                }
-                else
-                    $sMsg = $this->msgErrPostAllowed();
 
-                return ['msg' => $sMsg];
+        if(!$bCmtParentId && !$this->isPostAllowed()) {
+            $sMsg = '';
+            if(!isLogged()) {
+                $oPermalink = BxDolPermalinks::getInstance();
+                $sMsg = _t('_cmt_msg_login_required', $oPermalink->permalink('page.php?i=login'), $oPermalink->permalink('page.php?i=create-account'));
             }
+            else
+                $sMsg = $this->msgErrPostAllowed();
 
-            if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
-                return ['msg' => $this->msgErrReplyAllowed()];
+            return ['msg' => $sMsg];
         }
 
+        if($bCmtParentId && !$this->isReplyAllowed($iCmtParentId))
+            return ['msg' => $this->msgErrReplyAllowed()];
+
         $bDynamic = isset($aDp['dynamic_mode']) && (bool)$aDp['dynamic_mode'];
         $bQuote = isset($aDp['quote']) && (bool)$aDp['quote'];