Something went wrong. Try again.
decentralized and customizable links page on top of atproto ligo.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389import asyncioimport jsonfrom typing import Any, NamedTuple, cast
from aiohttp.client import ClientSessionfrom atproto import ( get_record, is_valid_did, resolve_did_from_handle, resolve_pds_from_did,)from atproto.oauth import pds_authed_reqfrom atproto.types import DID, Handle, OAuthSession, PdsUrlfrom flask import Flask, g, redirect, render_template, request, session, url_forfrom flask_htmx import HTMXfrom flask_htmx import make_response as htmx_responsefrom xrpc import xrpc
from src.auth import ( get_auth_session, refresh_auth_session, save_auth_session,)from src.config import AuthServer, Configfrom src.db import KV, close_db_connection, get_db, init_dbfrom src.oauth import oauth, oauth_metadata
app = Flask(__name__)_ = app.config.from_prefixed_env()app.register_blueprint(oauth)app.register_blueprint(xrpc)htmx = HTMX()htmx.init_app(app)init_db(app, name="config")init_db(app, name="keyval")
@app.before_requestasync def load_user_to_context(): g.user = get_auth_session(session)
async def get_user() -> OAuthSession | None: user = cast(OAuthSession | None, g.user) if user is not None and user.is_expired(): async with ClientSession() as client: user = await refresh_auth_session(session, client, user) return user
@app.teardown_appcontextasync def app_teardown(exception: BaseException | None): close_db_connection(exception)
@app.get("/")def page_home(): return render_template("index.html")
@app.get("/<string:atid>")async def page_profile(atid: str): reload = request.args.get("reload") is not None
db = get_db(app, name="keyval") didkv = KV[Handle, DID](db, app.logger, "did_from_handle") pdskv = KV[DID, PdsUrl](db, app.logger, "pds_from_did")
async with ClientSession() as client: if atid.startswith("@"): handle = atid[1:].lower() did = await resolve_did_from_handle(client, handle, kv=didkv, reload=reload) if did is None: return render_template("error.html", message="did not found"), 404 elif is_valid_did(atid): handle = None did = atid else: return render_template("error.html", message="invalid did or handle"), 400
if _is_did_blocked(did): return render_template("error.html", message="profile not found"), 404
pds = await resolve_pds_from_did(client, did=did, kv=pdskv, reload=reload) if pds is None: return render_template("error.html", message="pds not found"), 404 (profile, _), link_sections = await asyncio.gather( load_profile(client, pds, did, reload=reload), load_links(client, pds, did, reload=reload), )
if profile is None: profile = {} if link_sections is None: link_sections = []
if reload: # remove the ?reload parameter return redirect(request.path)
if handle: profile["handle"] = handle athref = f"at://{did}/at.ligo.actor.links/self" return render_template( "profile.html", did=did, profile=profile, sections=link_sections, athref=athref, )
@app.get("/login")async def page_login(): if await get_user() is not None: return redirect("/editor") config = Config(app) auth_servers = config.auth_servers() if app.debug: auth_servers.append(AuthServer("pds.rip", "https://pds.rip")) return render_template("login.html", auth_servers=auth_servers)
@app.post("/login")def auth_login(): value = request.form.get("username") or request.form.get("authserver") if value and value[0] == "@": value = value[1:] if not value: return redirect(url_for("page_login"), 303) return redirect(url_for("oauth.oauth_start", username_or_authserver=value), 303)
@app.route("/auth/logout")def auth_logout(): session.clear() return redirect(url_for("page_login"), 303)
@app.get("/editor")async def page_editor(): user = await get_user() if user is None: return redirect("/login", 302)
did: str = user.did pds: str = user.pds_url handle: str | None = user.handle
async with ClientSession() as client: (profile, from_bluesky), link_sections = await asyncio.gather( load_profile(client, pds, did), load_links(client, pds, did), )
links = [] if link_sections: links = link_sections[0].links
return render_template( "editor.html", handle=handle, profile=profile, profile_from_bluesky=from_bluesky, links=json.dumps(links), )
@app.post("/editor/profile")async def post_editor_profile(): user = await get_user() if user is None: url = url_for("auth_logout") return htmx_response(redirect=url) if htmx else redirect(url, 303)
display_name = request.form.get("displayName") description = request.form.get("description", "") if not display_name: return redirect("/editor", 303)
record = { "$type": "at.ligo.actor.profile", "displayName": display_name, "description": description, }
success = await put_record( user=user, pds=user.pds_url, repo=user.did, collection="at.ligo.actor.profile", rkey="self", record=record, )
if success: kv = KV(app, app.logger, "profile_from_did") kv.set(user.did, json.dumps(record)) else: app.logger.warning("log out user for now") url = url_for("auth_logout") return htmx_response(redirect=url) if htmx else redirect(url, 303)
if htmx: return htmx_response( render_template("_editor_profile.html", profile=record), reswap="outerHTML", )
return redirect(url_for("page_editor"), 303)
@app.post("/editor/links")async def post_editor_links(): user = await get_user() if user is None: url = url_for("auth_logout") return htmx_response(redirect=url) if htmx else redirect(url, 303)
links: list[dict[str, str]] = [] hrefs = request.form.getlist("link-href") titles = request.form.getlist("link-title") subtitles = request.form.getlist("link-subtitle") backgrounds = request.form.getlist("link-background-color") for href, title, subtitle, background in zip(hrefs, titles, subtitles, backgrounds): if not href or not title or not background: break link: dict[str, str] = { "href": href, "title": title, "backgroundColor": background, } if subtitle: link["subtitle"] = subtitle links.append(link)
record = { "$type": "at.ligo.actor.links", "sections": [ { "title": "", "links": links, } ], }
success = await put_record( user=user, pds=user.pds_url, repo=user.did, collection="at.ligo.actor.links", rkey="self", record=record, )
if success: kv = KV(app, app.logger, "links_from_did") kv.set(user.did, json.dumps(record)) else: app.logger.warning("log out user for now") url = url_for("auth_logout") return htmx_response(redirect=url) if htmx else redirect(url, 303)
if htmx: return htmx_response( render_template("_editor_links.html", links=links), reswap="outerHTML", )
return redirect(url_for("page_editor"), 303)
@app.get("/terms")def page_terms(): return render_template("terms.html")
@app.get("/.well-known/oauth-client-metadata.json")def well_known_oauth_metadata(): response = oauth_metadata() return response, 200, {"Access-Control-Allow-Origin": "*"}
class LinkSection(NamedTuple): title: str links: list[dict[str, str]]
async def load_links( client: ClientSession, pds: str, did: str, reload: bool = False,) -> list[LinkSection] | None: kv = KV(app, app.logger, "links_from_did") record_json = kv.get(did)
if record_json is not None and not reload: parsed = json.loads(record_json) return _links_or_sections(parsed)
record = await get_record(client, pds, did, "at.ligo.actor.links", "self") if record is None: return None
kv.set(did, value=json.dumps(record)) return _links_or_sections(record)
def _links_or_sections(raw: dict[str, Any]) -> list[LinkSection] | None: if "sections" in raw: return list(map(lambda s: LinkSection(**s), raw["sections"])) elif "links" in raw: return [LinkSection("", raw["links"])] else: return None
async def load_profile( client: ClientSession, pds: str, did: str, fallback_with_bluesky: bool = True, reload: bool = False,) -> tuple[dict[str, str] | None, bool]: kv = KV(app, app.logger, "profile_from_did") record_json = kv.get(did)
if record_json is not None and not reload: return json.loads(record_json), False
(record, bsky_record) = await asyncio.gather( get_record(client, pds, did, "at.ligo.actor.profile", "self"), get_record(client, pds, did, "app.bsky.actor.profile", "self"), )
from_bluesky = False if record is None and fallback_with_bluesky: record = bsky_record from_bluesky = True
if record is not None: kv.set(did, value=json.dumps(record))
return record, from_bluesky
# TODO: move to .atprotoasync def put_record( user: OAuthSession, pds: PdsUrl, repo: str, collection: str, rkey: str, record: dict[str, Any],) -> bool: """Writes the record onto the users PDS. Returns bool for success."""
endpoint = f"{pds}/xrpc/com.atproto.repo.putRecord" body = { "repo": repo, "collection": collection, "rkey": rkey, "record": record, }
def update_dpop_pds_nonce(nonce: str): session_ = user._replace(dpop_pds_nonce=nonce) save_auth_session(session, session_)
response = await pds_authed_req( method="POST", url=endpoint, body=body, user=user, update_dpop_pds_nonce=update_dpop_pds_nonce, )
if not response.ok: app.logger.warning(f"put_record failed with status {response.status}") app.logger.warning(await response.text())
return response.ok
def _is_did_blocked(did: str) -> bool: kv = KV(app, app.logger, "blockeddids") return kv.get(did) is not None