Procházet zdrojové kódy

Ticket #4369 - New PUSH server integration: Usage in Comments (updated).

AntonLV před 3 roky
rodič
revize
9939b7a327

+ 18 - 2
inc/classes/BxDolCmts.php

@@ -643,11 +643,22 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
         return $oReport;
         return $oReport;
     }
     }
 
 
+    /**
+     * Is used as: 
+     * 1. Live Update's session key.
+     * 2. HTML ID for Live Update's 'New Content' button.
+     * @return string with ID.
+     */
     public function getNotificationId()
     public function getNotificationId()
     {
     {
         return 'cmts-notification-' . $this->_sSystem . '-' . $this->_iId;
         return 'cmts-notification-' . $this->_sSystem . '-' . $this->_iId;
     }
     }
 
 
+    public function getSocketName()
+    {
+        return 'cmts_' . $this->_sSystem;
+    }
+
     /**
     /**
      * @deprecated since version 10.0.0-B3 and can be removed in later versions.
      * @deprecated since version 10.0.0-B3 and can be removed in later versions.
      */
      */
@@ -1573,8 +1584,13 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
         bx_alert($this->_sSystem, 'commentPost', $iObjId, $iPerformerId, $aAlertParams);
         bx_alert($this->_sSystem, 'commentPost', $iObjId, $iPerformerId, $aAlertParams);
         bx_alert('comment', 'added', $iCmtId, $iPerformerId, $aAlertParams);
         bx_alert('comment', 'added', $iCmtId, $iPerformerId, $aAlertParams);
 
 
-        BxDolSockets::getInstance()->sendEvent($this->_sSystem , $iObjId, 'comment_added', json_encode(['anchor' => $this->getItemAnchor($iCmtId), 'id' => $iCmtId, 'author_id' => $iPerformerId, 'notif_id' => $this->getNotificationId()]));
-        
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnable())
+            $oSockets->sendEvent($this->getSocketName(), $iObjId, 'comment_added', json_encode([
+                'id' => $iCmtId, 
+                'author_id' => $iPerformerId, 
+                'anchor' => $this->getItemAnchor($iCmtId)
+            ]));
+
         $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
         $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
         bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_add_comment', $aAuditParams);
         bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_add_comment', $aAuditParams);
 
 

+ 3 - 2
inc/classes/BxDolScore.php

@@ -195,7 +195,7 @@ class BxDolScore extends BxDolObject
 
 
     public function getSocketName()
     public function getSocketName()
     {
     {
-        return $this->_sSystem;
+        return $this->_sSystem . '_scores';
     }
     }
 
 
     /**
     /**
@@ -277,7 +277,8 @@ class BxDolScore extends BxDolObject
             'disabled' => !$bVoted,
             'disabled' => !$bVoted,
         ];
         ];
 
 
-        BxDolSockets::getInstance()->sendEvent($this->getSocketName(), $iObjectId, 'voted', json_encode($this->_returnVoteDataForSocket($aResult)));
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnable())
+            $oSockets->sendEvent($this->getSocketName(), $iObjectId, 'voted', json_encode($this->_returnVoteDataForSocket($aResult)));
 
 
         return $aResult;
         return $aResult;
     }
     }

+ 2 - 1
inc/classes/BxDolVote.php

@@ -259,7 +259,8 @@ class BxDolVote extends BxDolObject
 
 
         $aResult = $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);
         $aResult = $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);
 
 
-        BxDolSockets::getInstance()->sendEvent($this->getSocketName(), $iObjectId, 'voted', json_encode($this->_returnVoteDataForSocket($aResult)));
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnable())
+            $oSockets->sendEvent($this->getSocketName(), $iObjectId, 'voted', json_encode($this->_returnVoteDataForSocket($aResult)));
 
 
         return $aResult;
         return $aResult;
     }
     }

+ 16 - 11
inc/js/classes/BxDolCmts.js

@@ -702,15 +702,6 @@ BxDolCmts.prototype.goTo = function(oLink, sGoToId, sBlinkIds, onLoad)
     });
     });
 };
 };
 
 
-
-BxDolCmts.prototype.showNew = function(sData)
-{
-    let oData = JSON.parse(sData)
-    if ($('#' + oData.notif_id).hasClass('hidden') &&  this._iAuthorId != oData.author_id ){
-        $('#' + oData.notif_id).removeClass('hidden');
-        $('#' + oData.notif_id + ' A').first().attr('onclick', "javascript:oCmtsBxPosts_53.goToBtn(this, '" + oData.anchor + "', " + oData.id + ");")
-    }
-}
 BxDolCmts.prototype.goToBtn = function(oLink, sGoToId, sBlinkIds, onLoad)
 BxDolCmts.prototype.goToBtn = function(oLink, sGoToId, sBlinkIds, onLoad)
 {
 {
     var $this = this;
     var $this = this;
@@ -730,8 +721,7 @@ BxDolCmts.prototype.goToBtn = function(oLink, sGoToId, sBlinkIds, onLoad)
             if(typeof onLoad == 'function')
             if(typeof onLoad == 'function')
                 onLoad();
                 onLoad();
 
 
-            $this._loadingInButton(oLink, false);
-            $(oLink).parents('.cmt-lu-button:first').addClass('hidden');
+            $(oLink).parents('.cmt-lu-button:first').remove();
 
 
             $this.resumeLiveUpdates();
             $this.resumeLiveUpdates();
         });
         });
@@ -764,6 +754,21 @@ BxDolCmts.prototype.showLiveUpdate = function(oData)
     oButton.prependTo(this._sRootId);
     oButton.prependTo(this._sRootId);
 };
 };
 
 
+BxDolCmts.prototype.showLiveUpdateForSocket = function(sData)
+{
+    var oData = JSON.parse(sData);
+    if(this._iAuthorId == oData.author_id)
+        return;
+
+    var oElement = $(this._sRootId + ' .' + this._sSP + '-lu-button:hidden').clone();
+    if(!oElement || oElement.length == 0)
+        return;
+
+    var sOnClick = oElement.find('.bx-btn').attr('onclick');
+    oElement.find('.bx-btn').attr('onclick', sOnClick.replace('{cmt_id}', oData.id).replace('{cmt_anchor}', oData.anchor));
+    oElement.prependTo(this._sRootId).show();
+};
+
 BxDolCmts.prototype.showLiveUpdates = function(oData)
 BxDolCmts.prototype.showLiveUpdates = function(oData)
 {
 {
 	/*
 	/*

+ 1 - 1
template/comments_lu_button.html

@@ -1,4 +1,4 @@
-<div id="__html_id__" class="__style_prefix__-lu-button bx-def-padding-bottom __class__">
+<div class="__style_prefix__-lu-button bx-def-padding-bottom __class__">
     <a class="bx-btn" href="javascript:void(0)" onclick="__onclick_show__" title="<bx_text_attribute:_cmt_txt_link_show />">
     <a class="bx-btn" href="javascript:void(0)" onclick="__onclick_show__" title="<bx_text_attribute:_cmt_txt_link_show />">
         <i class="sys-icon sync-alt bx-def-padding-right"></i> <bx_text:_cmt_txt_new_content />
         <i class="sys-icon sync-alt bx-def-padding-right"></i> <bx_text:_cmt_txt_new_content />
     </a>
     </a>

+ 30 - 18
template/scripts/BxBaseCmts.php

@@ -113,11 +113,11 @@ class BxBaseCmts extends BxDolCmts
      *
      *
      * @return string
      * @return string
      */
      */
-    public function getJsScript($aBp = array(), $aDp = array())
+    public function getJsScript($aBp = [], $aDp = [])
     {
     {
         $bMinPostForm = isset($aDp['min_post_form']) ? $aDp['min_post_form'] : $this->_bMinPostForm;
         $bMinPostForm = isset($aDp['min_post_form']) ? $aDp['min_post_form'] : $this->_bMinPostForm;
 
 
-        $aParams = array(
+        $aParams = [
             'sObjName' => $this->_sJsObjName,
             'sObjName' => $this->_sJsObjName,
             'sRootUrl' => BX_DOL_URL_ROOT,
             'sRootUrl' => BX_DOL_URL_ROOT,
             'sSystem' => $this->getSystemName(),
             'sSystem' => $this->getSystemName(),
@@ -131,11 +131,22 @@ class BxBaseCmts extends BxDolCmts
             'iMinPostForm' => $bMinPostForm ? 1 : 0,
             'iMinPostForm' => $bMinPostForm ? 1 : 0,
             'sStylePrefix' => $this->_sStylePrefix,
             'sStylePrefix' => $this->_sStylePrefix,
             'aHtmlIds' => $this->_aHtmlIds,
             'aHtmlIds' => $this->_aHtmlIds,
-        );
+        ];
 
 
         $this->addCssJs();
         $this->addCssJs();
         return $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
         return $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
     }
     }
