This commit is contained in:
retoor 2025-08-18 22:28:14 +02:00
parent 486d78610c
commit 6f61fa323b
5 changed files with 53 additions and 54 deletions

View File

@ -1,3 +1,6 @@
import faulthandler
faulthandler.enable()
import asyncio import asyncio
import logging import logging
import pathlib import pathlib
@ -24,7 +27,7 @@ from aiohttp_session import (
from aiohttp_session.cookie_storage import EncryptedCookieStorage from aiohttp_session.cookie_storage import EncryptedCookieStorage
from app.app import Application as BaseApplication from app.app import Application as BaseApplication
from jinja2 import FileSystemLoader from jinja2 import FileSystemLoader
from snek.system.session import SQLiteSessionStorage
from snek.forum import setup_forum from snek.forum import setup_forum
from snek.mapper import get_mappers from snek.mapper import get_mappers
from snek.service import get_services from snek.service import get_services
@ -151,7 +154,8 @@ class Application(BaseApplication):
**kwargs, **kwargs,
) )
self.db = AsyncDataSet(kwargs["db_path"].replace("sqlite:///", "")) self.db = AsyncDataSet(kwargs["db_path"].replace("sqlite:///", ""))
session_setup(self, EncryptedCookieStorage(SESSION_KEY)) self.session_storage = SQLiteSessionStorage(SESSION_KEY)
session_setup(self, self.session_storage)
self.tasks = asyncio.Queue() self.tasks = asyncio.Queue()
self._middlewares.append(session_middleware) self._middlewares.append(session_middleware)
self._middlewares.append(auth_middleware) self._middlewares.append(auth_middleware)
@ -179,12 +183,17 @@ class Application(BaseApplication):
self.ip2location = IP2Location.IP2Location( self.ip2location = IP2Location.IP2Location(
base_path.joinpath("IP2LOCATION-LITE-DB11.BIN") base_path.joinpath("IP2LOCATION-LITE-DB11.BIN")
) )
self.on_startup.append(self.prepare_session_storage)
self.on_startup.append(self.prepare_stats) self.on_startup.append(self.prepare_stats)
self.on_startup.append(self.prepare_asyncio) self.on_startup.append(self.prepare_asyncio)
self.on_startup.append(self.start_user_availability_service) self.on_startup.append(self.start_user_availability_service)
self.on_startup.append(self.start_ssh_server) self.on_startup.append(self.start_ssh_server)
# self.on_startup.append(self.prepare_database) # self.on_startup.append(self.prepare_database)
async def prepare_session_storage(self, app):
await self.session_storage.setup()
print("Session storage prepared", flush=True)
async def prepare_stats(self, app): async def prepare_stats(self, app):
app["stats"] = create_stats_structure() app["stats"] = create_stats_structure()
print("Stats prepared", flush=True) print("Stats prepared", flush=True)

View File

