replace get_group_suggestions

- adds **filters argument to get_suggestions()
- passes local=True arg to get_suggestions() for group member suggestions
- removes get_group_suggestions()

Thanks to Mouse for the suggestion to keep the codebase DRY!
This commit is contained in:
Hugh Rundle 2021-10-04 21:25:38 +11:00
parent 2b96b3365c
commit 782512b2ce
2 changed files with 9 additions and 27 deletions

View file

@ -81,7 +81,7 @@ class SuggestedUsers(RedisStore):
"""take a user out of someone's suggestions"""
self.bulk_remove_objects_from_store([suggested_user], self.store_id(user))
def get_suggestions(self, user):
def get_suggestions(self, user, **filters):
"""get suggestions"""
values = self.get_store(self.store_id(user), withscores=True)
results = []
@ -97,34 +97,15 @@ class SuggestedUsers(RedisStore):
logger.exception(err)
continue
user.mutuals = counts["mutuals"]
# user.shared_books = counts["shared_books"]
if filters.local:
if user.local:
results.append(user)
else:
results.append(user)
if len(results) >= 5:
break
return results
def get_group_suggestions(self, user):
"""get suggestions for new group members"""
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:
counts = self.get_counts_from_rank(rank)
try:
user = models.User.objects.get(
id=user_id, is_active=True, bookwyrm_user=True
)
except models.User.DoesNotExist as err:
# if this happens, the suggestions are janked way up
logger.exception(err)
continue
user.mutuals = counts["mutuals"]
# only suggest local users until Groups are ActivityPub compliant
if user.local:
results.append(user)
if len(results) >= 5:
break
return results
def get_annotated_users(viewer, *args, **kwargs):
"""Users, annotated with things they have in common"""

View file

@ -107,8 +107,9 @@ class FindUsers(View):
data = {"no_results": not user_results}
if user_results.count() < 5:
user_results = list(user_results) + suggested_users.get_group_suggestions(
request.user
user_results = list(user_results) + suggested_users.get_suggestions(
request.user,
local=True
)
group = get_object_or_404(models.Group, id=group_id)