From 252f09325a9b3622cfade6acb8db44fdb951b7f1 Mon Sep 17 00:00:00 2001 From: Jim Fingal Date: Sun, 21 Feb 2021 23:07:58 -0800 Subject: [PATCH 01/25] Add black command --- Makefile | 5 +++++ requirements.txt | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..44abad0b3 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +.PHONY: itblack + +itblack: + docker-compose run --rm web black celerywyrm + docker-compose run --rm web black bookwyrm \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e5d7798d7..f354fd439 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ celery==4.4.2 -coverage==5.1 Django==3.0.7 django-model-utils==4.0.0 environs==7.2.0 @@ -8,11 +7,15 @@ Markdown==3.3.3 Pillow>=7.1.0 psycopg2==2.8.4 pycryptodome==3.9.4 -pytest-django==4.1.0 -pytest==6.1.2 -pytest-cov==2.10.1 python-dateutil==2.8.1 redis==3.4.1 requests==2.22.0 responses==0.10.14 django-rename-app==0.1.2 + +# Dev +black==20.8b1 +coverage==5.1 +pytest-django==4.1.0 +pytest==6.1.2 +pytest-cov==2.10.1 From 09c5275ec41efb0a7467081e24b98668de7a299c Mon Sep 17 00:00:00 2001 From: erion Date: Sun, 7 Mar 2021 13:18:10 +0100 Subject: [PATCH 02/25] Fix typo. --- bookwyrm/templates/feed/status.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/feed/status.html b/bookwyrm/templates/feed/status.html index fc92f8556..9f90f355d 100644 --- a/bookwyrm/templates/feed/status.html +++ b/bookwyrm/templates/feed/status.html @@ -4,7 +4,7 @@ {% block panel %}
- + {% trans "Back" %}
From cb8ec01ccfff8ca71296226aea19f4050a5a12b8 Mon Sep 17 00:00:00 2001 From: erion Date: Sun, 7 Mar 2021 13:55:50 +0100 Subject: [PATCH 03/25] Indicate which page is the current. --- bookwyrm/templates/feed/feed.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bookwyrm/templates/feed/feed.html b/bookwyrm/templates/feed/feed.html index 1eae24d4e..4eb363e4a 100644 --- a/bookwyrm/templates/feed/feed.html +++ b/bookwyrm/templates/feed/feed.html @@ -6,13 +6,13 @@

{% blocktrans %}{{ tab_title }} Timeline{% endblocktrans %}

From ae8d39995d514f62b2a83e1e401f3244dcb7938b Mon Sep 17 00:00:00 2001 From: erion Date: Sun, 7 Mar 2021 14:39:18 +0100 Subject: [PATCH 04/25] Hide avatar image to screen readers on the status pages, since there is a link present for a user already. --- bookwyrm/templates/snippets/avatar.html | 2 +- bookwyrm/templates/snippets/status/status_header.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templates/snippets/avatar.html b/bookwyrm/templates/snippets/avatar.html index ca49075cf..6d27cd856 100644 --- a/bookwyrm/templates/snippets/avatar.html +++ b/bookwyrm/templates/snippets/avatar.html @@ -1,3 +1,3 @@ {% load bookwyrm_tags %} -{{ user.alt_text }} +{{ user.alt_text }} diff --git a/bookwyrm/templates/snippets/status/status_header.html b/bookwyrm/templates/snippets/status/status_header.html index 2b9418200..a9a8d72c5 100644 --- a/bookwyrm/templates/snippets/status/status_header.html +++ b/bookwyrm/templates/snippets/status/status_header.html @@ -1,6 +1,6 @@ {% load bookwyrm_tags %} {% load i18n %} -{% include 'snippets/avatar.html' with user=status.user %} +{% include 'snippets/avatar.html' with user=status.user ariaHide="true" %} {% include 'snippets/username.html' with user=status.user %} {% if status.status_type == 'GeneratedNote' %} From 9082eefd8fd7073342af5116ac2432b82cb6a28d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 07:08:19 -0800 Subject: [PATCH 05/25] Sets specific proportions for book page columns --- bookwyrm/templates/book.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index d80daca24..ec6c504aa 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -35,7 +35,7 @@
-
+
{% include 'snippets/book_cover.html' with book=book size=large %} {% include 'snippets/rate_action.html' with user=request.user book=book %} {% include 'snippets/shelve_button/shelve_button.html' %} @@ -93,7 +93,7 @@
-
+

{% include 'snippets/stars.html' with rating=rating %} @@ -201,7 +201,7 @@

-
+
{% if book.subjects %}

{% trans "Subjects" %}

