maarcadetweet: initial commit
AT Protocol PDS + AppView + Tauri Desktop Client, 160-char post limit. - PDS (Rust + axum + sqlx) - Auth: createAccount, createSession, refreshSession - Records: createRecord, deleteRecord (race-safe via SELECT FOR UPDATE) - Feed: feed.like.create, feed.repost.create - Sync: getRepo, getBlocks, getLatestCommit, getRecord (with MST proof), listRepos - Identity: resolveHandle - MST: spec-conformant (at-mst crate, 27 tests) - Repo: signed commits, TID counter (monotonic, 4096 wrap safe) - AppView (Rust + axum + sqlx) - Jetstream consumer (WebSocket, exponential backoff, 38k+ events indexed) - REST API: timeline/home (graph-aware), profile, search, post (with thread hydration) - Handle-sync worker (did:plc + did:web) - JSONB embed storage + thread columns (migration 0003) - Like/repost counter cache (migration 0004) - Tauri 2 + Svelte 5 Desktop Client - System tray (Show/Compose/Quit menu) - OS notifications (tauri-plugin-notification) - Auto-update (tauri-plugin-updater, placeholder endpoint) - Window-state (tauri-plugin-window-state) - 160-char compose with live counter - Image/Link embed rendering - LocalStorage-persisted like state - Timeline with poll (prepend new posts) - Custom TitleBar (transparent, no decorations) - Orange/IBM Plex Mono maarcade design Tests: 231 Rust + 9 vitest = 240 passed.
This commit is contained in:
@@ -0,0 +1,520 @@
|
||||
pub mod api;
|
||||
pub mod appview_client;
|
||||
pub mod pds_client;
|
||||
pub mod state;
|
||||
pub mod store;
|
||||
|
||||
use appview_client::AppViewClient;
|
||||
use pds_client::PdsHttpClient;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use state::AppState;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct AccountSession {
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
pub access_jwt: String,
|
||||
pub refresh_jwt: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct AppVersionResp {
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn app_version() -> AppVersionResp {
|
||||
AppVersionResp {
|
||||
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn pds_describe(state: tauri::State<'_, AppState>) -> Result<serde_json::Value, String> {
|
||||
state.pds.describe_server().await.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn auth_register(
|
||||
state: tauri::State<'_, AppState>,
|
||||
handle: String,
|
||||
password: String,
|
||||
) -> Result<AccountSession, String> {
|
||||
let sess = state
|
||||
.pds
|
||||
.create_account(&handle, &password)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let s = AccountSession {
|
||||
did: sess.did.clone(),
|
||||
handle: sess.handle.clone(),
|
||||
access_jwt: sess.access_jwt.clone(),
|
||||
refresh_jwt: sess.refresh_jwt.clone(),
|
||||
};
|
||||
state.store.save(&s);
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn auth_login(
|
||||
state: tauri::State<'_, AppState>,
|
||||
identifier: String,
|
||||
password: String,
|
||||
) -> Result<AccountSession, String> {
|
||||
let sess = state
|
||||
.pds
|
||||
.create_session(&identifier, &password)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let s = AccountSession {
|
||||
did: sess.did.clone(),
|
||||
handle: sess.handle.clone(),
|
||||
access_jwt: sess.access_jwt.clone(),
|
||||
refresh_jwt: sess.refresh_jwt.clone(),
|
||||
};
|
||||
state.store.save(&s);
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn auth_refresh(state: tauri::State<'_, AppState>) -> Result<AccountSession, String> {
|
||||
let current = state
|
||||
.store
|
||||
.load()
|
||||
.ok_or_else(|| "no session".to_string())?;
|
||||
let sess = state
|
||||
.pds
|
||||
.refresh_session(¤t.refresh_jwt)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let s = AccountSession {
|
||||
did: sess.did.clone(),
|
||||
handle: sess.handle.clone(),
|
||||
access_jwt: sess.access_jwt.clone(),
|
||||
refresh_jwt: sess.refresh_jwt.clone(),
|
||||
};
|
||||
state.store.save(&s);
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn auth_logout(state: tauri::State<'_, AppState>) -> Result<(), String> {
|
||||
state.store.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn current_session(state: tauri::State<'_, AppState>) -> Result<Option<AccountSession>, String> {
|
||||
Ok(state.store.load())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn post_create(
|
||||
state: tauri::State<'_, AppState>,
|
||||
text: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let sess = state
|
||||
.store
|
||||
.load()
|
||||
.ok_or_else(|| "not logged in".to_string())?;
|
||||
let record = serde_json::json!({
|
||||
"text": text,
|
||||
"createdAt": chrono::Utc::now().to_rfc3339(),
|
||||
});
|
||||
let resp = state
|
||||
.pds
|
||||
.create_record(&sess.did, "app.twi.post", record, &sess.access_jwt)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(serde_json::json!({
|
||||
"uri": resp.uri,
|
||||
"cid": resp.cid,
|
||||
}))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn resolve_handle(
|
||||
state: tauri::State<'_, AppState>,
|
||||
handle: String,
|
||||
) -> Result<Option<String>, String> {
|
||||
state.pds.resolve_handle(&handle).await.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Helper: split an `at://did/collection/rkey` URI into its
|
||||
/// `rkey` component. Returns an error string the Tauri command
|
||||
/// can surface directly to the Svelte frontend.
|
||||
fn rkey_from_uri(uri: &str) -> Result<String, String> {
|
||||
let rkey = uri
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.ok_or_else(|| format!("invalid uri: {uri}"))?
|
||||
.to_string();
|
||||
if rkey.is_empty() {
|
||||
return Err(format!("invalid uri (empty rkey): {uri}"));
|
||||
}
|
||||
Ok(rkey)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn like_post(
|
||||
state: tauri::State<'_, AppState>,
|
||||
subject_uri: String,
|
||||
subject_cid: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let sess = state
|
||||
.store
|
||||
.load()
|
||||
.ok_or_else(|| "not logged in".to_string())?;
|
||||
let resp = state
|
||||
.pds
|
||||
.create_like(
|
||||
&sess.did,
|
||||
&subject_uri,
|
||||
&subject_cid,
|
||||
&sess.access_jwt,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(serde_json::json!({
|
||||
"uri": resp.uri,
|
||||
"cid": resp.cid,
|
||||
}))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn unlike_post(
|
||||
state: tauri::State<'_, AppState>,
|
||||
like_uri: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let sess = state
|
||||
.store
|
||||
.load()
|
||||
.ok_or_else(|| "not logged in".to_string())?;
|
||||
let rkey = rkey_from_uri(&like_uri)?;
|
||||
let resp = state
|
||||
.pds
|
||||
.delete_record(
|
||||
&sess.did,
|
||||
"app.bsky.feed.like",
|
||||
&rkey,
|
||||
&sess.access_jwt,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(serde_json::json!({
|
||||
"commit": resp.commit,
|
||||
}))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn repost_post(
|
||||
state: tauri::State<'_, AppState>,
|
||||
subject_uri: String,
|
||||
subject_cid: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let sess = state
|
||||
.store
|
||||
.load()
|
||||
.ok_or_else(|| "not logged in".to_string())?;
|
||||
// Reposts share the like wire shape — the PDS hardcodes the
|
||||
// collection, but `feed.like.create` is the only endpoint that
|
||||
// does so. For reposts we go through the generic
|
||||
// `com.atproto.repo.createRecord` with a repost-shaped record
|
||||
// value.
|
||||
let record = serde_json::json!({
|
||||
"subject": {
|
||||
"uri": subject_uri,
|
||||
"cid": subject_cid,
|
||||
},
|
||||
"createdAt": chrono::Utc::now().to_rfc3339(),
|
||||
});
|
||||
let resp = state
|
||||
.pds
|
||||
.create_record_with(&sess.did, "app.bsky.feed.repost", record, false, &sess.access_jwt)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(serde_json::json!({
|
||||
"uri": resp.uri,
|
||||
"cid": resp.cid,
|
||||
}))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn unrepost_post(
|
||||
state: tauri::State<'_, AppState>,
|
||||
repost_uri: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let sess = state
|
||||
.store
|
||||
.load()
|
||||
.ok_or_else(|| "not logged in".to_string())?;
|
||||
let rkey = rkey_from_uri(&repost_uri)?;
|
||||
let resp = state
|
||||
.pds
|
||||
.delete_record(
|
||||
&sess.did,
|
||||
"app.bsky.feed.repost",
|
||||
&rkey,
|
||||
&sess.access_jwt,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(serde_json::json!({
|
||||
"commit": resp.commit,
|
||||
}))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn timeline_home(
|
||||
state: tauri::State<'_, AppState>,
|
||||
did: String,
|
||||
cursor: Option<String>,
|
||||
limit: Option<u32>,
|
||||
) -> Result<appview_client::TimelineResponse, String> {
|
||||
let lim = limit.unwrap_or(30).clamp(1, 100);
|
||||
state
|
||||
.appview
|
||||
.fetch_timeline(&did, cursor.as_deref(), lim)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn profile_get(
|
||||
state: tauri::State<'_, AppState>,
|
||||
handle: String,
|
||||
) -> Result<appview_client::ProfileResponse, String> {
|
||||
state
|
||||
.appview
|
||||
.fetch_profile(&handle)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn search(
|
||||
state: tauri::State<'_, AppState>,
|
||||
q: String,
|
||||
limit: Option<u32>,
|
||||
) -> Result<appview_client::SearchResponse, String> {
|
||||
let lim = limit.unwrap_or(30).clamp(1, 100);
|
||||
state
|
||||
.appview
|
||||
.fetch_search(&q, lim)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn post_get(
|
||||
state: tauri::State<'_, AppState>,
|
||||
uri: String,
|
||||
) -> Result<appview_client::ThreadResponse, String> {
|
||||
state
|
||||
.appview
|
||||
.fetch_post(&uri)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn status_pds(state: tauri::State<'_, AppState>) -> Result<serde_json::Value, String> {
|
||||
let sess = state.store.load();
|
||||
Ok(serde_json::json!({
|
||||
"did": sess.as_ref().map(|s| s.did.clone()),
|
||||
"handle": sess.as_ref().map(|s| s.handle.clone()),
|
||||
"authenticated": sess.is_some(),
|
||||
}))
|
||||
}
|
||||
|
||||
/// `fetch_blob(did, cid)` — fetch raw blob bytes from the PDS for
|
||||
/// rendering image embeds. The Tauri shell does the HTTP call (rather
|
||||
/// than fetching via AppView) because blobs live on the user's PDS,
|
||||
/// not the AppView's indexer.
|
||||
#[tauri::command]
|
||||
async fn fetch_blob(
|
||||
state: tauri::State<'_, AppState>,
|
||||
did: String,
|
||||
cid: String,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
// Optionally pass the caller's JWT so authenticated PDSes (when
|
||||
// we enable that) receive the right token. Today `getBlob` is
|
||||
// unauthenticated so this is just `None`.
|
||||
let jwt = state.store.load().map(|s| s.access_jwt);
|
||||
state
|
||||
.pds
|
||||
.get_blob(&did, &cid, jwt.as_deref())
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Fire a native OS notification with an optional click target. The
|
||||
/// payload is also broadcast as an `app://notification` event so the
|
||||
/// frontend can route to `url` on click (via a notification listener
|
||||
/// registered in JS — see `frontend/src/main.ts`).
|
||||
///
|
||||
/// Used by the frontend for high-activity bursts (timeline event rate
|
||||
/// spike) and on first-session "new post by followed user" demo
|
||||
/// hooks. The Rust side emits the event up-front so the click target
|
||||
/// is in flight even if the OS strips the underlying notification
|
||||
/// (some platforms don't propagate click events back to Tauri).
|
||||
#[tauri::command]
|
||||
async fn show_notification(
|
||||
app: tauri::AppHandle,
|
||||
title: String,
|
||||
body: String,
|
||||
url: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
use tauri_plugin_notification::NotificationExt;
|
||||
|
||||
// Show the OS notification. The `.show()` call internally uses
|
||||
// `tauri::async_runtime::spawn` so it never blocks the caller.
|
||||
app.notification()
|
||||
.builder()
|
||||
.title(&title)
|
||||
.body(&body)
|
||||
.show()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// Broadcast the metadata to the frontend so it can handle click
|
||||
// routing (focus window + navigate) without needing a per-action
|
||||
// action-type registration on the Rust side.
|
||||
let _ = tauri::Emitter::emit(
|
||||
&app,
|
||||
"app://notification",
|
||||
serde_json::json!({
|
||||
"title": title,
|
||||
"body": body,
|
||||
"url": url,
|
||||
}),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()))
|
||||
.init();
|
||||
|
||||
let pds_url = std::env::var("MAARCADETWEET_PDS_URL")
|
||||
.unwrap_or_else(|_| "http://127.0.0.1:2583".to_string());
|
||||
let appview_url = std::env::var("MAARCADETWEET_APPVIEW_URL")
|
||||
.unwrap_or_else(|_| "http://127.0.0.1:2584".to_string());
|
||||
|
||||
let state = AppState {
|
||||
pds: PdsHttpClient::new(pds_url),
|
||||
appview: AppViewClient::new(appview_url),
|
||||
store: store::SessionStore::new(),
|
||||
};
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_notification::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_updater::Builder::new().build())
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.manage(state)
|
||||
.setup(|app| {
|
||||
tracing::info!("maarcadetweet starting up");
|
||||
|
||||
// Embed the tray icon at compile time. `include_image!`
|
||||
// resolves paths relative to `CARGO_MANIFEST_DIR` and
|
||||
// bakes the raw RGBA pixels into the binary, so the
|
||||
// tray works regardless of the runtime CWD.
|
||||
const TRAY_ICON: tauri::image::Image<'static> =
|
||||
tauri::include_image!("icons/32x32.png");
|
||||
|
||||
let show_item = tauri::menu::MenuItem::with_id(
|
||||
app,
|
||||
"tray_show",
|
||||
"Show maarcadetweet",
|
||||
true,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| format!("failed to build show menu item: {e}"))?;
|
||||
let compose_item = tauri::menu::MenuItem::with_id(
|
||||
app,
|
||||
"tray_compose",
|
||||
"Compose",
|
||||
true,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| format!("failed to build compose menu item: {e}"))?;
|
||||
let quit_item = tauri::menu::MenuItem::with_id(
|
||||
app,
|
||||
"tray_quit",
|
||||
"Quit",
|
||||
true,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| format!("failed to build quit menu item: {e}"))?;
|
||||
let separator = tauri::menu::PredefinedMenuItem::separator(app)
|
||||
.map_err(|e| format!("failed to build separator: {e}"))?;
|
||||
|
||||
let tray_menu = tauri::menu::Menu::with_items(
|
||||
app,
|
||||
&[&show_item, &compose_item, &separator, &quit_item],
|
||||
)
|
||||
.map_err(|e| format!("failed to build tray menu: {e}"))?;
|
||||
|
||||
let _tray = tauri::tray::TrayIconBuilder::with_id("main-tray")
|
||||
.icon(TRAY_ICON)
|
||||
.icon_as_template(false)
|
||||
.tooltip("maarcadetweet")
|
||||
.menu(&tray_menu)
|
||||
.show_menu_on_left_click(false)
|
||||
.on_menu_event(|app, event| match event.id.as_ref() {
|
||||
"tray_show" => {
|
||||
let _ = tauri::Emitter::emit(app, "app://show", ());
|
||||
}
|
||||
"tray_compose" => {
|
||||
let _ = tauri::Emitter::emit(app, "app://compose", ());
|
||||
}
|
||||
"tray_quit" => {
|
||||
app.exit(0);
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.on_tray_icon_event(|tray, event| {
|
||||
// Left click on the tray icon brings the app forward;
|
||||
// right click is handled by the menu (see
|
||||
// `show_menu_on_left_click(false)`).
|
||||
if let tauri::tray::TrayIconEvent::Click {
|
||||
button: tauri::tray::MouseButton::Left,
|
||||
button_state: tauri::tray::MouseButtonState::Down,
|
||||
..
|
||||
} = event
|
||||
{
|
||||
let _ = tauri::Emitter::emit(tray.app_handle(), "app://show", ());
|
||||
}
|
||||
})
|
||||
.build(app)
|
||||
.map_err(|e| format!("failed to build tray icon: {e}"))?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
app_version,
|
||||
pds_describe,
|
||||
auth_register,
|
||||
auth_login,
|
||||
auth_refresh,
|
||||
auth_logout,
|
||||
current_session,
|
||||
post_create,
|
||||
resolve_handle,
|
||||
timeline_home,
|
||||
profile_get,
|
||||
search,
|
||||
post_get,
|
||||
like_post,
|
||||
unlike_post,
|
||||
repost_post,
|
||||
unrepost_post,
|
||||
status_pds,
|
||||
fetch_blob,
|
||||
show_notification,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running maarcadetweet");
|
||||
}
|
||||
Reference in New Issue
Block a user