Adds shared books as a metric for recommending follows

This commit is contained in:
Mouse Reeve
2021-03-27 07:36:14 -07:00
parent abc732cdfe
commit 0ef33d2acb
6 changed files with 72 additions and 52 deletions

View File

@ -2,7 +2,7 @@
import re
from requests import HTTPError
from django.core.exceptions import FieldError
from django.db.models import Max, Q
from django.db.models import Count, Max, Q
from bookwyrm import activitypub, models
from bookwyrm.connectors import ConnectorException, get_data
@ -190,3 +190,29 @@ def get_discover_books():
.order_by("-review__published_date__max")[:6]
)
)
def get_suggested_users(user, *args, **kwargs):
""" Users, annotated with things they have in common """
return models.User.objects.filter(
discoverable=True, is_active=True, *args, **kwargs
).exclude(
Q(id__in=user.blocks.all()) | Q(blocks=user)
).annotate(
mutuals=Count(
"following",
filter=Q(
~Q(id=user.id),
~Q(id__in=user.following.all()),
following__in=user.following.all()
),
),
shared_books=Count(
"shelfbook",
filter=Q(
~Q(id=user.id),
shelfbook__book__parent_work__in=[
s.book.parent_work for s in user.shelfbook_set.all()]
)
)
)