2022-02-17 21:51:09 +09:00
|
|
|
from quart import current_app, request, render_template, redirect, url_for, escape, Markup
|
2022-02-16 18:55:30 +09:00
|
|
|
|
|
|
|
from anonstream.stream import get_stream_title
|
2022-02-18 14:16:54 +09:00
|
|
|
from anonstream.user import add_notice, pop_notice, try_change_appearance
|
2022-02-16 18:55:30 +09:00
|
|
|
from anonstream.chat import add_chat_message, Rejected
|
2022-02-18 21:24:19 +09:00
|
|
|
from anonstream.routes.wrappers import with_user_from, render_template_with_etag
|
2022-02-17 09:20:51 +09:00
|
|
|
from anonstream.helpers.user import get_default_name
|
2022-02-18 21:24:19 +09:00
|
|
|
from anonstream.helpers.chat import get_scrollback
|
2022-02-16 18:55:30 +09:00
|
|
|
from anonstream.utils.chat import generate_nonce
|
2022-02-17 21:51:09 +09:00
|
|
|
from anonstream.utils.user import concatenate_for_notice
|
2022-02-16 18:55:30 +09:00
|
|
|
|
|
|
|
@current_app.route('/info.html')
|
|
|
|
@with_user_from(request)
|
|
|
|
async def nojs_info(user):
|
|
|
|
return await render_template(
|
|
|
|
'nojs_info.html',
|
|
|
|
user=user,
|
|
|
|
title=get_stream_title(),
|
|
|
|
)
|
|
|
|
|
|
|
|
@current_app.route('/chat/messages.html')
|
|
|
|
@with_user_from(request)
|
|
|
|
async def nojs_chat(user):
|
2022-02-18 21:24:19 +09:00
|
|
|
return await render_template_with_etag(
|
2022-02-16 18:55:30 +09:00
|
|
|
'nojs_chat.html',
|
|
|
|
user=user,
|
2022-02-18 10:32:34 +09:00
|
|
|
users_by_token=current_app.users_by_token,
|
2022-02-18 21:24:19 +09:00
|
|
|
messages=get_scrollback(current_app.messages),
|
|
|
|
timeout=current_app.config['THRESHOLD_NOJS_CHAT_TIMEOUT'],
|
2022-02-16 18:55:30 +09:00
|
|
|
get_default_name=get_default_name,
|
|
|
|
)
|
|
|
|
|
2022-02-18 21:24:19 +09:00
|
|
|
@current_app.route('/chat/redirect')
|
|
|
|
@with_user_from(request)
|
|
|
|
async def nojs_chat_redirect(user):
|
|
|
|
return redirect(url_for('nojs_chat', _anchor='end'))
|
|
|
|
|
2022-02-16 18:55:30 +09:00
|
|
|
@current_app.route('/chat/form.html')
|
|
|
|
@with_user_from(request)
|
|
|
|
async def nojs_form(user):
|
|
|
|
notice_id = request.args.get('notice', type=int)
|
2022-02-17 21:51:09 +09:00
|
|
|
notice, verbose = pop_notice(user, notice_id)
|
2022-02-16 18:55:30 +09:00
|
|
|
prefer_chat_form = request.args.get('landing') != 'appearance'
|
|
|
|
return await render_template(
|
|
|
|
'nojs_form.html',
|
|
|
|
user=user,
|
2022-02-17 21:51:09 +09:00
|
|
|
notice=notice,
|
|
|
|
verbose=verbose,
|
2022-02-16 18:55:30 +09:00
|
|
|
prefer_chat_form=prefer_chat_form,
|
|
|
|
nonce=generate_nonce(),
|
|
|
|
default_name=get_default_name(user),
|
|
|
|
)
|
|
|
|
|
|
|
|
@current_app.post('/chat/message')
|
|
|
|
@with_user_from(request)
|
|
|
|
async def nojs_submit_message(user):
|
|
|
|
form = await request.form
|
|
|
|
comment = form.get('comment', '')
|
|
|
|
nonce = form.get('nonce', '')
|
|
|
|
|
|
|
|
try:
|
2022-02-18 10:32:34 +09:00
|
|
|
await add_chat_message(user, nonce, comment)
|
2022-02-16 18:55:30 +09:00
|
|
|
except Rejected as e:
|
|
|
|
notice, *_ = e.args
|
|
|
|
notice_id = add_notice(user, notice)
|
|
|
|
else:
|
|
|
|
notice_id = None
|
|
|
|
|
2022-02-17 21:51:09 +09:00
|
|
|
return redirect(url_for('nojs_form', token=user['token'], landing='chat', notice=notice_id))
|
2022-02-16 18:55:30 +09:00
|
|
|
|
|
|
|
@current_app.post('/chat/appearance')
|
|
|
|
@with_user_from(request)
|
|
|
|
async def nojs_submit_appearance(user):
|
2022-02-17 21:51:09 +09:00
|
|
|
form = await request.form
|
|
|
|
name = form.get('name', '') or None
|
|
|
|
color = form.get('color', '')
|
|
|
|
password = form.get('password', '')
|
|
|
|
want_delete_tripcode = form.get('clear-tripcode', type=bool)
|
|
|
|
want_change_tripcode = form.get('set-tripcode', type=bool)
|
|
|
|
|
2022-02-18 14:16:54 +09:00
|
|
|
errors = await try_change_appearance(
|
|
|
|
user,
|
|
|
|
name,
|
|
|
|
color,
|
|
|
|
password,
|
|
|
|
want_delete_tripcode,
|
|
|
|
want_change_tripcode,
|
|
|
|
)
|
2022-02-17 21:51:09 +09:00
|
|
|
if errors:
|
|
|
|
notice = Markup('<br>').join(
|
|
|
|
concatenate_for_notice(*error.args) for error in errors
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
notice = 'Changed appearance'
|
|
|
|
|
|
|
|
notice_id = add_notice(user, notice, verbose=len(errors) > 1)
|
|
|
|
return redirect(url_for(
|
|
|
|
'nojs_form',
|
|
|
|
token=user['token'],
|
|
|
|
landing='appearance' if errors else 'chat',
|
|
|
|
notice=notice_id,
|
|
|
|
))
|