Skip to content

Commit

Permalink
Add pagination to user following/follower lists (#9323)
Browse files Browse the repository at this point in the history
* paginate following page

---------

Co-authored-by: Stef Kischak <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 29, 2024
1 parent 0972686 commit c8203dd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
15 changes: 10 additions & 5 deletions openlibrary/core/follows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import web
from typing import cast, Any
from typing import cast
from openlibrary.core.bookshelves import Bookshelves

from . import db
Expand Down Expand Up @@ -38,17 +37,21 @@ def is_subscribed(cls, subscriber, publisher):
return len(subscription)

@classmethod
def get_followers(cls, publisher):
def get_followers(cls, publisher, limit=None, offset=0):
"""Get publishers subscribers"""
oldb = db.get_db()
where = 'publisher=$publisher'
subscribers = oldb.select(
cls.TABLENAME, where=where, vars={'publisher': publisher}
cls.TABLENAME,
where=where,
vars={'publisher': publisher},
limit=limit,
offset=offset,
)
return subscribers

@classmethod
def get_following(cls, subscriber, exclude_disabled=False):
def get_following(cls, subscriber, limit=None, offset=0, exclude_disabled=False):
"""Get subscriber's subscriptions"""
oldb = db.get_db()
where = 'subscriber=$subscriber'
Expand All @@ -58,6 +61,8 @@ def get_following(cls, subscriber, exclude_disabled=False):
cls.TABLENAME,
where=where,
vars={'subscriber': subscriber},
limit=limit,
offset=offset,
)
return [dict(s) for s in subscriptions]

Expand Down
23 changes: 19 additions & 4 deletions openlibrary/plugins/upstream/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,14 +1134,29 @@ class my_follows(delegate.page):
path = r"/people/([^/]+)/(followers|following)"

def GET(self, username, key=""):
mb = MyBooksTemplate(username, 'following')
i = web.input(page=1)
page_size = 25

# Force invalid page query params to default to the first page
page = 1 if not i.page.isdigit() else max(1, int(i.page))
offset = (page - 1) * page_size

follows = (
PubSub.get_followers(username)
PubSub.get_followers(username, page_size, offset)
if key == 'followers'
else PubSub.get_following(username)
else PubSub.get_following(username, page_size, offset)
)
follow_count = (
PubSub.count_followers(username)
if key == 'followers'
else PubSub.count_following(username)
)

mb = MyBooksTemplate(username, 'following')
manage = key == 'following' and mb.is_my_page
template = render['account/follows'](mb.user, follows, manage=manage)
template = render['account/follows'](
mb.user, follow_count, page, page_size, follows, manage=manage
)
return mb.render(header_title=_(key.capitalize()), template=template)


Expand Down
13 changes: 8 additions & 5 deletions openlibrary/templates/account/follows.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
$def with (user, follows=None, manage=False)
$def with (user, num_found, page, results_per_page, follows=None, manage=False)

$if follows:
$ username = user.key.split('/')[-1]
$for follow in follows:
<ul>
<li class="follow-row">
<div class="follow-row--name">
<div class="follow-row--name">
$if follow['publisher'] == username:
<a href="/people/$follow['subscriber']/books" class="username-avatar"><img src="/people/$follow['subscriber']/avatar" class="account-avatar">$follow['subscriber']</a>
$else:
<a href="/people/$follow['publisher']/books" class="username-avatar"><img src="/people/$follow['publisher']/avatar" class="account-avatar">$follow['publisher']</a>
</div>
<div>
</div>
<div>
$if manage:
$:macros.Follow(follow['publisher'], following=1)
</div>
</div>
</li>
</ul>
<div class="pager">
$:macros.Pager(page, num_found, results_per_page)
</div>
$else:
<p>$_("There is nothing to see here yet.")</p>

0 comments on commit c8203dd

Please sign in to comment.