Repository: bookwyrm-social/bookwyrm

Issue:
List Followers You Know When Looking at User's Follower List
**Is your feature request related to a problem? Please describe.**
When you look at a user's profile, the link to the user's followers will be titled 'X followers you follow', but the followers list is supplied in reverse-chronological order by date followed, with followers in common given no priority.
When trying to decide how to handle a follow request, seeing what people we both know is the most relevant information to me.

**Describe the solution you'd like**
Sort followers that I know (follow) ahead of all other followers when showing a users Followers list (honestly, sorting the Following list this way would be nice too)

**Describe alternatives you've considered**
Have a separate view for 'Followers you know' (and rename the link to the Followers list if it doesn't point to that list)

**Additional context**
Hi mouse



Discussion:
Yeah! I think the right approach is to add an annotation indicating the presence of a relationship to the queryset and then sort by the annotation + follow date in the view

Resolving patch:
diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py
--- a/bookwyrm/views/user.py
+++ b/bookwyrm/views/user.py
@@ -1,6 +1,7 @@
 """ non-interactive pages """
 from django.contrib.auth.decorators import login_required
 from django.core.paginator import Paginator
+from django.db.models import Q, Count
 from django.http import Http404
 from django.shortcuts import redirect
 from django.template.response import TemplateResponse
@@ -105,9 +106,8 @@ def get(self, request, username):
         if is_api_request(request):
             return ActivitypubResponse(user.to_followers_activity(**request.GET))
 
-        paginated = Paginator(
-            user.followers.order_by("-created_date").all(), PAGE_LENGTH
-        )
+        followers = annotate_if_follows(request.user, user.followers)
+        paginated = Paginator(followers.all(), PAGE_LENGTH)
         data = {
             "user": user,
             "is_self": request.user.id == user.id,
@@ -126,9 +126,8 @@ def get(self, request, username):
         if is_api_request(request):
             return ActivitypubResponse(user.to_following_activity(**request.GET))
 
-        paginated = Paginator(
-            user.following.order_by("-created_date").all(), PAGE_LENGTH
-        )
+        following = annotate_if_follows(request.user, user.following)
+        paginated = Paginator(following.all(), PAGE_LENGTH)
         data = {
             "user": user,
             "is_self": request.user.id == user.id,
@@ -137,6 +136,16 @@ def get(self, request, username):
         return TemplateResponse(request, "user/relationships/following.html", data)
 
 
+def annotate_if_follows(user, queryset):
+    """Sort a list of users by if you follow them"""
+    if not user.is_authenticated:
+        return queryset.order_by("-created_date")
+
+    return queryset.annotate(
+        request_user_follows=Count("followers", filter=Q(followers=user))
+    ).order_by("-request_user_follows", "-created_date")
+
+
 class Groups(View):
     """list of user's groups view"""
 


Test patch: