From 03e5da12ddea4debf364b8ee734af7e8284a49e8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 6 Apr 2021 08:31:18 -0700 Subject: [PATCH] Call suggestions redis in feed --- bookwyrm/redis_store.py | 4 ++-- bookwyrm/suggested_users.py | 15 ++++++++++++++- bookwyrm/views/feed.py | 7 ++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bookwyrm/redis_store.py b/bookwyrm/redis_store.py index 4236d6df2..5a9bb2f69 100644 --- a/bookwyrm/redis_store.py +++ b/bookwyrm/redis_store.py @@ -56,9 +56,9 @@ class RedisStore(ABC): pipeline.zrem(store, -1, obj.id) pipeline.execute() - def get_store(self, store): # pylint: disable=no-self-use + def get_store(self, store, **kwargs): # pylint: disable=no-self-use """ load the values in a store """ - return r.zrevrange(store, 0, -1) + return r.zrevrange(store, 0, -1, **kwargs) def populate_store(self, store): """ go from zero to a store """ diff --git a/bookwyrm/suggested_users.py b/bookwyrm/suggested_users.py index 38dc37b0e..c6e6af85a 100644 --- a/bookwyrm/suggested_users.py +++ b/bookwyrm/suggested_users.py @@ -11,7 +11,7 @@ from bookwyrm.views.helpers import get_annotated_users class SuggestedUsers(RedisStore): """ suggested users for a user """ - max_length = 30 + max_length = 10 def get_rank(self, obj): """ get computed rank """ @@ -58,6 +58,19 @@ class SuggestedUsers(RedisStore): """ update the ranks of the follows suggested to a user """ self.populate_store(self.store_id(user)) + def get_suggestions(self, user): + """ get suggestions """ + values = self.get_store(self.store_id(user), withscores=True) + results = [] + # annotate users with mutuals and shared book counts + for user_id, rank in values[:5]: + counts = self.get_counts_from_rank(rank) + user = models.User.objects.get(id=user_id) + user.mutuals = counts["mutuals"] + user.shared_books = counts["shared_books"] + results.append(user) + return results + suggested_users = SuggestedUsers() diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index cda115867..71e486f2b 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -11,7 +11,8 @@ from django.views import View from bookwyrm import activitystreams, forms, models from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.settings import PAGE_LENGTH, STREAMS -from .helpers import get_user_from_username, privacy_filter, get_suggested_users +from bookwyrm.suggested_users import suggested_users +from .helpers import get_user_from_username, privacy_filter from .helpers import is_api_request, is_bookwyrm_request, object_visible_to_user @@ -33,14 +34,14 @@ class Feed(View): activities = activitystreams.streams[tab].get_activity_stream(request.user) paginated = Paginator(activities, PAGE_LENGTH) - suggested_users = get_suggested_users(request.user) + suggestions = suggested_users.get_suggestions(request.user) data = { **feed_page_data(request.user), **{ "user": request.user, "activities": paginated.page(page), - "suggested_users": suggested_users, + "suggested_users": suggestions, "tab": tab, "goal_form": forms.GoalForm(), "path": "/%s" % tab,