forked from mirrors/bookwyrm
fixes tagging
This commit is contained in:
parent
d31071ddb0
commit
ad25e75282
6 changed files with 31 additions and 28 deletions
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
||||
|
|
|
@ -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 '''
|
||||
|
|
|
@ -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<author_id>[\w\-]+)(.json)?/?$', views.author_page),
|
||||
# TODO: tag needs a .json path
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue