bookwyrm/fedireads/models.py

242 lines
7.9 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-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-27 01:55:02 +00:00
private_key = models.TextField(blank=True, null=True)
2020-01-27 02:49:57 +00:00
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-27 03:50:22 +00:00
actor = models.CharField(max_length=255)
2020-01-28 08:44:51 +00:00
inbox = models.CharField(max_length=255)
2020-01-28 19:13:13 +00:00
shared_inbox = models.CharField(max_length=255)
2020-01-28 08:44:51 +00:00
outbox = models.CharField(max_length=255)
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,
blank=True,
unique=True
)
2020-01-28 06:49:56 +00:00
avatar = models.ImageField(upload_to='uploads/', blank=True, null=True)
2020-01-28 02:47:54 +00:00
# TODO: a field for if non-local users are readers or others
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
if instance.id:
2020-01-28 08:44:51 +00:00
return
# TODO: how do I know this properly???
if not instance.local:
instance.inbox = instance.actor = 'inbox'
instance.outbox = instance.actor = 'outbox'
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-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-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):
if not self.activity_type:
self.activity_type = 'Add'
shelf.fedireads_type = 'Shelve'
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):
if not self.activity_type:
self.activity_type = 'Follow'
super().save(*args, **kwargs)
class Review(Activity):
''' a book review '''
book = models.ForeignKey('Book', on_delete=models.PROTECT)
work = models.ForeignKey('Work', on_delete=models.PROTECT)
name = models.TextField()
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):
if not self.activity_type:
self.activity_type = 'Article'
2020-01-28 19:45:27 +00:00
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):
if not self.activity_type:
self.activity_type = 'Note'
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)
identifier = models.CharField(max_length=255)
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):
''' a non-canonical copy from open library '''
2020-01-27 01:55:02 +00:00
activitypub_id = models.CharField(max_length=255)
2020-01-28 02:47:54 +00:00
openlibrary_key = models.CharField(max_length=255)
2020-01-25 06:32:41 +00:00
data = JSONField()
2020-01-25 21:46:30 +00:00
works = models.ManyToManyField('Work')
2020-01-25 23:25:19 +00:00
authors = models.ManyToManyField('Author')
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 21:46:30 +00:00
class Work(models.Model):
2020-01-26 20:14:27 +00:00
''' encompassses all editions of a book '''
2020-01-28 02:47:54 +00:00
openlibrary_key = models.CharField(max_length=255)
2020-01-25 21:46:30 +00:00
data = JSONField()
added_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
2020-01-25 23:25:19 +00:00
2020-01-28 02:47:54 +00:00
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)