code cleanup

This commit is contained in:
Mouse Reeve 2020-11-01 08:54:10 -08:00
parent 4684a83e6f
commit c334451216
3 changed files with 26 additions and 24 deletions

View file

@ -59,6 +59,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
@property @property
def ap_tag(self): def ap_tag(self):
''' books or (eventually) users tagged in a post '''
tags = [] tags = []
for book in self.mention_books.all(): for book in self.mention_books.all():
tags.append(activitypub.Link( tags.append(activitypub.Link(
@ -117,7 +118,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
**kwargs **kwargs
) )
def to_activity(self, **kwargs): def to_activity(self, pure=False):
''' return tombstone if the status is deleted ''' ''' return tombstone if the status is deleted '''
if self.deleted: if self.deleted:
return activitypub.Tombstone( return activitypub.Tombstone(
@ -126,7 +127,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
deleted=self.deleted_date.isoformat(), deleted=self.deleted_date.isoformat(),
published=self.deleted_date.isoformat() published=self.deleted_date.isoformat()
).serialize() ).serialize()
return ActivitypubMixin.to_activity(self, **kwargs) return ActivitypubMixin.to_activity(self, pure=pure)
class GeneratedNote(Status): class GeneratedNote(Status):

View file

@ -167,28 +167,29 @@ class User(OrderedCollectionPageMixin, AbstractUser):
return activity_object return activity_object
@receiver(models.signals.pre_save, sender=User) def save(self, *args, **kwargs):
def execute_before_save(sender, instance, *args, **kwargs): ''' populate fields for new local users '''
''' populate fields for new local users ''' # this user already exists, no need to populate fields
# this user already exists, no need to poplate fields if self.id:
if instance.id: return
return if not self.local:
if not instance.local: # generate a username that uses the domain (webfinger format)
# we need to generate a username that uses the domain (webfinger format) actor_parts = urlparse(self.remote_id)
actor_parts = urlparse(instance.remote_id) self.username = '%s@%s' % (self.username, actor_parts.netloc)
instance.username = '%s@%s' % (instance.username, actor_parts.netloc) return
return
# populate fields for local users # populate fields for local users
instance.remote_id = 'https://%s/user/%s' % (DOMAIN, instance.username) self.remote_id = 'https://%s/user/%s' % (DOMAIN, self.username)
instance.localname = instance.username self.localname = self.username
instance.username = '%s@%s' % (instance.username, DOMAIN) self.username = '%s@%s' % (self.username, DOMAIN)
instance.actor = instance.remote_id self.actor = self.remote_id
instance.inbox = '%s/inbox' % instance.remote_id self.inbox = '%s/inbox' % self.remote_id
instance.shared_inbox = 'https://%s/inbox' % DOMAIN self.shared_inbox = 'https://%s/inbox' % DOMAIN
instance.outbox = '%s/outbox' % instance.remote_id self.outbox = '%s/outbox' % self.remote_id
if not instance.private_key: if not self.private_key:
instance.private_key, instance.public_key = create_key_pair() self.private_key, self.public_key = create_key_pair()
super().save(*args, **kwargs)
@receiver(models.signals.post_save, sender=User) @receiver(models.signals.post_save, sender=User)

View file

@ -17,7 +17,7 @@ status_types = [
'comment', 'comment',
'quotation', 'quotation',
'boost', 'boost',
'generatedstatus' 'generatednote'
] ]
status_path = r'%s/(%s)/(?P<status_id>\d+)' % \ status_path = r'%s/(%s)/(?P<status_id>\d+)' % \
(local_user_path, '|'.join(status_types)) (local_user_path, '|'.join(status_types))