Update follow and unfollow to use UserFollowRequest & UserFollows.

This commit is contained in:
Adam Kelly 2020-03-11 12:32:50 +00:00
parent e050704c2c
commit b21c5fc98f
4 changed files with 20 additions and 10 deletions

View file

@ -201,10 +201,9 @@ def handle_incoming_follow(activity):
user = get_or_create_remote_user(activity['actor']) user = get_or_create_remote_user(activity['actor'])
# TODO: allow users to manually approve requests # TODO: allow users to manually approve requests
try: try:
models.UserRelationship.objects.create( models.UserFollowRequest.objects.create(
user_subject=user, user_subject=user,
user_object=to_follow, user_object=to_follow,
status='follow_request',
relationship_id=activity['id'] relationship_id=activity['id']
) )
except django.db.utils.IntegrityError: except django.db.utils.IntegrityError:

View file

@ -2,4 +2,4 @@
from .book import Book, Work, Edition, Author from .book import Book, Work, Edition, Author
from .shelf import Shelf, ShelfBook from .shelf import Shelf, ShelfBook
from .status import Status, Review, Favorite, Tag, Notification from .status import Status, Review, Favorite, Tag, Notification
from .user import User, UserRelationship, FederatedServer from .user import User, FederatedServer, UserFollows, UserFollowRequest, UserBlocks

View file

@ -109,6 +109,14 @@ class UserFollows(UserRelationship):
def status(self): def status(self):
return 'follows' return 'follows'
@classmethod
def from_request(cls, follow_request):
return cls(
user_subject=follow_request.user_subject,
user_object=follow_request.user_object,
relationship_id=follow_request.relationship_id,
)
class UserFollowRequest(UserRelationship): class UserFollowRequest(UserRelationship):
@property @property
def status(self): def status(self):

View file

@ -1,5 +1,5 @@
''' handles all the activity coming out of the server ''' ''' handles all the activity coming out of the server '''
from django.db import IntegrityError from django.db import IntegrityError, transaction
from django.http import HttpResponseNotFound, JsonResponse from django.http import HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import requests import requests
@ -88,7 +88,7 @@ def handle_outgoing_follow(user, to_follow):
def handle_outgoing_unfollow(user, to_unfollow): def handle_outgoing_unfollow(user, to_unfollow):
''' someone local wants to follow someone ''' ''' someone local wants to follow someone '''
relationship = models.UserRelationship.objects.get( relationship = models.UserFollows.objects.get(
user_subject=user, user_subject=user,
user_object=to_unfollow user_object=to_unfollow
) )
@ -101,11 +101,14 @@ def handle_outgoing_unfollow(user, to_unfollow):
def handle_outgoing_accept(user, to_follow, request_activity): def handle_outgoing_accept(user, to_follow, request_activity):
''' send an acceptance message to a follow request ''' ''' send an acceptance message to a follow request '''
relationship = models.UserRelationship.objects.get( with transaction.atomic():
follow_request = models.UserFollowRequest.objects.get(
relationship_id=request_activity['id'] relationship_id=request_activity['id']
) )
relationship.status = 'follow' relationship = models.UserFollows.from_request(follow_request)
follow_request.delete()
relationship.save() relationship.save()
activity = activitypub.get_accept(to_follow, request_activity) activity = activitypub.get_accept(to_follow, request_activity)
recipient = get_recipients(to_follow, 'direct', direct_recipients=[user]) recipient = get_recipients(to_follow, 'direct', direct_recipients=[user])
broadcast(to_follow, activity, recipient) broadcast(to_follow, activity, recipient)