Use remote_id resolver to load books, user

This commit is contained in:
Mouse Reeve
2020-11-28 10:18:24 -08:00
parent 81bdd2b3f1
commit a93b5cf5bc
15 changed files with 115 additions and 93 deletions

View File

@ -227,12 +227,16 @@ class OrderedCollectionPageMixin(ActivitypubMixin):
name = ''
if hasattr(self, 'name'):
name = self.name
owner = ''
if hasattr(self, 'user'):
owner = self.user.remote_id
size = queryset.count()
return activitypub.OrderedCollection(
id=remote_id,
totalItems=size,
name=name,
owner=owner,
first='%s%s' % (remote_id, self.page()),
last='%s%s' % (remote_id, self.page(min_id=0))
).serialize()

View File

@ -41,10 +41,10 @@ class Book(ActivitypubMixin, BookWyrmModel):
series = models.CharField(max_length=255, blank=True, null=True)
series_number = models.CharField(max_length=255, blank=True, null=True)
subjects = ArrayField(
models.CharField(max_length=255), blank=True, default=list
models.CharField(max_length=255), blank=True, null=True, default=list
)
subject_places = ArrayField(
models.CharField(max_length=255), blank=True, default=list
models.CharField(max_length=255), blank=True, null=True, default=list
)
# TODO: include an annotation about the type of authorship (ie, translator)
authors = models.ManyToManyField('Author')
@ -132,7 +132,8 @@ class Work(OrderedCollectionPageMixin, Book):
''' it'd be nice to serialize the edition instead but, recursion '''
default = self.default_edition
ed_list = [
e.remote_id for e in self.edition_set.filter(~Q(id=default.id)).all()
e.remote_id for e in \
self.edition_set.filter(~Q(id=default.id)).all()
]
return [default.remote_id] + ed_list

View File

@ -3,7 +3,8 @@ import re
from django.db import models
from bookwyrm import activitypub
from .base_model import BookWyrmModel, OrderedCollectionMixin, PrivacyLevels
from .base_model import ActivityMapping, BookWyrmModel
from .base_model import OrderedCollectionMixin, PrivacyLevels
class Shelf(OrderedCollectionMixin, BookWyrmModel):
@ -47,6 +48,12 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
''' user/shelf unqiueness '''
unique_together = ('user', 'identifier')
activity_mappings = [
ActivityMapping('id', 'remote_id'),
ActivityMapping('owner', 'user'),
ActivityMapping('name', 'name'),
]
class ShelfBook(BookWyrmModel):
''' many to many join table for books and shelves '''
@ -59,6 +66,15 @@ class ShelfBook(BookWyrmModel):
on_delete=models.PROTECT
)
activity_mappings = [
ActivityMapping('id', 'remote_id'),
ActivityMapping('actor', 'added_by'),
ActivityMapping('object', 'book'),
ActivityMapping('target', 'shelf')
]
activity_serializer = activitypub.AddBook
def to_add_activity(self, user):
''' AP for shelving a book'''
return activitypub.Add(

View File

@ -80,12 +80,10 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
ActivityMapping(
'tag', 'mention_books',
lambda x: tag_formatter(x, 'title', 'Book'),
lambda x: [i for i in x if x.get('type') == 'Book']
),
ActivityMapping(
'tag', 'mention_users',
lambda x: tag_formatter(x, 'username', 'Mention'),
lambda x: [i for i in x if x.get('type') == 'Mention']
),
ActivityMapping(
'attachment', 'attachments',

View File

@ -11,7 +11,6 @@ from bookwyrm.models.status import Status
from bookwyrm.settings import DOMAIN
from bookwyrm.signatures import create_key_pair
from .base_model import ActivityMapping, OrderedCollectionPageMixin
from .base_model import image_formatter
class User(OrderedCollectionPageMixin, AbstractUser):