From 38ec3c554ba35a8e911b203d4261cc4ec9c3daa3 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 23 Feb 2020 09:07:00 +0000 Subject: [PATCH 1/3] Capture parameter from username_regex. --- fedireads/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fedireads/urls.py b/fedireads/urls.py index e883eff6..2fd33e26 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -6,7 +6,7 @@ from django.urls import path, re_path from fedireads import incoming, outgoing, views, settings, wellknown from fedireads import view_actions as actions -username_regex = r'[\w@\.-]+' +username_regex = r'(?P[\w@\.-]+)' localname_regex = r'(?P[\w\.-]+)' user_path = r'^user/%s' % username_regex local_user_path = r'^user/%s' % localname_regex From e0266cb7547d76a19dbe7adc3c004b73484599c5 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 23 Feb 2020 09:08:11 +0000 Subject: [PATCH 2/3] Add unique constraint to followers relationship. #47 --- fedireads/migrations/0007_auto_20200223_0902.py | 17 +++++++++++++++++ fedireads/models/user.py | 5 +++++ 2 files changed, 22 insertions(+) create mode 100644 fedireads/migrations/0007_auto_20200223_0902.py diff --git a/fedireads/migrations/0007_auto_20200223_0902.py b/fedireads/migrations/0007_auto_20200223_0902.py new file mode 100644 index 00000000..e50f7518 --- /dev/null +++ b/fedireads/migrations/0007_auto_20200223_0902.py @@ -0,0 +1,17 @@ +# Generated by Django 3.0.3 on 2020-02-23 09:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fedireads', '0006_auto_20200221_1702'), + ] + + operations = [ + migrations.AddConstraint( + model_name='userrelationship', + constraint=models.UniqueConstraint(fields=('user_subject', 'user_object'), name='followers_unique'), + ), + ] diff --git a/fedireads/models/user.py b/fedireads/models/user.py index 5cab14ec..d4135cef 100644 --- a/fedireads/models/user.py +++ b/fedireads/models/user.py @@ -73,6 +73,11 @@ class UserRelationship(FedireadsModel): status = models.CharField(max_length=100, default='follows', null=True) relationship_id = models.CharField(max_length=100) + class Meta: + constraints = [ + models.UniqueConstraint(fields=['user_subject', 'user_object'], name='followers_unique') + ] + @property def absolute_id(self): ''' use shelf identifier as absolute id ''' From 25e49d8ba2b514aebe6e4d6a22584a6041da3487 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 23 Feb 2020 09:27:10 +0000 Subject: [PATCH 3/3] Handle and ignore duplicate follower error. --- fedireads/incoming.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/fedireads/incoming.py b/fedireads/incoming.py index 3fff64ec..6fd61209 100644 --- a/fedireads/incoming.py +++ b/fedireads/incoming.py @@ -6,6 +6,7 @@ from Crypto.Hash import SHA256 from django.http import HttpResponse, HttpResponseBadRequest, \ HttpResponseNotFound, JsonResponse from django.views.decorators.csrf import csrf_exempt +import django.db.utils import json import requests @@ -198,12 +199,19 @@ def handle_incoming_follow(activity): # figure out who they are user = get_or_create_remote_user(activity['actor']) # TODO: allow users to manually approve requests - models.UserRelationship.objects.create( - user_subject=to_follow, - user_object=user, - status='follow_request', - relationship_id=activity['id'] - ) + try: + models.UserRelationship.objects.create( + user_subject=to_follow, + user_object=user, + status='follow_request', + relationship_id=activity['id'] + ) + except django.db.utils.IntegrityError: + # Duplicate follow request. Not sure what the correct behaviour is, but just dropping + # it works for now. We should perhaps generate the Accept, but then do we need to match + # the activity id? + return HttpResponse() + outgoing.handle_outgoing_accept(user, to_follow, activity) return HttpResponse()