2022-06-16 10:12:37 +09:00
|
|
|
# SPDX-FileCopyrightText: 2022 n9k <https://git.076.ne.jp/ninya9k>
|
2022-03-07 23:51:59 +09:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2022-02-17 09:20:51 +09:00
|
|
|
import hashlib
|
2022-07-20 15:04:55 +09:00
|
|
|
from functools import lru_cache
|
2022-02-17 09:20:51 +09:00
|
|
|
|
2022-07-16 13:25:47 +09:00
|
|
|
import markupsafe
|
2022-07-20 15:04:55 +09:00
|
|
|
from quart import current_app, escape, url_for, Markup
|
2022-02-17 09:20:51 +09:00
|
|
|
|
|
|
|
CONFIG = current_app.config
|
2022-07-15 01:31:11 +09:00
|
|
|
EMOTES = current_app.emotes
|
2022-02-17 09:20:51 +09:00
|
|
|
|
|
|
|
def generate_nonce_hash(nonce):
|
|
|
|
parts = CONFIG['SECRET_KEY'] + b'nonce-hash\0' + nonce.encode()
|
2022-06-14 20:40:16 +09:00
|
|
|
return hashlib.sha256(parts).hexdigest()
|
2022-02-18 21:24:19 +09:00
|
|
|
|
|
|
|
def get_scrollback(messages):
|
|
|
|
n = CONFIG['MAX_CHAT_SCROLLBACK']
|
|
|
|
if len(messages) < n:
|
|
|
|
return messages
|
|
|
|
return list(messages)[-n:]
|
2022-07-15 01:31:11 +09:00
|
|
|
|
2022-07-20 15:04:55 +09:00
|
|
|
@lru_cache
|
|
|
|
def get_emote_markup(emote_name, emote_file, emote_width, emote_height):
|
|
|
|
emote_name_markup = escape(emote_name)
|
|
|
|
return Markup(
|
|
|
|
f'''<img class="emote" '''
|
2022-07-24 20:01:03 +09:00
|
|
|
f'''src="{escape(url_for('static', filename=emote_file))}" '''
|
2022-07-20 15:04:55 +09:00
|
|
|
f'''width="{escape(emote_width)}" height="{escape(emote_height)}" '''
|
|
|
|
f'''alt="{emote_name_markup}" title="{emote_name_markup}">'''
|
|
|
|
)
|
|
|
|
|
2022-07-15 01:31:11 +09:00
|
|
|
def insert_emotes(markup):
|
2022-07-16 13:25:47 +09:00
|
|
|
assert isinstance(markup, markupsafe.Markup)
|
2022-07-20 15:04:55 +09:00
|
|
|
for emote in EMOTES:
|
|
|
|
emote_markup = get_emote_markup(
|
|
|
|
emote['name'], emote['file'], emote['width'], emote['height'],
|
2022-07-15 01:31:11 +09:00
|
|
|
)
|
2022-07-20 15:04:55 +09:00
|
|
|
markup = emote['regex'].sub(emote_markup, markup)
|
2022-07-15 01:31:11 +09:00
|
|
|
return Markup(markup)
|