From 9c94be8804b3bc7bcdb5c5bfbcb9bba83ffb4972 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 07:35:38 -0800 Subject: [PATCH 06/25] Fixes typo in subject places block --- bookwyrm/templates/book.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index ec6c504aa..c4cede2ed 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -217,7 +217,7 @@

{% trans "Places" %}

    - {% for place in book.subject_placess %} + {% for place in book.subject_places %}
  • {{ place }}
  • {% endfor %}
From e5bdb4b9d1a9ef38ad49227362df28d1d9945099 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 07:35:50 -0800 Subject: [PATCH 07/25] Make empty cover value null --- bookwyrm/activitypub/book.py | 2 +- bookwyrm/tests/actions/__init__.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 bookwyrm/tests/actions/__init__.py diff --git a/bookwyrm/activitypub/book.py b/bookwyrm/activitypub/book.py index 87c40c90a..8c32be967 100644 --- a/bookwyrm/activitypub/book.py +++ b/bookwyrm/activitypub/book.py @@ -26,7 +26,7 @@ class Book(ActivityObject): librarythingKey: str = '' goodreadsKey: str = '' - cover: Image = field(default_factory=lambda: {}) + cover: Image = None type: str = 'Book' diff --git a/bookwyrm/tests/actions/__init__.py b/bookwyrm/tests/actions/__init__.py deleted file mode 100644 index b6e690fd5..000000000 --- a/bookwyrm/tests/actions/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from . import * From b895059f03f25a866cffde77d600c8a5cb8f3034 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 07:42:02 -0800 Subject: [PATCH 08/25] Uses same alt text generation for books with no cover --- bookwyrm/templates/snippets/book_cover.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bookwyrm/templates/snippets/book_cover.html b/bookwyrm/templates/snippets/book_cover.html index 6d15b37f8..921378537 100644 --- a/bookwyrm/templates/snippets/book_cover.html +++ b/bookwyrm/templates/snippets/book_cover.html @@ -6,8 +6,7 @@
No cover
-

{{ book.title }}

-

({{ book.edition_info }})

+

{{ book.alt_text }}

{% endif %} From dfecdca6f93bb4f52c4a7ac9f807041fab816495 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 07:56:33 -0800 Subject: [PATCH 09/25] Fixes display of ratings --- bookwyrm/templates/book.html | 4 ++-- bookwyrm/templates/layout.html | 2 +- bookwyrm/templates/snippets/username.html | 4 +--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index c4cede2ed..06578e894 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -254,8 +254,8 @@
{% include 'snippets/username.html' with user=rating.user %}
-
-
{% trans "rated it" %}
+
+

{% trans "rated it" %}

