Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
5e99e894e9 |
@ -40,7 +40,8 @@ dependencies = [
|
|||||||
"pillow-heif",
|
"pillow-heif",
|
||||||
"IP2Location",
|
"IP2Location",
|
||||||
"bleach",
|
"bleach",
|
||||||
"sentry-sdk"
|
"sentry-sdk",
|
||||||
|
"aiosqlite"
|
||||||
]
|
]
|
||||||
|
|
||||||
[tool.setuptools.packages.find]
|
[tool.setuptools.packages.find]
|
||||||
|
@ -9,7 +9,7 @@ from contextlib import asynccontextmanager
|
|||||||
|
|
||||||
from snek import snode
|
from snek import snode
|
||||||
from snek.view.threads import ThreadsView
|
from snek.view.threads import ThreadsView
|
||||||
|
from snek.system.ads import AsyncDataSet
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
@ -142,6 +142,7 @@ class Application(BaseApplication):
|
|||||||
client_max_size=1024 * 1024 * 1024 * 5 * args,
|
client_max_size=1024 * 1024 * 1024 * 5 * args,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
self.db = AsyncDataSet(kwargs["db_path"].replace("sqlite:///", ""))
|
||||||
session_setup(self, EncryptedCookieStorage(SESSION_KEY))
|
session_setup(self, EncryptedCookieStorage(SESSION_KEY))
|
||||||
self.tasks = asyncio.Queue()
|
self.tasks = asyncio.Queue()
|
||||||
self._middlewares.append(session_middleware)
|
self._middlewares.append(session_middleware)
|
||||||
@ -174,7 +175,7 @@ class Application(BaseApplication):
|
|||||||
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_stats(self, app):
|
async def prepare_stats(self, app):
|
||||||
app['stats'] = create_stats_structure()
|
app['stats'] = create_stats_structure()
|
||||||
@ -245,18 +246,8 @@ class Application(BaseApplication):
|
|||||||
|
|
||||||
|
|
||||||
async def prepare_database(self, app):
|
async def prepare_database(self, app):
|
||||||
self.db.query("PRAGMA journal_mode=WAL")
|
await self.db.query_raw("PRAGMA journal_mode=WAL")
|
||||||
self.db.query("PRAGMA syncnorm=off")
|
await self.db.query_raw("PRAGMA syncnorm=off")
|
||||||
|
|
||||||
try:
|
|
||||||
if not self.db["user"].has_index("username"):
|
|
||||||
self.db["user"].create_index("username", unique=True)
|
|
||||||
if not self.db["channel_member"].has_index(["channel_uid", "user_uid"]):
|
|
||||||
self.db["channel_member"].create_index(["channel_uid", "user_uid"])
|
|
||||||
if not self.db["channel_message"].has_index(["channel_uid", "user_uid"]):
|
|
||||||
self.db["channel_message"].create_index(["channel_uid", "user_uid"])
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
await self.services.drive.prepare_all()
|
await self.services.drive.prepare_all()
|
||||||
self.loop.create_task(self.task_runner())
|
self.loop.create_task(self.task_runner())
|
||||||
|
@ -101,6 +101,8 @@ class UserService(BaseService):
|
|||||||
model.username.value = username
|
model.username.value = username
|
||||||
model.password.value = await security.hash(password)
|
model.password.value = await security.hash(password)
|
||||||
if await self.save(model):
|
if await self.save(model):
|
||||||
|
for x in range(10):
|
||||||
|
print("Jazeker!!!")
|
||||||
if model:
|
if model:
|
||||||
channel = await self.services.channel.ensure_public_channel(
|
channel = await self.services.channel.ensure_public_channel(
|
||||||
model["uid"]
|
model["uid"]
|
||||||
|
@ -7,7 +7,8 @@ class UserPropertyService(BaseService):
|
|||||||
mapper_name = "user_property"
|
mapper_name = "user_property"
|
||||||
|
|
||||||
async def set(self, user_uid, name, value):
|
async def set(self, user_uid, name, value):
|
||||||
self.mapper.db["user_property"].upsert(
|
self.mapper.db.upsert(
|
||||||
|
"user_property",
|
||||||
{
|
{
|
||||||
"user_uid": user_uid,
|
"user_uid": user_uid,
|
||||||
"name": name,
|
"name": name,
|
||||||
|
1207
src/snek/system/ads.py
Normal file
1207
src/snek/system/ads.py
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,7 @@
|
|||||||
DEFAULT_LIMIT = 30
|
DEFAULT_LIMIT = 30
|
||||||
import asyncio
|
import asyncio
|
||||||
import typing
|
import typing
|
||||||
|
import traceback
|
||||||
from snek.system.model import BaseModel
|
from snek.system.model import BaseModel
|
||||||
|
|
||||||
|
|
||||||
@ -51,7 +51,9 @@ class BaseMapper:
|
|||||||
kwargs["uid"] = uid
|
kwargs["uid"] = uid
|
||||||
if not kwargs.get("deleted_at"):
|
if not kwargs.get("deleted_at"):
|
||||||
kwargs["deleted_at"] = None
|
kwargs["deleted_at"] = None
|
||||||
record = await self.run_in_executor(self.table.find_one, **kwargs)
|
#traceback.print_exc()
|
||||||
|
|
||||||
|
record = await self.db.get(self.table_name, kwargs)
|
||||||
if not record:
|
if not record:
|
||||||
return None
|
return None
|
||||||
record = dict(record)
|
record = dict(record)
|
||||||
@ -61,23 +63,29 @@ class BaseMapper:
|
|||||||
return model
|
return model
|
||||||
|
|
||||||
async def exists(self, **kwargs):
|
async def exists(self, **kwargs):
|
||||||
return await self.run_in_executor(self.table.exists, **kwargs)
|
return await self.db.count(self.table_name, kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
#return await self.run_in_executor(self.table.exists, **kwargs)
|
||||||
|
|
||||||
async def count(self, **kwargs) -> int:
|
async def count(self, **kwargs) -> int:
|
||||||
return await self.run_in_executor(self.table.count, **kwargs)
|
return await self.db.count(self.table_name,kwargs)
|
||||||
|
|
||||||
|
|
||||||
async def save(self, model: BaseModel) -> bool:
|
async def save(self, model: BaseModel) -> bool:
|
||||||
if not model.record.get("uid"):
|
if not model.record.get("uid"):
|
||||||
raise Exception(f"Attempt to save without uid: {model.record}.")
|
raise Exception(f"Attempt to save without uid: {model.record}.")
|
||||||
model.updated_at.update()
|
model.updated_at.update()
|
||||||
return await self.run_in_executor(self.table.upsert, model.record, ["uid"],use_semaphore=True)
|
await self.upsert(model)
|
||||||
|
return model
|
||||||
|
#return await self.run_in_executor(self.table.upsert, model.record, ["uid"],use_semaphore=True)
|
||||||
|
|
||||||
async def find(self, **kwargs) -> typing.AsyncGenerator:
|
async def find(self, **kwargs) -> typing.AsyncGenerator:
|
||||||
if not kwargs.get("_limit"):
|
if not kwargs.get("_limit"):
|
||||||
kwargs["_limit"] = self.default_limit
|
kwargs["_limit"] = self.default_limit
|
||||||
if not kwargs.get("deleted_at"):
|
if not kwargs.get("deleted_at"):
|
||||||
kwargs["deleted_at"] = None
|
kwargs["deleted_at"] = None
|
||||||
for record in await self.run_in_executor(self.table.find, **kwargs):
|
for record in await self.db.find(self.table_name, kwargs):
|
||||||
model = await self.new()
|
model = await self.new()
|
||||||
for key, value in record.items():
|
for key, value in record.items():
|
||||||
model[key] = value
|
model[key] = value
|
||||||
@ -88,21 +96,21 @@ class BaseMapper:
|
|||||||
return "insert" in sql or "update" in sql or "delete" in sql
|
return "insert" in sql or "update" in sql or "delete" in sql
|
||||||
|
|
||||||
async def query(self, sql, *args):
|
async def query(self, sql, *args):
|
||||||
for record in await self.run_in_executor(self.db.query, sql, *args, use_semaphore=await self._use_semaphore(sql)):
|
for record in await self.db.query(sql, *args):
|
||||||
yield dict(record)
|
yield dict(record)
|
||||||
|
|
||||||
async def update(self, model):
|
async def update(self, model):
|
||||||
if not model["deleted_at"] is None:
|
if not model["deleted_at"] is None:
|
||||||
raise Exception("Can't update deleted record.")
|
raise Exception("Can't update deleted record.")
|
||||||
model.updated_at.update()
|
model.updated_at.update()
|
||||||
return await self.run_in_executor(self.table.update, model.record, ["uid"],use_semaphore=True)
|
return await self.db.update(self.table_name, model.record, {"uid": model["uid"]})
|
||||||
|
|
||||||
async def upsert(self, model):
|
async def upsert(self, model):
|
||||||
model.updated_at.update()
|
model.updated_at.update()
|
||||||
return await self.run_in_executor(self.table.upsert, model.record, ["uid"],use_semaphore=True)
|
await self.db.upsert(self.table_name, model.record, {"uid": model["uid"]})
|
||||||
|
return model
|
||||||
|
|
||||||
async def delete(self, **kwargs) -> int:
|
async def delete(self, **kwargs) -> int:
|
||||||
if not kwargs or not isinstance(kwargs, dict):
|
if not kwargs or not isinstance(kwargs, dict):
|
||||||
raise Exception("Can't execute delete with no filter.")
|
raise Exception("Can't execute delete with no filter.")
|
||||||
kwargs["use_semaphore"] = True
|
return await self.db.delete(self.table_name, kwargs)
|
||||||
return await self.run_in_executor(self.table.delete, **kwargs)
|
|
||||||
|
@ -50,7 +50,7 @@ class BaseService:
|
|||||||
if result and result.__class__ == self.mapper.model_class:
|
if result and result.__class__ == self.mapper.model_class:
|
||||||
return result
|
return result
|
||||||
kwargs["uid"] = uid
|
kwargs["uid"] = uid
|
||||||
|
print(kwargs,"ZZZZZZZ")
|
||||||
result = await self.mapper.get(**kwargs)
|
result = await self.mapper.get(**kwargs)
|
||||||
if result:
|
if result:
|
||||||
await self.cache.set(result["uid"], result)
|
await self.cache.set(result["uid"], result)
|
||||||
|
@ -38,6 +38,10 @@ class WebView(BaseView):
|
|||||||
channel = await self.services.channel.get(
|
channel = await self.services.channel.get(
|
||||||
uid=self.request.match_info.get("channel")
|
uid=self.request.match_info.get("channel")
|
||||||
)
|
)
|
||||||
|
print(self.session.get("uid"),"ZZZZZZZZZZ")
|
||||||
|
qq = await self.services.user.get(uid=self.session.get("uid"))
|
||||||
|
|
||||||
|
print("GGGGGGGGGG",qq)
|
||||||
if not channel:
|
if not channel:
|
||||||
user = await self.services.user.get(
|
user = await self.services.user.get(
|
||||||
uid=self.request.match_info.get("channel")
|
uid=self.request.match_info.get("channel")
|
||||||
|
Loading…
Reference in New Issue
Block a user