moviewyrm/bookwyrm/views/status.py

115 lines
4 KiB
Python
Raw Normal View History

2021-01-13 16:10:50 +00:00
''' what are we here for if not for posting '''
2021-01-12 21:47:00 +00:00
import re
from django.contrib.auth.decorators import login_required
2021-01-29 18:25:31 +00:00
from django.http import HttpResponseBadRequest
2021-01-12 21:47:00 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
from django.views import View
from markdown import markdown
from bookwyrm import forms, models
from bookwyrm.sanitize_html import InputHtmlParser
from bookwyrm.settings import DOMAIN
2021-02-10 22:13:36 +00:00
from bookwyrm.status import delete_status
2021-01-12 21:47:00 +00:00
from bookwyrm.utils import regex
2021-01-29 18:25:31 +00:00
from .helpers import handle_remote_webfinger
2021-01-12 21:47:00 +00:00
# pylint: disable= no-self-use
@method_decorator(login_required, name='dispatch')
class CreateStatus(View):
2021-01-18 17:57:44 +00:00
''' the view for *posting* '''
2021-01-12 21:47:00 +00:00
def post(self, request, status_type):
''' create status of whatever type '''
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:
form = getattr(forms, '%sForm' % status_type)(request.POST)
except AttributeError:
2021-01-12 21:47:00 +00:00
return HttpResponseBadRequest()
if not form.is_valid():
return redirect(request.headers.get('Referer', '/'))
status = form.save(commit=False)
if not status.sensitive and status.content_warning:
# the cw text field remains populated when you click "remove"
status.content_warning = None
status.save(broadcast=False)
2021-01-12 21:47:00 +00:00
# inspect the text for user tags
content = status.content
for (mention_text, mention_user) in find_mentions(content):
# add them to status mentions fk
status.mention_users.add(mention_user)
# turn the mention into a link
content = re.sub(
r'%s([^@]|$)' % mention_text,
r'<a href="%s">%s</a>\g<1>' % \
(mention_user.remote_id, mention_text),
content)
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)
# deduplicate mentions
status.mention_users.set(set(status.mention_users.all()))
# don't apply formatting to generated notes
if not isinstance(status, models.GeneratedNote):
status.content = to_markdown(content)
# do apply formatting to quotes
if hasattr(status, 'quote'):
status.quote = to_markdown(status.quote)
status.save(created=True)
2021-01-12 21:47:00 +00:00
return redirect(request.headers.get('Referer', '/'))
class DeleteStatus(View):
''' tombstone that bad boy '''
def post(self, request, status_id):
''' delete and tombstone a status '''
status = get_object_or_404(models.Status, id=status_id)
# don't let people delete other people's statuses
if status.user != request.user:
return HttpResponseBadRequest()
# perform deletion
delete_status(status)
return redirect(request.headers.get('Referer', '/'))
def find_mentions(content):
''' detect @mentions in raw status content '''
for match in re.finditer(regex.strict_username, content):
username = match.group().strip().split('@')[1:]
if len(username) == 1:
# this looks like a local user (@user), fill in the domain
username.append(DOMAIN)
username = '@'.join(username)
mention_user = handle_remote_webfinger(username)
if not mention_user:
# we can ignore users we don't know about
continue
yield (match.group(), mention_user)
def format_links(content):
''' detect and format links '''
return re.sub(
r'([^(href=")]|^|\()(https?:\/\/(%s([\w\.\-_\/+&\?=:;,])*))' % \
regex.domain,
r'\g<1><a href="\g<2>">\g<3></a>',
content)
def to_markdown(content):
''' catch links and convert to markdown '''
content = markdown(content)
content = format_links(content)
2021-01-12 21:47:00 +00:00
# sanitize resulting html
sanitizer = InputHtmlParser()
sanitizer.feed(content)
return sanitizer.get_output()