prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN 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
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `bookwyrm/views/user.py`
  Content:
  ```
  1 """ non-interactive pages """
  2 from django.contrib.auth.decorators import login_required
  3 from django.core.paginator import Paginator
  4 from django.http import Http404
  5 from django.shortcuts import redirect
  6 from django.template.response import TemplateResponse
  7 from django.utils import timezone
  8 from django.views import View
  9 from django.views.decorators.http import require_POST
  10 
  11 from bookwyrm import models
  12 from bookwyrm.activitypub import ActivitypubResponse
  13 from bookwyrm.settings import PAGE_LENGTH
  14 from .helpers import get_user_from_username, is_api_request
  15 
  16 
  17 # pylint: disable=no-self-use
  18 class User(View):
  19     """user profile page"""
  20 
  21     def get(self, request, username):
  22         """profile page for a user"""
  23         user = get_user_from_username(request.user, username)
  24 
  25         if is_api_request(request):
  26             # we have a json request
  27             return ActivitypubResponse(user.to_activity())
  28         # otherwise we're at a UI view
  29 
  30         shelf_preview = []
  31 
  32         # only show other shelves that should be visible
  33         shelves = user.shelf_set
  34         is_self = request.user.id == user.id
  35         if not is_self:
  36             follower = user.followers.filter(id=request.user.id).exists()
  37             if follower:
  38                 shelves = shelves.filter(privacy__in=["public", "followers"])
  39             else:
  40                 shelves = shelves.filter(privacy="public")
  41 
  42         for user_shelf in shelves.all():
  43             if not user_shelf.books.count():
  44                 continue
  45             shelf_preview.append(
  46                 {
  47                     "name": user_shelf.name,
  48                     "local_path": user_shelf.local_path,
  49                     "books": user_shelf.books.all()[:3],
  50                     "size": user_shelf.books.count(),
  51                 }
  52             )
  53             if len(shelf_preview) > 2:
  54                 break
  55 
  56         # user's posts
  57         activities = (
  58             models.Status.privacy_filter(
  59                 request.user,
  60             )
  61             .filter(user=user)
  62             .select_related(
  63                 "user",
  64                 "reply_parent",
  65                 "review__book",
  66                 "comment__book",
  67                 "quotation__book",
  68             )
  69             .prefetch_related(
  70                 "mention_books",
  71                 "mention_users",
  72                 "attachments",
  73             )
  74         )
  75 
  76         paginated = Paginator(activities, PAGE_LENGTH)
  77         goal = models.AnnualGoal.objects.filter(
  78             user=user, year=timezone.now().year
  79         ).first()
  80         if goal:
  81             try:
  82                 goal.raise_visible_to_user(request.user)
  83             except Http404:
  84                 goal = None
  85 
  86         data = {
  87             "user": user,
  88             "is_self": is_self,
  89             "shelves": shelf_preview,
  90             "shelf_count": shelves.count(),
  91             "activities": paginated.get_page(request.GET.get("page", 1)),
  92             "goal": goal,
  93         }
  94 
  95         return TemplateResponse(request, "user/user.html", data)
  96 
  97 
  98 class Followers(View):
  99     """list of followers view"""
  100 
  101     def get(self, request, username):
  102         """list of followers"""
  103         user = get_user_from_username(request.user, username)
  104 
  105         if is_api_request(request):
  106             return ActivitypubResponse(user.to_followers_activity(**request.GET))
  107 
  108         paginated = Paginator(
  109             user.followers.order_by("-created_date").all(), PAGE_LENGTH
  110         )
  111         data = {
  112             "user": user,
  113             "is_self": request.user.id == user.id,
  114             "follow_list": paginated.get_page(request.GET.get("page")),
  115         }
  116         return TemplateResponse(request, "user/relationships/followers.html", data)
  117 
  118 
  119 class Following(View):
  120     """list of following view"""
  121 
  122     def get(self, request, username):
  123         """list of followers"""
  124         user = get_user_from_username(request.user, username)
  125 
  126         if is_api_request(request):
  127             return ActivitypubResponse(user.to_following_activity(**request.GET))
  128 
  129         paginated = Paginator(
  130             user.following.order_by("-created_date").all(), PAGE_LENGTH
  131         )
  132         data = {
  133             "user": user,
  134             "is_self": request.user.id == user.id,
  135             "follow_list": paginated.get_page(request.GET.get("page")),
  136         }
  137         return TemplateResponse(request, "user/relationships/following.html", data)
  138 
  139 
  140 class Groups(View):
  141     """list of user's groups view"""
  142 
  143     def get(self, request, username):
  144         """list of groups"""
  145         user = get_user_from_username(request.user, username)
  146 
  147         paginated = Paginator(
  148             models.Group.memberships.filter(user=user).order_by("-created_date"),
  149             PAGE_LENGTH,
  150         )
  151         data = {
  152             "user": user,
  153             "is_self": request.user.id == user.id,
  154             "group_list": paginated.get_page(request.GET.get("page")),
  155         }
  156         return TemplateResponse(request, "user/groups.html", data)
  157 
  158 
  159 @require_POST
  160 @login_required
  161 def hide_suggestions(request):
  162     """not everyone wants user suggestions"""
  163     request.user.show_suggested_users = False
  164     request.user.save(broadcast=False, update_fields=["show_suggested_users"])
  165     return redirect(request.headers.get("Referer", "/"))
  166
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: 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 @@
           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 @@
           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 @@
           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"""
  
verification_info: {"golden_diff": "diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py\n--- a/bookwyrm/views/user.py\n+++ b/bookwyrm/views/user.py\n@@ -1,6 +1,7 @@\n \"\"\" non-interactive pages \"\"\"\n from django.contrib.auth.decorators import login_required\n from django.core.paginator import Paginator\n+from django.db.models import Q, Count\n from django.http import Http404\n from django.shortcuts import redirect\n from django.template.response import TemplateResponse\n@@ -105,9 +106,8 @@\n         if is_api_request(request):\n             return ActivitypubResponse(user.to_followers_activity(**request.GET))\n \n-        paginated = Paginator(\n-            user.followers.order_by(\"-created_date\").all(), PAGE_LENGTH\n-        )\n+        followers = annotate_if_follows(request.user, user.followers)\n+        paginated = Paginator(followers.all(), PAGE_LENGTH)\n         data = {\n             \"user\": user,\n             \"is_self\": request.user.id == user.id,\n@@ -126,9 +126,8 @@\n         if is_api_request(request):\n             return ActivitypubResponse(user.to_following_activity(**request.GET))\n \n-        paginated = Paginator(\n-            user.following.order_by(\"-created_date\").all(), PAGE_LENGTH\n-        )\n+        following = annotate_if_follows(request.user, user.following)\n+        paginated = Paginator(following.all(), PAGE_LENGTH)\n         data = {\n             \"user\": user,\n             \"is_self\": request.user.id == user.id,\n@@ -137,6 +136,16 @@\n         return TemplateResponse(request, \"user/relationships/following.html\", data)\n \n \n+def annotate_if_follows(user, queryset):\n+    \"\"\"Sort a list of users by if you follow them\"\"\"\n+    if not user.is_authenticated:\n+        return queryset.order_by(\"-created_date\")\n+\n+    return queryset.annotate(\n+        request_user_follows=Count(\"followers\", filter=Q(followers=user))\n+    ).order_by(\"-request_user_follows\", \"-created_date\")\n+\n+\n class Groups(View):\n     \"\"\"list of user's groups view\"\"\"\n", "issue": "List Followers You Know When Looking at User's Follower List\n**Is your feature request related to a problem? Please describe.**\r\nWhen 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.\r\nWhen trying to decide how to handle a follow request, seeing what people we both know is the most relevant information to me.\r\n\r\n**Describe the solution you'd like**\r\nSort 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)\r\n\r\n**Describe alternatives you've considered**\r\nHave a separate view for 'Followers you know' (and rename the link to the Followers list if it doesn't point to that list)\r\n\r\n**Additional context**\r\nHi mouse\r\n\n", "before_files": [{"content": "\"\"\" non-interactive pages \"\"\"\nfrom django.contrib.auth.decorators import login_required\nfrom django.core.paginator import Paginator\nfrom django.http import Http404\nfrom django.shortcuts import redirect\nfrom django.template.response import TemplateResponse\nfrom django.utils import timezone\nfrom django.views import View\nfrom django.views.decorators.http import require_POST\n\nfrom bookwyrm import models\nfrom bookwyrm.activitypub import ActivitypubResponse\nfrom bookwyrm.settings import PAGE_LENGTH\nfrom .helpers import get_user_from_username, is_api_request\n\n\n# pylint: disable=no-self-use\nclass User(View):\n    \"\"\"user profile page\"\"\"\n\n    def get(self, request, username):\n        \"\"\"profile page for a user\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        if is_api_request(request):\n            # we have a json request\n            return ActivitypubResponse(user.to_activity())\n        # otherwise we're at a UI view\n\n        shelf_preview = []\n\n        # only show other shelves that should be visible\n        shelves = user.shelf_set\n        is_self = request.user.id == user.id\n        if not is_self:\n            follower = user.followers.filter(id=request.user.id).exists()\n            if follower:\n                shelves = shelves.filter(privacy__in=[\"public\", \"followers\"])\n            else:\n                shelves = shelves.filter(privacy=\"public\")\n\n        for user_shelf in shelves.all():\n            if not user_shelf.books.count():\n                continue\n            shelf_preview.append(\n                {\n                    \"name\": user_shelf.name,\n                    \"local_path\": user_shelf.local_path,\n                    \"books\": user_shelf.books.all()[:3],\n                    \"size\": user_shelf.books.count(),\n                }\n            )\n            if len(shelf_preview) > 2:\n                break\n\n        # user's posts\n        activities = (\n            models.Status.privacy_filter(\n                request.user,\n            )\n            .filter(user=user)\n            .select_related(\n                \"user\",\n                \"reply_parent\",\n                \"review__book\",\n                \"comment__book\",\n                \"quotation__book\",\n            )\n            .prefetch_related(\n                \"mention_books\",\n                \"mention_users\",\n                \"attachments\",\n            )\n        )\n\n        paginated = Paginator(activities, PAGE_LENGTH)\n        goal = models.AnnualGoal.objects.filter(\n            user=user, year=timezone.now().year\n        ).first()\n        if goal:\n            try:\n                goal.raise_visible_to_user(request.user)\n            except Http404:\n                goal = None\n\n        data = {\n            \"user\": user,\n            \"is_self\": is_self,\n            \"shelves\": shelf_preview,\n            \"shelf_count\": shelves.count(),\n            \"activities\": paginated.get_page(request.GET.get(\"page\", 1)),\n            \"goal\": goal,\n        }\n\n        return TemplateResponse(request, \"user/user.html\", data)\n\n\nclass Followers(View):\n    \"\"\"list of followers view\"\"\"\n\n    def get(self, request, username):\n        \"\"\"list of followers\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        if is_api_request(request):\n            return ActivitypubResponse(user.to_followers_activity(**request.GET))\n\n        paginated = Paginator(\n            user.followers.order_by(\"-created_date\").all(), PAGE_LENGTH\n        )\n        data = {\n            \"user\": user,\n            \"is_self\": request.user.id == user.id,\n            \"follow_list\": paginated.get_page(request.GET.get(\"page\")),\n        }\n        return TemplateResponse(request, \"user/relationships/followers.html\", data)\n\n\nclass Following(View):\n    \"\"\"list of following view\"\"\"\n\n    def get(self, request, username):\n        \"\"\"list of followers\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        if is_api_request(request):\n            return ActivitypubResponse(user.to_following_activity(**request.GET))\n\n        paginated = Paginator(\n            user.following.order_by(\"-created_date\").all(), PAGE_LENGTH\n        )\n        data = {\n            \"user\": user,\n            \"is_self\": request.user.id == user.id,\n            \"follow_list\": paginated.get_page(request.GET.get(\"page\")),\n        }\n        return TemplateResponse(request, \"user/relationships/following.html\", data)\n\n\nclass Groups(View):\n    \"\"\"list of user's groups view\"\"\"\n\n    def get(self, request, username):\n        \"\"\"list of groups\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        paginated = Paginator(\n            models.Group.memberships.filter(user=user).order_by(\"-created_date\"),\n            PAGE_LENGTH,\n        )\n        data = {\n            \"user\": user,\n            \"is_self\": request.user.id == user.id,\n            \"group_list\": paginated.get_page(request.GET.get(\"page\")),\n        }\n        return TemplateResponse(request, \"user/groups.html\", data)\n\n\n@require_POST\n@login_required\ndef hide_suggestions(request):\n    \"\"\"not everyone wants user suggestions\"\"\"\n    request.user.show_suggested_users = False\n    request.user.save(broadcast=False, update_fields=[\"show_suggested_users\"])\n    return redirect(request.headers.get(\"Referer\", \"/\"))\n", "path": "bookwyrm/views/user.py"}], "after_files": [{"content": "\"\"\" non-interactive pages \"\"\"\nfrom django.contrib.auth.decorators import login_required\nfrom django.core.paginator import Paginator\nfrom django.db.models import Q, Count\nfrom django.http import Http404\nfrom django.shortcuts import redirect\nfrom django.template.response import TemplateResponse\nfrom django.utils import timezone\nfrom django.views import View\nfrom django.views.decorators.http import require_POST\n\nfrom bookwyrm import models\nfrom bookwyrm.activitypub import ActivitypubResponse\nfrom bookwyrm.settings import PAGE_LENGTH\nfrom .helpers import get_user_from_username, is_api_request\n\n\n# pylint: disable=no-self-use\nclass User(View):\n    \"\"\"user profile page\"\"\"\n\n    def get(self, request, username):\n        \"\"\"profile page for a user\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        if is_api_request(request):\n            # we have a json request\n            return ActivitypubResponse(user.to_activity())\n        # otherwise we're at a UI view\n\n        shelf_preview = []\n\n        # only show other shelves that should be visible\n        shelves = user.shelf_set\n        is_self = request.user.id == user.id\n        if not is_self:\n            follower = user.followers.filter(id=request.user.id).exists()\n            if follower:\n                shelves = shelves.filter(privacy__in=[\"public\", \"followers\"])\n            else:\n                shelves = shelves.filter(privacy=\"public\")\n\n        for user_shelf in shelves.all():\n            if not user_shelf.books.count():\n                continue\n            shelf_preview.append(\n                {\n                    \"name\": user_shelf.name,\n                    \"local_path\": user_shelf.local_path,\n                    \"books\": user_shelf.books.all()[:3],\n                    \"size\": user_shelf.books.count(),\n                }\n            )\n            if len(shelf_preview) > 2:\n                break\n\n        # user's posts\n        activities = (\n            models.Status.privacy_filter(\n                request.user,\n            )\n            .filter(user=user)\n            .select_related(\n                \"user\",\n                \"reply_parent\",\n                \"review__book\",\n                \"comment__book\",\n                \"quotation__book\",\n            )\n            .prefetch_related(\n                \"mention_books\",\n                \"mention_users\",\n                \"attachments\",\n            )\n        )\n\n        paginated = Paginator(activities, PAGE_LENGTH)\n        goal = models.AnnualGoal.objects.filter(\n            user=user, year=timezone.now().year\n        ).first()\n        if goal:\n            try:\n                goal.raise_visible_to_user(request.user)\n            except Http404:\n                goal = None\n\n        data = {\n            \"user\": user,\n            \"is_self\": is_self,\n            \"shelves\": shelf_preview,\n            \"shelf_count\": shelves.count(),\n            \"activities\": paginated.get_page(request.GET.get(\"page\", 1)),\n            \"goal\": goal,\n        }\n\n        return TemplateResponse(request, \"user/user.html\", data)\n\n\nclass Followers(View):\n    \"\"\"list of followers view\"\"\"\n\n    def get(self, request, username):\n        \"\"\"list of followers\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        if is_api_request(request):\n            return ActivitypubResponse(user.to_followers_activity(**request.GET))\n\n        followers = annotate_if_follows(request.user, user.followers)\n        paginated = Paginator(followers.all(), PAGE_LENGTH)\n        data = {\n            \"user\": user,\n            \"is_self\": request.user.id == user.id,\n            \"follow_list\": paginated.get_page(request.GET.get(\"page\")),\n        }\n        return TemplateResponse(request, \"user/relationships/followers.html\", data)\n\n\nclass Following(View):\n    \"\"\"list of following view\"\"\"\n\n    def get(self, request, username):\n        \"\"\"list of followers\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        if is_api_request(request):\n            return ActivitypubResponse(user.to_following_activity(**request.GET))\n\n        following = annotate_if_follows(request.user, user.following)\n        paginated = Paginator(following.all(), PAGE_LENGTH)\n        data = {\n            \"user\": user,\n            \"is_self\": request.user.id == user.id,\n            \"follow_list\": paginated.get_page(request.GET.get(\"page\")),\n        }\n        return TemplateResponse(request, \"user/relationships/following.html\", data)\n\n\ndef annotate_if_follows(user, queryset):\n    \"\"\"Sort a list of users by if you follow them\"\"\"\n    if not user.is_authenticated:\n        return queryset.order_by(\"-created_date\")\n\n    return queryset.annotate(\n        request_user_follows=Count(\"followers\", filter=Q(followers=user))\n    ).order_by(\"-request_user_follows\", \"-created_date\")\n\n\nclass Groups(View):\n    \"\"\"list of user's groups view\"\"\"\n\n    def get(self, request, username):\n        \"\"\"list of groups\"\"\"\n        user = get_user_from_username(request.user, username)\n\n        paginated = Paginator(\n            models.Group.memberships.filter(user=user).order_by(\"-created_date\"),\n            PAGE_LENGTH,\n        )\n        data = {\n            \"user\": user,\n            \"is_self\": request.user.id == user.id,\n            \"group_list\": paginated.get_page(request.GET.get(\"page\")),\n        }\n        return TemplateResponse(request, \"user/groups.html\", data)\n\n\n@require_POST\n@login_required\ndef hide_suggestions(request):\n    \"\"\"not everyone wants user suggestions\"\"\"\n    request.user.show_suggested_users = False\n    request.user.save(broadcast=False, update_fields=[\"show_suggested_users\"])\n    return redirect(request.headers.get(\"Referer\", \"/\"))\n", "path": "bookwyrm/views/user.py"}]}