forked from mirrors/bookwyrm
ActivityPub serialize book
This commit is contained in:
parent
0557a5cc69
commit
5c475e448a
5 changed files with 68 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
||||||
''' bring activitypub functions into the namespace '''
|
''' bring activitypub functions into the namespace '''
|
||||||
from .actor import get_actor
|
from .actor import get_actor
|
||||||
from .book import get_book, get_book_list
|
from .book import get_book
|
||||||
from .create import get_create
|
from .create import get_create
|
||||||
from .follow import get_following, get_followers
|
from .follow import get_following, get_followers
|
||||||
from .follow import get_follow_request, get_unfollow, get_accept, get_reject
|
from .follow import get_follow_request, get_unfollow, get_accept, get_reject
|
||||||
|
|
56
fedireads/activitypub/book.py
Normal file
56
fedireads/activitypub/book.py
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
''' federate book data '''
|
||||||
|
from fedireads.settings import DOMAIN
|
||||||
|
|
||||||
|
def get_book(book):
|
||||||
|
''' activitypub serialize a book '''
|
||||||
|
|
||||||
|
activity = {
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
'type': 'Document',
|
||||||
|
'book_type': type(book).__name__,
|
||||||
|
'name': book.title,
|
||||||
|
'url': book.absolute_id,
|
||||||
|
|
||||||
|
'sort_title': book.sort_title,
|
||||||
|
'subtitle': book.subtitle,
|
||||||
|
|
||||||
|
'openlibrary_key': book.openlibrary_key,
|
||||||
|
'librarything_key': book.librarything_key,
|
||||||
|
'fedireads_key': book.fedireads_key,
|
||||||
|
'misc_identifiers': book.misc_identifiers,
|
||||||
|
|
||||||
|
'source_url': book.source_url,
|
||||||
|
'sync': book.sync,
|
||||||
|
'last_sync_date': book.last_sync_date,
|
||||||
|
|
||||||
|
'description': book.description,
|
||||||
|
'language': book.language,
|
||||||
|
'series': book.series,
|
||||||
|
'series_number': book.series_number,
|
||||||
|
'first_published_date': book.first_published_date.isoformat() if \
|
||||||
|
book.first_published_date else None,
|
||||||
|
'published_date': book.published_date.isoformat() if \
|
||||||
|
book.published_date else None,
|
||||||
|
'parent_work': book.parent_work.absolute_id if \
|
||||||
|
book.parent_work else None,
|
||||||
|
'authors': [get_author(a) for a in book.authors.all()],
|
||||||
|
}
|
||||||
|
|
||||||
|
if book.cover:
|
||||||
|
image_path = book.cover.url
|
||||||
|
image_type = image_path.split('.')[-1]
|
||||||
|
activity['attachment'] = [{
|
||||||
|
'type': 'Document',
|
||||||
|
'mediaType': 'image/%s' % image_type,
|
||||||
|
'url': 'https://%s%s' % (DOMAIN, image_path),
|
||||||
|
'name': 'Cover of "%s"' % book.title,
|
||||||
|
}]
|
||||||
|
return {k: v for (k, v) in activity.items() if v}
|
||||||
|
|
||||||
|
|
||||||
|
def get_author(author):
|
||||||
|
''' serialize an author '''
|
||||||
|
return {
|
||||||
|
'name': author.name,
|
||||||
|
'url': author.absolute_id,
|
||||||
|
}
|
|
@ -56,12 +56,12 @@ urlpatterns = [
|
||||||
re_path(r'%s/replies\.json$' % status_path, views.replies_page),
|
re_path(r'%s/replies\.json$' % status_path, views.replies_page),
|
||||||
|
|
||||||
# books
|
# books
|
||||||
re_path(r'^book/(?P<book_identifier>[\w\-]+)/?$', views.book_page),
|
re_path(r'^book/(?P<book_identifier>[\w\-]+)(.json)?/?$', views.book_page),
|
||||||
re_path(r'^book/(?P<book_identifier>[\w\-]+)/(?P<tab>friends|local|federated)?$', views.book_page),
|
re_path(r'^book/(?P<book_identifier>[\w\-]+)/(?P<tab>friends|local|federated)?$', views.book_page),
|
||||||
re_path(r'^author/(?P<author_identifier>\w+)/?$', views.author_page),
|
re_path(r'^author/(?P<author_identifier>\w+)/?$', views.author_page),
|
||||||
re_path(r'^tag/(?P<tag_id>.+)/?$', views.tag_page),
|
re_path(r'^tag/(?P<tag_id>.+)/?$', views.tag_page),
|
||||||
re_path(r'^shelf/%s/(?P<shelf_identifier>[\w-]+)/?$' % username_regex, views.shelf_page),
|
re_path(r'^shelf/%s/(?P<shelf_identifier>[\w-]+)(.json)?/?$' % username_regex, views.shelf_page),
|
||||||
re_path(r'^shelf/%s/(?P<shelf_identifier>[\w-]+)/?$' % localname_regex, views.shelf_page),
|
re_path(r'^shelf/%s/(?P<shelf_identifier>[\w-]+)(.json)?/?$' % localname_regex, views.shelf_page),
|
||||||
|
|
||||||
# internal action endpoints
|
# internal action endpoints
|
||||||
re_path(r'^logout/?$', actions.user_logout),
|
re_path(r'^logout/?$', actions.user_logout),
|
||||||
|
|
|
@ -5,6 +5,7 @@ from fedireads.settings import DOMAIN
|
||||||
|
|
||||||
# TODO maybe this should be in /models?
|
# TODO maybe this should be in /models?
|
||||||
class FedireadsModel(models.Model):
|
class FedireadsModel(models.Model):
|
||||||
|
''' fields and functions for every model '''
|
||||||
created_date = models.DateTimeField(auto_now_add=True)
|
created_date = models.DateTimeField(auto_now_add=True)
|
||||||
updated_date = models.DateTimeField(auto_now=True)
|
updated_date = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
|
|
@ -309,6 +309,9 @@ def book_page(request, book_identifier, tab='friends'):
|
||||||
''' info about a book '''
|
''' info about a book '''
|
||||||
book = books_manager.get_or_create_book(book_identifier)
|
book = books_manager.get_or_create_book(book_identifier)
|
||||||
|
|
||||||
|
if is_api_request(request):
|
||||||
|
return JsonResponse(activitypub.get_book(book))
|
||||||
|
|
||||||
if isinstance(book, models.Work):
|
if isinstance(book, models.Work):
|
||||||
book_reviews = models.Review.objects.filter(
|
book_reviews = models.Review.objects.filter(
|
||||||
Q(book=book) | Q(book__parent_work=book),
|
Q(book=book) | Q(book__parent_work=book),
|
||||||
|
@ -426,6 +429,10 @@ def shelf_page(request, username, shelf_identifier):
|
||||||
return HttpResponseNotFound()
|
return HttpResponseNotFound()
|
||||||
|
|
||||||
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
|
shelf = models.Shelf.objects.get(user=user, identifier=shelf_identifier)
|
||||||
|
|
||||||
|
if is_api_request(request):
|
||||||
|
return activitypub.get_shelf(shelf)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'shelf': shelf,
|
'shelf': shelf,
|
||||||
'user': user,
|
'user': user,
|
||||||
|
|
Loading…
Reference in a new issue