Quellcode durchsuchen

Ticket #4452 - Timeline: Send 'mark_as_read' for all read items in the same request.

AntonLV vor 2 Jahren
Ursprung
Commit
fbe72935b6

+ 18 - 9
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -299,18 +299,27 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
 
     function actionMarkAsRead()
     {
-        $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
-        $aEvent = $this->_oDb->getEvents(['browse' => 'id', 'value' => $iId]);
-        if(empty($aEvent) || !is_array($aEvent))
-            return echoJson(['code' => 1]);
-
         $aParams = $this->_prepareParamsGet();
-        if(!$this->_oDb->markAsRead($aParams['viewer_id'], $iId))
-            return echoJson(['code' => 2]);
 
-        echoJson([
+        $mixedId = bx_process_input(bx_get('id'), BX_DATA_INT);
+        if(!is_array($mixedId))
+            $mixedId = [$mixedId];
+
+        $iProcessed = 0;
+        foreach($mixedId as $iId) {
+            $aEvent = $this->_oDb->getEvents(['browse' => 'id', 'value' => $iId]);
+            if(empty($aEvent) || !is_array($aEvent))
+                continue;
+
+            if(!$this->_oDb->markAsRead($aParams['viewer_id'], $iId))
+                continue;
+
+            $iProcessed++;
+        }
+
+        return echoJson($iProcessed != count($mixedId) ? ['code' => 1] : [
             'code' => 0, 
-            'id' => $iId
+            'id' => $mixedId
         ]);
     }
 

+ 58 - 1
modules/boonex/timeline/js/view.js

@@ -25,6 +25,7 @@ function BxTimelineView(oOptions) {
     this._sVideosAutoplay = oOptions.sVideosAutoplay == undefined ? 'off' : oOptions.sVideosAutoplay;
     this._bEventsToLoad = oOptions.bEventsToLoad == undefined ? false : oOptions.bEventsToLoad;
     this._bAutoMarkAsViewed = oOptions.bAutoMarkAsViewed == undefined ? false : oOptions.bAutoMarkAsViewed;
+    this._aMarkAsViewed = oOptions.aMarkAsViewed == undefined ? [] : oOptions.aMarkAsViewed;
     this._aMarkedAsViewed = oOptions.aMarkedAsViewed == undefined ? [] : oOptions.aMarkedAsViewed;
     this._iLimitAttachLinks = oOptions.iLimitAttachLinks == undefined ? 0 : oOptions.iLimitAttachLinks;
     this._sLimitAttachLinksErr = oOptions.sLimitAttachLinksErr == undefined ? '' : oOptions.sLimitAttachLinksErr;
@@ -127,6 +128,10 @@ BxTimelineView.prototype.init = function(bForceInit)
                         $this.markPostAsViewed($this.oView);
                     });
             });
+
+            window.addEventListener('beforeunload', (event) => {
+                $this.sendMarkPostAsViewed();
+            });
         }
     }
 
@@ -809,7 +814,11 @@ BxTimelineView.prototype.promotePost = function(oLink, iId, iWay)
     );
 };
 
-BxTimelineView.prototype.markPostAsViewed = function(oView)
+/**
+ * Sends request to a server everytime an Item was viewed.
+ * Note. Isn't used for now.
+ */
+BxTimelineView.prototype.markPostAsViewedSingle = function(oView)
 {
     var $this = this;
 
@@ -850,6 +859,54 @@ BxTimelineView.prototype.markPostAsViewed = function(oView)
     });
 };
 
+/**
+ * Collects all viewed items in an array and sends only one request to a server on 'beforeunload'.
+ */
+BxTimelineView.prototype.markPostAsViewed = function(oView)
+{
+    var $this = this;
+
+    var oItems = oView.find('.' + this.sClassItem);
+    var sPrefix = this._getHtmlId('item', this._oRequestParams, {whole: false}).replace('#', '');
+
+    oItems.each(function() {
+        var oItem = $(this);
+        var iId = parseInt(oItem.attr('id').replace(sPrefix, ''));
+        if($this._aMarkAsViewed.includes(iId) || $this._aMarkedAsViewed.includes(iId))
+            return;
+
+        var iItemTop = oItem.offset().top;
+        var iItemBottom = iItemTop + oItem.height();
+        var iWindowTop = $(window).scrollTop();
+        var iWindowHeight = $(window).height();
+        if(iItemBottom < iWindowTop + iWindowHeight)
+            $this._aMarkAsViewed.push(iId);        
+    });
+};
+
+BxTimelineView.prototype.sendMarkPostAsViewed = function()
+{
+    var $this = this;
+    var oData = $this._getDefaultData();
+    oData['id'] = this._aMarkAsViewed;
+
+    $.post({
+        url: this._sActionsUrl + 'mark_as_read/',
+        data: oData,
+        dataType: 'json',
+        success: function(oData) {
+            if(!oData || !oData.id) 
+                return;
+
+            if(Array.isArray(oData.id)) 
+                $this._aMarkedAsViewed = [...$this._aMarkedAsViewed, ...oData.id];
+            else
+                $this._aMarkedAsViewed.push(oData.id);
+        },
+        async: false
+    });
+};
+
 BxTimelineView.prototype.muteAuthor = function(oLink, iId)
 {
     var $this = this;