10 votes

Weird bug with the pagination of notifications

Okay, this is a bit confusing to explain, but when I go to the notifications page:

https://tildes.net/notifications

The last notification I received, at time of writing, was 5 hours, 50 minutes ago.

That's the notification at the top of the list on that page. It's the last notification I have received, as of writing this post. We will call that notification P1A (Page one: P1, A: first item).

That's correct, and how it should be.

At the bottom of that page is a reply that I received on June 19. That is also correct. These items are in the correct order. This last item will be P1B.

So then I navigate to the second page of my notifications.

On the second page, the top item in the list is another notification from June 19. That makes sense. The last item in the list is from May 19. That also makes sense. These are P2A and P2B.

On to the third page.

On the third page of my notifications, the top item is from May 19 as well. That makes sense. The last item is from May 9. Once again, that makes sense. These are P3A and P3B.

But now... let's go back a page.

Going back to the second page in my notifications, the comment at the top of the page is from June 19, as it was before.

Except, that specific comment from June 19 is the last comment from the first page. P1B, and it's now at the top of the page, not the bottom (like it was on page one).

And indeed, after going back to the second page of notifications from the third page, the last item of the second page is my most recently received notification, which is the first item from the first page. P1A.

That... does not seem correct.

Here is a nice table that hopefully helps to illustrate what I'm describing:

Page First Item Last Item
P1 P1A P1B
P2 P2A P2B
P3 P3A P3B
P2 P1B P1A

2 comments

  1. Deimos
    Link
    Thanks for the detailed explanation, I suspect I know where the problem's coming from, since it seems to be caused by going backwards through pages. This is kind of a confusing subject, but to...

    Thanks for the detailed explanation, I suspect I know where the problem's coming from, since it seems to be caused by going backwards through pages. This is kind of a confusing subject, but to reciprocate for writing it up in detail, let me try to explain what's probably causing it:

    Tildes uses a kind of "item-based" pagination, instead of "page-based". That is, you don't say something like "show me page 3", you say "show me the next page of items that comes after item X", where item X is the last one on the previous page.

    The reason to do it that way is because there are so many things that can add/remove items to a list or change the order of them that you can't really "trust" the page numbers to stay the same. For example, if there are 10 notifications per page, you would expect "show me page 3" to show you notifications #21-30. But if you cleared two of the notifications on page 2 before clicking Next, everything after them would have "moved up" by two positions, and you'd end up not seeing two notifications that were previously #21 and #22 but are now #19 and #20 and shouldn't be on page 3 any more.

    So to avoid that, you know that notification X was the last one on the page, and say "show me the next items after X". Their relative position hasn't changed compared to that item, so it works as you'd expect.

    When you're paging forward, it's simple. For something like notifications that are sorted by time, you just do "get the next 10 items, ordered by age, that are older than item X", and display them in the same order you fetch them - the first item on the page will be the "youngest" (closest to X's age) and the last item will be the oldest.

    But when you're paging backwards, it needs to be done a little differently. Since you're looking for the next set of items younger than X, they end up being fetched in the opposite order of how you want to display them (the first item fetched is the oldest, but it should be displayed last). So when you're paging forwards, you can display items in the same order they're fetched, but when you're paging backwards you need to display items in the opposite order.

    It sounds like that order-reversing went wrong in some way, and then of course everything else goes to hell if the first/last items are wrong, since everything's completely dependent on those. That it was "P1B" at the top of page 2 and not "P2B" is weird too, but it's probably all related.

    I'll take a look, thanks again for all the detail.

    8 votes
  2. Bauke
    (edited )
    Link
    It looks like when you go to the previous page it reverses the order it's supposed to be in. If I go from the first page to the second, and then back, it will show all the same notifications...

    It looks like when you go to the previous page it reverses the order it's supposed to be in.

    If I go from the first page to the second, and then back, it will show all the same notifications except they'll be reversed. Oldest at the top, newest at the bottom.

    Looked at the code for this and didn't see anything immediately wrong, it's supposed to be sorted descending by when the notification was created (so most recent first). Weird. @Deimos

    5 votes