@ -12,8 +12,8 @@ class ChannelMemberService(BaseService):
async def get_user_uids(self, channel_uid): async def get_user_uids(self, channel_uid):
async for model in self.mapper.query( async for model in self.mapper.query(
"SELECT user_uid FROM channel_member WHERE channel_uid=:channel_uid", "SELECT user_uid FROM channel_member WHERE channel_uid=?",
{"channel_uid": channel_uid}, channel_uid
): ):
yield model["user_uid"] yield model["user_uid"]
@ -46,17 +46,16 @@ class ChannelMemberService(BaseService):
async def get_dm(self, from_user, to_user): async def get_dm(self, from_user, to_user):
async for model in self.query( async for model in self.query(
"SELECT channel_member.* FROM channel_member INNER JOIN channel ON (channel.uid = channel_member.channel_uid and channel.tag = 'dm') INNER JOIN channel_member AS channel_member2 ON(channel_member2.channel_uid = channel.uid AND channel_member2.user_uid = :to_user) WHERE channel_member.user_uid=:from_user ", "SELECT channel_member.* FROM channel_member INNER JOIN channel ON (channel.uid = channel_member.channel_uid and channel.tag = 'dm') INNER JOIN channel_member AS channel_member2 ON(channel_member2.channel_uid = channel.uid AND channel_member2.user_uid = ?) WHERE channel_member.user_uid=?",
{"from_user": from_user, "to_user": to_user}, to_user, from_user
): ):
return model return model
if not from_user == to_user: if not from_user == to_user:
return None return None
async for model in self.query( async for model in self.query(
"SELECT channel_member.* FROM channel_member INNER JOIN channel ON (channel.uid = channel_member.channel_uid and channel.tag = 'dm') LEFT JOIN channel_member AS channel_member2 ON(channel_member2.channel_uid = NULL AND channel_member2.user_uid = NULL) WHERE channel_member.user_uid=:from_user ", "SELECT channel_member.* FROM channel_member INNER JOIN channel ON (channel.uid = channel_member.channel_uid and channel.tag = 'dm') LEFT JOIN channel_member AS channel_member2 ON(channel_member2.channel_uid IS NULL AND channel_member2.user_uid IS NULL) WHERE channel_member.user_uid=?",
{"from_user": from_user, "to_user": to_user}, to_user
): ):
return model return model
async def get_other_dm_user(self, channel_uid, user_uid): async def get_other_dm_user(self, channel_uid, user_uid):
@ -72,3 +71,4 @@ class ChannelMemberService(BaseService):
result = await self.create(channel_uid, from_user_uid) result = await self.create(channel_uid, from_user_uid)
await self.create(channel_uid, to_user_uid) await self.create(channel_uid, to_user_uid)
return result return result

View File

