bookwyrm/fedireads/models.py

242 lines
8.2 KiB
Python
Raw Normal View History

2020-01-25 06:32:41 +00:00
''' database schema for the whole dang thing '''
from django.db import models
2020-01-28 23:23:49 +00:00
from model_utils.managers import InheritanceManager
2020-01-25 23:25:19 +00:00
from django.dispatch import receiver
2020-01-25 06:32:41 +00:00
from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField
2020-01-25 21:46:30 +00:00
from Crypto import Random
2020-01-28 19:45:27 +00:00
from Crypto.PublicKey import RSA
2020-01-27 01:55:02 +00:00
import re
2020-01-25 06:32:41 +00:00
2020-01-28 19:45:27 +00:00
from fedireads.settings import DOMAIN, OL_URL
2020-01-25 06:32:41 +00:00
class User(AbstractUser):
''' a user who wants to read books '''
2020-01-29 20:07:20 +00:00
private_key = models.TextField(blank=True, null=True)
public_key = models.TextField(blank=True, null=True)
2020-01-25 06:32:41 +00:00
api_key = models.CharField(max_length=255, blank=True, null=True)
2020-01-29 03:05:59 +00:00
actor = models.CharField(max_length=255, unique=True)
inbox = models.CharField(max_length=255, unique=True)
2020-02-07 21:39:48 +00:00
shared_inbox = models.CharField(max_length=255, blank=True, null=True)
2020-01-29 19:45:19 +00:00
federated_server = models.ForeignKey(
'FederatedServer',
on_delete=models.PROTECT,
null=True,
)
2020-01-29 03:05:59 +00:00
outbox = models.CharField(max_length=255, unique=True)
2020-01-28 08:44:51 +00:00
summary = models.TextField(blank=True, null=True)
2020-01-26 20:14:27 +00:00
local = models.BooleanField(default=True)
2020-01-28 04:56:45 +00:00
localname = models.CharField(
max_length=255,
null=True,
unique=True
)
2020-01-29 07:23:05 +00:00
# name is your display name, which you can change at will
name = models.CharField(max_length=100, blank=True, null=True)
2020-01-29 08:05:58 +00:00
avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
2020-01-28 02:47:54 +00:00
followers = models.ManyToManyField('self', symmetrical=False)
2020-01-25 21:46:30 +00:00
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
2020-01-26 20:14:27 +00:00
2020-01-28 08:44:51 +00:00
@receiver(models.signals.pre_save, sender=User)
def execute_before_save(sender, instance, *args, **kwargs):
''' create shelves for new users '''
# this user already exists, no need to poplate fields
2020-01-29 20:07:20 +00:00
if instance.id or not instance.local:
return
# populate fields for local users
2020-01-28 08:44:51 +00:00
instance.localname = instance.username
instance.username = '%s@%s' % (instance.username, DOMAIN)
instance.actor = 'https://%s/user/%s' % (DOMAIN, instance.localname)
instance.inbox = 'https://%s/user/%s/inbox' % (DOMAIN, instance.localname)
2020-01-28 19:13:13 +00:00
instance.shared_inbox = 'https://%s/inbox' % DOMAIN
2020-01-28 08:44:51 +00:00
instance.outbox = 'https://%s/user/%s/outbox' % (DOMAIN, instance.localname)
if not instance.private_key:
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
instance.private_key = key.export_key().decode('utf8')
instance.public_key = key.publickey().export_key().decode('utf8')
2020-01-25 06:32:41 +00:00
2020-01-26 20:14:27 +00:00
2020-01-25 23:25:19 +00:00
@receiver(models.signals.post_save, sender=User)
def execute_after_save(sender, instance, created, *args, **kwargs):
2020-01-26 20:14:27 +00:00
''' create shelves for new users '''
# TODO: how are remote users handled? what if they aren't readers?
2020-01-28 02:47:54 +00:00
if not instance.local or not created:
2020-01-25 23:25:19 +00:00
return
2020-01-28 02:47:54 +00:00
2020-01-25 23:25:19 +00:00
shelves = [{
'name': 'To Read',
'type': 'to-read',
}, {
'name': 'Currently Reading',
'type': 'reading',
}, {
'name': 'Read',
'type': 'read',
}]
for shelf in shelves:
2020-01-26 20:14:27 +00:00
Shelf(
name=shelf['name'],
shelf_type=shelf['type'],
user=instance,
editable=False
).save()
2020-01-25 23:25:19 +00:00
2020-01-25 06:32:41 +00:00
2020-01-29 19:45:19 +00:00
class FederatedServer(models.Model):
''' store which server's we federate with '''
server_name = models.CharField(max_length=255, unique=True)
shared_inbox = models.CharField(max_length=255, unique=True)
# federated, blocked, whatever else
status = models.CharField(max_length=255, default='federated')
# is it mastodon, fedireads, etc
application_type = models.CharField(max_length=255, null=True)
2020-01-28 02:47:54 +00:00
class Activity(models.Model):
''' basic fields for storing activities '''
uuid = models.CharField(max_length=255, unique=True)
2020-01-27 04:57:48 +00:00
user = models.ForeignKey('User', on_delete=models.PROTECT)
2020-01-25 06:32:41 +00:00
content = JSONField(max_length=5000)
2020-01-28 19:45:27 +00:00
# the activitypub activity type (Create, Add, Follow, ...)
2020-01-28 02:47:54 +00:00
activity_type = models.CharField(max_length=255)
2020-01-28 19:45:27 +00:00
# custom types internal to fedireads (Review, Shelve, ...)
fedireads_type = models.CharField(max_length=255, blank=True, null=True)
2020-01-25 21:46:30 +00:00
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
2020-01-28 23:23:49 +00:00
objects = InheritanceManager()
2020-01-25 06:32:41 +00:00
2020-01-28 02:47:54 +00:00
class ShelveActivity(Activity):
''' someone put a book on a shelf '''
book = models.ForeignKey('Book', on_delete=models.PROTECT)
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
2020-01-28 19:45:27 +00:00
def save(self, *args, **kwargs):
2020-01-29 08:22:48 +00:00
if not self.activity_type:
self.activity_type = 'Add'
2020-01-28 23:23:49 +00:00
self.fedireads_type = 'Shelve'
2020-01-28 19:45:27 +00:00
super().save(*args, **kwargs)
2020-01-28 02:47:54 +00:00
class FollowActivity(Activity):
''' record follow requests sent out '''
followed = models.ForeignKey(
'User',
related_name='followed',
on_delete=models.PROTECT
)
def save(self, *args, **kwargs):
2020-01-28 23:23:49 +00:00
self.activity_type = 'Follow'
2020-01-28 02:47:54 +00:00
super().save(*args, **kwargs)
class Review(Activity):
''' a book review '''
book = models.ForeignKey('Book', on_delete=models.PROTECT)
2020-01-29 07:23:05 +00:00
name = models.CharField(max_length=255)
2020-01-28 19:45:27 +00:00
# TODO: validation
2020-01-28 02:47:54 +00:00
rating = models.IntegerField(default=0)
review_content = models.TextField()
def save(self, *args, **kwargs):
2020-01-28 23:23:49 +00:00
self.activity_type = 'Article'
self.fedireads_type = 'Review'
2020-01-28 02:47:54 +00:00
super().save(*args, **kwargs)
class Note(Activity):
''' reply to a review, etc '''
def save(self, *args, **kwargs):
2020-01-28 23:23:49 +00:00
self.activity_type = 'Note'
2020-01-28 02:47:54 +00:00
super().save(*args, **kwargs)
2020-01-25 06:32:41 +00:00
class Shelf(models.Model):
2020-01-27 01:55:02 +00:00
activitypub_id = models.CharField(max_length=255)
2020-01-29 03:05:59 +00:00
identifier = models.CharField(max_length=255, unique=True)
2020-01-25 06:32:41 +00:00
name = models.CharField(max_length=100)
user = models.ForeignKey('User', on_delete=models.PROTECT)
editable = models.BooleanField(default=True)
2020-01-25 23:25:19 +00:00
shelf_type = models.CharField(default='custom', max_length=100)
books = models.ManyToManyField(
'Book',
symmetrical=False,
through='ShelfBook',
through_fields=('shelf', 'book')
)
2020-01-25 21:46:30 +00:00
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
2020-01-25 06:32:41 +00:00
2020-01-27 01:55:02 +00:00
class Meta:
unique_together = ('user', 'name')
def save(self, *args, **kwargs):
if not self.identifier:
self.identifier = '%s_%s' % (
2020-01-28 04:56:45 +00:00
self.user.localname,
2020-01-27 01:55:02 +00:00
re.sub(r'\W', '-', self.name).lower()
)
if not self.activitypub_id:
2020-01-28 02:47:54 +00:00
self.activitypub_id = 'https://%s/shelf/%s' % \
(DOMAIN, self.identifier)
2020-01-27 01:55:02 +00:00
super().save(*args, **kwargs)
2020-01-25 06:32:41 +00:00
2020-01-25 23:25:19 +00:00
class ShelfBook(models.Model):
# many to many join table for books and shelves
book = models.ForeignKey('Book', on_delete=models.PROTECT)
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
2020-01-26 20:14:27 +00:00
added_by = models.ForeignKey(
'User',
blank=True,
null=True,
on_delete=models.PROTECT
)
2020-01-25 23:25:19 +00:00
added_date = models.DateTimeField(auto_now_add=True)
2020-01-28 02:47:54 +00:00
2020-01-27 01:55:02 +00:00
class Meta:
unique_together = ('book', 'shelf')
2020-01-25 23:25:19 +00:00
2020-01-25 06:32:41 +00:00
class Book(models.Model):
2020-01-29 09:05:27 +00:00
''' a non-canonical copy of a work (not book) from open library '''
2020-01-27 01:55:02 +00:00
activitypub_id = models.CharField(max_length=255)
2020-01-29 01:23:38 +00:00
openlibrary_key = models.CharField(max_length=255, unique=True)
2020-01-25 06:32:41 +00:00
data = JSONField()
2020-01-25 23:25:19 +00:00
authors = models.ManyToManyField('Author')
2020-02-07 23:11:53 +00:00
# TODO: also store cover thumbnail
2020-01-29 08:05:58 +00:00
cover = models.ImageField(upload_to='covers/', blank=True, null=True)
2020-01-26 20:14:27 +00:00
shelves = models.ManyToManyField(
'Shelf',
symmetrical=False,
through='ShelfBook',
through_fields=('book', 'shelf')
)
added_by = models.ForeignKey(
'User',
blank=True,
null=True,
on_delete=models.PROTECT
)
2020-01-25 21:46:30 +00:00
added_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
2020-01-25 06:32:41 +00:00
2020-01-27 01:55:02 +00:00
def save(self, *args, **kwargs):
2020-01-28 02:47:54 +00:00
self.activitypub_id = '%s%s' % (OL_URL, self.openlibrary_key)
2020-01-27 01:55:02 +00:00
super().save(*args, **kwargs)
2020-01-25 23:25:19 +00:00
class Author(models.Model):
2020-01-28 02:47:54 +00:00
openlibrary_key = models.CharField(max_length=255)
2020-01-25 23:25:19 +00:00
data = JSONField()
added_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)