kopia lustrzana https://gitlab.com/marnanel/chapeau
Side-effects for "Follow" work properly now.
rodzic
77d02c0317
commit
acf2ff5b04
|
@ -31,72 +31,78 @@ class Following(models.Model):
|
|||
)
|
||||
return result
|
||||
|
||||
def _get_follow(follower, following):
|
||||
try:
|
||||
return Following.objects.get(follower=follower, following=following)
|
||||
except Following.DoesNotExist:
|
||||
return None
|
||||
@classmethod
|
||||
def _get_follow(cls, follower, following):
|
||||
try:
|
||||
return Following.objects.get(follower=follower, following=following)
|
||||
except Following.DoesNotExist:
|
||||
return None
|
||||
|
||||
def request(follower, following):
|
||||
@classmethod
|
||||
def make_request(cls, follower, following):
|
||||
|
||||
f = _get_follow(follower, following)
|
||||
f = cls._get_follow(follower, following)
|
||||
|
||||
if f is not None:
|
||||
if f is not None:
|
||||
|
||||
logger.warn('follow request failed; %s already exists',
|
||||
f)
|
||||
logger.warn('follow request failed; %s already exists',
|
||||
f)
|
||||
|
||||
if f.pending:
|
||||
raise ValueError('%s has already requested to follow %s',
|
||||
follower, following)
|
||||
else:
|
||||
raise ValueError('%s is already following %s',
|
||||
follower, following)
|
||||
|
||||
result = Following(
|
||||
follower = follower,
|
||||
following = following,
|
||||
pending = True,
|
||||
)
|
||||
result.save()
|
||||
|
||||
logger.info('%s has requested to follow %s',
|
||||
follower, following)
|
||||
|
||||
def accept(follower, following):
|
||||
|
||||
result = _get_follow(follower, following)
|
||||
|
||||
if result is None:
|
||||
|
||||
logger.warn('accepting follow request that we didn\'t know about')
|
||||
if f.pending:
|
||||
raise ValueError('%s has already requested to follow %s',
|
||||
follower, following)
|
||||
else:
|
||||
raise ValueError('%s is already following %s',
|
||||
follower, following)
|
||||
|
||||
result = Following(
|
||||
follower = follower,
|
||||
following = following,
|
||||
pending = False,
|
||||
pending = True,
|
||||
)
|
||||
else:
|
||||
result.pending = False
|
||||
result.save()
|
||||
|
||||
result.save()
|
||||
logger.info('%s has requested to follow %s',
|
||||
follower, following)
|
||||
|
||||
logger.info('%s has started to follow %s: %s',
|
||||
follower, following, result)
|
||||
@classmethod
|
||||
def accept_request(cls, follower, following,
|
||||
warn_on_unknown = True):
|
||||
|
||||
return result
|
||||
result = cls._get_follow(follower, following)
|
||||
|
||||
def reject(follower, following):
|
||||
if result is None:
|
||||
|
||||
f = _get_follow(follower, following)
|
||||
if warn_on_unknown:
|
||||
logger.warn('accepting follow request that we didn\'t know about')
|
||||
|
||||
if f is None:
|
||||
logger.warn('rejecting follow request; '+
|
||||
'that\'s fine because we didn\'t know about it')
|
||||
else:
|
||||
f.delete()
|
||||
result = cls(
|
||||
follower = follower,
|
||||
following = following,
|
||||
pending = False,
|
||||
)
|
||||
else:
|
||||
result.pending = False
|
||||
|
||||
logger.info('%s was rejected as a follower of %s',
|
||||
follower, following)
|
||||
result.save()
|
||||
|
||||
logger.info('%s has started to follow %s: %s',
|
||||
follower, following, result)
|
||||
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def reject_request(cls, follower, following):
|
||||
|
||||
f = cls._get_follow(follower, following)
|
||||
|
||||
if f is None:
|
||||
logger.warn('rejecting follow request; '+
|
||||
'that\'s fine because we didn\'t know about it')
|
||||
else:
|
||||
f.delete()
|
||||
|
||||
logger.info('%s was rejected as a follower of %s',
|
||||
follower, following)
|
||||
|
||||
|
||||
|
|
|
@ -26,20 +26,23 @@ def accept(activity):
|
|||
|
||||
def follow(activity):
|
||||
|
||||
from django_kepi.models.following import Following
|
||||
from django_kepi.create import create
|
||||
|
||||
local_user = find(activity['object'], local_only=True)
|
||||
remote_user = find(activity['actor'])
|
||||
|
||||
if local_user is not None and local_user.auto_follow:
|
||||
logger.info('Local user %s has auto_follow set; must Accept',
|
||||
local_user)
|
||||
django_kepi.models.following.accept(
|
||||
|
||||
Following.accept_request(
|
||||
follower = activity['actor'],
|
||||
following = activity['object'],
|
||||
# XXX this causes a warning; add param to disable it
|
||||
warn_on_unknown = False,
|
||||
)
|
||||
|
||||
from django_kepi.create import kepi_create
|
||||
accept_the_request = kepi_create(
|
||||
accept_the_request = create(
|
||||
f_to = remote_user.url,
|
||||
f_type = 'Accept',
|
||||
f_actor = activity['object'],
|
||||
|
@ -50,7 +53,10 @@ def follow(activity):
|
|||
deliver(accept_the_request.number)
|
||||
|
||||
else:
|
||||
django_kepi.models.Following.request(
|
||||
logger.info('Local user %s does not have auto_follow set.',
|
||||
local_user)
|
||||
|
||||
Following.make_request(
|
||||
follower = activity['actor'],
|
||||
following = activity['object'],
|
||||
)
|
||||
|
@ -58,6 +64,9 @@ def follow(activity):
|
|||
return True
|
||||
|
||||
def reject(activity):
|
||||
|
||||
from django_kepi.models.following import Following
|
||||
|
||||
obj = activity['object__obj']
|
||||
|
||||
if obj['type']!='Follow':
|
||||
|
@ -67,7 +76,7 @@ def reject(activity):
|
|||
|
||||
logger.debug(' -- follow rejected')
|
||||
|
||||
django_kepi.models.following.reject(
|
||||
Following.reject_request(
|
||||
follower = obj['actor'],
|
||||
following = activity['actor'],
|
||||
)
|
||||
|
@ -75,6 +84,7 @@ def reject(activity):
|
|||
return True
|
||||
|
||||
def create(activity):
|
||||
|
||||
from django_kepi.create import create as kepi_create
|
||||
|
||||
raw_material = dict([('f_'+f, v)
|
||||
|
|
|
@ -6,6 +6,7 @@ from django_kepi.models.audience import Audience, AUDIENCE_FIELD_NAMES
|
|||
from django_kepi.models.mention import Mention
|
||||
from django_kepi.models.item import Item
|
||||
from django_kepi.models.thing import Thing
|
||||
from django_kepi.models.following import Following
|
||||
from django_kepi.models.activity import Activity
|
||||
from django.test import Client
|
||||
from urllib.parse import urlparse
|
||||
|
@ -27,6 +28,7 @@ INBOX_HOST = 'altair.example.com'
|
|||
INBOX_PATH = '/users/alice/inbox'
|
||||
|
||||
BOB_ID = 'https://bobs-computer.example.net/users/bob'
|
||||
BOB_INBOX_URL = 'https://bobs-computer.example.net/users/bob/inbox'
|
||||
|
||||
# as given in https://www.w3.org/TR/activitypub/
|
||||
OBJECT_FORM = {
|
||||
|
@ -48,10 +50,8 @@ logger = logging.getLogger(name='django_kepi')
|
|||
|
||||
class TestInbox2(TestCase):
|
||||
|
||||
@httpretty.activate
|
||||
def _send(self,
|
||||
content,
|
||||
keys = None,
|
||||
recipient = None,
|
||||
recipientKeys = None,
|
||||
sender = None,
|
||||
|
@ -121,3 +121,53 @@ class TestInbox2(TestCase):
|
|||
self.assertEqual(
|
||||
len(items),
|
||||
1)
|
||||
|
||||
@httpretty.activate
|
||||
def test_follow(self):
|
||||
|
||||
alice_keys = json.load(open('tests/keys/keys-0001.json', 'r'))
|
||||
bob_keys = json.load(open('tests/keys/keys-0002.json', 'r'))
|
||||
|
||||
create_local_person(
|
||||
name = 'alice',
|
||||
publicKey = alice_keys['public'],
|
||||
privateKey = alice_keys['private'],
|
||||
)
|
||||
|
||||
create_remote_person(
|
||||
url = BOB_ID,
|
||||
name = 'bob',
|
||||
publicKey=bob_keys['public'],
|
||||
inbox=BOB_INBOX_URL,
|
||||
sharedInbox=None,
|
||||
)
|
||||
|
||||
httpretty.register_uri(
|
||||
httpretty.POST,
|
||||
BOB_INBOX_URL,
|
||||
status=200,
|
||||
body='Thank you!',
|
||||
)
|
||||
|
||||
self._send(
|
||||
content = {
|
||||
'type': 'Follow',
|
||||
'object': ALICE_ID,
|
||||
'actor': BOB_ID,
|
||||
},
|
||||
recipient = ALICE_ID,
|
||||
recipientKeys = alice_keys,
|
||||
sender = BOB_ID,
|
||||
senderKeys = bob_keys,
|
||||
)
|
||||
|
||||
self.assertDictContainsSubset(
|
||||
subset = {
|
||||
"actor": "https://altair.example.com/users/alice",
|
||||
"to": [
|
||||
"https://bobs-computer.example.net/users/bob"
|
||||
],
|
||||
"type": "Accept"
|
||||
},
|
||||
dictionary = json.loads(httpretty.last_request().body),
|
||||
msg='Acceptance of follow request matched')
|
||||
|
|
Ładowanie…
Reference in New Issue