Merge pull request #286 from mouse-reeve/federation

Federation
This commit is contained in:
Mouse Reeve 2020-11-04 16:32:02 -08:00 committed by GitHub
commit c3e5ac2d14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 23 deletions

View file

@ -76,6 +76,12 @@ class ActivityObject:
if not isinstance(self, model.activity_serializer): if not isinstance(self, model.activity_serializer):
raise TypeError('Wrong activity type for model') raise TypeError('Wrong activity type for model')
# check for an existing instance
try:
return model.objects.get(remote_id=self.id)
except model.DoesNotExist:
pass
model_fields = [m.name for m in model._meta.get_fields()] model_fields = [m.name for m in model._meta.get_fields()]
mapped_fields = {} mapped_fields = {}

View file

@ -8,7 +8,7 @@ from .base_activity import ActivityObject
class Like(ActivityObject): class Like(ActivityObject):
''' a user faving an object ''' ''' a user faving an object '''
actor: str actor: str
object: ActivityObject object: str
type: str = 'Like' type: str = 'Like'
@ -16,5 +16,5 @@ class Like(ActivityObject):
class Boost(ActivityObject): class Boost(ActivityObject):
''' boosting a status ''' ''' boosting a status '''
actor: str actor: str
object: ActivityObject object: str
type: str = 'Announce' type: str = 'Announce'

View file

@ -68,6 +68,7 @@ def shared_inbox(request):
'Undo': { 'Undo': {
'Follow': handle_unfollow, 'Follow': handle_unfollow,
'Like': handle_unfavorite, 'Like': handle_unfavorite,
'Announce': handle_unboost,
}, },
'Update': { 'Update': {
'Person': handle_update_user, 'Person': handle_update_user,
@ -267,48 +268,50 @@ def handle_delete_status(activity):
@app.task @app.task
def handle_favorite(activity): def handle_favorite(activity):
''' approval of your good good post ''' ''' approval of your good good post '''
fav = activitypub.Like(**activity['object']) fav = activitypub.Like(**activity)
# raises ValueError in to_model if a foreign key could not be resolved in
liker = get_or_create_remote_user(activity['actor']) liker = get_or_create_remote_user(activity['actor'])
if liker.local: if liker.local:
return return
status = fav.to_model(models.Favorite) fav = fav.to_model(models.Favorite)
status_builder.create_notification( status_builder.create_notification(
status.user, fav.status.user,
'FAVORITE', 'FAVORITE',
related_user=liker, related_user=liker,
related_status=status, related_status=fav.status,
) )
@app.task @app.task
def handle_unfavorite(activity): def handle_unfavorite(activity):
''' approval of your good good post ''' ''' approval of your good good post '''
like = activitypub.Like(**activity['object']) like = activitypub.Like(**activity['object']).to_model(models.Favorite)
fav = models.Favorite.objects.filter(remote_id=like.id).first() like.delete()
fav.delete()
@app.task @app.task
def handle_boost(activity): def handle_boost(activity):
''' someone gave us a boost! ''' ''' someone gave us a boost! '''
status_id = activity['object'].split('/')[-1] boost = activitypub.Boost(**activity).to_model(models.Boost)
status = models.Status.objects.get(id=status_id)
booster = get_or_create_remote_user(activity['actor'])
if not booster.local: if not boost.user.local:
status_builder.create_boost_from_activity(booster, activity) status_builder.create_notification(
boost.boosted_status.user,
'BOOST',
related_user=boost.user,
related_status=boost.boosted_status,
)
status_builder.create_notification(
status.user, @app.task
'BOOST', def handle_unboost(activity):
related_user=booster, ''' someone gave us a boost! '''
related_status=status, boost = models.Boost.objects.filter(remote_id=activity['object']['id']).first()
) if not boost:
return
status_builder.delete_status(boost)
@app.task @app.task

View file

@ -76,8 +76,8 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
return tags return tags
shared_mappings = [ shared_mappings = [
ActivityMapping('url', 'remote_id', lambda x: None),
ActivityMapping('id', 'remote_id'), ActivityMapping('id', 'remote_id'),
ActivityMapping('url', 'remote_id'),
ActivityMapping('inReplyTo', 'reply_parent'), ActivityMapping('inReplyTo', 'reply_parent'),
ActivityMapping('published', 'published_date'), ActivityMapping('published', 'published_date'),
ActivityMapping('attributedTo', 'user'), ActivityMapping('attributedTo', 'user'),
@ -136,6 +136,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
return ActivitypubMixin.to_activity(self, pure=pure) return ActivitypubMixin.to_activity(self, pure=pure)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
''' update user active time '''
self.user.last_active_date = timezone.now() self.user.last_active_date = timezone.now()
self.user.save() self.user.save()
super().save(*args, **kwargs) super().save(*args, **kwargs)
@ -240,6 +241,7 @@ class Favorite(ActivitypubMixin, BookWyrmModel):
activity_serializer = activitypub.Like activity_serializer = activitypub.Like
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
''' update user active time '''
self.user.last_active_date = timezone.now() self.user.last_active_date = timezone.now()
self.user.save() self.user.save()
super().save(*args, **kwargs) super().save(*args, **kwargs)
@ -285,6 +287,7 @@ class ReadThrough(BookWyrmModel):
null=True) null=True)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
''' update user active time '''
self.user.last_active_date = timezone.now() self.user.last_active_date = timezone.now()
self.user.save() self.user.save()
super().save(*args, **kwargs) super().save(*args, **kwargs)