fixes tagging

This commit is contained in:
Mouse Reeve 2020-11-06 14:25:48 -08:00
parent d31071ddb0
commit ad25e75282
6 changed files with 31 additions and 28 deletions

View file

@ -1,6 +1,6 @@
''' handles all of the activity coming in to the server ''' ''' handles all of the activity coming in to the server '''
import json import json
from urllib.parse import urldefrag from urllib.parse import urldefrag, unquote_plus
import django.db.utils import django.db.utils
from django.http import HttpResponse from django.http import HttpResponse
@ -308,10 +308,11 @@ def handle_boost(activity):
@app.task @app.task
def handle_unboost(activity): def handle_unboost(activity):
''' someone gave us a boost! ''' ''' someone gave us a boost! '''
boost = models.Boost.objects.filter(remote_id=activity['object']['id']).first() boost = models.Boost.objects.filter(
if not boost: remote_id=activity['object']['id']
return ).first()
status_builder.delete_status(boost) if boost:
status_builder.delete_status(boost)
@app.task @app.task
@ -319,8 +320,15 @@ def handle_tag(activity):
''' someone is tagging a book ''' ''' someone is tagging a book '''
user = get_or_create_remote_user(activity['actor']) user = get_or_create_remote_user(activity['actor'])
if not user.local: if not user.local:
book = activity['target']['id'] # ordered collection weirndess so we can't just to_model
status_builder.create_tag(user, book, activity['object']['name']) 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 @app.task

View file

@ -35,8 +35,8 @@ class Tag(OrderedCollectionMixin, BookWyrmModel):
return activitypub.Add( return activitypub.Add(
id='%s#add' % self.remote_id, id='%s#add' % self.remote_id,
actor=user.remote_id, actor=user.remote_id,
object=self.book.to_activity(), object=self.book.local_id,
target=self.to_activity(), target=self.remote_id,
).serialize() ).serialize()
def to_remove_activity(self, user): def to_remove_activity(self, user):

View file

@ -9,7 +9,7 @@ import requests
from bookwyrm import activitypub from bookwyrm import activitypub
from bookwyrm import models from bookwyrm import models
from bookwyrm.broadcast import broadcast 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 create_generated_note
from bookwyrm.status import delete_status from bookwyrm.status import delete_status
from bookwyrm.remote_user import get_or_create_remote_user 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') broadcast(user, remote_activity, software='other')
def handle_tag(user, book, name): def handle_tag(user, tag):
''' tag a book ''' ''' tag a book '''
tag = create_tag(user, book, name)
broadcast(user, tag.to_add_activity(user)) broadcast(user, tag.to_add_activity(user))

View file

@ -1,9 +1,7 @@
''' Handle user activity ''' ''' Handle user activity '''
from datetime import datetime from datetime import datetime
from django.db import IntegrityError
from bookwyrm import models from bookwyrm import models
from bookwyrm.books_manager import get_or_create_book
from bookwyrm.sanitize_html import InputHtmlParser from bookwyrm.sanitize_html import InputHtmlParser
@ -34,17 +32,6 @@ def create_generated_note(user, content, mention_books=None, privacy='public'):
return status 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, \ def create_notification(user, notification_type, related_user=None, \
related_book=None, related_status=None, related_import=None): related_book=None, related_status=None, related_import=None):
''' let a user know when someone interacts with their content ''' ''' let a user know when someone interacts with their content '''

View file

@ -75,7 +75,7 @@ urlpatterns = [
# books # books
re_path(r'%s(.json)?/?$' % book_path, views.book_page), 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/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<author_id>[\w\-]+)(.json)?/?$', views.author_page), re_path(r'^author/(?P<author_id>[\w\-]+)(.json)?/?$', views.author_page),
# TODO: tag needs a .json path # TODO: tag needs a .json path

View file

@ -468,9 +468,18 @@ def tag(request):
# field which doesn't validate # field which doesn't validate
name = request.POST.get('name') name = request.POST.get('name')
book_id = request.POST.get('book') 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) return redirect('/book/%s' % book_id)