Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB: Fix offset quotes #1201

Merged
merged 1 commit into from
Feb 27, 2021
Merged

DB: Fix offset quotes #1201

merged 1 commit into from
Feb 27, 2021

Conversation

SMillerDev
Copy link
Contributor

Fixes GH-1200

@codecov-io
Copy link

codecov-io commented Feb 23, 2021

Codecov Report

Merging #1201 (a3b1ed3) into master (43deed2) will increase coverage by 0.07%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1201      +/-   ##
============================================
+ Coverage     87.07%   87.15%   +0.07%     
- Complexity      691      693       +2     
============================================
  Files            60       60              
  Lines          2492     2484       -8     
============================================
- Hits           2170     2165       -5     
+ Misses          322      319       -3     
Impacted Files Coverage Δ Complexity Δ
lib/Db/ItemMapperV2.php 100.00% <100.00%> (ø) 53.00 <2.00> (+2.00)
lib/Fetcher/FeedFetcher.php 88.23% <0.00%> (-0.18%) 36.00% <0.00%> (ø%)
lib/Service/FeedServiceV2.php 100.00% <0.00%> (ø) 28.00% <0.00%> (ø%)
lib/Command/Debug/ItemList.php 0.00% <0.00%> (ø) 6.00% <0.00%> (ø%)
lib/Command/Debug/FeedItemList.php 0.00% <0.00%> (ø) 6.00% <0.00%> (ø%)
lib/Command/Debug/FolderItemList.php 0.00% <0.00%> (ø) 8.00% <0.00%> (ø%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 43deed2...a3b1ed3. Read the comment docs.

@Grotax
Copy link
Member

Grotax commented Feb 24, 2021

Looks good 👍

@anoymouserver
Copy link
Contributor

It fetches the exact same articles again, resulting in not showing any new ones

// inital: /apps/news/items?limit=40&oldestFirst=false&search=&showAll=true&type=6
{"newestItemId": 129155, "feeds": [ /* feed data */ ], "starred": 543, "items": [ /* 40 items */ ]}
// after scrolling: /apps/news/items?limit=40&offset=129115&oldestFirst=false&showAll=true&type=6
{"items": [ /* 39 items */ ]}

I had to make a few changes to make it work:

diff --git a/lib/Db/ItemMapperV2.php b/lib/Db/ItemMapperV2.php
index bfa1d111c..24c70ea89 100644
--- a/lib/Db/ItemMapperV2.php
+++ b/lib/Db/ItemMapperV2.php
@@ -412,10 +412,10 @@ class ItemMapperV2 extends NewsMapperV2
     private function offsetWhere(bool $oldestFirst): string
     {
         if ($oldestFirst === true) {
-            return 'items.id < :offset';
+            return 'items.id > :offset';
         }
 
-        return 'items.id > :offset';
+        return 'items.id < :offset';
     }
 
     /**
@@ -445,14 +445,17 @@ class ItemMapperV2 extends NewsMapperV2
             ->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
             ->andWhere('feeds.user_id = :userId')
             ->andWhere('items.feed_id = :feedId')
-            ->andWhere($this->offsetWhere($oldestFirst))
             ->setParameter('userId', $userId)
             ->setParameter('feedId', $feedId)
-            ->setParameter('offset', $offset)
             ->setMaxResults($limit)
             ->orderBy('items.last_modified', ($oldestFirst ? 'ASC' : 'DESC'))
             ->addOrderBy('items.id', ($oldestFirst ? 'ASC' : 'DESC'));
 
+        if ($offset !== 0) {
+            $builder->andWhere($this->offsetWhere($oldestFirst))
+                    ->setParameter('offset', $offset);
+        }
+
         if ($search !== []) {
             foreach ($search as $key => $term) {
                 $term = $this->db->escapeLikeParameter($term);
@@ -501,13 +504,16 @@ class ItemMapperV2 extends NewsMapperV2
             ->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
             ->andWhere('feeds.user_id = :userId')
             ->andWhere($folderWhere)
-            ->andWhere($this->offsetWhere($oldestFirst))
             ->setParameter('userId', $userId)
-            ->setParameter('offset', $offset)
             ->setMaxResults($limit)
             ->orderBy('items.last_modified', ($oldestFirst ? 'ASC' : 'DESC'))
             ->addOrderBy('items.id', ($oldestFirst ? 'ASC' : 'DESC'));
 
+        if ($offset !== 0) {
+            $builder->andWhere($this->offsetWhere($oldestFirst))
+                    ->setParameter('offset', $offset);
+        }
+
         if ($search !== []) {
             foreach ($search as $key => $term) {
                 $term = $this->db->escapeLikeParameter($term);
@@ -548,13 +554,16 @@ class ItemMapperV2 extends NewsMapperV2
             ->from($this->tableName, 'items')
             ->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id')
             ->andWhere('feeds.user_id = :userId')
-            ->andWhere($this->offsetWhere($oldestFirst))
             ->setParameter('userId', $userId)
-            ->setParameter('offset', $offset)
             ->setMaxResults($limit)
             ->orderBy('items.last_modified', ($oldestFirst ? 'ASC' : 'DESC'))
             ->addOrderBy('items.id', ($oldestFirst ? 'ASC' : 'DESC'));
 
+        if ($offset !== 0) {
+            $builder->andWhere($this->offsetWhere($oldestFirst))
+                    ->setParameter('offset', $offset);
+        }
+
         if ($search !== []) {
             foreach ($search as $key => $term) {
                 $term = $this->db->escapeLikeParameter($term);
I have adopted most of them from the old ItemMapper, after seaching for the differences..

The operator has to be other way around

news/lib/Db/ItemMapper.php

Lines 202 to 209 in ceba810

private function getOperator(bool $oldestFirst): string
{
if ($oldestFirst) {
return '>';
} else {
return '<';
}
}

The offset is apparently 0 for the initial request, so no offset filter should be applied

news/lib/Db/ItemMapper.php

Lines 333 to 337 in ceba810

if ($offset !== 0) {
$sql .= 'AND `items`.`id` ' .
$this->getOperator($oldestFirst) . ' ? ';
$params[] = $offset;
}

@Grotax Grotax added the patch label Feb 26, 2021
Issue GH-1200

Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
@SMillerDev
Copy link
Contributor Author

Applied the patch, thanks @anoymouserver

@SMillerDev SMillerDev requested a review from Grotax February 27, 2021 12:20
@SMillerDev
Copy link
Contributor Author

Really github? Request review means "dismiss existing reviews"?

Copy link
Member

@Grotax Grotax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work guys :)

@Grotax Grotax merged commit 9d5d35c into master Feb 27, 2021
@delete-merged-branch delete-merged-branch bot deleted the fix/db/item_offset branch February 27, 2021 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DBAL Exception: Invalid parameter number: mixed named and positional parameters
4 participants