diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index 134c54845..4bff25b43 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -1,6 +1,6 @@ ''' handles all of the activity coming in to the server ''' import json -from urllib.parse import urldefrag +from urllib.parse import urldefrag, unquote_plus import django.db.utils from django.http import HttpResponse @@ -308,10 +308,11 @@ def handle_boost(activity): @app.task def handle_unboost(activity): ''' someone gave us a boost! ''' - boost = models.Boost.objects.filter(remote_id=activity['object']['id']).first() - if not boost: - return - status_builder.delete_status(boost) + boost = models.Boost.objects.filter( + remote_id=activity['object']['id'] + ).first() + if boost: + status_builder.delete_status(boost) @app.task @@ -319,8 +320,15 @@ def handle_tag(activity): ''' someone is tagging a book ''' user = get_or_create_remote_user(activity['actor']) if not user.local: - book = activity['target']['id'] - status_builder.create_tag(user, book, activity['object']['name']) + # ordered collection weirndess so we can't just to_model + book = books_manager.get_or_create_book(activity['object']['id']) + name = activity['object']['target'].split('/')[-1] + name = unquote_plus(name) + models.Tag.objects.get_or_create( + user=user, + book=book, + name=name + ) @app.task diff --git a/bookwyrm/models/tag.py b/bookwyrm/models/tag.py index 2c6d7a980..510ad13bc 100644 --- a/bookwyrm/models/tag.py +++ b/bookwyrm/models/tag.py @@ -35,8 +35,8 @@ class Tag(OrderedCollectionMixin, BookWyrmModel): return activitypub.Add( id='%s#add' % self.remote_id, actor=user.remote_id, - object=self.book.to_activity(), - target=self.to_activity(), + object=self.book.local_id, + target=self.remote_id, ).serialize() def to_remove_activity(self, user): diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 0ea10ab91..1b21603e4 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -9,7 +9,7 @@ import requests from bookwyrm import activitypub from bookwyrm import models from bookwyrm.broadcast import broadcast -from bookwyrm.status import create_tag, create_notification +from bookwyrm.status import create_notification from bookwyrm.status import create_generated_note from bookwyrm.status import delete_status from bookwyrm.remote_user import get_or_create_remote_user @@ -257,9 +257,8 @@ def handle_status(user, form): broadcast(user, remote_activity, software='other') -def handle_tag(user, book, name): +def handle_tag(user, tag): ''' tag a book ''' - tag = create_tag(user, book, name) broadcast(user, tag.to_add_activity(user)) diff --git a/bookwyrm/status.py b/bookwyrm/status.py index c8c01c990..92a0379ef 100644 --- a/bookwyrm/status.py +++ b/bookwyrm/status.py @@ -1,9 +1,7 @@ ''' Handle user activity ''' from datetime import datetime -from django.db import IntegrityError from bookwyrm import models -from bookwyrm.books_manager import get_or_create_book from bookwyrm.sanitize_html import InputHtmlParser @@ -34,17 +32,6 @@ def create_generated_note(user, content, mention_books=None, privacy='public'): return status -def create_tag(user, possible_book, name): - ''' add a tag to a book ''' - book = get_or_create_book(possible_book) - - try: - tag = models.Tag.objects.create(name=name, book=book, user=user) - except IntegrityError: - return models.Tag.objects.get(name=name, book=book, user=user) - return tag - - def create_notification(user, notification_type, related_user=None, \ related_book=None, related_status=None, related_import=None): ''' let a user know when someone interacts with their content ''' diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 480958507..d85aaaacb 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -75,7 +75,7 @@ urlpatterns = [ # books re_path(r'%s(.json)?/?$' % book_path, views.book_page), re_path(r'%s/edit/?$' % book_path, views.edit_book_page), - re_path(r'%s/editions(.json)?/?' % book_path, views.editions_page), + re_path(r'%s/editions(.json)?/?$' % book_path, views.editions_page), re_path(r'^author/(?P[\w\-]+)(.json)?/?$', views.author_page), # TODO: tag needs a .json path diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index 7339f2d55..653a9c23a 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -468,9 +468,18 @@ def tag(request): # field which doesn't validate name = request.POST.get('name') book_id = request.POST.get('book') - remote_id = 'https://%s/book/%s' % (DOMAIN, book_id) + try: + book = models.Edition.objects.get(id=book_id) + except models.Edition.DoesNotExist: + return HttpResponseNotFound() + tag_obj, created = models.Tag.objects.get_or_create( + name=name, + book=book, + user=request.user + ) - outgoing.handle_tag(request.user, remote_id, name) + if created: + outgoing.handle_tag(request.user, tag_obj) return redirect('/book/%s' % book_id)