Unify concept of absolute_id and remote_id

This commit is contained in:
Mouse Reeve
2020-05-12 18:56:28 -07:00
parent 93493fc8e4
commit e76f96eb6c
33 changed files with 263 additions and 236 deletions

View File

@ -1,5 +1,6 @@
''' base model with default fields '''
from django.db import models
from django.dispatch import receiver
from fedireads.settings import DOMAIN
@ -7,18 +8,24 @@ class FedireadsModel(models.Model):
''' fields and functions for every model '''
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
remote_id = models.CharField(max_length=255, null=True)
@property
def absolute_id(self):
''' constructs the absolute reference to any db object '''
if hasattr(self, 'remote_id') and self.remote_id:
return self.remote_id
def get_remote_id(self):
''' generate a url that resolves to the local object '''
base_path = 'https://%s' % DOMAIN
if hasattr(self, 'user'):
base_path = self.user.absolute_id
base_path = self.user.remote_id
model_name = type(self).__name__.lower()
return '%s/%s/%d' % (base_path, model_name, self.id)
class Meta:
abstract = True
@receiver(models.signals.post_save, sender=FedireadsModel)
def execute_after_save(sender, instance, created, *args, **kwargs):
''' set the remote_id after save (when the id is available) '''
if not created:
return
instance.remote_id = instance.get_remote_id()
instance.save()

View File

@ -5,7 +5,7 @@ from model_utils.managers import InheritanceManager
from fedireads import activitypub
from fedireads.settings import DOMAIN
from fedireads.utils.fields import JSONField, ArrayField
from fedireads.utils.fields import ArrayField
from .base_model import FedireadsModel
from fedireads.connectors.settings import CONNECTORS
@ -47,7 +47,6 @@ class Connector(FedireadsModel):
class Book(FedireadsModel):
''' a generic book, which can mean either an edition or a work '''
remote_id = models.CharField(max_length=255, null=True)
# these identifiers apply to both works and editions
openlibrary_key = models.CharField(max_length=255, blank=True, null=True)
librarything_key = models.CharField(max_length=255, blank=True, null=True)
@ -87,20 +86,27 @@ class Book(FedireadsModel):
published_date = models.DateTimeField(blank=True, null=True)
objects = InheritanceManager()
@property
def absolute_id(self):
''' constructs the absolute reference to any db object '''
if self.sync and self.remote_id:
return self.remote_id
base_path = 'https://%s' % DOMAIN
return '%s/book/%d' % (base_path, self.id)
def save(self, *args, **kwargs):
''' can't be abstract for query reasons, but you shouldn't USE it '''
if not isinstance(self, Edition) and not isinstance(self, Work):
raise ValueError('Books should be added as Editions or Works')
super().save(*args, **kwargs)
def get_remote_id(self):
''' editions and works both use "book" instead of model_name '''
return 'https://%s/book/%d' % (DOMAIN, self.id)
@property
def local_id(self):
''' when a book is ingested from an outside source, it becomes local to
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 '''
return 'https://%s/book/%d' % (DOMAIN, self.id)
def __repr__(self):
return "<{} key={!r} title={!r}>".format(
self.__class__,
@ -152,7 +158,6 @@ class Edition(Book):
class Author(FedireadsModel):
''' copy of an author from OL '''
remote_id = models.CharField(max_length=255, null=True)
openlibrary_key = models.CharField(max_length=255, blank=True, null=True)
sync = models.BooleanField(default=True)
last_sync_date = models.DateTimeField(default=timezone.now)
@ -168,6 +173,14 @@ class Author(FedireadsModel):
)
bio = models.TextField(null=True, blank=True)
@property
def local_id(self):
''' when a book is ingested from an outside source, it becomes local to
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)
@property
def activitypub_serialize(self):
return activitypub.get_author(self)

View File

@ -1,7 +1,6 @@
''' puttin' books on shelves '''
from django.db import models
from fedireads import activitypub
from .base_model import FedireadsModel
@ -17,12 +16,10 @@ class Shelf(FedireadsModel):
through_fields=('shelf', 'book')
)
@property
def absolute_id(self):
''' use shelf identifier as absolute id '''
base_path = self.user.absolute_id
model_name = type(self).__name__.lower()
return '%s/%s/%s' % (base_path, model_name, self.identifier)
def get_remote_id(self):
''' shelf identifier instead of id '''
base_path = self.user.remote_id
return '%s/shelf/%s' % (base_path, self.identifier)
class Meta:
unique_together = ('user', 'identifier')

