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; vertical-align: middle;
display: inline; display: inline;
} }
.navbar .avatar { .is-32x32 {
max-height: none; min-width: 32px;
min-height: 32px;
} }
.is-96x96 {
min-width: 96px;
min-height: 96px;
}
/* --- QUOTES --- */ /* --- QUOTES --- */

View file

@ -121,12 +121,21 @@
</div> </div>
<footer class="card-footer content"> <footer class="card-footer content">
{% if user != request.user %} {% if user != request.user %}
{% if user.mutuals %}
<div class="card-footer-item"> <div class="card-footer-item">
<div class="has-text-centered"> <div class="has-text-centered">
<p class="title is-6 mb-0">{{ user.mutuals }}</p> <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>
</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 %} {% endif %}
<div class="card-footer-item"> <div class="card-footer-item">
<div class="has-text-centered"> <div class="has-text-centered">

View file

@ -54,25 +54,28 @@
{# suggested users on the first page, two statuses down #} {# suggested users on the first page, two statuses down #}
<section class="block"> <section class="block">
<h2 class="title is-5">{% trans "Who to follow" %}</h2> <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 %} {% for user in suggested_users %}
<div class="column is-flex"> <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"> <a href="{{ user.local_path }}" class="has-text-black">
{% include 'snippets/avatar.html' with user=user large=True %} {% 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> <span title="{{ user.display_name }}" class="is-block is-6 has-text-weight-bold">{{ user.display_name|truncatechars:10 }}</span>
<span title="@{{ user|username }}" class="is-block pb-3">@{{ user|username|truncatechars:8 }}</span> <span title="@{{ user|username }}" class="is-block pb-3">@{{ user|username|truncatechars:8 }}</span>
</a> </a>
{% include 'snippets/follow_button.html' with user=user minimal=True %} {% include 'snippets/follow_button.html' with user=user minimal=True %}
{% if user.mutuals %} {% if user.mutuals %}
<p class="help"> <p class="help">
{% blocktrans with mutuals=user.mutuals|intcomma count counter=user.mutuals %}{{ mutuals }} follower you follow{% plural %}{{ mutuals }} followers you follow{% endblocktrans %} {% blocktrans with mutuals=user.mutuals|intcomma count counter=user.mutuals %}{{ mutuals }} follower you follow{% plural %}{{ mutuals }} followers you follow{% endblocktrans %}
</p> </p>
{% endif %} {% 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> </div>
{% endfor %}
</div> </div>
{% endfor %} <a class="help" href="{% url 'directory' %}">View directory <span class="icon icon-arrow-right"></a>
</div>
</section> </section>
{% endif %} {% endif %}
<div class="block"> <div class="block">

View file

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

View file

@ -11,7 +11,7 @@ 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 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 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) paginated = Paginator(activities, PAGE_LENGTH)
suggested_users = ( suggested_users = get_suggested_users(
models.User.objects.filter( request.user, ~Q(id=request.user.id), bookwyrm_user=True
~Q(id__in=request.user.following.all()), ).order_by("-mutuals", "-last_active_date").all()[:5]
~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]
)
data = { data = {
**feed_page_data(request.user), **feed_page_data(request.user),

View file

@ -2,7 +2,7 @@
import re import re
from requests import HTTPError from requests import HTTPError
from django.core.exceptions import FieldError 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 import activitypub, models
from bookwyrm.connectors import ConnectorException, get_data from bookwyrm.connectors import ConnectorException, get_data
@ -190,3 +190,29 @@ def get_discover_books():
.order_by("-review__published_date__max")[:6] .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()]
)
)
)