diff --git a/anonstream/chat.py b/anonstream/chat.py index c30ce69..91e282c 100644 --- a/anonstream/chat.py +++ b/anonstream/chat.py @@ -62,14 +62,21 @@ def add_chat_message(user, nonce, comment, ignore_empty=False): raise Rejected('Message was empty') if len(comment.strip()) == 0: raise Rejected('Message was practically empty') - if len(comment) > 512: - raise Rejected('Message exceeded 512 chars') - if comment.count('\n') + 1 > 12: - raise Rejected('Message exceeded 12 lines') + if len(comment) > CONFIG['CHAT_COMMENT_MAX_LENGTH']: + raise Rejected( + f'Message exceeded {CONFIG["CHAT_COMMENT_MAX_LENGTH"]} chars' + ) + if comment.count('\n') + 1 > CONFIG['CHAT_COMMENT_MAX_LINES']: + raise Rejected( + f'Message exceeded {CONFIG["CHAT_COMMENT_MAX_LINES"]} lines' + ) linespan = get_approx_linespan(comment) - if linespan > 12: - raise Rejected('Message would span too many lines') + if linespan > CONFIG['CHAT_COMMENT_MAX_LINES']: + raise Rejected( + f'Message would span {CONFIG["CHAT_COMMENT_MAX_LINES"]} ' + f'or more lines' + ) # Record linespan linespan_tuple = (timestamp, linespan) diff --git a/anonstream/config.py b/anonstream/config.py index 0fdced9..376505d 100644 --- a/anonstream/config.py +++ b/anonstream/config.py @@ -33,10 +33,11 @@ def toml_to_flask_sections(config): toml_to_flask_section_names, toml_to_flask_section_memory, toml_to_flask_section_tasks, - toml_to_flask_section_thresholds, + toml_to_flask_section_presence, toml_to_flask_section_chat, toml_to_flask_section_flood, toml_to_flask_section_captcha, + toml_to_flask_section_nojs, ) for toml_to_flask_section in TOML_TO_FLASK_SECTIONS: yield toml_to_flask_section(config) @@ -103,23 +104,24 @@ def toml_to_flask_section_tasks(config): 'TASK_BROADCAST_STREAM_INFO_UPDATE': cfg['broadcast_stream_info_update'], } -def toml_to_flask_section_thresholds(config): - cfg = config['thresholds'] - assert cfg['user_notwatching'] <= cfg['user_tentative'] <= cfg['user_absent'] +def toml_to_flask_section_presence(config): + cfg = config['presence'] + assert cfg['notwatching'] <= cfg['tentative'] <= cfg['absent'] return { - 'THRESHOLD_USER_NOTWATCHING': cfg['user_notwatching'], - 'THRESHOLD_USER_TENTATIVE': cfg['user_tentative'], - 'THRESHOLD_USER_ABSENT': cfg['user_absent'], - 'THRESHOLD_NOJS_CHAT_TIMEOUT': cfg['nojs_chat_timeout'], + 'PRESENCE_NOTWATCHING': cfg['notwatching'], + 'PRESENCE_TENTATIVE': cfg['tentative'], + 'PRESENCE_ABSENT': cfg['absent'], } def toml_to_flask_section_chat(config): cfg = config['chat'] return { - 'CHAT_COMMENT_MAX_LENGTH': cfg['max_name_length'], + 'CHAT_COMMENT_MAX_LENGTH': cfg['max_comment_length'], + 'CHAT_COMMENT_MAX_LINES': cfg['max_comment_lines'], 'CHAT_NAME_MAX_LENGTH': cfg['max_name_length'], 'CHAT_NAME_MIN_CONTRAST': cfg['min_name_contrast'], 'CHAT_BACKGROUND_COLOUR': color_to_colour(cfg['background_color']), + 'CHAT_TRIPCODE_PASSWORD_MAX_LENGTH': cfg['max_tripcode_password_length'], 'CHAT_LEGACY_TRIPCODE_ALGORITHM': cfg['legacy_tripcode_algorithm'], } @@ -147,3 +149,12 @@ def toml_to_flask_section_captcha(config): 'CAPTCHA_BACKGROUND_COLOUR': color_to_colour(cfg['background_color']), 'CAPTCHA_FOREGROUND_COLOUR': color_to_colour(cfg['foreground_color']), } + +def toml_to_flask_section_nojs(config): + cfg = config['nojs'] + return { + 'NOJS_REFRESH_MESSAGES': round(cfg['refresh_messages']), + 'NOJS_REFRESH_INFO': round(cfg['refresh_info']), + 'NOJS_REFRESH_USERS': round(cfg['refresh_users']), + 'NOJS_TIMEOUT_CHAT': round(cfg['timeout_chat']), + } diff --git a/anonstream/helpers/user.py b/anonstream/helpers/user.py index f18561e..072230c 100644 --- a/anonstream/helpers/user.py +++ b/anonstream/helpers/user.py @@ -62,16 +62,16 @@ def get_default_name(user): def get_presence(timestamp, user): last_watching_ago = timestamp - user['last']['watching'] - if last_watching_ago < CONFIG['THRESHOLD_USER_NOTWATCHING']: + if last_watching_ago < CONFIG['PRESENCE_NOTWATCHING']: return Presence.WATCHING last_seen_ago = timestamp - user['last']['seen'] - if last_seen_ago < CONFIG['THRESHOLD_USER_TENTATIVE']: + if last_seen_ago < CONFIG['PRESENCE_TENTATIVE']: return Presence.NOTWATCHING if user['websockets']: return Presence.NOTWATCHING - if last_seen_ago < CONFIG['THRESHOLD_USER_ABSENT']: + if last_seen_ago < CONFIG['PRESENCE_ABSENT']: return Presence.TENTATIVE return Presence.ABSENT diff --git a/anonstream/routes/nojs.py b/anonstream/routes/nojs.py index 569269e..0a6c836 100644 --- a/anonstream/routes/nojs.py +++ b/anonstream/routes/nojs.py @@ -35,6 +35,7 @@ async def nojs_info(user): return await render_template( 'nojs_info.html', csp=generate_csp(), + refresh=CONFIG['NOJS_REFRESH_INFO'], user=user, viewership=viewership, uptime=uptime, @@ -48,10 +49,11 @@ async def nojs_chat_messages(user): return await render_template_with_etag( 'nojs_chat_messages.html', {'csp': generate_csp()}, + refresh=CONFIG['NOJS_REFRESH_MESSAGES'], user=user, users_by_token=USERS_BY_TOKEN, messages=get_scrollback(current_app.messages), - timeout=CONFIG['THRESHOLD_NOJS_CHAT_TIMEOUT'], + timeout=CONFIG['NOJS_TIMEOUT_CHAT'], get_default_name=get_default_name, ) @@ -67,11 +69,12 @@ async def nojs_chat_users(user): return await render_template_with_etag( 'nojs_chat_users.html', {'csp': generate_csp()}, + refresh=CONFIG['NOJS_REFRESH_USERS'], user=user, get_default_name=get_default_name, users_watching=users_by_presence[Presence.WATCHING], users_notwatching=users_by_presence[Presence.NOTWATCHING], - timeout=CONFIG['THRESHOLD_NOJS_CHAT_TIMEOUT'], + timeout=CONFIG['NOJS_TIMEOUT_CHAT'], ) @current_app.route('/chat/form.html') @@ -89,20 +92,22 @@ async def nojs_chat_form(user): nonce=generate_nonce(), digest=get_random_captcha_digest_for(user), default_name=get_default_name(user), + max_comment_length=CONFIG['CHAT_COMMENT_MAX_LENGTH'], + max_name_length=CONFIG['CHAT_NAME_MAX_LENGTH'], + max_password_length=CONFIG['CHAT_TRIPCODE_PASSWORD_MAX_LENGTH'], ) @current_app.post('/chat/form') @with_user_from(request) async def nojs_chat_form_redirect(user): comment = (await request.form).get('comment', '') - if len(comment) > CONFIG['CHAT_COMMENT_MAX_LENGTH']: - comment = '' - if comment: - state_id = add_state(user, comment=comment) + state_id = add_state( + user, + comment=comment[:CONFIG['CHAT_COMMENT_MAX_LENGTH']], + ) else: state_id = None - return redirect(url_for('nojs_chat_form', token=user['token'], state=state_id)) @current_app.post('/chat/message') @@ -117,7 +122,11 @@ async def nojs_submit_message(user): verification_happened = verify(user, digest, answer) except BadCaptcha as e: notice, *_ = e.args - state_id = add_state(user, notice=notice, comment=comment) + state_id = add_state( + user, + notice=notice, + comment=comment[:CONFIG['CHAT_COMMENT_MAX_LENGTH']], + ) else: nonce = form.get('nonce', '') try: @@ -131,7 +140,11 @@ async def nojs_submit_message(user): ) except Rejected as e: notice, *_ = e.args - state_id = add_state(user, notice=notice, comment=comment) + state_id = add_state( + user, + notice=notice, + comment=comment[:CONFIG['CHAT_COMMENT_MAX_LENGTH']], + ) else: state_id = None if message_was_added: diff --git a/anonstream/static/anonstream.js b/anonstream/static/anonstream.js index 2392443..1c68be4 100644 --- a/anonstream/static/anonstream.js +++ b/anonstream/static/anonstream.js @@ -33,7 +33,7 @@ const jsmarkup_chat_users = `\ const jsmarkup_chat_form = `\
- +
@@ -55,10 +55,10 @@ const jsmarkup_chat_form = `\
Name: - + Tripcode: - +
diff --git a/anonstream/templates/nojs_chat_form.html b/anonstream/templates/nojs_chat_form.html index 67f8bf2..c759b0d 100644 --- a/anonstream/templates/nojs_chat_form.html +++ b/anonstream/templates/nojs_chat_form.html @@ -220,7 +220,7 @@ {% endif %} - +
{% if digest %} @@ -231,7 +231,7 @@
- + @@ -247,7 +247,7 @@ {% endif %}
- +
diff --git a/anonstream/templates/nojs_chat_messages.html b/anonstream/templates/nojs_chat_messages.html index a22cf54..0647188 100644 --- a/anonstream/templates/nojs_chat_messages.html +++ b/anonstream/templates/nojs_chat_messages.html @@ -9,8 +9,8 @@ - - + +