+    
+    public function getJsScriptSocket($aBp = [], $aDp = [])
+    {
+        $oSockets = BxDolSockets::getInstance();
+        if(!$oSockets->isEnable())
+            return '';
+
+        $sCode = $oSockets->getSubscribeJsCode($this->getSocketName(), $this->_iId, 'comment_added', $this->getJsObjectName() . '.showLiveUpdateForSocket(data)');
+
+        return $this->_oTemplate->_wrapInTagJsCode($sCode);
+    }
 
 
     function getCommentsBlockAPI($aParams, $aBp = [], $aDp = ['in_designbox' => false, 'show_empty' => false])
     function getCommentsBlockAPI($aParams, $aBp = [], $aDp = ['in_designbox' => false, 'show_empty' => false])
     {
     {
@@ -193,15 +204,11 @@ class BxBaseCmts extends BxDolCmts
             return $mixedResult;
             return $mixedResult;
 
 
         $this->_getParams($aBp, $aDp);
         $this->_getParams($aBp, $aDp);
-        
-        $oSockets= BxDolSockets::getInstance();
-        
-        $sJs = '';
-        
-        if ($oSockets->isEnable()){
-            $sJs .= $this->getLiveUpdateButton(-1, 'hidden') . $this->_oTemplate->_wrapInTagJsCode($oSockets->getSubscribeJsCode($this->_sSystem , $this->_iId, 'comment_added', $this->getJsObjectName().'.showNew(data)'));
-        }
-        else{
+
+        $oSockets = BxDolSockets::getInstance();
+        $bSockets = $oSockets->isEnable();
+
+        if(!$bSockets) {
             //add live update
             //add live update
             $this->actionResumeLiveUpdate();
             $this->actionResumeLiveUpdate();
 
 
@@ -216,9 +223,13 @@ class BxBaseCmts extends BxDolCmts
         $sCommentsPinned = $this->getCommentsPinned(array_merge($aBp, ['pinned' => 1]), $aDp);
         $sCommentsPinned = $this->getCommentsPinned(array_merge($aBp, ['pinned' => 1]), $aDp);
         $sContentBefore = $this->_getContentBefore($aBp, $aDp);
         $sContentBefore = $this->_getContentBefore($aBp, $aDp);
         $sContentAfter = $this->_getContentAfter($aBp, $aDp);
         $sContentAfter = $this->_getContentAfter($aBp, $aDp);
+        if($bSockets)
+            $sContentAfter .= $this->getLiveUpdateButton();
         $sPostFormTop = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP]));
         $sPostFormTop = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_TOP]));
         $sPostFormBottom = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM]));
         $sPostFormBottom = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM]));
         $sJsContent = $this->getJsScript($aBp, $aDp);
         $sJsContent = $this->getJsScript($aBp, $aDp);
+        if($bSockets)
+            $sJsContent .= $this->getJsScriptSocket($aBp, $aDp);
 
 
         $sBlockTitle = _t($this->_aT['block_comments_title'], $this->getCommentsCountAll(0, true));
         $sBlockTitle = _t($this->_aT['block_comments_title'], $this->getCommentsCountAll(0, true));
         $sBlockMenu = $this->_getControlsBox();
         $sBlockMenu = $this->_getControlsBox();
@@ -240,7 +251,7 @@ class BxBaseCmts extends BxDolCmts
             'block_menu' => &$sBlockMenu,
             'block_menu' => &$sBlockMenu,
         ]);
         ]);
 
 
-        $sContent = $sJs . $this->_oTemplate->parseHtmlByName('comments_block.html', [
+        $sContent = $this->_oTemplate->parseHtmlByName('comments_block.html', [
             'system' => $this->_sSystem,
             'system' => $this->_sSystem,
             'list_anchor' => $this->getListAnchor(),
             'list_anchor' => $this->getListAnchor(),
             'id' => $this->getId(),
             'id' => $this->getId(),
@@ -601,15 +612,16 @@ class BxBaseCmts extends BxDolCmts
 
 
         return $this->getLiveUpdateButton($aComment['cmt_id']);
         return $this->getLiveUpdateButton($aComment['cmt_id']);
     }
     }
-    
-    function getLiveUpdateButton($aCommentId, $sCssClass = '')
+
+    function getLiveUpdateButton($iCommentId = 0)
     {
     {
         $sJsObject = $this->getJsObjectName();
         $sJsObject = $this->getJsObjectName();
+
+        $bComment = is_numeric($iCommentId) && (int)$iCommentId != 0;
         return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
         return $this->_oTemplate->parseHtmlByName('comments_lu_button.html', array(
             'style_prefix' => $this->_sStylePrefix,
             'style_prefix' => $this->_sStylePrefix,
-            'class' => $sCssClass,
-            'html_id' => $this->getNotificationId(),
-            'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . $this->getItemAnchor($aCommentId) . "', '" . $aCommentId . "');",
+            'class' => !$bComment ? 'hidden' : '',
+            'onclick_show' => "javascript:" . $sJsObject . ".goToBtn(this, '" . ($bComment ? $this->getItemAnchor($iCommentId) : '{cmt_anchor}') . "', '" . ($bComment ? $iCommentId : '{cmt_id}') . "');",
         ));
         ));
     }
     }