Repository: bookwyrm-social/bookwyrm

Issue:
Number of followers of suggested users is wrong
**Describe the bug**
Recommendations for new users to follow in my feed appear with a number of followers we have in common, but that number looks off. The user recommendation I saw said they had 306 followers I follow, but neither of us has that many followers.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to your home feed after logging in
3. Scroll down until you see a recommendation for a user to follow
4. See the weird follower count

**Expected behavior**
The number of mutual followers should be accurate 

**Screenshots**
If applicable, add screenshots to help explain your problem.

Screenshot
![409385BB-8652-4B7C-9137-5B6CDB7F0866](https://user-images.githubusercontent.com/1128631/112738864-2ca94580-8f3d-11eb-92af-a86dc7148986.jpeg)

**Smartphone (please complete the following information):**
    - Device: iPhone 11
    - OS: 
    - Browser safari
    - Version

**Additional context**




Discussion:
The same person is suggested to me and also with 306 followers I follow. Plus someone else with 1722 followers.

Resolving patch:
diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py
--- a/bookwyrm/views/helpers.py
+++ b/bookwyrm/views/helpers.py
@@ -205,6 +205,7 @@ def get_suggested_users(user, *args, **kwargs):
                     ~Q(id__in=user.following.all()),
                     following__in=user.following.all(),
                 ),
+                distinct=True,
             ),
             shared_books=Count(
                 "shelfbook",
@@ -214,6 +215,7 @@ def get_suggested_users(user, *args, **kwargs):
                         s.book.parent_work for s in user.shelfbook_set.all()
                     ],
                 ),
+                distinct=True,
             ),
         )
     )


Test patch:
diff --git a/bookwyrm/tests/views/test_helpers.py b/bookwyrm/tests/views/test_helpers.py
--- a/bookwyrm/tests/views/test_helpers.py
+++ b/bookwyrm/tests/views/test_helpers.py
@@ -2,6 +2,7 @@
 import json
 from unittest.mock import patch
 import pathlib
+from django.db.models import Q
 from django.test import TestCase
 from django.test.client import RequestFactory
 import responses
@@ -287,3 +288,48 @@ def test_get_suggested_users(self, _):
         remote_user_annotated = result.get(id=self.remote_user.id)
         self.assertEqual(remote_user_annotated.mutuals, 0)
         self.assertEqual(remote_user_annotated.shared_books, 0)
+
+    def test_get_suggested_users_counts(self, _):
+        """ correct counting for multiple shared attributed """
+        user_1 = models.User.objects.create_user(
+            "nutria@local.com",
+            "nutria@nutria.com",
+            "nutriaword",
+            local=True,
+            localname="nutria",
+            discoverable=True,
+        )
+        for i in range(3):
+            user = models.User.objects.create_user(
+                "{:d}@local.com".format(i),
+                "{:d}@nutria.com".format(i),
+                "password",
+                local=True,
+                localname=i,
+            )
+            user.followers.add(user_1)
+            user.followers.add(self.local_user)
+
+        with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
+            for i in range(3):
+                book = models.Edition.objects.create(
+                    title=i,
+                    parent_work=models.Work.objects.create(title=i),
+                )
+                models.ShelfBook.objects.create(
+                    user=self.local_user,
+                    book=book,
+                    shelf=self.local_user.shelf_set.first(),
+                )
+                models.ShelfBook.objects.create(
+                    user=user_1, book=book, shelf=user_1.shelf_set.first()
+                )
+
+        result = views.helpers.get_suggested_users(
+            self.local_user,
+            ~Q(id=self.local_user.id),
+            ~Q(followers=self.local_user),
+        )
+        self.assertEqual(result.count(), 2)
+        user_1_annotated = result.get(id=user_1.id)
+        self.assertEqual(user_1_annotated.mutuals, 3)