code cleanup

This commit is contained in:
Hugh Rundle 2021-11-28 21:38:28 +11:00
parent f828f36cc5
commit e9bb2a04a4
No known key found for this signature in database
GPG key ID: CD23D6039184286B
7 changed files with 37 additions and 26 deletions

View file

@ -92,7 +92,6 @@
</div>
</footer>
<!-- TODO: do we need any of this stuff? -->
<script>
var csrf_token = '{{ csrf_token }}';
</script>

View file

@ -453,5 +453,7 @@ urlpatterns = [
re_path(r"^delete-follow-request/?$", views.delete_follow_request),
re_path(r"^ostatus_follow/?$", views.remote_follow, name="remote-follow"),
re_path(r"^remote_follow/?$", views.remote_follow_page, name="remote-follow-page"),
re_path(r"^ostatus_success/?$", views.ostatus_follow_success, name="ostatus-success"),
re_path(
r"^ostatus_success/?$", views.ostatus_follow_success, name="ostatus-success"
),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -58,7 +58,14 @@ from .author import Author, EditAuthor
from .directory import Directory
from .discover import Discover
from .feed import DirectMessage, Feed, Replies, Status
from .follow import follow, unfollow, ostatus_follow_request, ostatus_follow_success, remote_follow, remote_follow_page
from .follow import (
follow,
unfollow,
ostatus_follow_request,
ostatus_follow_success,
remote_follow,
remote_follow_page,
)
from .follow import accept_follow_request, delete_follow_request
from .get_started import GetStartedBooks, GetStartedProfile, GetStartedUsers
from .goal import Goal, hide_goal

View file

@ -8,7 +8,11 @@ from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
from bookwyrm import models
from .helpers import get_user_from_username, handle_remote_webfinger, subscribe_remote_webfinger
from .helpers import (
get_user_from_username,
handle_remote_webfinger,
subscribe_remote_webfinger,
)
@login_required
@ -32,7 +36,6 @@ def follow(request):
return redirect(to_follow.local_path)
@login_required
@require_POST
def unfollow(request):
@ -92,12 +95,15 @@ def delete_follow_request(request):
follow_request.delete()
return redirect(f"/user/{request.user.localname}")
def ostatus_follow_request(request):
"""prepare an outgoing remote follow request"""
# parse the acct URI into a user string
uri = urllib.parse.unquote(request.GET.get("acct"))
username_parts = re.search("(?:^http(?:s?):\/\/)([\w\-\.]*)(?:.)*(?:(?:\/)([\w]*))", uri)
username_parts = re.search(
"(?:^http(?:s?):\/\/)([\w\-\.]*)(?:.)*(?:(?:\/)([\w]*))", uri
)
account = f"{username_parts[2]}@{username_parts[1]}"
user = handle_remote_webfinger(account)
error = None
@ -111,14 +117,13 @@ def ostatus_follow_request(request):
if hasattr(user, "followers") and request.user in user.followers.all():
error = "already_following"
if hasattr(user, "follower_requests") and request.user in user.follower_requests.all():
if (
hasattr(user, "follower_requests")
and request.user in user.follower_requests.all()
):
error = "already_requested"
data = {
"account": account,
"user": user,
"error": error
}
data = {"account": account, "user": user, "error": error}
return TemplateResponse(request, "ostatus/subscribe.html", data)
@ -127,21 +132,17 @@ def ostatus_follow_request(request):
def ostatus_follow_success(request):
"""display success message for remote follow"""
user = get_user_from_username(request.user, request.GET.get("following"))
data = {
"account": user.name,
"user": user,
"error": None
}
data = {"account": user.name, "user": user, "error": None}
return TemplateResponse(request, "ostatus/success.html", data)
def remote_follow_page(request):
"""Display remote follow page"""
user = get_user_from_username(request.user, request.GET.get("user"))
data = {
"user": user
}
data = {"user": user}
return TemplateResponse(request, "ostatus/remote_follow.html", data)
@require_POST
def remote_follow(request):
"""direct user to follow from remote account using ostatus subscribe protocol"""

View file

@ -84,6 +84,7 @@ def handle_remote_webfinger(query):
return None
return user
def subscribe_remote_webfinger(query):
"""get subscribe template from other servers"""
template = None
@ -112,6 +113,7 @@ def subscribe_remote_webfinger(query):
return template
def get_edition(book_id):
"""look up a book in the db and return an edition"""
book = models.Book.objects.select_subclasses().get(id=book_id)

View file

@ -33,8 +33,8 @@ def webfinger(request):
},
{
"rel": "http://ostatus.org/schema/1.0/subscribe",
"template": f"https://{DOMAIN}/ostatus_subscribe?acct={{uri}}"
}
"template": f"https://{DOMAIN}/ostatus_subscribe?acct={{uri}}",
},
],
}
)