Merge branch 'main' into review-rate

This commit is contained in:
Mouse Reeve
2021-03-08 09:48:25 -08:00
committed by GitHub
293 changed files with 19710 additions and 9166 deletions

View File

@ -1,4 +1,4 @@
''' what are we here for if not for posting '''
""" what are we here for if not for posting """
import re
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest
@ -16,19 +16,20 @@ from .helpers import handle_remote_webfinger
# pylint: disable= no-self-use
@method_decorator(login_required, name='dispatch')
@method_decorator(login_required, name="dispatch")
class CreateStatus(View):
''' the view for *posting* '''
""" the view for *posting* """
def post(self, request, status_type):
''' create status of whatever type '''
""" create status of whatever type """
status_type = status_type[0].upper() + status_type[1:]
try:
form = getattr(forms, '%sForm' % status_type)(request.POST)
form = getattr(forms, "%sForm" % status_type)(request.POST)
except AttributeError:
return HttpResponseBadRequest()
if not form.is_valid():
return redirect(request.headers.get('Referer', '/'))
return redirect(request.headers.get("Referer", "/"))
status = form.save(commit=False)
if not status.sensitive and status.content_warning:
@ -44,10 +45,10 @@ class CreateStatus(View):
# 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)
r"%s([^@]|$)" % mention_text,
r'<a href="%s">%s</a>\g<1>' % (mention_user.remote_id, mention_text),
content,
)
# add reply parent to mentions
if status.reply_parent:
status.mention_users.add(status.reply_parent.user)
@ -59,17 +60,18 @@ class CreateStatus(View):
if not isinstance(status, models.GeneratedNote) and content:
status.content = to_markdown(content)
# do apply formatting to quotes
if hasattr(status, 'quote'):
if hasattr(status, "quote"):
status.quote = to_markdown(status.quote)
status.save(created=True)
return redirect(request.headers.get('Referer', '/'))
return redirect(request.headers.get("Referer", "/"))
class DeleteStatus(View):
''' tombstone that bad boy '''
""" tombstone that bad boy """
def post(self, request, status_id):
''' delete and tombstone a status '''
""" delete and tombstone a status """
status = get_object_or_404(models.Status, id=status_id)
# don't let people delete other people's statuses
@ -78,18 +80,19 @@ class DeleteStatus(View):
# perform deletion
delete_status(status)
return redirect(request.headers.get('Referer', '/'))
return redirect(request.headers.get("Referer", "/"))
def find_mentions(content):
''' detect @mentions in raw status content '''
""" detect @mentions in raw status content """
if not content:
return
for match in re.finditer(regex.strict_username, content):
username = match.group().strip().split('@')[1:]
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)
username = "@".join(username)
mention_user = handle_remote_webfinger(username)
if not mention_user:
@ -99,15 +102,16 @@ def find_mentions(content):
def format_links(content):
''' detect and format links '''
""" detect and format links """
return re.sub(
r'([^(href=")]|^|\()(https?:\/\/(%s([\w\.\-_\/+&\?=:;,])*))' % \
regex.domain,
r'([^(href=")]|^|\()(https?:\/\/(%s([\w\.\-_\/+&\?=:;,])*))' % regex.domain,
r'\g<1><a href="\g<2>">\g<3></a>',
content)
content,
)
def to_markdown(content):
''' catch links and convert to markdown '''
""" catch links and convert to markdown """
content = markdown(content)
content = format_links(content)
# sanitize resulting html