associate reports with users

this will definitely need some more work.
This commit is contained in:
Jack Case
2025-10-26 16:05:20 +00:00
parent 5840b82bcc
commit 360872fdd0
2 changed files with 23 additions and 4 deletions

View File

@@ -1,9 +1,10 @@
from collections.abc import Iterable from collections.abc import Iterable
from datetime import datetime
from urllib.parse import ParseResult from urllib.parse import ParseResult
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.engine import Engine from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from slopserver.models import Domain, Path, User from slopserver.models import Domain, Path, User, Report
def select_slop(urls: list[ParseResult], engine: Engine) -> Iterable[Domain]: def select_slop(urls: list[ParseResult], engine: Engine) -> Iterable[Domain]:
query = select(Domain).where(Domain.domain_name.in_(url[1] for url in urls)) query = select(Domain).where(Domain.domain_name.in_(url[1] for url in urls))
@@ -11,7 +12,7 @@ def select_slop(urls: list[ParseResult], engine: Engine) -> Iterable[Domain]:
rows = session.scalars(query).all() rows = session.scalars(query).all()
return rows return rows
def insert_slop(urls: list[ParseResult], engine: Engine): def insert_slop(urls: list[ParseResult], engine: Engine, user: User | None = None):
domain_dict: dict[str. set[str]] = dict() domain_dict: dict[str. set[str]] = dict()
for url in urls: for url in urls:
if not domain_dict.get(url[1]): if not domain_dict.get(url[1]):
@@ -35,13 +36,25 @@ def insert_slop(urls: list[ParseResult], engine: Engine):
new_domain = Domain(domain_name=domain, paths=list()) new_domain = Domain(domain_name=domain, paths=list())
new_domain.paths = [Path(path=path) for path in paths] new_domain.paths = [Path(path=path) for path in paths]
session.add(new_domain) session.add(new_domain)
if user:
for path in new_domain.paths:
new_report = Report(path_id=path.id, user_id=user.id)
session.add(new_report)
else: else:
existing_domain = existing_dict[domain] existing_domain = existing_dict[domain]
existing_paths = set((path.path for path in existing_domain.paths)) existing_paths = set((path.path for path in existing_domain.paths))
for path in paths: for path in paths:
if not path in existing_paths: if not path in existing_paths:
existing_domain.paths.append(Path(path=path)) new_path = Path(path=path)
existing_domain.paths.append(new_path)
session.add(new_path)
session.flush([new_path])
session.refresh(new_path)
if user:
new_report = Report(
path_id=new_path.id, user_id=user.id, timestamp=datetime.now())
session.add(new_report)
session.commit() session.commit()

View File

@@ -95,15 +95,21 @@ def generate_auth_token(username):
encoded_jwt = jwt.encode(bearer_token, TOKEN_SECRET, ALGO) encoded_jwt = jwt.encode(bearer_token, TOKEN_SECRET, ALGO)
return encoded_jwt return encoded_jwt
def get_token_user(decoded_token):
user = get_user(decoded_token["sub"], DB_ENGINE)
return user
def verify_auth_token(token: str): def verify_auth_token(token: str):
try: try:
token = jwt.decode(token, TOKEN_SECRET, ALGO, audience="slopserver") token = jwt.decode(token, TOKEN_SECRET, ALGO, audience="slopserver")
return token
except: except:
raise HTTPException(status_code=401, detail="invalid access token") raise HTTPException(status_code=401, detail="invalid access token")
@app.post("/report") @app.post("/report")
async def report_slop(report: SlopReport, bearer: Annotated[str, AfterValidator(verify_auth_token), Header()]): async def report_slop(report: SlopReport, bearer: Annotated[str, AfterValidator(verify_auth_token), Header()]):
insert_slop(report.slop_urls, DB_ENGINE) user = get_token_user(bearer)
insert_slop(report.slop_urls, DB_ENGINE, user)
@app.post("/check") @app.post("/check")
async def check_slop(check: Annotated[SlopReport, Body()], bearer: Annotated[str, AfterValidator(verify_auth_token), Header()]): async def check_slop(check: Annotated[SlopReport, Body()], bearer: Annotated[str, AfterValidator(verify_auth_token), Header()]):