@ -12,7 +12,7 @@ class ChannelMessageService(BaseService):
self._configured_indexes = False self._configured_indexes = False
async def maintenance(self): async def maintenance(self):
for message in self.mapper.db["channel_message"].find(): for message in await self.mapper.db.find("channel_message"):
print(message) print(message)
try: try:
message = await self.get(uid=message["uid"]) message = await self.get(uid=message["uid"])
@ -21,8 +21,7 @@ class ChannelMessageService(BaseService):
html = message["html"] html = message["html"]
await self.save(message) await self.save(message)
self.mapper.db["channel_message"].upsert( self.mapper.db.upsert("channel_message",{
{
"uid": message["uid"], "uid": message["uid"],
"updated_at": updated_at, "updated_at": updated_at,
}, },
@ -78,18 +77,19 @@ class ChannelMessageService(BaseService):
if await super().save(model): if await super().save(model):
if not self._configured_indexes: if not self._configured_indexes:
if not self.mapper.db["channel_message"].has_index( pass
["is_final", "user_uid", "channel_uid"] #if not self.mapper.db["channel_message"].has_index(
): # ["is_final", "user_uid", "channel_uid"]
self.mapper.db["channel_message"].create_index( #):
["is_final", "user_uid", "channel_uid"], unique=False # self.mapper.db["channel_message"].create_index(
) # ["is_final", "user_uid", "channel_uid"], unique=False
if not self.mapper.db["channel_message"].has_index(["uid"]): # )
self.mapper.db["channel_message"].create_index(["uid"], unique=True) #if not self.mapper.db["channel_message"].has_index(["uid"]):
if not self.mapper.db["channel_message"].has_index(["deleted_at"]): # self.mapper.db["channel_message"].create_index(["uid"], unique=True)
self.mapper.db["channel_message"].create_index( #if not self.mapper.db["channel_message"].has_index(["deleted_at"]):
["deleted_at"], unique=False # self.mapper.db["channel_message"].create_index(
) # ["deleted_at"], unique=False
#)
self._configured_indexes = True self._configured_indexes = True
return model return model
raise Exception(f"Failed to create channel message: {model.errors}.") raise Exception(f"Failed to create channel message: {model.errors}.")
@ -99,10 +99,6 @@ class ChannelMessageService(BaseService):
if not user: if not user:
return {} return {}
# if not message["html"].startswith("<chat-message"):
# message = await self.get(uid=message["uid"])
# await self.save(message)
return { return {
"uid": message["uid"], "uid": message["uid"],
"color": user["color"], "color": user["color"],
@ -138,40 +134,33 @@ class ChannelMessageService(BaseService):
return [] return []
history_start_filter = "" history_start_filter = ""
if channel["history_start"]: if channel["history_start"]:
history_start_filter = f" AND created_at > '{channel['history_start']}'" history_start_filter = f" AND created_at > ?"
results = [] results = []
offset = page * page_size offset = page * page_size
try: try:
if timestamp: if timestamp:
async for model in self.query( async for model in self.query(
f"SELECT * FROM channel_message WHERE channel_uid=:channel_uid AND created_at < :timestamp {history_start_filter} ORDER BY created_at DESC LIMIT :page_size OFFSET :offset", f"SELECT * FROM channel_message WHERE channel_uid=? AND created_at < ? {history_start_filter} ORDER BY created_at DESC LIMIT ? OFFSET ?",
{ channel_uid,
"channel_uid": channel_uid, timestamp,
"page_size": page_size, page_size,
"offset": offset, offset
"timestamp": timestamp,
},
): ):
results.append(model) results.append(model)
elif page > 0: elif page > 0:
async for model in self.query( async for model in self.query(
f"SELECT * FROM channel_message WHERE channel_uid=:channel_uid WHERE created_at < :timestamp {history_start_filter} ORDER BY created_at DESC LIMIT :page_size", f"SELECT * FROM channel_message WHERE channel_uid=? AND created_at < ? {history_start_filter} ORDER BY created_at DESC LIMIT ?",
*{ channel_uid,
"channel_uid": channel_uid, timestamp,
"page_size": page_size, page_size
"offset": offset,
"timestamp": timestamp,
}.values(),
): ):
results.append(model) results.append(model)
else: else:
async for model in self.query( async for model in self.query(
f"SELECT * FROM channel_message WHERE channel_uid=:channel_uid {history_start_filter} ORDER BY created_at DESC LIMIT :page_size OFFSET :offset", f"SELECT * FROM channel_message WHERE channel_uid=? {history_start_filter} ORDER BY created_at DESC LIMIT ? OFFSET ?",
*{ channel_uid,
"channel_uid": channel_uid, page_size,
"page_size": page_size, offset
"offset": offset,
}.values(),
): ):
results.append(model) results.append(model)
@ -179,3 +168,4 @@ class ChannelMessageService(BaseService):
print(ex) print(ex)
results.sort(key=lambda x: x["created_at"]) results.sort(key=lambda x: x["created_at"])
return results return results

View File

@ -72,11 +72,9 @@ class ChatInputComponent extends NjetComponent {
focus() { focus() {
this.textarea.focus(); this.textarea.focus();
} }
getAuthors() { getAuthors() {
return this.users.flatMap((user) => [user.username, user.nick]); return this.users && this.users.flatMap ? this.users.flatMap(user => [user.username, user.nick]).filter(Boolean) : [];
} }
extractMentions(text) { extractMentions(text) {
return Array.from(text.matchAll(/@([a-zA-Z0-9_-]+)/g), m => m[1]); return Array.from(text.matchAll(/@([a-zA-Z0-9_-]+)/g), m => m[1]);
} }
@ -254,6 +252,7 @@ textToLeetAdvanced(text) {
app.rpc.getRecentUsers(this.channelUid).then(users => { app.rpc.getRecentUsers(this.channelUid).then(users => {
this.users = users; this.users = users;
console.info(users)
}); });
this.messageUid = null; this.messageUid = null;

View File

@ -525,6 +525,7 @@ class RPCView(BaseView):
except Exception as ex: except Exception as ex:
print(str(ex), flush=True) print(str(ex), flush=True)
logger.exception(ex) logger.exception(ex)
traceback.print_exc()
await self._send_json( await self._send_json(
{"callId": call_id, "success": False, "data": str(ex)} {"callId": call_id, "success": False, "data": str(ex)}
) )