Skip to content

Commit

Permalink
implement getPreferences/putPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBuchanan314 committed Dec 21, 2024
1 parent a5d8437 commit a067288
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/millipds/appview_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async def service_proxy(request: web.Request, service: Optional[str] = None):
return web.Response(
body=body_bytes, content_type=r.content_type, status=r.status
) # XXX: allowlist safe content types!
elif request.method == "PUT":
elif request.method == "PUT": # are xrpc requests ever PUT?
raise NotImplementedError("TODO: PUT")
else:
raise NotImplementedError("TODO")
2 changes: 1 addition & 1 deletion src/millipds/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def create_account(
(
did,
handle,
b"{}",
b'{"preferences":[]}',
pw_hash,
privkey_pem,
bytes(commit_cid),
Expand Down
39 changes: 34 additions & 5 deletions src/millipds/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,44 @@ async def health(request: web.Request):

# we should not be implementing bsky-specific logic here!
# (ideally, a PDS should not be aware of app-specific logic)
@routes.post("/xrpc/app.bsky.actor.putPreferences")
@authenticated
async def actor_put_preferences(request: web.Request):
# NOTE: we don't try to pull out the specific "preferences" field
prefs = await request.json()
pref_bytes = json.dumps(
prefs,
ensure_ascii=False, # more compact
separators=(",", ":"), # likewise
check_circular=False, # impossible, checking would be a waste
).encode()
db = get_db(request)
db.con.execute(
"UPDATE user SET prefs=? WHERE did=?",
(pref_bytes, request["authed_did"]),
)
return web.Response()


@routes.get("/xrpc/app.bsky.actor.getPreferences")
@authenticated
async def actor_get_preferences(request: web.Request):
return web.json_response({"preferences": []}) # dummy response
db = get_db(request)
row = db.con.execute(
"SELECT prefs FROM user WHERE did=?", (request["authed_did"],)
).fetchone()

# should be impossible, otherwise we wouldn't be auth'd
assert row is not None

@routes.post("/xrpc/app.bsky.actor.putPreferences")
async def actor_put_preferences(request: web.Request):
# TODO: actually implement this
return web.Response()
prefs = json.loads(row[0])

# TODO: in the future™ this will be unnecessary because we initialize it
# properly during account creation and/or I wrote a db migration script
if not prefs:
prefs = {"preferences": []}

return web.json_response(prefs)


@routes.get("/xrpc/com.atproto.identity.resolveHandle")
Expand Down

0 comments on commit a067288

Please sign in to comment.