Fixes loading remote books

- saves remote_id correctly
- loads remote books for incoming statuses
This commit is contained in:
Mouse Reeve 2020-10-30 17:04:10 -07:00
parent 72219ace77
commit 203e526a83
8 changed files with 57 additions and 5 deletions

View file

@ -4,7 +4,7 @@ import sys
from .base_activity import ActivityEncoder, Image, PublicKey, Signature
from .note import Note, GeneratedNote, Article, Comment, Review, Quotation
from .note import Tombstone
from .note import Tombstone, Link
from .interaction import Boost, Like
from .ordered_collection import OrderedCollection, OrderedCollectionPage
from .person import Person

View file

@ -36,9 +36,17 @@ class Article(Note):
type: str = 'Article'
@dataclass
class Link():
''' for tagging a book in a status '''
href: str
name: str
type: str = 'Link'
@dataclass(init=False)
class GeneratedNote(Note):
''' just a re-typed note '''
tag: List[Link]
type: str = 'GeneratedNote'

View file

@ -122,11 +122,11 @@ class AbstractConnector(ABC):
# atomic so that we don't save a work with no edition for vice versa
with transaction.atomic():
if not work:
work_key = work_data.get('url')
work_key = self.get_remote_id_from_data(work_data)
work = self.create_book(work_key, work_data, models.Work)
if not edition:
ed_key = edition_data.get('url')
ed_key = self.get_remote_id_from_data(edition_data)
edition = self.create_book(ed_key, edition_data, models.Edition)
edition.default = True
edition.parent_work = work
@ -146,11 +146,13 @@ class AbstractConnector(ABC):
def create_book(self, remote_id, data, model):
''' create a work or edition from data '''
print(remote_id)
book = model.objects.create(
remote_id=remote_id,
title=data['title'],
connector=self.connector,
)
print(book.remote_id)
return self.update_book_from_data(book, data)
@ -161,7 +163,8 @@ class AbstractConnector(ABC):
author_text = []
for author in self.get_authors_from_data(data):
book.authors.add(author)
author_text.append(author.display_name)
if author.display_name:
author_text.append(author.display_name)
book.author_text = ', '.join(author_text)
book.save()
@ -215,6 +218,11 @@ class AbstractConnector(ABC):
return None
@abstractmethod
def get_remote_id_from_data(self, data):
''' otherwise we won't properly set the remote_id in the db '''
@abstractmethod
def is_work_data(self, data):
''' differentiate works and editions '''

View file

@ -47,8 +47,12 @@ class Connector(AbstractConnector):
]
def get_remote_id_from_data(self, data):
return data.get('id')
def is_work_data(self, data):
return data['book_type'] == 'Work'
return data['type'] == 'Work'
def get_edition_from_work_data(self, data):

View file

@ -73,6 +73,14 @@ class Connector(AbstractConnector):
def get_remote_id_from_data(self, data):
try:
key = data['key']
except KeyError:
raise ConnectorException('Invalid book data')
return '%s/%s' % (self.books_url, key)
def is_work_data(self, data):
return bool(re.match(r'^[\/\w]+OL\d+W$', data['key']))

View file

@ -51,6 +51,9 @@ class Connector(AbstractConnector):
)
def get_remote_id_from_data(self, data):
pass
def is_work_data(self, data):
pass

View file

@ -225,6 +225,15 @@ def handle_create(activity):
if not reply:
return
# look up books
book_urls = []
if hasattr(activity, 'inReplyToBook'):
book_urls.append(activity.inReplyToBook)
if hasattr(activity, 'tag'):
book_urls += [t.href for t in activity.tag if t.type == 'Book']
for remote_id in book_urls:
book = books_manager.get_or_create_book(remote_id)
model = models.activity_models[activity.type]
status = activity.to_model(model)

View file

@ -57,6 +57,17 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
''' structured replies block '''
return self.to_replies()
@property
def ap_tag(self):
tags = []
for book in self.mention_books.all():
tags.append(activitypub.Link(
href=book.local_id,
name=book.title,
type='Book'
))
return tags
shared_mappings = [
ActivityMapping('id', 'remote_id'),
ActivityMapping('url', 'remote_id'),
@ -66,6 +77,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
ActivityMapping('to', 'ap_to'),
ActivityMapping('cc', 'ap_cc'),
ActivityMapping('replies', 'ap_replies'),
ActivityMapping('tag', 'ap_tag'),
]
# serializing to bookwyrm expanded activitypub