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-13 13:00:10 +09:00
|
|
|
import base64
|
|
|
|
import hashlib
|
2022-05-28 14:37:41 +09:00
|
|
|
import math
|
2022-07-15 01:31:11 +09:00
|
|
|
import re
|
2022-02-13 13:00:10 +09:00
|
|
|
import secrets
|
2022-07-15 01:31:11 +09:00
|
|
|
from functools import lru_cache
|
|
|
|
|
|
|
|
from quart import escape
|
2022-02-13 13:00:10 +09:00
|
|
|
|
2022-02-15 19:11:53 +09:00
|
|
|
class NonceReuse(Exception):
|
|
|
|
pass
|
|
|
|
|
2022-02-13 13:00:10 +09:00
|
|
|
def generate_nonce():
|
|
|
|
return secrets.token_urlsafe(16)
|
|
|
|
|
2022-02-20 16:20:43 +09:00
|
|
|
def get_message_for_websocket(user, message):
|
2022-02-18 10:32:34 +09:00
|
|
|
message_keys = ('seq', 'date', 'time_minutes', 'time_seconds', 'markup')
|
2022-02-16 18:55:30 +09:00
|
|
|
user_keys = ('token_hash',)
|
|
|
|
return {
|
|
|
|
**{key: message[key] for key in message_keys},
|
|
|
|
**{key: user[key] for key in user_keys},
|
|
|
|
}
|
2022-05-28 14:37:41 +09:00
|
|
|
|
|
|
|
def get_approx_linespan(text):
|
|
|
|
def height(line):
|
|
|
|
return math.ceil(len(line) / 48)
|
|
|
|
linespan = sum(map(height, text.splitlines()))
|
|
|
|
linespan = linespan if linespan > 0 else 1
|
|
|
|
return linespan
|