diff --git a/bookwyrm/activitypub/book.py b/bookwyrm/activitypub/book.py index 49f2deb81..60d36bd02 100644 --- a/bookwyrm/activitypub/book.py +++ b/bookwyrm/activitypub/book.py @@ -55,7 +55,6 @@ class Work(Book): @dataclass(init=False) class Author(ActivityObject): ''' author of a book ''' - url: str name: str born: str died: str diff --git a/bookwyrm/broadcast.py b/bookwyrm/broadcast.py index 9b071b393..a1eebaee3 100644 --- a/bookwyrm/broadcast.py +++ b/bookwyrm/broadcast.py @@ -61,7 +61,7 @@ def broadcast_task(sender_id, activity, recipients): return errors -def sign_and_send(sender, activity, destination): +def sign_and_send(sender, data, destination): ''' crpyto whatever and http junk ''' now = http_date() @@ -69,7 +69,6 @@ def sign_and_send(sender, activity, destination): # this shouldn't happen. it would be bad if it happened. raise ValueError('No private key found for sender') - data = json.dumps(activity).encode('utf-8') digest = make_digest(data) response = requests.post( diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index 5f4cc15b1..c6a22caa1 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -62,6 +62,8 @@ def shared_inbox(request): 'Announce': handle_boost, 'Add': { 'Tag': handle_tag, + 'Edition': handle_shelve, + 'Work': handle_shelve, }, 'Undo': { 'Follow': handle_unfollow, @@ -318,6 +320,22 @@ def handle_tag(activity): status_builder.create_tag(user, book, activity['object']['name']) +@app.task +def handle_shelve(activity): + ''' putting a book on a shelf ''' + user = get_or_create_remote_user(activity['actor']) + book = books_manager.get_or_create_book(activity['object']) + try: + shelf = models.Shelf.objects.get(remote_id=activity['target']) + except models.Shelf.DoesNotExist: + return + if shelf.user != user: + # this doesn't add up. + return + shelf.books.add(book) + shelf.save() + + @app.task def handle_update_user(activity): ''' receive an updated user Person activity object ''' diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 8150d650c..2215a28a9 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -68,7 +68,10 @@ class ActivitypubMixin: if not hasattr(self, mapping.model_key) or not mapping.activity_key: continue value = getattr(self, mapping.model_key) - if hasattr(value, 'remote_id'): + print(value) + if hasattr(value, 'local_id'): + value = value.local_id + elif hasattr(value, 'remote_id'): value = value.remote_id if isinstance(value, datetime): value = value.isoformat() diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index b6261b8b7..93d9393de 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -56,7 +56,7 @@ class Book(ActivitypubMixin, BookWyrmModel): @property def ap_authors(self): ''' the activitypub serialization should be a list of author ids ''' - return [a.remote_id for a in self.authors.all()] + return [a.local_id for a in self.authors.all()] @property def ap_cover(self): @@ -73,7 +73,7 @@ class Book(ActivitypubMixin, BookWyrmModel): return self.parent_work.local_id activity_mappings = [ - ActivityMapping('id', 'remote_id'), + ActivityMapping('id', 'local_id'), ActivityMapping('authors', 'ap_authors'), ActivityMapping('first_published_date', 'first_published_date'), @@ -258,7 +258,7 @@ class Author(ActivitypubMixin, BookWyrmModel): an instance, so it needs a local url for federation. but it still needs the remote_id for easier deduplication and, if appropriate, to sync with the remote canonical copy (ditto here for author)''' - return 'https://%s/book/%d' % (DOMAIN, self.id) + return 'https://%s/author/%d' % (DOMAIN, self.id) @property def display_name(self): @@ -271,8 +271,7 @@ class Author(ActivitypubMixin, BookWyrmModel): return self.last_name or self.first_name activity_mappings = [ - ActivityMapping('id', 'remote_id'), - ActivityMapping('url', 'remote_id'), + ActivityMapping('id', 'local_id'), ActivityMapping('name', 'display_name'), ActivityMapping('born', 'born'), ActivityMapping('died', 'died'), diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index 53e557eea..24f1fdced 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -49,8 +49,8 @@ class ShelfBook(BookWyrmModel): return activitypub.Add( id='%s#add' % self.remote_id, actor=user.remote_id, - object=self.book.to_activity(), - target=self.shelf.to_activity() + object=self.book.local_id, + target=self.shelf.remote_id, ).serialize() def to_remove_activity(self, user): diff --git a/bookwyrm/signatures.py b/bookwyrm/signatures.py index 7e3c637ff..57c181dfb 100644 --- a/bookwyrm/signatures.py +++ b/bookwyrm/signatures.py @@ -44,7 +44,8 @@ def make_signature(sender, destination, date, digest): def make_digest(data): ''' creates a message digest for signing ''' - return 'SHA-256=' + b64encode(hashlib.sha256(data).digest()).decode('utf-8') + return 'SHA-256=' + b64encode(hashlib.sha256(data.encode('utf-8'))\ + .digest()).decode('utf-8') def verify_digest(request): diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index dfe483279..e4b7ce5e3 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -2,7 +2,7 @@
-