Alternative ATProto PDS implementation
1/// HACK: store private user preferences in the PDS.
2///
3/// We shouldn't have to know about any bsky endpoints to store private user data.
4/// This will _very likely_ be changed in the future.
5use atrium_api::app::bsky::actor;
6use axum::{Json, routing::post};
7use constcat::concat;
8use diesel::prelude::*;
9
10use crate::actor_store::ActorStore;
11
12use super::*;
13
14async fn put_preferences(
15 user: AuthenticatedUser,
16 State(actor_pools): State<std::collections::HashMap<String, ActorPools>>,
17 Json(input): Json<actor::put_preferences::Input>,
18) -> Result<()> {
19 let did = user.did();
20 let json_string =
21 serde_json::to_string(&input.preferences).context("failed to serialize preferences")?;
22
23 // let conn = &mut actor_pools
24 // .get(&did)
25 // .context("failed to get actor pool")?
26 // .repo
27 // .get()
28 // .await
29 // .expect("failed to get database connection");
30 // conn.interact(move |conn| {
31 // diesel::update(accounts::table)
32 // .filter(accounts::did.eq(did))
33 // .set(accounts::private_prefs.eq(json_string))
34 // .execute(conn)
35 // .context("failed to update user preferences")
36 // });
37 todo!("Use actor_store's preferences writer instead");
38 Ok(())
39}
40
41async fn get_preferences(
42 user: AuthenticatedUser,
43 State(actor_pools): State<std::collections::HashMap<String, ActorPools>>,
44) -> Result<Json<actor::get_preferences::Output>> {
45 let did = user.did();
46 // let conn = &mut actor_pools
47 // .get(&did)
48 // .context("failed to get actor pool")?
49 // .repo
50 // .get()
51 // .await
52 // .expect("failed to get database connection");
53
54 // #[derive(QueryableByName)]
55 // struct Prefs {
56 // #[diesel(sql_type = diesel::sql_types::Text)]
57 // private_prefs: Option<String>,
58 // }
59
60 // let result = conn
61 // .interact(move |conn| {
62 // diesel::sql_query("SELECT private_prefs FROM accounts WHERE did = ?")
63 // .bind::<diesel::sql_types::Text, _>(did)
64 // .get_result::<Prefs>(conn)
65 // })
66 // .await
67 // .expect("failed to fetch preferences");
68
69 // if let Some(prefs_json) = result.private_prefs {
70 // let prefs: actor::defs::Preferences =
71 // serde_json::from_str(&prefs_json).context("failed to deserialize preferences")?;
72
73 // Ok(Json(
74 // actor::get_preferences::OutputData { preferences: prefs }.into(),
75 // ))
76 // } else {
77 // Ok(Json(
78 // actor::get_preferences::OutputData {
79 // preferences: Vec::new(),
80 // }
81 // .into(),
82 // ))
83 // }
84 todo!("Use actor_store's preferences writer instead");
85}
86
87/// Register all actor endpoints.
88pub(crate) fn routes() -> Router<AppState> {
89 // AP /xrpc/app.bsky.actor.putPreferences
90 // AG /xrpc/app.bsky.actor.getPreferences
91 Router::new()
92 .route(
93 concat!("/", actor::put_preferences::NSID),
94 post(put_preferences),
95 )
96 .route(
97 concat!("/", actor::get_preferences::NSID),
98 get(get_preferences),
99 )
100}