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

@ -46,9 +46,9 @@
<span class="subtitle is-7 is-block">@{{ user|username }}</span> <span class="subtitle is-7 is-block">@{{ user|username }}</span>
</a> </a>
<form name="follow" method="post" action="{% url 'follow' %}/?next={% url 'ostatus-success' %}?following={{ user.username }}"> <form name="follow" method="post" action="{% url 'follow' %}/?next={% url 'ostatus-success' %}?following={{ user.username }}">
{% csrf_token %} {% csrf_token %}
<input name="user" value={{ user.username }} hidden> <input name="user" value={{ user.username }} hidden>
<button class="button is-link" type="submit">{% trans "Follow " %} {{ user.name }}</button> <button class="button is-link" type="submit">{% trans "Follow " %} {{ user.name }}</button>
</form> </form>
</div> </div>
</div> </div>

View file

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

View file

@ -453,5 +453,7 @@ urlpatterns = [
re_path(r"^delete-follow-request/?$", views.delete_follow_request), re_path(r"^delete-follow-request/?$", views.delete_follow_request),
re_path(r"^ostatus_follow/?$", views.remote_follow, name="remote-follow"), 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"^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) ] + 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 .directory import Directory
from .discover import Discover from .discover import Discover
from .feed import DirectMessage, Feed, Replies, Status 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 .follow import accept_follow_request, delete_follow_request
from .get_started import GetStartedBooks, GetStartedProfile, GetStartedUsers from .get_started import GetStartedBooks, GetStartedProfile, GetStartedUsers
from .goal import Goal, hide_goal 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 django.views.decorators.http import require_POST
from bookwyrm import models 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 @login_required
@ -32,7 +36,6 @@ def follow(request):
return redirect(to_follow.local_path) return redirect(to_follow.local_path)
@login_required @login_required
@require_POST @require_POST
def unfollow(request): def unfollow(request):
@ -92,12 +95,15 @@ def delete_follow_request(request):
follow_request.delete() follow_request.delete()
return redirect(f"/user/{request.user.localname}") return redirect(f"/user/{request.user.localname}")
def ostatus_follow_request(request): def ostatus_follow_request(request):
"""prepare an outgoing remote follow request""" """prepare an outgoing remote follow request"""
# parse the acct URI into a user string # parse the acct URI into a user string
uri = urllib.parse.unquote(request.GET.get("acct")) 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]}" account = f"{username_parts[2]}@{username_parts[1]}"
user = handle_remote_webfinger(account) user = handle_remote_webfinger(account)
error = None error = None
@ -111,14 +117,13 @@ def ostatus_follow_request(request):
if hasattr(user, "followers") and request.user in user.followers.all(): if hasattr(user, "followers") and request.user in user.followers.all():
error = "already_following" 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" error = "already_requested"
data = { data = {"account": account, "user": user, "error": error}
"account": account,
"user": user,
"error": error
}
return TemplateResponse(request, "ostatus/subscribe.html", data) return TemplateResponse(request, "ostatus/subscribe.html", data)
@ -127,21 +132,17 @@ def ostatus_follow_request(request):
def ostatus_follow_success(request): def ostatus_follow_success(request):
"""display success message for remote follow""" """display success message for remote follow"""
user = get_user_from_username(request.user, request.GET.get("following")) user = get_user_from_username(request.user, request.GET.get("following"))
data = { data = {"account": user.name, "user": user, "error": None}
"account": user.name,
"user": user,
"error": None
}
return TemplateResponse(request, "ostatus/success.html", data) return TemplateResponse(request, "ostatus/success.html", data)
def remote_follow_page(request): def remote_follow_page(request):
"""Display remote follow page""" """Display remote follow page"""
user = get_user_from_username(request.user, request.GET.get("user")) user = get_user_from_username(request.user, request.GET.get("user"))
data = { data = {"user": user}
"user": user
}
return TemplateResponse(request, "ostatus/remote_follow.html", data) return TemplateResponse(request, "ostatus/remote_follow.html", data)
@require_POST @require_POST
def remote_follow(request): def remote_follow(request):
"""direct user to follow from remote account using ostatus subscribe protocol""" """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 None
return user return user
def subscribe_remote_webfinger(query): def subscribe_remote_webfinger(query):
"""get subscribe template from other servers""" """get subscribe template from other servers"""
template = None template = None
@ -112,6 +113,7 @@ def subscribe_remote_webfinger(query):
return template return template
def get_edition(book_id): def get_edition(book_id):
"""look up a book in the db and return an edition""" """look up a book in the db and return an edition"""
book = models.Book.objects.select_subclasses().get(id=book_id) book = models.Book.objects.select_subclasses().get(id=book_id)

View file

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