View File

@ -12,7 +12,6 @@ from .base_model import FedireadsModel
class Status(FedireadsModel):
''' any post, like a reply to a review, etc '''
remote_id = models.CharField(max_length=255, unique=True, null=True)
user = models.ForeignKey('User', on_delete=models.PROTECT)
status_type = models.CharField(max_length=255, default='Note')
content = models.TextField(blank=True, null=True)
@ -39,16 +38,6 @@ class Status(FedireadsModel):
)
objects = InheritanceManager()
@property
def absolute_id(self):
''' constructs the absolute reference to any db object '''
if self.remote_id:
return self.remote_id
base_path = self.user.absolute_id
model_name = type(self).__name__.lower()
return '%s/%s/%d' % (base_path, model_name, self.id)
@property
def activitypub_serialize(self):
return activitypub.get_status(self)
@ -111,7 +100,6 @@ class Favorite(FedireadsModel):
''' fav'ing a post '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
status = models.ForeignKey('Status', on_delete=models.PROTECT)
remote_id = models.CharField(max_length=255, unique=True, null=True)
class Meta:
unique_together = ('user', 'status')

View File

@ -15,7 +15,6 @@ class User(AbstractUser):
''' a user who wants to read books '''
private_key = models.TextField(blank=True, null=True)
public_key = models.TextField(blank=True, null=True)
actor = models.CharField(max_length=255, unique=True)
inbox = models.CharField(max_length=255, unique=True)
shared_inbox = models.CharField(max_length=255, blank=True, null=True)
federated_server = models.ForeignKey(
@ -63,17 +62,11 @@ class User(AbstractUser):
through_fields=('user', 'status'),
related_name='favorite_statuses'
)
remote_id = models.CharField(max_length=255, null=True, unique=True)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
manually_approves_followers = models.BooleanField(default=False)
@property
def absolute_id(self):
''' users are identified by their username, so overriding this prop '''
model_name = type(self).__name__.lower()
username = self.localname or self.username
return 'https://%s/%s/%s' % (DOMAIN, model_name, username)
@property
def activitypub_serialize(self):
return activitypub.get_actor(self)
@ -107,10 +100,9 @@ class UserRelationship(FedireadsModel):
)
]
@property
def absolute_id(self):
''' use shelf identifier as absolute id '''
base_path = self.user_subject.absolute_id
def get_remote_id(self):
''' use shelf identifier in remote_id '''
base_path = self.user_subject.remote_id
return '%s#%s/%d' % (base_path, self.status, self.id)
@ -158,12 +150,13 @@ def execute_before_save(sender, instance, *args, **kwargs):
return
# populate fields for local users
instance.remote_id = 'https://%s/user/%s' % (DOMAIN, instance.username)
instance.localname = instance.username
instance.username = '%s@%s' % (instance.username, DOMAIN)
instance.actor = instance.absolute_id
instance.inbox = '%s/inbox' % instance.absolute_id
instance.actor = instance.remote_id
instance.inbox = '%s/inbox' % instance.remote_id
instance.shared_inbox = 'https://%s/inbox' % DOMAIN
instance.outbox = '%s/outbox' % instance.absolute_id
instance.outbox = '%s/outbox' % instance.remote_id
if not instance.private_key:
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)