2021-03-08 16:49:10 +00:00
|
|
|
""" views for actions you can take in the application """
|
2021-11-28 05:56:21 +00:00
|
|
|
import urllib.parse
|
|
|
|
import re
|
2021-01-13 21:36:01 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-02-17 19:28:54 +00:00
|
|
|
from django.db import IntegrityError
|
2021-09-27 23:04:40 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-11-28 05:56:21 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2021-01-13 21:36:01 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
|
|
|
|
|
|
|
from bookwyrm import models
|
2021-11-28 09:09:29 +00:00
|
|
|
from .helpers import get_user_from_username, handle_remote_webfinger, subscribe_remote_webfinger
|
2021-01-13 21:36:01 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-13 21:36:01 +00:00
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def follow(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""follow another user, here or abroad"""
|
2021-03-08 16:49:10 +00:00
|
|
|
username = request.POST["user"]
|
2021-04-30 16:33:36 +00:00
|
|
|
to_follow = get_user_from_username(request.user, username)
|
2021-01-13 21:36:01 +00:00
|
|
|
|
2021-02-17 19:28:54 +00:00
|
|
|
try:
|
|
|
|
models.UserFollowRequest.objects.create(
|
|
|
|
user_subject=request.user,
|
|
|
|
user_object=to_follow,
|
|
|
|
)
|
|
|
|
except IntegrityError:
|
|
|
|
pass
|
2021-02-13 00:16:22 +00:00
|
|
|
|
2021-11-28 05:56:21 +00:00
|
|
|
if request.GET.get("next"):
|
|
|
|
return redirect(request.GET.get("next", "/"))
|
|
|
|
|
2021-01-13 21:36:01 +00:00
|
|
|
return redirect(to_follow.local_path)
|
|
|
|
|
|
|
|
|
2021-11-28 05:56:21 +00:00
|
|
|
|
2021-01-13 21:36:01 +00:00
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def unfollow(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""unfollow a user"""
|
2021-03-08 16:49:10 +00:00
|
|
|
username = request.POST["user"]
|
2021-04-30 16:33:36 +00:00
|
|
|
to_unfollow = get_user_from_username(request.user, username)
|
2021-01-13 21:36:01 +00:00
|
|
|
|
2021-03-13 22:55:09 +00:00
|
|
|
try:
|
|
|
|
models.UserFollows.objects.get(
|
|
|
|
user_subject=request.user, user_object=to_unfollow
|
|
|
|
).delete()
|
|
|
|
except models.UserFollows.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
models.UserFollowRequest.objects.get(
|
|
|
|
user_subject=request.user, user_object=to_unfollow
|
|
|
|
).delete()
|
|
|
|
except models.UserFollowRequest.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# this is handled with ajax so it shouldn't really matter
|
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
2021-01-13 21:36:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def accept_follow_request(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a user accepts a follow request"""
|
2021-03-08 16:49:10 +00:00
|
|
|
username = request.POST["user"]
|
2021-04-30 16:33:36 +00:00
|
|
|
requester = get_user_from_username(request.user, username)
|
2021-01-13 21:36:01 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
follow_request = models.UserFollowRequest.objects.get(
|
2021-03-08 16:49:10 +00:00
|
|
|
user_subject=requester, user_object=request.user
|
2021-01-13 21:36:01 +00:00
|
|
|
)
|
|
|
|
except models.UserFollowRequest.DoesNotExist:
|
|
|
|
# Request already dealt with.
|
|
|
|
return redirect(request.user.local_path)
|
2021-02-07 03:12:49 +00:00
|
|
|
follow_request.accept()
|
2021-01-13 21:36:01 +00:00
|
|
|
|
|
|
|
return redirect(request.user.local_path)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def delete_follow_request(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a user rejects a follow request"""
|
2021-03-08 16:49:10 +00:00
|
|
|
username = request.POST["user"]
|
2021-04-30 16:33:36 +00:00
|
|
|
requester = get_user_from_username(request.user, username)
|
2021-01-13 21:36:01 +00:00
|
|
|
|
2021-09-27 23:08:52 +00:00
|
|
|
follow_request = get_object_or_404(
|
|
|
|
models.UserFollowRequest, user_subject=requester, user_object=request.user
|
2021-09-27 23:04:40 +00:00
|
|
|
)
|
|
|
|
follow_request.raise_not_deletable(request.user)
|
2021-01-13 21:36:01 +00:00
|
|
|
|
|
|
|
follow_request.delete()
|
2021-09-18 18:32:00 +00:00
|
|
|
return redirect(f"/user/{request.user.localname}")
|
2021-11-28 05:56:21 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
account = f"{username_parts[2]}@{username_parts[1]}"
|
|
|
|
user = handle_remote_webfinger(account)
|
|
|
|
error = None
|
|
|
|
|
|
|
|
if user is None or user == "":
|
|
|
|
error = "ostatus_subscribe"
|
|
|
|
|
|
|
|
if bool(user) and user in request.user.blocks.all():
|
|
|
|
error = "is_blocked"
|
|
|
|
|
|
|
|
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():
|
|
|
|
error = "already_requested"
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"account": account,
|
|
|
|
"user": user,
|
|
|
|
"error": error
|
|
|
|
}
|
|
|
|
|
|
|
|
return TemplateResponse(request, "ostatus/subscribe.html", data)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "ostatus/success.html", data)
|
|
|
|
|
2021-11-28 09:09:29 +00:00
|
|
|
def remote_follow_page(request):
|
|
|
|
"""Display remote follow page"""
|
|
|
|
user = get_user_from_username(request.user, request.GET.get("user"))
|
|
|
|
data = {
|
|
|
|
"user": user
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, "ostatus/remote_follow.html", data)
|
2021-11-28 05:56:21 +00:00
|
|
|
|
2021-11-28 09:09:29 +00:00
|
|
|
@require_POST
|
|
|
|
def remote_follow(request):
|
|
|
|
"""direct user to follow from remote account using ostatus subscribe protocol"""
|
|
|
|
remote_user = request.POST.get("remote_user")
|
|
|
|
template = subscribe_remote_webfinger(remote_user)
|
|
|
|
url = template.replace("{uri}", request.POST.get("user"))
|
|
|
|
return redirect(url)
|