2022-02-15 19:11:53 +09:00
|
|
|
import time
|
2022-02-16 18:55:30 +09:00
|
|
|
from math import inf
|
2022-02-15 19:11:53 +09:00
|
|
|
|
2022-02-14 19:16:09 +09:00
|
|
|
from quart import current_app
|
|
|
|
|
2022-02-16 18:55:30 +09:00
|
|
|
from anonstream.wrappers import with_timestamp, with_first_argument
|
2022-02-17 09:20:51 +09:00
|
|
|
from anonstream.helpers.user import is_visible
|
|
|
|
from anonstream.utils.user import user_for_websocket
|
2022-02-16 18:55:30 +09:00
|
|
|
from anonstream.utils import listmap
|
|
|
|
|
2022-02-17 09:20:51 +09:00
|
|
|
CONFIG = current_app.config
|
2022-02-14 19:16:09 +09:00
|
|
|
|
2022-02-15 19:11:53 +09:00
|
|
|
def add_notice(user, notice):
|
|
|
|
notice_id = time.time_ns() // 1_000_000
|
|
|
|
user['notices'][notice_id] = notice
|
2022-02-17 09:20:51 +09:00
|
|
|
if len(user['notices']) > CONFIG['MAX_NOTICES']:
|
2022-02-15 19:11:53 +09:00
|
|
|
user['notices'].popitem(last=False)
|
|
|
|
return notice_id
|
|
|
|
|
|
|
|
def pop_notice(user, notice_id):
|
|
|
|
try:
|
|
|
|
notice = user['notices'].pop(notice_id)
|
|
|
|
except KeyError:
|
|
|
|
notice = None
|
|
|
|
return notice
|
2022-02-16 18:55:30 +09:00
|
|
|
|
|
|
|
def see(user):
|
|
|
|
user['seen']['last'] = int(time.time())
|
|
|
|
|
|
|
|
@with_timestamp
|
|
|
|
def users_for_websocket(timestamp, messages, users):
|
|
|
|
visible_users = filter(
|
|
|
|
lambda user: is_visible(timestamp, messages, user),
|
|
|
|
users.values(),
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
user['token_hash']: user_for_websocket(user, include_token_hash=False)
|
|
|
|
for user in visible_users
|
|
|
|
}
|
|
|
|
|
|
|
|
last_checkup = -inf
|
|
|
|
|
|
|
|
def sunset(messages, users):
|
|
|
|
global last_checkup
|
|
|
|
|
|
|
|
timestamp = int(time.time())
|
2022-02-17 09:20:51 +09:00
|
|
|
if timestamp - last_checkup < CONFIG['USER_CHECKUP_PERIOD']:
|
2022-02-16 18:55:30 +09:00
|
|
|
return []
|
|
|
|
|
|
|
|
to_delete = []
|
|
|
|
for token in users:
|
|
|
|
user = users[token]
|
|
|
|
if not is_visible(timestamp, messages, user):
|
|
|
|
to_delete.append(token)
|
|
|
|
|
|
|
|
for index, token in enumerate(to_delete):
|
|
|
|
to_delete[index] = users.pop(token)['token_hash']
|
|
|
|
|
|
|
|
last_checkup = timestamp
|
|
|
|
return to_delete
|