264 lines
9.8 KiB
Python
264 lines
9.8 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import asyncio
|
|
import logging
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Form, Request
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
from devplacepy.database import get_setting
|
|
from devplacepy.database.sync import count_links, identity_map
|
|
from devplacepy.responses import action_result, json_error, respond
|
|
from devplacepy.schemas import AdminCommunitiesOut, AdminCommunityOut
|
|
from devplacepy.models import CommunityAddForm, CommunityRunForm, CommunityUpdateForm
|
|
from devplacepy.seo import base_seo_context, site_url, website_schema
|
|
from devplacepy.services.audit import record as audit
|
|
from devplacepy.services.community.discoverer import slug_for_query
|
|
from devplacepy.services.community.files import files
|
|
from devplacepy.services.community.runner import TRIGGER_ADMIN, CommunityRunner
|
|
from devplacepy.services.community.service import DRY_RUN_KEY
|
|
from devplacepy.services.community.spec import STATUS_LEARNING
|
|
from devplacepy.services.community.store import iso, now, store
|
|
from devplacepy.services.sync.gate import sync_enabled
|
|
from devplacepy.services.sync.options import SyncOptions
|
|
from devplacepy.utils import not_found, require_admin
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
background_jobs: set[asyncio.Task] = set()
|
|
LIST_URL = "/admin/communities"
|
|
|
|
|
|
def spawn(coroutine) -> None:
|
|
task = asyncio.create_task(coroutine)
|
|
background_jobs.add(task)
|
|
task.add_done_callback(background_jobs.discard)
|
|
|
|
|
|
def service_flags() -> dict:
|
|
return {
|
|
"service_enabled": (get_setting("service_community_enabled", "0") or "0") == "1",
|
|
"service_dry_run": (get_setting(DRY_RUN_KEY, "1") or "1") == "1",
|
|
}
|
|
|
|
|
|
def public(row: dict) -> dict:
|
|
view = {key: value for key, value in row.items() if key != "login_password"}
|
|
view["has_login"] = bool(row.get("login_password"))
|
|
return view
|
|
|
|
|
|
def require_community(slug: str) -> dict:
|
|
row = store.get(slug)
|
|
if row is None:
|
|
raise not_found("Unknown community")
|
|
return row
|
|
|
|
|
|
def credentials_of(data) -> dict:
|
|
return {"username": data.login_username.strip(), "email": data.login_email.strip(), "password": data.login_password}
|
|
|
|
|
|
def seo(request: Request, title: str, path: str, crumb: str):
|
|
base = site_url(request)
|
|
return base_seo_context(
|
|
request,
|
|
title=f"{title} - Admin",
|
|
description="External communities synchronised into this instance.",
|
|
breadcrumbs=[
|
|
{"name": "Home", "url": "/feed"},
|
|
{"name": "Admin", "url": "/admin"},
|
|
{"name": crumb, "url": path},
|
|
],
|
|
schemas=[website_schema(base)],
|
|
robots="noindex,nofollow",
|
|
)
|
|
|
|
|
|
def feed_summary(feed: dict) -> dict:
|
|
return {
|
|
"id": feed["id"],
|
|
"type": feed["type"],
|
|
"url": feed["url"],
|
|
"list_path": feed.get("list_path"),
|
|
"item_selector": feed.get("item_selector"),
|
|
"post_fields": feed["post"]["fields"],
|
|
"author_fields": (feed.get("author") or {}).get("fields", {}),
|
|
"comments": (feed.get("comments") or {}).get("mode", "none"),
|
|
"comments_reason": (feed.get("comments") or {}).get("reason", ""),
|
|
"pagination": (feed.get("pagination") or {}).get("style", "none"),
|
|
}
|
|
|
|
|
|
@router.get("/communities", response_class=HTMLResponse)
|
|
async def admin_communities(request: Request):
|
|
admin = require_admin(request)
|
|
return respond(
|
|
request,
|
|
"admin_communities.html",
|
|
{
|
|
**seo(request, "Communities", LIST_URL, "Communities"),
|
|
"request": request,
|
|
"user": admin,
|
|
"items": [public(row) for row in store.list()],
|
|
"sync_enabled": sync_enabled(),
|
|
**service_flags(),
|
|
"admin_section": "communities",
|
|
},
|
|
model=AdminCommunitiesOut,
|
|
)
|
|
|
|
|
|
@router.post("/communities")
|
|
async def admin_community_add(request: Request, data: Annotated[CommunityAddForm, Form()]):
|
|
admin = require_admin(request)
|
|
slug = slug_for_query(data.query)
|
|
if not slug:
|
|
return json_error(400, "Enter a community name or address")
|
|
existing = store.get(slug)
|
|
if existing is None:
|
|
secrets = credentials_of(data)
|
|
existing = store.create(
|
|
slug,
|
|
data.query,
|
|
admin["uid"],
|
|
name=data.query,
|
|
status=STATUS_LEARNING,
|
|
claimed_at=iso(now()),
|
|
login_username=secrets["username"],
|
|
login_email=secrets["email"],
|
|
login_password=secrets["password"],
|
|
)
|
|
spawn(CommunityRunner().discover(data.query, admin["uid"], relearn_slug=slug))
|
|
audit.record(
|
|
request,
|
|
"admin.community.add",
|
|
user=admin,
|
|
target_type="community",
|
|
target_uid=existing["uid"],
|
|
target_label=slug,
|
|
metadata={"query": data.query},
|
|
summary=f"{admin['username']} added community {data.query!r}",
|
|
)
|
|
return action_result(request, f"{LIST_URL}/{existing['slug']}")
|
|
|
|
|
|
@router.get("/communities/{slug}", response_class=HTMLResponse)
|
|
async def admin_community(request: Request, slug: str):
|
|
admin = require_admin(request)
|
|
row = require_community(slug)
|
|
source = files.read(row["slug"])
|
|
return respond(
|
|
request,
|
|
"admin_community.html",
|
|
{
|
|
**seo(request, row["name"] or slug, f"{LIST_URL}/{slug}", "Communities"),
|
|
"request": request,
|
|
"user": admin,
|
|
"community": public(row),
|
|
"rules": row.get("rules") or "",
|
|
"favicon": row.get("favicon") or "",
|
|
"totals": row.get("totals") or {},
|
|
"feeds": [feed_summary(feed) for feed in (row.get("schema") or {}).get("feeds", [])],
|
|
"runs": store.runs(row["uid"]),
|
|
"collectors_path": str(files.collectors_path(row["slug"])) if source else "",
|
|
"collectors_lines": len(source.splitlines()),
|
|
"busy": store.busy(row["uid"]),
|
|
"sync_enabled": sync_enabled(),
|
|
"admin_section": "communities",
|
|
},
|
|
model=AdminCommunityOut,
|
|
)
|
|
|
|
|
|
@router.post("/communities/{slug}/run")
|
|
async def admin_community_run(request: Request, slug: str, data: Annotated[CommunityRunForm, Form()]):
|
|
admin = require_admin(request)
|
|
row = require_community(slug)
|
|
if store.busy(row["uid"]):
|
|
return json_error(409, "A run is already in progress for this community")
|
|
spawn(CommunityRunner().run(row, dry_run=data.dry_run, trigger=TRIGGER_ADMIN))
|
|
audit.record(
|
|
request,
|
|
"admin.community.run",
|
|
user=admin,
|
|
target_type="community",
|
|
target_uid=row["uid"],
|
|
target_label=slug,
|
|
metadata={"dry_run": data.dry_run},
|
|
summary=f"{admin['username']} started a {'dry run' if data.dry_run else 'run'} of community {slug}",
|
|
)
|
|
return action_result(request, f"{LIST_URL}/{slug}")
|
|
|
|
|
|
@router.post("/communities/{slug}/relearn")
|
|
async def admin_community_relearn(request: Request, slug: str):
|
|
admin = require_admin(request)
|
|
row = require_community(slug)
|
|
if store.busy(row["uid"]):
|
|
return json_error(409, "A run is already in progress for this community")
|
|
spawn(CommunityRunner().discover(row["query"] or row["url"], admin["uid"], relearn_slug=slug))
|
|
audit.record(
|
|
request,
|
|
"admin.community.relearn",
|
|
user=admin,
|
|
target_type="community",
|
|
target_uid=row["uid"],
|
|
target_label=slug,
|
|
summary=f"{admin['username']} started relearning community {slug}",
|
|
)
|
|
return action_result(request, f"{LIST_URL}/{slug}")
|
|
|
|
|
|
@router.post("/communities/{slug}/update")
|
|
async def admin_community_update(request: Request, slug: str, data: Annotated[CommunityUpdateForm, Form()]):
|
|
admin = require_admin(request)
|
|
row = require_community(slug)
|
|
changes = {"interval_seconds": data.interval_seconds, "enabled": int(data.enabled)}
|
|
if data.clear_login:
|
|
changes.update(login_username="", login_email="", login_password="")
|
|
else:
|
|
for column, value in (("login_username", data.login_username.strip()), ("login_email", data.login_email.strip()), ("login_password", data.login_password)):
|
|
if value:
|
|
changes[column] = value
|
|
submitted = {name: getattr(data, name) for name in SyncOptions.__dataclass_fields__ if getattr(data, name) is not None}
|
|
if submitted:
|
|
try:
|
|
options = SyncOptions.from_row(row).merged(**submitted)
|
|
except ValueError as exc:
|
|
return json_error(400, str(exc))
|
|
if options.alias and not SyncOptions.from_row(row).alias and not identity_map(row["origin"]) and count_links(row["origin"]):
|
|
return json_error(400, "This community already has records stored under their original names; aliasing cannot be switched on afterwards")
|
|
changes["sync_options"] = options.to_dict()
|
|
store.update(row["uid"], **changes)
|
|
audit.record(
|
|
request,
|
|
"admin.community.update",
|
|
user=admin,
|
|
target_type="community",
|
|
target_uid=row["uid"],
|
|
target_label=slug,
|
|
metadata={"interval_seconds": data.interval_seconds, "enabled": data.enabled, "sync_options": changes.get("sync_options"), "login_changed": data.clear_login or bool(data.login_password or data.login_username or data.login_email)},
|
|
summary=f"{admin['username']} updated community {slug}",
|
|
)
|
|
return action_result(request, f"{LIST_URL}/{slug}")
|
|
|
|
|
|
@router.post("/communities/{slug}/delete")
|
|
async def admin_community_delete(request: Request, slug: str):
|
|
admin = require_admin(request)
|
|
row = require_community(slug)
|
|
store.delete(row["uid"], admin["uid"])
|
|
audit.record(
|
|
request,
|
|
"admin.community.delete",
|
|
user=admin,
|
|
target_type="community",
|
|
target_uid=row["uid"],
|
|
target_label=slug,
|
|
summary=f"{admin['username']} deleted community {slug}",
|
|
)
|
|
return action_result(request, LIST_URL)
|