bookwyrm/bookwyrm/views/status.py

354 lines
12 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" what are we here for if not for posting """
2021-01-12 21:47:00 +00:00
import re
2022-05-26 17:58:11 +00:00
import logging
2021-08-30 16:47:19 +00:00
2021-01-12 21:47:00 +00:00
from django.contrib.auth.decorators import login_required
2021-08-30 16:12:05 +00:00
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from django.db import transaction
from django.db.models import Q
from django.http import HttpResponse, HttpResponseBadRequest, Http404
2023-05-30 18:35:47 +00:00
from django.shortcuts import get_object_or_404
2021-04-03 19:30:18 +00:00
from django.template.response import TemplateResponse
from django.utils import timezone
2021-01-12 21:47:00 +00:00
from django.utils.decorators import method_decorator
from django.views import View
2021-09-29 17:59:36 +00:00
from django.views.decorators.http import require_POST
2021-08-30 16:12:05 +00:00
2021-08-30 16:47:19 +00:00
from markdown import markdown
2021-01-12 21:47:00 +00:00
from bookwyrm import forms, models
2023-05-30 18:35:47 +00:00
from bookwyrm.models.report import DELETE_ITEM
2022-07-04 20:14:22 +00:00
from bookwyrm.utils import regex, sanitizer
from bookwyrm.views.helpers import get_mergeable_object_or_404
from .helpers import handle_remote_webfinger, is_api_request
from .helpers import load_date_in_user_tz_as_utc, redirect_to_referer
2021-01-12 21:47:00 +00:00
2022-05-26 17:58:11 +00:00
logger = logging.getLogger(__name__)
2021-01-12 21:47:00 +00:00
2021-10-14 23:30:27 +00:00
# pylint: disable= no-self-use
@method_decorator(login_required, name="dispatch")
class EditStatus(View):
"""the view for *posting*"""
def get(self, request, status_id): # pylint: disable=unused-argument
"""load the edit panel"""
2021-10-14 23:57:58 +00:00
status = get_object_or_404(
models.Status.objects.select_subclasses(), id=status_id
)
2021-10-14 23:30:27 +00:00
2021-10-15 02:14:47 +00:00
status_type = "reply" if status.reply_parent else status.status_type.lower()
2021-10-14 23:30:27 +00:00
data = {
2021-10-15 02:14:47 +00:00
"type": status_type,
"book": getattr(status, "book", None),
"draft": status,
2021-10-14 23:30:27 +00:00
}
return TemplateResponse(request, "compose.html", data)
2021-01-12 21:47:00 +00:00
# pylint: disable= no-self-use
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-12 21:47:00 +00:00
class CreateStatus(View):
2021-04-26 16:15:42 +00:00
"""the view for *posting*"""
2021-03-08 16:49:10 +00:00
def get(self, request, status_type): # pylint: disable=unused-argument
2021-10-14 23:30:27 +00:00
"""compose view (...not used?)"""
book = get_mergeable_object_or_404(models.Edition, id=request.GET.get("book"))
2021-04-03 21:32:34 +00:00
data = {"book": book}
return TemplateResponse(request, "compose.html", data)
2021-11-20 08:19:51 +00:00
# pylint: disable=too-many-branches
@transaction.atomic
2021-10-15 00:13:54 +00:00
def post(self, request, status_type, existing_status_id=None):
2021-10-13 20:12:56 +00:00
"""create status of whatever type"""
2021-10-15 15:15:48 +00:00
created = not existing_status_id
2021-10-15 00:32:38 +00:00
existing_status = None
2021-10-15 00:13:54 +00:00
if existing_status_id:
existing_status = get_object_or_404(
2021-10-15 00:23:54 +00:00
models.Status.objects.select_subclasses(), id=existing_status_id
2021-10-15 00:13:54 +00:00
)
existing_status.edited_date = timezone.now()
2021-10-15 00:13:54 +00:00
2021-01-12 22:02:38 +00:00
status_type = status_type[0].upper() + status_type[1:]
2021-01-18 17:57:44 +00:00
2021-01-12 22:02:38 +00:00
try:
2021-10-15 00:13:54 +00:00
form = getattr(forms, f"{status_type}Form")(
request.POST, instance=existing_status
)
2022-05-26 17:58:11 +00:00
except AttributeError as err:
logger.exception(err)
2021-01-12 21:47:00 +00:00
return HttpResponseBadRequest()
2022-05-26 17:58:11 +00:00
2021-01-12 21:47:00 +00:00
if not form.is_valid():
if is_api_request(request):
2022-05-26 17:58:11 +00:00
logger.exception(form.errors)
return HttpResponseBadRequest()
return redirect_to_referer(request)
2021-01-12 21:47:00 +00:00
status = form.save(request, commit=False)
2021-10-15 01:34:26 +00:00
# save the plain, unformatted version of the status for future editing
status.raw_content = status.content
if hasattr(status, "quote"):
status.raw_quote = status.quote
status.sensitive = status.content_warning not in [None, ""]
# the status has to be saved now before we can add many to many fields
# like mentions
status.save(broadcast=False)
2021-01-12 21:47:00 +00:00
# inspect the text for user tags
content = status.content
mentions = find_mentions(request.user, content)
for _, mention_user in mentions.items():
2021-01-12 21:47:00 +00:00
# add them to status mentions fk
status.mention_users.add(mention_user)
content = format_mentions(content, mentions)
2021-01-12 21:47:00 +00:00
2021-02-10 22:13:36 +00:00
# add reply parent to mentions
2021-01-12 21:47:00 +00:00
if status.reply_parent:
status.mention_users.add(status.reply_parent.user)
# inspect the text for hashtags
2023-03-29 15:39:41 +00:00
hashtags = find_or_create_hashtags(content)
for _, mention_hashtag in hashtags.items():
# add them to status mentions fk
status.mention_hashtags.add(mention_hashtag)
2023-03-29 15:39:41 +00:00
content = format_hashtags(content, hashtags)
2021-01-12 21:47:00 +00:00
# deduplicate mentions
status.mention_users.set(set(status.mention_users.all()))
# don't apply formatting to generated notes
if not isinstance(status, models.GeneratedNote) and content:
2021-01-12 21:47:00 +00:00
status.content = to_markdown(content)
# do apply formatting to quotes
2021-03-08 16:49:10 +00:00
if hasattr(status, "quote"):
2021-01-12 21:47:00 +00:00
status.quote = to_markdown(status.quote)
2021-10-15 15:15:48 +00:00
status.save(created=created)
2021-03-21 00:34:58 +00:00
# update a readthrough, if needed
if bool(request.POST.get("id")):
try:
edit_readthrough(request)
except Http404:
pass
2021-03-21 00:34:58 +00:00
if is_api_request(request):
return HttpResponse()
return redirect_to_referer(request)
2021-01-12 21:47:00 +00:00
2023-03-29 15:39:41 +00:00
def format_mentions(content, mentions):
"""Detect @mentions and make them links"""
for mention_text, mention_user in mentions.items():
# turn the mention into a link
content = re.sub(
2023-03-29 16:20:58 +00:00
rf"(?<!/)\B{mention_text}\b(?!@)",
rf'<a href="{mention_user.remote_id}">{mention_text}</a>',
content,
)
return content
2021-01-12 21:47:00 +00:00
2023-03-29 15:39:41 +00:00
def format_hashtags(content, hashtags):
"""Detect #hashtags and make them links"""
for mention_text, mention_hashtag in hashtags.items():
2023-03-29 15:39:41 +00:00
# turn the mention into a link
content = re.sub(
2023-03-29 16:20:58 +00:00
rf"(?<!/)\B{mention_text}\b(?!@)",
2023-03-29 15:39:41 +00:00
rf'<a href="{mention_hashtag.remote_id}" data-mention="hashtag">'
+ rf"{mention_text}</a>",
content,
)
return content
2021-04-03 19:30:18 +00:00
@method_decorator(login_required, name="dispatch")
2021-01-12 21:47:00 +00:00
class DeleteStatus(View):
2021-04-26 16:15:42 +00:00
"""tombstone that bad boy"""
2021-03-08 16:49:10 +00:00
2023-05-30 18:35:47 +00:00
def post(self, request, status_id, report_id=None):
2021-04-26 16:15:42 +00:00
"""delete and tombstone a status"""
2021-01-12 21:47:00 +00:00
status = get_object_or_404(models.Status, id=status_id)
# don't let people delete other people's statuses
2021-09-27 21:03:17 +00:00
status.raise_not_deletable(request.user)
2021-01-12 21:47:00 +00:00
# perform deletion
2021-04-03 19:30:18 +00:00
status.delete()
2023-05-30 18:35:47 +00:00
# record deletion if it's related to a report
if report_id:
models.Report.record_action(report_id, DELETE_ITEM, request.user)
return redirect_to_referer(request, "/")
2021-03-08 16:49:10 +00:00
2021-01-12 21:47:00 +00:00
2021-09-29 17:59:36 +00:00
@login_required
@require_POST
def update_progress(request, book_id): # pylint: disable=unused-argument
2021-09-29 17:59:36 +00:00
"""Either it's just a progress update, or it's a comment with a progress update"""
if request.POST.get("post-status"):
return CreateStatus.as_view()(request, "comment")
return edit_readthrough(request)
@login_required
@require_POST
def edit_readthrough(request):
"""can't use the form because the dates are too finnicky"""
2022-01-11 18:40:32 +00:00
# TODO: remove this, it duplicates the code in the ReadThrough view
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
readthrough.start_date = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
readthrough.finish_date = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
progress = request.POST.get("progress")
try:
progress = int(progress)
readthrough.progress = progress
except (ValueError, TypeError):
pass
progress_mode = request.POST.get("progress_mode")
try:
progress_mode = models.ProgressMode(progress_mode)
readthrough.progress_mode = progress_mode
except ValueError:
pass
readthrough.save()
# record the progress update individually
# use default now for date field
readthrough.create_update()
if is_api_request(request):
return HttpResponse()
return redirect_to_referer(request)
def find_mentions(user, content):
2021-04-26 16:15:42 +00:00
"""detect @mentions in raw status content"""
if not content:
return {}
# The regex has nested match groups, so the 0th entry has the full (outer) match
# And because the strict username starts with @, the username is 1st char onward
usernames = [m[0][1:] for m in re.findall(regex.STRICT_USERNAME, content)]
known_users = (
models.User.viewer_aware_objects(user)
.filter(Q(username__in=usernames) | Q(localname__in=usernames))
.distinct()
)
# Prepare a lookup based on both username and localname
username_dict = {
**{f"@{u.username}": u for u in known_users},
**{f"@{u.localname}": u for u in known_users.filter(local=True)},
}
# Users not captured here could be blocked or not yet loaded on the server
not_found = set(usernames) - set(username_dict.keys())
for username in not_found:
mention_user = handle_remote_webfinger(username, unknown_only=True)
2021-01-12 21:47:00 +00:00
if not mention_user:
# this user is blocked or can't be found
2021-01-12 21:47:00 +00:00
continue
username_dict[f"@{mention_user.username}"] = mention_user
username_dict[f"@{mention_user.localname}"] = mention_user
return username_dict
2021-01-12 21:47:00 +00:00
def find_or_create_hashtags(content):
"""detect #hashtags in raw status content
it stores hashtags case-sensitive, but ensures that an existing
hashtag with different case are found and re-used. for example,
an existing #BookWyrm hashtag will be found and used even if the
status content is using #bookwyrm.
"""
if not content:
return {}
found_hashtags = {t.lower(): t for t in re.findall(regex.HASHTAG, content)}
if len(found_hashtags) == 0:
return {}
known_hashtags = {
t.name.lower(): t
for t in models.Hashtag.objects.filter(
Q(name__in=found_hashtags.keys())
).distinct()
}
not_found = found_hashtags.keys() - known_hashtags.keys()
for lower_name in not_found:
tag_name = found_hashtags[lower_name]
mention_hashtag = models.Hashtag(name=tag_name)
mention_hashtag.save()
known_hashtags[lower_name] = mention_hashtag
return {found_hashtags[k]: v for k, v in known_hashtags.items()}
2021-01-12 21:47:00 +00:00
def format_links(content):
2021-04-26 16:15:42 +00:00
"""detect and format links"""
validator = URLValidator(["http", "https"])
schema_re = re.compile(r"\bhttps?://")
split_content = re.split(r"(\s+)", content)
for i, potential_link in enumerate(split_content):
if not schema_re.search(potential_link):
continue
2021-08-30 16:12:05 +00:00
# Strip surrounding brackets and trailing punctuation.
prefix, potential_link, suffix = _unwrap(potential_link)
try:
# raises an error on anything that's not a valid link
validator(potential_link)
# use everything but the scheme in the presentation of the link
link = schema_re.sub("", potential_link)
split_content[i] = f'{prefix}<a href="{potential_link}">{link}</a>{suffix}'
except (ValidationError, UnicodeError):
pass
2021-08-30 16:12:05 +00:00
return "".join(split_content)
2021-08-30 16:12:05 +00:00
2023-07-31 20:12:37 +00:00
def _unwrap(text):
"""split surrounding brackets and trailing punctuation from a string of text"""
punct = re.compile(r'([.,;:!?"’”»]+)$')
prefix = suffix = ""
2021-08-30 16:12:05 +00:00
if punct.search(text):
# Move punctuation to suffix segment.
text, suffix, _ = punct.split(text)
2021-08-30 16:38:00 +00:00
for wrapper in ("()", "[]", "{}"):
2021-08-30 16:47:19 +00:00
if text[0] == wrapper[0] and text[-1] == wrapper[-1]:
# Split out wrapping chars.
suffix = text[-1] + suffix
prefix, text = text[:1], text[1:-1]
break # Nested wrappers not supported atm.
if punct.search(text):
# Move inner punctuation to suffix segment.
text, inner_punct, _ = punct.split(text)
suffix = inner_punct + suffix
2021-08-30 16:38:00 +00:00
return prefix, text, suffix
2023-07-31 20:12:37 +00:00
2021-01-12 21:47:00 +00:00
def to_markdown(content):
2021-04-26 16:15:42 +00:00
"""catch links and convert to markdown"""
content = format_links(content)
content = markdown(content)
2021-01-12 21:47:00 +00:00
# sanitize resulting html
2022-07-04 20:14:22 +00:00
return sanitizer.clean(content)