57 lines
2.3 KiB
Python
57 lines
2.3 KiB
Python
from datetime import timedelta
|
|
from hashlib import sha256
|
|
import json
|
|
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import HttpResponseForbidden
|
|
from django.shortcuts import redirect
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.http import require_POST
|
|
|
|
from .forms import CommentForm
|
|
from .models import DiscussionComment, SiteSettings, StreamComment
|
|
|
|
|
|
def can_watch_stream(user):
|
|
return user.is_authenticated and user.is_active and user.has_perm('catalog.watch_slx_live')
|
|
|
|
|
|
def stream_thread_key(config):
|
|
public_id = config.stream_public_id
|
|
if config.stream_provider == 'twitch':
|
|
public_id = public_id.lower()
|
|
parts = [config.stream_provider, public_id, config.stream_discussion_slug]
|
|
return sha256(json.dumps(parts).encode()).hexdigest()
|
|
|
|
|
|
@never_cache
|
|
@require_POST
|
|
@login_required
|
|
def post_comment(request):
|
|
if not can_watch_stream(request.user):
|
|
return HttpResponseForbidden('This broadcast is invite-only.')
|
|
config = SiteSettings.objects.filter(pk=1).first() or SiteSettings()
|
|
if not config.stream_comments_open:
|
|
return HttpResponseForbidden('Stream comments are closed.')
|
|
destination = reverse('stream') + '#stream-discussion'
|
|
key = stream_thread_key(config)
|
|
if request.POST.get('discussion_key') != key:
|
|
messages.error(request, 'The featured broadcast changed. Reopen the discussion before posting.')
|
|
return redirect(destination)
|
|
form = CommentForm(request.POST)
|
|
if not form.is_valid():
|
|
messages.error(request, 'Enter a comment between 1 and 1,800 characters.')
|
|
return redirect(destination)
|
|
if StreamComment.objects.filter(author=request.user, created_at__gt=timezone.now()-timedelta(seconds=10)).exists():
|
|
messages.warning(request, 'Please wait a few seconds before posting another comment.')
|
|
return redirect(destination)
|
|
StreamComment.objects.create(
|
|
thread_key=key, broadcast_title=config.stream_title or 'SLX Live',
|
|
author=request.user, body=form.cleaned_data['body'],
|
|
)
|
|
messages.success(request, 'Your comment was submitted for moderation.')
|
|
return redirect(destination)
|