{% include 'snippets/stars.html' with rating=rating.rating %}
diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index ee42b4f6e..8a708f633 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -79,7 +79,7 @@ aria-controls="navbar-dropdown" > {% include 'snippets/avatar.html' with user=request.user %} - {% include 'snippets/username.html' with user=request.user anchor=false %} + {% include 'snippets/username.html' with user=request.user anchor=false %}
-{% include 'snippets/delete_readthrough_modal.html' with controls_text="delete-readthrough" controls_uid=readthrough.id %} +{% include 'snippets/delete_readthrough_modal.html' with controls_text="delete-readthrough" controls_uid=readthrough.id no_body=True %} From 5ddb3b810e800648d8607026d97bdad611cb4d65 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 08:37:39 -0800 Subject: [PATCH 11/25] Don't show books lists when item isn't approved --- bookwyrm/views/books.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 4d6afba96..cf246446c 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -89,7 +89,7 @@ class Book(View): 'rating': reviews.aggregate(Avg('rating'))['rating__avg'], 'tags': models.UserTag.objects.filter(book=book), 'lists': privacy_filter( - request.user, book.list_set.all() + request.user, book.list_set.filter(listitem__approved=True) ), 'user_tags': user_tags, 'user_shelves': user_shelves, From a70264c12c71b1a08ee86d53da71480cc769c7a1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 08:42:30 -0800 Subject: [PATCH 12/25] Fixes showing link to user lists --- bookwyrm/templates/snippets/status/status_header.html | 2 ++ bookwyrm/templates/user/user_layout.html | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/templates/snippets/status/status_header.html b/bookwyrm/templates/snippets/status/status_header.html index a9a8d72c5..cecdddf61 100644 --- a/bookwyrm/templates/snippets/status/status_header.html +++ b/bookwyrm/templates/snippets/status/status_header.html @@ -1,7 +1,9 @@ {% load bookwyrm_tags %} {% load i18n %} + {% include 'snippets/avatar.html' with user=status.user ariaHide="true" %} {% include 'snippets/username.html' with user=status.user %} + {% if status.status_type == 'GeneratedNote' %} {{ status.content | safe }} diff --git a/bookwyrm/templates/user/user_layout.html b/bookwyrm/templates/user/user_layout.html index d76291608..75dc61dc3 100644 --- a/bookwyrm/templates/user/user_layout.html +++ b/bookwyrm/templates/user/user_layout.html @@ -56,7 +56,7 @@ {% trans "Reading Goal" %} {% endif %} - {% if is_self or user.lists.exists %} + {% if is_self or user.list_set.exists %} {% url 'user-lists' user|username as url %} {% trans "Lists" %} From ec92aff7930c4f4d2749d939c7c3704ce20fc7fe Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 08:50:07 -0800 Subject: [PATCH 13/25] Clearer notification preview for generated notes --- bookwyrm/templates/notifications.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/templates/notifications.html b/bookwyrm/templates/notifications.html index 007e6b053..d27196ba9 100644 --- a/bookwyrm/templates/notifications.html +++ b/bookwyrm/templates/notifications.html @@ -114,7 +114,9 @@
{% if related_status.content %} - {{ related_status.content | safe | truncatewords_html:10 }} + + {{ related_status.content | safe | truncatewords_html:10 }}{% if related_status.mention_books %} {{ related_status.mention_books.first.title }}{% endif %} + {% elif related_status.quote %} {{ related_status.quote | safe | truncatewords_html:10 }} {% elif related_status.rating %} From c0ccb7065c450f24ea67c2958603ddc90857c8b3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 09:22:35 -0800 Subject: [PATCH 14/25] Safer federation of book data changes Only broadcast to other BW instances, plus bonus error handling --- bookwyrm/models/activitypub_mixin.py | 2 +- bookwyrm/models/book.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/bookwyrm/models/activitypub_mixin.py b/bookwyrm/models/activitypub_mixin.py index bebe00d02..10015bf14 100644 --- a/bookwyrm/models/activitypub_mixin.py +++ b/bookwyrm/models/activitypub_mixin.py @@ -449,7 +449,7 @@ def broadcast_task(sender_id, activity, recipients): for recipient in recipients: try: sign_and_send(sender, activity, recipient) - except (HTTPError, SSLError) as e: + except (HTTPError, SSLError, ConnectionError) as e: logger.exception(e) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 6a1a18b1e..84bfbc6bd 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -37,6 +37,10 @@ class BookDataModel(ObjectMixin, BookWyrmModel): self.remote_id = None return super().save(*args, **kwargs) + def broadcast(self, activity, sender, software='bookwyrm'): + ''' only send book data updates to other bookwyrm instances ''' + super().broadcast(activity, sender, software=software) + class Book(BookDataModel): ''' a generic book, which can mean either an edition or a work ''' From 71bbea83f97ca6cda270180aab940ab882687a7b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 09:42:31 -0800 Subject: [PATCH 15/25] Adds discard check to favs --- bookwyrm/activitypub/base_activity.py | 2 +- bookwyrm/models/favorite.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 57f1a7134..c732fe1d3 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -102,7 +102,7 @@ class ActivityObject: if allow_create and \ hasattr(model, 'ignore_activity') and \ model.ignore_activity(self): - return None + raise ActivitySerializerError() # check for an existing instance instance = instance or model.find_existing(self.serialize()) diff --git a/bookwyrm/models/favorite.py b/bookwyrm/models/favorite.py index f90195016..66befd80c 100644 --- a/bookwyrm/models/favorite.py +++ b/bookwyrm/models/favorite.py @@ -17,6 +17,11 @@ class Favorite(ActivityMixin, BookWyrmModel): activity_serializer = activitypub.Like + @classmethod + def ignore_activity(cls, activity): + ''' don't bother with incoming favs of unknown statuses ''' + return cls.objects.filter(remote_id=activity.object).exists() + def save(self, *args, **kwargs): ''' update user active time ''' self.user.last_active_date = timezone.now() From 09b77e567f8dc886ed4a505a87d79141fbddc8d2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 09:44:42 -0800 Subject: [PATCH 16/25] Check for invalid json before verifying signature --- bookwyrm/views/inbox.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bookwyrm/views/inbox.py b/bookwyrm/views/inbox.py index 4da4e5b6e..46385093c 100644 --- a/bookwyrm/views/inbox.py +++ b/bookwyrm/views/inbox.py @@ -20,7 +20,7 @@ class Inbox(View): ''' requests sent by outside servers''' def post(self, request, username=None): ''' only works as POST request ''' - # first let's do some basic checks to see if this is legible + # make sure the user's inbox even exists if username: try: models.User.objects.get(localname=username) @@ -33,6 +33,11 @@ class Inbox(View): except json.decoder.JSONDecodeError: return HttpResponseBadRequest() + if not 'object' in activity_json or \ + not 'type' in activity_json or \ + not activity_json['type'] in activitypub.activity_objects: + return HttpResponseNotFound() + # verify the signature if not has_valid_signature(request, activity_json): if activity_json['type'] == 'Delete': @@ -42,12 +47,6 @@ class Inbox(View): return HttpResponse() return HttpResponse(status=401) - # just some quick smell tests before we try to parse the json - if not 'object' in activity_json or \ - not 'type' in activity_json or \ - not activity_json['type'] in activitypub.activity_objects: - return HttpResponseNotFound() - activity_task.delay(activity_json) return HttpResponse() @@ -63,7 +62,11 @@ def activity_task(activity_json): # cool that worked, now we should do the action described by the type # (create, update, delete, etc) - activity.action() + try: + activity.action() + except activitypub.ActivitySerializerError: + # this is raised if the activity is discarded + return def has_valid_signature(request, activity): From 47cf77145d19adb1ffc11364a8f45ec948ec8018 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 09:45:02 -0800 Subject: [PATCH 17/25] Updates tests for inbox tweaks --- bookwyrm/models/favorite.py | 2 +- bookwyrm/tests/views/test_inbox.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/favorite.py b/bookwyrm/models/favorite.py index 66befd80c..de500e51b 100644 --- a/bookwyrm/models/favorite.py +++ b/bookwyrm/models/favorite.py @@ -20,7 +20,7 @@ class Favorite(ActivityMixin, BookWyrmModel): @classmethod def ignore_activity(cls, activity): ''' don't bother with incoming favs of unknown statuses ''' - return cls.objects.filter(remote_id=activity.object).exists() + return not cls.objects.filter(remote_id=activity.object).exists() def save(self, *args, **kwargs): ''' update user active time ''' diff --git a/bookwyrm/tests/views/test_inbox.py b/bookwyrm/tests/views/test_inbox.py index ff55ad042..b0bd3e42f 100644 --- a/bookwyrm/tests/views/test_inbox.py +++ b/bookwyrm/tests/views/test_inbox.py @@ -74,7 +74,7 @@ class Inbox(TestCase): mock_valid.return_value = False result = self.client.post( '/user/mouse/inbox', - '{"type": "Test", "object": "exists"}', + '{"type": "Announce", "object": "exists"}', content_type="application/json" ) self.assertEqual(result.status_code, 401) @@ -494,6 +494,21 @@ class Inbox(TestCase): self.assertEqual(fav.remote_id, 'https://example.com/fav/1') self.assertEqual(fav.user, self.remote_user) + def test_ignore_favorite(self): + ''' don't try to save an unknown status ''' + activity = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': 'https://example.com/fav/1', + 'actor': 'https://example.com/users/rat', + 'type': 'Like', + 'published': 'Mon, 25 May 2020 19:31:20 GMT', + 'object': 'https://unknown.status/not-found', + } + + views.inbox.activity_task(activity) + + self.assertFalse(models.Favorite.objects.exists()) + def test_handle_unfavorite(self): ''' fav a status ''' activity = { From 0bd27928e4d0641738a7fffe0b6d51e73e26b8e7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 7 Mar 2021 10:24:46 -0800 Subject: [PATCH 18/25] Removes username snippet --- bookwyrm/templates/book.html | 2 +- bookwyrm/templates/layout.html | 2 +- bookwyrm/templates/lists/created_text.html | 10 ++++++++++ bookwyrm/templates/lists/curate.html | 2 +- bookwyrm/templates/lists/list_items.html | 5 +++-- bookwyrm/templates/lists/list_layout.html | 5 +++-- bookwyrm/templates/notifications.html | 6 ++++-- bookwyrm/templates/preferences/blocks.html | 2 +- bookwyrm/templates/search_results.html | 6 ++++-- bookwyrm/templates/snippets/status/status.html | 6 ++++-- .../templates/snippets/status/status_header.html | 14 ++++++++++++-- bookwyrm/templates/snippets/username.html | 13 ------------- bookwyrm/templates/user/followers.html | 13 ++++++++----- bookwyrm/templates/user/following.html | 7 +++++-- bookwyrm/templates/user/user_layout.html | 2 +- 15 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 bookwyrm/templates/lists/created_text.html delete mode 100644 bookwyrm/templates/snippets/username.html diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index 06578e894..16bf11972 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -252,7 +252,7 @@
{% include 'snippets/avatar.html' with user=rating.user %}
- {% include 'snippets/username.html' with user=rating.user %} + {{ rating.user.display_name }}

{% trans "rated it" %}

diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index 8a708f633..377acb6c5 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -79,7 +79,7 @@ aria-controls="navbar-dropdown" > {% include 'snippets/avatar.html' with user=request.user %} - {% include 'snippets/username.html' with user=request.user anchor=false %} + {{ user.display_name }}