Partial checkin. The "followers" property on RemotePersons and LocalPersons is now handled separately.

On a LocalPerson, our own models are the authoritative source for their followers.
On a RemotePerson, we have to grab the list from the remote server.

I've made some tests for the LocalPerson part, but the RemotePerson part
still needs testing. This has involved creating a test_person module
in trilby, because test_account is for integration tests.
status-serialisers
Marnanel Thurman 2020-09-14 01:06:38 +01:00
rodzic 4530a517ca
commit e3520eee53
2 zmienionych plików z 83 dodań i 6 usunięć

Wyświetl plik

@ -105,12 +105,6 @@ class Person(PolymorphicModel):
# this matches the behaviour of Mastodon
return self.url
@property
def followers(self):
return Person.objects.filter(
rel_following__following = self,
)
@property
def following(self):
return Person.objects.filter(
@ -242,6 +236,48 @@ class RemotePerson(Person):
return None
@property
def followers(self):
from kepi.sombrero_sendpub.fetch import fetch
from kepi.sombrero_sendpub.collections import Collection
class RemotePersonFollowers(object):
def __init__(self, address):
logger.debug(
"Initialising RemotePerson's followers iterator: %s",
address,
)
self.collection = None
self.address = address
def __iter__(self):
self.collection = fetch(
self.address,
Collection,
)
return self
def __next__(self):
url = self.collection.__next__()
logger.debug("Next follower is at %s", url)
person = fetch(
url,
Person,
)
logger.debug(" -- which is %s", person)
return person
result = RemotePersonFollowers(
self.followers_url,
)
return result
########################################
class TrilbyUser(AbstractUser):
@ -476,6 +512,12 @@ class LocalPerson(Person):
return result
@property
def followers(self):
return Person.objects.filter(
rel_following__following = self,
)
def inbox_url(self):
return uri_to_url(settings.KEPI['USER_INBOX_LINK'] % {
'username': self.local_user.username,

Wyświetl plik

@ -0,0 +1,35 @@
# test_person.py
#
# Part of kepi.
# Copyright (c) 2018-2020 Marnanel Thurman.
# Licensed under the GNU Public License v2.
from kepi.trilby_api.tests import *
from unittest import skip
from rest_framework.test import APIClient, force_authenticate
import logging
logger = logging.getLogger(name='kepi')
# This needs expanding into a full unit test.
class TestPerson(TrilbyTestCase):
def test_followers(self):
alice = create_local_person(name='alice')
bob = create_local_person(name='bob')
carol = create_local_person(name='carol')
Follow(follower=bob, following=alice).save()
Follow(follower=carol, following=alice).save()
followers = sorted(list(
[x.url for x in alice.followers]))
self.assertEqual(
followers,
['https://testserver/users/bob', 'https://testserver/users/carol']
)
# FIXME test the "followers" property on a RemotePerson