Call suggestions redis in feed

This commit is contained in:
Mouse Reeve 2021-04-06 08:31:18 -07:00
parent 18ba33e050
commit 03e5da12dd
3 changed files with 20 additions and 6 deletions

View file

@ -56,9 +56,9 @@ class RedisStore(ABC):
pipeline.zrem(store, -1, obj.id) pipeline.zrem(store, -1, obj.id)
pipeline.execute() 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 """ """ load the values in a store """
return r.zrevrange(store, 0, -1) return r.zrevrange(store, 0, -1, **kwargs)
def populate_store(self, store): def populate_store(self, store):
""" go from zero to a store """ """ go from zero to a store """

View file

@ -11,7 +11,7 @@ from bookwyrm.views.helpers import get_annotated_users
class SuggestedUsers(RedisStore): class SuggestedUsers(RedisStore):
""" suggested users for a user """ """ suggested users for a user """
max_length = 30 max_length = 10
def get_rank(self, obj): def get_rank(self, obj):
""" get computed rank """ """ get computed rank """
@ -58,6 +58,19 @@ class SuggestedUsers(RedisStore):
""" update the ranks of the follows suggested to a user """ """ update the ranks of the follows suggested to a user """
self.populate_store(self.store_id(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() suggested_users = SuggestedUsers()

View file

@ -11,7 +11,8 @@ from django.views import View
from bookwyrm import activitystreams, forms, models from bookwyrm import activitystreams, forms, models
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH, STREAMS 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 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) activities = activitystreams.streams[tab].get_activity_stream(request.user)
paginated = Paginator(activities, PAGE_LENGTH) paginated = Paginator(activities, PAGE_LENGTH)
suggested_users = get_suggested_users(request.user) suggestions = suggested_users.get_suggestions(request.user)
data = { data = {
**feed_page_data(request.user), **feed_page_data(request.user),
**{ **{
"user": request.user, "user": request.user,
"activities": paginated.page(page), "activities": paginated.page(page),
"suggested_users": suggested_users, "suggested_users": suggestions,
"tab": tab, "tab": tab,
"goal_form": forms.GoalForm(), "goal_form": forms.GoalForm(),
"path": "/%s" % tab, "path": "/%s" % tab,