Keep track of remote servers

Fixes #32
This commit is contained in:
Mouse Reeve 2020-03-29 15:51:43 -07:00
parent 92790d520f
commit e7d18dada1
4 changed files with 62 additions and 4 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.3 on 2020-03-29 22:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('fedireads', '0023_auto_20200328_2203'),
]
operations = [
migrations.AddField(
model_name='federatedserver',
name='application_version',
field=models.CharField(max_length=255, null=True),
),
]

View file

@ -108,6 +108,7 @@ class UserRelationship(FedireadsModel):
base_path = self.user_subject.absolute_id base_path = self.user_subject.absolute_id
return '%s#%s/%d' % (base_path, self.status, self.id) return '%s#%s/%d' % (base_path, self.status, self.id)
class UserFollows(UserRelationship): class UserFollows(UserRelationship):
@property @property
def status(self): def status(self):
@ -121,16 +122,19 @@ class UserFollows(UserRelationship):
relationship_id=follow_request.relationship_id, relationship_id=follow_request.relationship_id,
) )
class UserFollowRequest(UserRelationship): class UserFollowRequest(UserRelationship):
@property @property
def status(self): def status(self):
return 'follow_request' return 'follow_request'
class UserBlocks(UserRelationship): class UserBlocks(UserRelationship):
@property @property
def status(self): def status(self):
return 'blocks' return 'blocks'
class FederatedServer(FedireadsModel): class FederatedServer(FedireadsModel):
''' store which server's we federate with ''' ''' store which server's we federate with '''
server_name = models.CharField(max_length=255, unique=True) server_name = models.CharField(max_length=255, unique=True)
@ -138,6 +142,7 @@ class FederatedServer(FedireadsModel):
status = models.CharField(max_length=255, default='federated') status = models.CharField(max_length=255, default='federated')
# is it mastodon, fedireads, etc # is it mastodon, fedireads, etc
application_type = models.CharField(max_length=255, null=True) application_type = models.CharField(max_length=255, null=True)
application_version = models.CharField(max_length=255, null=True)
@receiver(models.signals.pre_save, sender=User) @receiver(models.signals.pre_save, sender=User)

View file

@ -13,8 +13,6 @@ def get_or_create_remote_user(actor):
except models.User.DoesNotExist: except models.User.DoesNotExist:
pass pass
# TODO: handle remote server and connector
# load the user's info from the actor url # load the user's info from the actor url
response = requests.get( response = requests.get(
actor, actor,
@ -30,6 +28,9 @@ def get_or_create_remote_user(actor):
shared_inbox = data.get('endpoints').get('sharedInbox') if \ shared_inbox = data.get('endpoints').get('sharedInbox') if \
data.get('endpoints') else None data.get('endpoints') else None
server = get_or_create_remote_server(actor_parts.netloc)
# throws a key error if it can't find any of these fields # throws a key error if it can't find any of these fields
user = models.User.objects.create_user( user = models.User.objects.create_user(
username, username,
@ -46,6 +47,7 @@ def get_or_create_remote_user(actor):
fedireads_user=data.get('fedireadsUser', False), fedireads_user=data.get('fedireadsUser', False),
manually_approves_followers=data.get( manually_approves_followers=data.get(
'manuallyApprovesFollowers', False), 'manuallyApprovesFollowers', False),
federated_server=server,
) )
if user.fedireads_user: if user.fedireads_user:
get_remote_reviews(user) get_remote_reviews(user)
@ -65,3 +67,36 @@ def get_remote_reviews(user):
if status.get('fedireadsType') == 'Review': if status.get('fedireadsType') == 'Review':
create_review_from_activity(user, status) create_review_from_activity(user, status)
def get_or_create_remote_server(domain):
''' get info on a remote server '''
try:
return models.FederatedServer.objects.get(
server_name=domain
)
except models.FederatedServer.DoesNotExist:
pass
response = requests.get(
'https://%s/.well-known/nodeinfo' % domain,
headers={'Accept': 'application/activity+json'}
)
data = response.json()
try:
nodeinfo_url = data.get('links')[0].get('href')
except (TypeError, KeyError):
return None
response = requests.get(
nodeinfo_url,
headers={'Accept': 'application/activity+json'}
)
data = response.json()
server = models.FederatedServer.objects.create(
server_name=domain,
application_type=data['software']['name'],
application_version=data['software']['version'],
)
return server

View file

@ -105,6 +105,6 @@ def peers(request):
if request.method != 'GET': if request.method != 'GET':
return HttpResponseNotFound() return HttpResponseNotFound()
# TODO names = models.FederatedServer.objects.values_list('server_name', flat=True)
return JsonResponse([]) return JsonResponse(list(names), safe=False)