diff --git a/README.md b/README.md index 52e5c4cf..f07d9cef 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Social reading and reviewing, decentralized with ActivityPub ## Contents - - [The overall idea](#the-overall-idea) +- [Joining BookWyrm](#joining-bookwyrm) +- [The overall idea](#the-overall-idea) - [What it is and isn't](#what-it-is-and-isnt) - [The role of federation](#the-role-of-federation) - [Features](#features) @@ -13,42 +14,46 @@ Social reading and reviewing, decentralized with ActivityPub - [Book data](#book-data) - [Contributing](#contributing) +## Joining BookWyrm +BookWyrm is still a young piece of software, and isn't at the level of stability and feature-richness that you'd find in a production-ready application. But it does what it says on the box! If you'd like to join an instance, you can check out the [instances](https://github.com/mouse-reeve/bookwyrm/blob/main/instances.md) list. + +I, the maintianer of this project, run https://bookwyrm.social, and I generally give out invite codes to those who ask by [email](mailto:mousereeve@riseup.net), [Mastodon direct message](https://friend.camp/@tripofmice), or [Twitter direct message](https://twitter.com/tripofmice). + ## The overall idea ### What it is and isn't BookWyrm is a platform for social reading! You can use it to track what you're reading, review books, and follow your friends. It isn't primarily meant for cataloguing or as a datasource for books, but it does do both of those things to some degree. ### The role of federation -BookWyrm is built on [ActivityPub](http://activitypub.rocks/). With ActivityPub, it inter-operates with different instances of BookWyrm, and other ActivityPub compliant services, like Mastodon and Pixelfed. This means you can run an instance for your book club, and still follow your friend who posts on a server devoted to 20th century Russian speculative fiction. It also means that your friend on mastodon can read and comment on a book review that you post on your BookWyrm instance. +BookWyrm is built on [ActivityPub](http://activitypub.rocks/). With ActivityPub, it inter-operates with different instances of BookWyrm, and other ActivityPub compliant services, like Mastodon. This means you can run an instance for your book club, and still follow your friend who posts on a server devoted to 20th century Russian speculative fiction. It also means that your friend on mastodon can read and comment on a book review that you post on your BookWyrm instance. -Federation makes it possible to have small, self-determining communities, in contrast to the monolithic service you find on GoodReads or Twitter. An instance can be focused on a particular type of literature, be just for use by people who are in a book club together, or anything else that brings people together. Each community can choose which other instances they want to federate with, and moderate and run their community autonomously. Check out https://runyourown.social/ to get a sense of the philosophy and logistics behind small, high-trust social networks. +Federation makes it possible to have small, self-determining communities, in contrast to the monolithic service you find on GoodReads or Twitter. An instance can be focused on a particular interest, be just for a group of friends, or anything else that brings people together. Each community can choose which other instances they want to federate with, and moderate and run their community autonomously. Check out https://runyourown.social/ to get a sense of the philosophy and logistics behind small, high-trust social networks. ### Features -Since the project is still in its early stages, not everything here is fully implemented. There is plenty of room for suggestions and ideas. Open an [issue](https://github.com/mouse-reeve/bookwyrm/issues) to get the conversation going! +Since the project is still in its early stages, the features are growing every day, and there is plenty of room for suggestions and ideas. Open an [issue](https://github.com/mouse-reeve/bookwyrm/issues) to get the conversation going! - Posting about books - Compose reviews, with or without ratings, which are aggregated in the book page - Compose other kinds of statuses about books, such as: - Comments on a book - Quotes or excerpts - - Recommenations of other books - Reply to statuses - - Aggregate reviews of a book across connected BookWyrm instances - - Differentiate local and federated reviews and rating + - View aggregate reviews of a book across connected BookWyrm instances + - Differentiate local and federated reviews and rating in your activity feed - Track reading activity - Shelve books on default "to-read," "currently reading," and "read" shelves - Create custom shelves - - Store started reading/finished reading dates + - Store started reading/finished reading dates, as well as progress updates along the way - Update followers about reading activity (optionally, and with granular privacy controls) + - Create lists of books which can be open to submissions from anyone, curated, or only edited by the creator - Federation with ActivityPub - Broadcast and receive user statuses and activity - - Broadcast copies of books that can be used as canonical data sources + - Share book data between instances to create a networked database of metadata - Identify shared books across instances and aggregate related content - Follow and interact with users across BookWyrm instances - - Inter-operate with non-BookWyrm ActivityPub services + - Inter-operate with non-BookWyrm ActivityPub services (currently, Mastodon is supported) - Granular privacy controls - - Local-only, followers-only, and public posting + - Private, followers-only, and public privacy levels for posting, shelves, and lists - Option for users to manually approve followers - Allow blocking and flagging for moderation - - Control which instances you want to federate with ## Setting up the developer environment @@ -88,6 +93,7 @@ This project is still young and isn't, at the momoment, very stable, so please p `cp .env.example .env` - Add your domain, email address, mailgun credentials - Set a secure redis password and secret key + - Set a secure database password for postgres - Update your nginx configuration in `nginx/default.conf` - Replace `your-domain.com` with your domain name - Run the application (this should also set up a Certbot ssl cert for your domain) @@ -99,6 +105,7 @@ This project is still young and isn't, at the momoment, very stable, so please p `docker-compose up -d` - Initialize the database `./bw-dev initdb` + - Set up schedule backups with cron that runs that `docker-compose exec db pg_dump -U ` and saves the backup to a safe locationgi - Congrats! You did it, go to your domain and enjoy the fruits of your labors ### Configure your instance - Register a user account in the applcation UI @@ -114,21 +121,11 @@ This project is still young and isn't, at the momoment, very stable, so please p user.is_superuser = True user.save() ``` - - Go to the admin panel (`/admin/bookwyrm/sitesettings/1/change` on your domain) and set your instance name, description, code of conduct, and toggle whether registration is open on your instance - - -## Project structure -All the url routing is in `bookwyrm/urls.py`. This includes the application views (your home page, user page, book page, etc), application endpoints (things that happen when you click buttons), and federation api endpoints (inboxes, outboxes, webfinger, etc). - -The application views and actions are in `bookwyrm/views.py`. The internal actions call api handlers which deal with federating content. Outgoing messages (any action done by a user that is federated out), as well as outboxes, live in `bookwyrm/outgoing.py`, and all handlers for incoming messages, as well as inboxes and webfinger, live in `bookwyrm/incoming.py`. Connection to openlibrary.org to get book data is handled in `bookwyrm/connectors/openlibrary.py`. ActivityPub serialization is handled in the `bookwyrm/activitypub/` directory. - -Celery is used for background tasks, which includes receiving incoming ActivityPub activities, ActivityPub broadcasting, and external data import. - -The UI is all django templates because that is the default. You can replace it with a complex javascript framework over my ~dead body~ mild objections. + - Go to the site settings (`/settings/site-settings` on your domain) and configure your instance name, description, code of conduct, and toggle whether registration is open on your instance ## Book data -The application is set up to get book data from arbitrary outside sources -- right now, it's only able to connect to OpenLibrary, but other connectors could be written. By default, a book is non-canonical copy of an OpenLibrary book, and will be updated with OpenLibrary if the data there changes. However, a book can edited and decoupled from its original data source, or added locally with no external data source. +The application is set up to share book and author data between instances, and get book data from arbitrary outside sources. Right now, the only connector is to OpenLibrary, but other connectors could be written. There are three concepts in the book data model: - `Book`, an abstract, high-level concept that could mean either a `Work` or an `Edition`. No data is saved as a `Book`, it serves as shared model for `Work` and `Edition` diff --git a/bookwyrm/activitypub/__init__.py b/bookwyrm/activitypub/__init__.py index a7439722..201e8042 100644 --- a/bookwyrm/activitypub/__init__.py +++ b/bookwyrm/activitypub/__init__.py @@ -10,6 +10,7 @@ from .note import Note, GeneratedNote, Article, Comment, Review, Quotation from .note import Tombstone from .interaction import Boost, Like from .ordered_collection import OrderedCollection, OrderedCollectionPage +from .ordered_collection import BookList, Shelf from .person import Person, PublicKey from .response import ActivitypubResponse from .book import Edition, Work, Author diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 7ef0920f..3ebf2fab 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -130,6 +130,7 @@ class ActivityObject: def serialize(self): ''' convert to dictionary with context attr ''' data = self.__dict__ + data = {k:v for (k, v) in data.items() if v is not None} data['@context'] = 'https://www.w3.org/ns/activitystreams' return data diff --git a/bookwyrm/activitypub/ordered_collection.py b/bookwyrm/activitypub/ordered_collection.py index 9aeaf664..cf642994 100644 --- a/bookwyrm/activitypub/ordered_collection.py +++ b/bookwyrm/activitypub/ordered_collection.py @@ -1,5 +1,5 @@ ''' defines activitypub collections (lists) ''' -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import List from .base_activity import ActivityObject @@ -10,11 +10,28 @@ class OrderedCollection(ActivityObject): ''' structure of an ordered collection activity ''' totalItems: int first: str - last: str = '' - name: str = '' - owner: str = '' + last: str = None + name: str = None + owner: str = None type: str = 'OrderedCollection' +@dataclass(init=False) +class OrderedCollectionPrivate(OrderedCollection): + to: List[str] = field(default_factory=lambda: []) + cc: List[str] = field(default_factory=lambda: []) + +@dataclass(init=False) +class Shelf(OrderedCollectionPrivate): + ''' structure of an ordered collection activity ''' + type: str = 'Shelf' + +@dataclass(init=False) +class BookList(OrderedCollectionPrivate): + ''' structure of an ordered collection activity ''' + summary: str = None + curation: str = 'closed' + type: str = 'BookList' + @dataclass(init=False) class OrderedCollectionPage(ActivityObject): diff --git a/bookwyrm/activitypub/verbs.py b/bookwyrm/activitypub/verbs.py index 6977ee8e..5502ced0 100644 --- a/bookwyrm/activitypub/verbs.py +++ b/bookwyrm/activitypub/verbs.py @@ -18,7 +18,7 @@ class Create(Verb): ''' Create activity ''' to: List cc: List - signature: Signature + signature: Signature = None type: str = 'Create' diff --git a/bookwyrm/connectors/connector_manager.py b/bookwyrm/connectors/connector_manager.py index d3b01f7a..a63a788e 100644 --- a/bookwyrm/connectors/connector_manager.py +++ b/bookwyrm/connectors/connector_manager.py @@ -35,10 +35,10 @@ def search(query, min_confidence=0.1): return results -def local_search(query, min_confidence=0.1): +def local_search(query, min_confidence=0.1, raw=False): ''' only look at local search results ''' connector = load_connector(models.Connector.objects.get(local=True)) - return connector.search(query, min_confidence=min_confidence) + return connector.search(query, min_confidence=min_confidence, raw=raw) def first_search_result(query, min_confidence=0.1): diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index 55355131..cd196d27 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -27,9 +27,9 @@ class Connector(AbstractConnector): Mapping('series', formatter=get_first), Mapping('seriesNumber', remote_field='series_number'), Mapping('subjects'), - Mapping('subjectPlaces'), - Mapping('isbn13', formatter=get_first), - Mapping('isbn10', formatter=get_first), + Mapping('subjectPlaces', remote_field='subject_places'), + Mapping('isbn13', remote_field='isbn_13', formatter=get_first), + Mapping('isbn10', remote_field='isbn_10', formatter=get_first), Mapping('lccn', formatter=get_first), Mapping( 'oclcNumber', remote_field='oclc_numbers', @@ -144,9 +144,34 @@ class Connector(AbstractConnector): # we can mass download edition data from OL to avoid repeatedly querying edition_options = self.load_edition_data(work.openlibrary_key) for edition_data in edition_options.get('entries'): + # does this edition have ANY interesting data? + if ignore_edition(edition_data): + continue self.create_edition_from_data(work, edition_data) +def ignore_edition(edition_data): + ''' don't load a million editions that have no metadata ''' + # an isbn, we love to see it + if edition_data.get('isbn_13') or edition_data.get('isbn_10'): + print(edition_data.get('isbn_10')) + return False + # grudgingly, oclc can stay + if edition_data.get('oclc_numbers'): + print(edition_data.get('oclc_numbers')) + return False + # if it has a cover it can stay + if edition_data.get('covers'): + print(edition_data.get('covers')) + return False + # keep non-english editions + if edition_data.get('languages') and \ + 'languages/eng' not in str(edition_data.get('languages')): + print(edition_data.get('languages')) + return False + return True + + def get_description(description_blob): ''' descriptions can be a string or a dict ''' if isinstance(description_blob, dict): diff --git a/bookwyrm/connectors/self_connector.py b/bookwyrm/connectors/self_connector.py index c5d58a59..f57fbc1c 100644 --- a/bookwyrm/connectors/self_connector.py +++ b/bookwyrm/connectors/self_connector.py @@ -11,7 +11,8 @@ from .abstract_connector import AbstractConnector, SearchResult class Connector(AbstractConnector): ''' instantiate a connector ''' - def search(self, query, min_confidence=0.1): + # pylint: disable=arguments-differ + def search(self, query, min_confidence=0.1, raw=False): ''' search your local database ''' if not query: return [] @@ -22,10 +23,14 @@ class Connector(AbstractConnector): results = search_title_author(query, min_confidence) search_results = [] for result in results: - search_results.append(self.format_search_result(result)) + if raw: + search_results.append(result) + else: + search_results.append(self.format_search_result(result)) if len(search_results) >= 10: break - search_results.sort(key=lambda r: r.confidence, reverse=True) + if not raw: + search_results.sort(key=lambda r: r.confidence, reverse=True) return search_results diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index c8212794..ba2fcc8c 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -206,3 +206,9 @@ class SiteForm(CustomForm): class Meta: model = models.SiteSettings exclude = [] + + +class ListForm(CustomForm): + class Meta: + model = models.List + fields = ['user', 'name', 'description', 'curation', 'privacy'] diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index 1e42d32a..103b24fc 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -47,12 +47,20 @@ def shared_inbox(request): return HttpResponse() return HttpResponse(status=401) + # if this isn't a file ripe for refactor, I don't know what is. handlers = { 'Follow': handle_follow, 'Accept': handle_follow_accept, 'Reject': handle_follow_reject, 'Block': handle_block, - 'Create': handle_create, + 'Create': { + 'BookList': handle_create_list, + 'Note': handle_create_status, + 'Article': handle_create_status, + 'Review': handle_create_status, + 'Comment': handle_create_status, + 'Quotation': handle_create_status, + }, 'Delete': handle_delete_status, 'Like': handle_favorite, 'Announce': handle_boost, @@ -69,6 +77,7 @@ def shared_inbox(request): 'Person': handle_update_user, 'Edition': handle_update_edition, 'Work': handle_update_work, + 'BookList': handle_update_list, }, } activity_type = activity['type'] @@ -204,7 +213,25 @@ def handle_unblock(activity): @app.task -def handle_create(activity): +def handle_create_list(activity): + ''' a new list ''' + activity = activity['object'] + activitypub.BookList(**activity).to_model(models.List) + + +@app.task +def handle_update_list(activity): + ''' update a list ''' + try: + book_list = models.List.objects.get(id=activity['object']['id']) + except models.List.DoesNotExist: + return + activitypub.BookList( + **activity['object']).to_model(models.List, instance=book_list) + + +@app.task +def handle_create_status(activity): ''' someone did something, good on them ''' # deduplicate incoming activities activity = activity['object'] diff --git a/bookwyrm/management/commands/remove_editions.py b/bookwyrm/management/commands/remove_editions.py new file mode 100644 index 00000000..c5153f44 --- /dev/null +++ b/bookwyrm/management/commands/remove_editions.py @@ -0,0 +1,34 @@ +''' PROCEED WITH CAUTION: this permanently deletes book data ''' +from django.core.management.base import BaseCommand +from django.db.models import Count, Q +from bookwyrm import models + + +def remove_editions(): + ''' combine duplicate editions and update related models ''' + # not in use + filters = {'%s__isnull' % r.name: True \ + for r in models.Edition._meta.related_objects} + # no cover, no identifying fields + filters['cover'] = '' + null_fields = {'%s__isnull' % f: True for f in \ + ['isbn_10', 'isbn_13', 'oclc_number']} + + editions = models.Edition.objects.filter( + Q(languages=[]) | Q(languages__contains=['English']), + **filters, **null_fields + ).annotate(Count('parent_work__editions')).filter( + # mustn't be the only edition for the work + parent_work__editions__count__gt=1 + ) + print(editions.count()) + editions.delete() + + +class Command(BaseCommand): + ''' dedplucate allllll the book data models ''' + help = 'merges duplicate book data' + # pylint: disable=no-self-use,unused-argument + def handle(self, *args, **options): + ''' run deudplications ''' + remove_editions() diff --git a/bookwyrm/migrations/0041_auto_20210131_1614.py b/bookwyrm/migrations/0041_auto_20210131_1614.py new file mode 100644 index 00000000..8deb69a8 --- /dev/null +++ b/bookwyrm/migrations/0041_auto_20210131_1614.py @@ -0,0 +1,65 @@ +# Generated by Django 3.0.7 on 2021-01-31 16:14 + +import bookwyrm.models.base_model +import bookwyrm.models.fields +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0040_auto_20210122_0057'), + ] + + operations = [ + migrations.CreateModel( + name='List', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_date', models.DateTimeField(auto_now_add=True)), + ('updated_date', models.DateTimeField(auto_now=True)), + ('remote_id', bookwyrm.models.fields.RemoteIdField(max_length=255, null=True, validators=[bookwyrm.models.fields.validate_remote_id])), + ('name', bookwyrm.models.fields.CharField(max_length=100)), + ('description', bookwyrm.models.fields.TextField(blank=True, null=True)), + ('privacy', bookwyrm.models.fields.CharField(choices=[('public', 'Public'), ('unlisted', 'Unlisted'), ('followers', 'Followers'), ('direct', 'Direct')], default='public', max_length=255)), + ('curation', bookwyrm.models.fields.CharField(choices=[('closed', 'Closed'), ('open', 'Open'), ('curated', 'Curated')], default='closed', max_length=255)), + ], + options={ + 'abstract': False, + }, + bases=(bookwyrm.models.base_model.OrderedCollectionMixin, models.Model), + ), + migrations.CreateModel( + name='ListItem', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_date', models.DateTimeField(auto_now_add=True)), + ('updated_date', models.DateTimeField(auto_now=True)), + ('remote_id', bookwyrm.models.fields.RemoteIdField(max_length=255, null=True, validators=[bookwyrm.models.fields.validate_remote_id])), + ('notes', bookwyrm.models.fields.TextField(blank=True, null=True)), + ('approved', models.BooleanField(default=True)), + ('order', bookwyrm.models.fields.IntegerField(blank=True, null=True)), + ('added_by', bookwyrm.models.fields.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ('book', bookwyrm.models.fields.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='bookwyrm.Edition')), + ('book_list', bookwyrm.models.fields.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.List')), + ('endorsement', models.ManyToManyField(related_name='endorsers', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'ordering': ('-created_date',), + 'unique_together': {('book', 'book_list')}, + }, + bases=(bookwyrm.models.base_model.ActivitypubMixin, models.Model), + ), + migrations.AddField( + model_name='list', + name='books', + field=models.ManyToManyField(through='bookwyrm.ListItem', to='bookwyrm.Edition'), + ), + migrations.AddField( + model_name='list', + name='user', + field=bookwyrm.models.fields.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/bookwyrm/migrations/0042_auto_20210201_2108.py b/bookwyrm/migrations/0042_auto_20210201_2108.py new file mode 100644 index 00000000..95a144de --- /dev/null +++ b/bookwyrm/migrations/0042_auto_20210201_2108.py @@ -0,0 +1,28 @@ +# Generated by Django 3.0.7 on 2021-02-01 21:08 + +import bookwyrm.models.fields +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0041_auto_20210131_1614'), + ] + + operations = [ + migrations.AlterModelOptions( + name='list', + options={'ordering': ('-updated_date',)}, + ), + migrations.AlterField( + model_name='list', + name='privacy', + field=bookwyrm.models.fields.PrivacyField(choices=[('public', 'Public'), ('unlisted', 'Unlisted'), ('followers', 'Followers'), ('direct', 'Direct')], default='public', max_length=255), + ), + migrations.AlterField( + model_name='shelf', + name='privacy', + field=bookwyrm.models.fields.PrivacyField(choices=[('public', 'Public'), ('unlisted', 'Unlisted'), ('followers', 'Followers'), ('direct', 'Direct')], default='public', max_length=255), + ), + ] diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index b232e98f..0aef6385 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -7,6 +7,7 @@ from .author import Author from .connector import Connector from .shelf import Shelf, ShelfBook +from .list import List, ListItem from .status import Status, GeneratedNote, Review, Comment, Quotation from .status import Boost diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index 3f1ed66d..ba0a54be 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -140,20 +140,7 @@ class ActivitypubMixin: def to_activity(self): ''' convert from a model to an activity ''' - activity = {} - for field in self.activity_fields: - field.set_activity_from_field(activity, self) - - if hasattr(self, 'serialize_reverse_fields'): - # for example, editions of a work - for model_field_name, activity_field_name, sort_field in \ - self.serialize_reverse_fields: - related_field = getattr(self, model_field_name) - activity[activity_field_name] = \ - unfurl_related_field(related_field, sort_field) - - if not activity.get('id'): - activity['id'] = self.get_remote_id() + activity = generate_activity(self) return self.activity_serializer(**activity).serialize() @@ -161,16 +148,18 @@ class ActivitypubMixin: ''' returns the object wrapped in a Create activity ''' activity_object = self.to_activity(**kwargs) - signer = pkcs1_15.new(RSA.import_key(user.key_pair.private_key)) - content = activity_object['content'] - signed_message = signer.sign(SHA256.new(content.encode('utf8'))) + signature = None create_id = self.remote_id + '/activity' + if 'content' in activity_object: + signer = pkcs1_15.new(RSA.import_key(user.key_pair.private_key)) + content = activity_object['content'] + signed_message = signer.sign(SHA256.new(content.encode('utf8'))) - signature = activitypub.Signature( - creator='%s#main-key' % user.remote_id, - created=activity_object['published'], - signatureValue=b64encode(signed_message).decode('utf8') - ) + signature = activitypub.Signature( + creator='%s#main-key' % user.remote_id, + created=activity_object['published'], + signatureValue=b64encode(signed_message).decode('utf8') + ) return activitypub.Create( id=create_id, @@ -223,7 +212,7 @@ class OrderedCollectionPageMixin(ActivitypubMixin): def to_ordered_collection(self, queryset, \ - remote_id=None, page=False, **kwargs): + remote_id=None, page=False, collection_only=False, **kwargs): ''' an ordered collection of whatevers ''' if not queryset.ordered: raise RuntimeError('queryset must be ordered') @@ -232,18 +221,25 @@ class OrderedCollectionPageMixin(ActivitypubMixin): if page: return to_ordered_collection_page( queryset, remote_id, **kwargs) - name = self.name if hasattr(self, 'name') else None - owner = self.user.remote_id if hasattr(self, 'user') else '' + + if collection_only or not hasattr(self, 'activity_serializer'): + serializer = activitypub.OrderedCollection + activity = {} + else: + serializer = self.activity_serializer + # a dict from the model fields + activity = generate_activity(self) + + if remote_id: + activity['id'] = remote_id paginated = Paginator(queryset, PAGE_LENGTH) - return activitypub.OrderedCollection( - id=remote_id, - totalItems=paginated.count, - name=name, - owner=owner, - first='%s?page=1' % remote_id, - last='%s?page=%d' % (remote_id, paginated.num_pages) - ).serialize() + # add computed fields specific to orderd collections + activity['totalItems'] = paginated.count + activity['first'] = '%s?page=1' % remote_id + activity['last'] = '%s?page=%d' % (remote_id, paginated.num_pages) + + return serializer(**activity).serialize() # pylint: disable=unused-argument @@ -285,3 +281,22 @@ class OrderedCollectionMixin(OrderedCollectionPageMixin): def to_activity(self, **kwargs): ''' an ordered collection of the specified model queryset ''' return self.to_ordered_collection(self.collection_queryset, **kwargs) + + +def generate_activity(obj): + ''' go through the fields on an object ''' + activity = {} + for field in obj.activity_fields: + field.set_activity_from_field(activity, obj) + + if hasattr(obj, 'serialize_reverse_fields'): + # for example, editions of a work + for model_field_name, activity_field_name, sort_field in \ + obj.serialize_reverse_fields: + related_field = getattr(obj, model_field_name) + activity[activity_field_name] = \ + unfurl_related_field(related_field, sort_field) + + if not activity.get('id'): + activity['id'] = obj.get_remote_id() + return activity diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index c6571ff4..bc10156b 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -213,7 +213,10 @@ class PrivacyField(ActivitypubFieldMixin, models.CharField): setattr(instance, self.name, 'followers') def set_activity_from_field(self, activity, instance): - mentions = [u.remote_id for u in instance.mention_users.all()] + # explicitly to anyone mentioned (statuses only) + mentions = [] + if hasattr(instance, 'mention_users'): + mentions = [u.remote_id for u in instance.mention_users.all()] # this is a link to the followers list followers = instance.user.__class__._meta.get_field('followers')\ .field_to_activity(instance.user.followers) diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py new file mode 100644 index 00000000..9298920f --- /dev/null +++ b/bookwyrm/models/list.py @@ -0,0 +1,93 @@ +''' make a list of books!! ''' +from django.db import models + +from bookwyrm import activitypub +from bookwyrm.settings import DOMAIN +from .base_model import ActivitypubMixin, BookWyrmModel +from .base_model import OrderedCollectionMixin +from . import fields + + +CurationType = models.TextChoices('Curation', [ + 'closed', + 'open', + 'curated', +]) + +class List(OrderedCollectionMixin, BookWyrmModel): + ''' a list of books ''' + name = fields.CharField(max_length=100) + user = fields.ForeignKey( + 'User', on_delete=models.PROTECT, activitypub_field='owner') + description = fields.TextField( + blank=True, null=True, activitypub_field='summary') + privacy = fields.PrivacyField() + curation = fields.CharField( + max_length=255, + default='closed', + choices=CurationType.choices + ) + books = models.ManyToManyField( + 'Edition', + symmetrical=False, + through='ListItem', + through_fields=('book_list', 'book'), + ) + activity_serializer = activitypub.BookList + + def get_remote_id(self): + ''' don't want the user to be in there in this case ''' + return 'https://%s/list/%d' % (DOMAIN, self.id) + + @property + def collection_queryset(self): + ''' list of books for this shelf, overrides OrderedCollectionMixin ''' + return self.books.filter( + listitem__approved=True + ).all().order_by('listitem') + + class Meta: + ''' default sorting ''' + ordering = ('-updated_date',) + + +class ListItem(ActivitypubMixin, BookWyrmModel): + ''' ok ''' + book = fields.ForeignKey( + 'Edition', on_delete=models.PROTECT, activitypub_field='object') + book_list = fields.ForeignKey( + 'List', on_delete=models.CASCADE, activitypub_field='target') + added_by = fields.ForeignKey( + 'User', + on_delete=models.PROTECT, + activitypub_field='actor' + ) + notes = fields.TextField(blank=True, null=True) + approved = models.BooleanField(default=True) + order = fields.IntegerField(blank=True, null=True) + endorsement = models.ManyToManyField('User', related_name='endorsers') + + activity_serializer = activitypub.AddBook + + def to_add_activity(self, user): + ''' AP for shelving a book''' + return activitypub.Add( + id='%s#add' % self.remote_id, + actor=user.remote_id, + object=self.book.to_activity(), + target=self.book_list.remote_id, + ).serialize() + + def to_remove_activity(self, user): + ''' AP for un-shelving a book''' + return activitypub.Remove( + id='%s#remove' % self.remote_id, + actor=user.remote_id, + object=self.book.to_activity(), + target=self.book_list.remote_id + ).serialize() + + class Meta: + ''' an opinionated constraint! you can't put a book on a list twice ''' + unique_together = ('book', 'book_list') + ordering = ('-created_date',) diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index 84575137..ff5660dd 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -15,11 +15,7 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel): user = fields.ForeignKey( 'User', on_delete=models.PROTECT, activitypub_field='owner') editable = models.BooleanField(default=True) - privacy = fields.CharField( - max_length=255, - default='public', - choices=fields.PrivacyLevels.choices - ) + privacy = fields.PrivacyField() books = models.ManyToManyField( 'Edition', symmetrical=False, @@ -27,6 +23,8 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel): through_fields=('shelf', 'book') ) + activity_serializer = activitypub.Shelf + def save(self, *args, **kwargs): ''' set the identifier ''' saved = super().save(*args, **kwargs) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index dad65974..093dd773 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -94,6 +94,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel): return self.to_ordered_collection( self.replies(self), remote_id='%s/replies' % self.remote_id, + collection_only=True, **kwargs ) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 79d11206..3fd0eaf7 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -131,7 +131,7 @@ class User(OrderedCollectionPageMixin, AbstractUser): privacy__in=['public', 'unlisted'], ).select_subclasses().order_by('-published_date') return self.to_ordered_collection(queryset, \ - remote_id=self.outbox, **kwargs) + collection_only=True, remote_id=self.outbox, **kwargs) def to_following_activity(self, **kwargs): ''' activitypub following list ''' @@ -266,6 +266,7 @@ class AnnualGoal(BookWyrmModel): @property def progress_percent(self): + ''' how close to your goal, in percent form ''' return int(float(self.book_count / self.goal) * 100) diff --git a/bookwyrm/static/css/fonts/icomoon.eot b/bookwyrm/static/css/fonts/icomoon.eot index 48bd3f62..566fb13d 100644 Binary files a/bookwyrm/static/css/fonts/icomoon.eot and b/bookwyrm/static/css/fonts/icomoon.eot differ diff --git a/bookwyrm/static/css/fonts/icomoon.svg b/bookwyrm/static/css/fonts/icomoon.svg index 00ee337f..6be97327 100644 --- a/bookwyrm/static/css/fonts/icomoon.svg +++ b/bookwyrm/static/css/fonts/icomoon.svg @@ -36,6 +36,10 @@ + + + + diff --git a/bookwyrm/static/css/fonts/icomoon.ttf b/bookwyrm/static/css/fonts/icomoon.ttf index 6abaa591..55df6418 100644 Binary files a/bookwyrm/static/css/fonts/icomoon.ttf and b/bookwyrm/static/css/fonts/icomoon.ttf differ diff --git a/bookwyrm/static/css/fonts/icomoon.woff b/bookwyrm/static/css/fonts/icomoon.woff index 2b8d3330..fa53e8cf 100644 Binary files a/bookwyrm/static/css/fonts/icomoon.woff and b/bookwyrm/static/css/fonts/icomoon.woff differ diff --git a/bookwyrm/static/css/icons.css b/bookwyrm/static/css/icons.css index 8f1f4e90..c84446af 100644 --- a/bookwyrm/static/css/icons.css +++ b/bookwyrm/static/css/icons.css @@ -1,10 +1,10 @@ @font-face { font-family: 'icomoon'; - src: url('fonts/icomoon.eot?uh765c'); - src: url('fonts/icomoon.eot?uh765c#iefix') format('embedded-opentype'), - url('fonts/icomoon.ttf?uh765c') format('truetype'), - url('fonts/icomoon.woff?uh765c') format('woff'), - url('fonts/icomoon.svg?uh765c#icomoon') format('svg'); + src: url('fonts/icomoon.eot?n5x55'); + src: url('fonts/icomoon.eot?n5x55#iefix') format('embedded-opentype'), + url('fonts/icomoon.ttf?n5x55') format('truetype'), + url('fonts/icomoon.woff?n5x55') format('woff'), + url('fonts/icomoon.svg?n5x55#icomoon') format('svg'); font-weight: normal; font-style: normal; font-display: block; @@ -25,7 +25,16 @@ -moz-osx-font-smoothing: grayscale; } -.icon-sparkle:before { +.icon-graphic-heart:before { + content: "\e91e"; +} +.icon-graphic-paperplane:before { + content: "\e91f"; +} +.icon-graphic-banknote:before { + content: "\e920"; +} +.icon-stars:before { content: "\e91a"; } .icon-warning:before { @@ -37,6 +46,9 @@ .icon-bookmark:before { content: "\e91c"; } +.icon-rss:before { + content: "\e91d"; +} .icon-envelope:before { content: "\e901"; } diff --git a/bookwyrm/static/js/shared.js b/bookwyrm/static/js/shared.js index c2fa8b54..758b76dc 100644 --- a/bookwyrm/static/js/shared.js +++ b/bookwyrm/static/js/shared.js @@ -46,13 +46,14 @@ function back(e) { history.back(); } -function polling(el) { - let delay = 10000 + (Math.random() * 1000); +function polling(el, delay) { + delay = delay || 10000; + delay += (Math.random() * 1000); setTimeout(function() { fetch('/api/updates/' + el.getAttribute('data-poll')) .then(response => response.json()) .then(data => updateCountElement(el, data)); - polling(el); + polling(el, delay * 1.25); }, delay, el); } diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index f24f8f2c..1d8b5d35 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -147,7 +147,7 @@ {% endfor %} - {% if readthroughs.exists %} + {% if request.user.is_authenticated %}

Your reading activity

@@ -155,6 +155,9 @@ {% include 'snippets/toggle/open_button.html' with text="Add read dates" icon="plus" class="is-small" controls_text="add-readthrough" %}
+ {% if not readthroughs.exists %} +

You don't have any reading activity for this book.

+ {% endif %}