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

@ -163,9 +163,15 @@ html {
vertical-align: middle;
display: inline;
}
.navbar .avatar {
max-height: none;
.is-32x32 {
min-width: 32px;
min-height: 32px;
}
.is-96x96 {
min-width: 96px;
min-height: 96px;
}
/* --- QUOTES --- */

View file

@ -121,12 +121,21 @@
</div>
<footer class="card-footer content">
{% if user != request.user %}
{% if user.mutuals %}
<div class="card-footer-item">
<div class="has-text-centered">
<p class="title is-6 mb-0">{{ user.mutuals }}</p>
<p class="help">{% trans "followers you follow" %}</p>
<p class="help">{% blocktrans count counter=user.mutuals %}follower you follow{% plural %}followers you follow{% endblocktrans %}</p>
</div>
</div>
{% elif user.shared_books %}
<div class="card-footer-item">
<div class="has-text-centered">
<p class="title is-6 mb-0">{{ user.shared_books }}</p>
<p class="help">{% blocktrans count counter=user.shared_books %}book on your shelves{% plural %}books on your shelves{% endblocktrans %}</p>
</div>
</div>
{% endif %}
{% endif %}
<div class="card-footer-item">
<div class="has-text-centered">

View file

@ -54,10 +54,10 @@
{# suggested users on the first page, two statuses down #}
<section class="block">
<h2 class="title is-5">{% trans "Who to follow" %}</h2>
<div class="columns is-mobile is-gapless scroll-x">
<div class="columns is-mobile scroll-x mb-0">
{% for user in suggested_users %}
<div class="column is-flex">
<div class="box has-text-centered is-shadowless has-background-white-bis">
<div class="box has-text-centered is-shadowless has-background-white-bis m-0">
<a href="{{ user.local_path }}" class="has-text-black">
{% include 'snippets/avatar.html' with user=user large=True %}
<span title="{{ user.display_name }}" class="is-block is-6 has-text-weight-bold">{{ user.display_name|truncatechars:10 }}</span>
@ -68,11 +68,14 @@
<p class="help">
{% blocktrans with mutuals=user.mutuals|intcomma count counter=user.mutuals %}{{ mutuals }} follower you follow{% plural %}{{ mutuals }} followers you follow{% endblocktrans %}
</p>
{% elif user.shared_books %}
<p class="help">{% blocktrans with shared_books=user.shared_books|intcomma count counter=user.shared_books %}{{ shared_books }} book on your shelves{% plural %}{{ shared_books }} books on your shelves{% endblocktrans %}</p>
{% endif %}
</div>
</div>
{% endfor %}
</div>
<a class="help" href="{% url 'directory' %}">View directory <span class="icon icon-arrow-right"></a>
</section>
{% endif %}
<div class="block">

View file

@ -1,13 +1,12 @@
""" who all's here? """
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.db.models import Count, Q
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.views import View
from django.utils.decorators import method_decorator
from bookwyrm import models
from .helpers import get_suggested_users
# pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch")
@ -30,21 +29,12 @@ class Directory(View):
if scope == "local":
filters["local"] = True
users = models.User.objects.filter(
discoverable=True, is_active=True, **filters
).annotate(
mutuals=Count(
"following",
filter=Q(
~Q(id=request.user.id), following__in=request.user.following.all()
),
)
)
users = get_suggested_users(request.user, **filters)
sort = request.GET.get("sort")
if sort == "recent":
users = users.order_by("-last_active_date")
else:
users = users.order_by("-mutuals", "-last_active_date")
users = users.order_by("-mutuals", "-shared_books", "-last_active_date")
paginated = Paginator(users, 12)

View file

@ -11,7 +11,7 @@ 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
from .helpers import get_user_from_username, privacy_filter, get_suggested_users
from .helpers import is_api_request, is_bookwyrm_request, object_visible_to_user
@ -34,23 +34,9 @@ class Feed(View):
paginated = Paginator(activities, PAGE_LENGTH)
suggested_users = (
models.User.objects.filter(
~Q(id__in=request.user.following.all()),
~Q(id=request.user.id),
discoverable=True,
is_active=True,
bookwyrm_user=True,
)
.annotate(
mutuals=Count(
"following",
filter=Q(following__in=request.user.following.all()),
)
)
.order_by("-mutuals", "-last_active_date")
.all()[:5]
)
suggested_users = get_suggested_users(
request.user, ~Q(id=request.user.id), bookwyrm_user=True
).order_by("-mutuals", "-last_active_date").all()[:5]
data = {
**feed_page_data(request.user),

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()]
)
)
)