#![forbid(unsafe_code)]
#![deny(
clippy::all,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::todo,
clippy::unimplemented,
missing_docs,
rust_2018_idioms
)]
pub mod appview;
pub mod chat;
pub mod error;
#[cfg(feature = "storage")]
pub mod index;
pub mod ingest;
pub mod lexicon;
pub mod repo;
pub use appview::{
ActorProfile, ActorSuggestion, AppViewClient, PostView, DEFAULT_APPVIEW_ENDPOINT,
DEFAULT_APPVIEW_TIMEOUT_MS, DEFAULT_CDN_ENDPOINT,
};
pub use chat::{
ChatClient, ChatMessageView, ConvoMember, ConvoView, DeletedMessageView, MessageSender,
MessageView, SendMessagePayload, DEFAULT_CHAT_ENDPOINT,
};
pub use error::{Result, SkybaseError};
#[cfg(feature = "storage")]
pub use index::{
parse_at_uri, BatchStats, BroadcastBus, ChangeNotification, QueryBuilder, QueryOp, RecordInput,
RecordRow, RecordStore, RecordStoreConfig, SortDirection, StoreOperation,
DEFAULT_BROADCAST_CAPACITY,
};
pub use ingest::{
build_subscription_url, build_subscription_url_full, normalize_indexed_at,
parse_frame_timestamp, parse_jetstream_commit, parse_jetstream_frame, resolve_resume_cursor,
run_jetstream_streamer, BackoffManager, CommitOperation, CursorTracker, JetstreamCommit,
JetstreamEvent, MockJetstreamServer, MockServerCommand, RawJetstreamCommit,
RawJetstreamMessage, StreamConfig, CURSOR_REWIND_OVERLAP_US, DEFAULT_JETSTREAM_ENDPOINT,
DEFAULT_PING_INTERVAL, DEFAULT_READ_TIMEOUT, INITIAL_BACKOFF, MAX_BACKOFF,
MAX_JETSTREAM_REPLAY_WINDOW_US,
};
#[cfg(feature = "storage")]
pub use ingest::{
sync_commit_to_store, ConsumerStats, IngesterConfig, JetstreamConsumer, JetstreamConsumerHandle,
};
pub use lexicon::{
extract_link_facets, format_system_time_iso8601, format_unix_secs_iso8601, has_nsfw_self_label,
now_iso8601, ByteSlice, Embed, Facet, FacetFeature, FacetIndex, FollowRecord, ListBlockRecord,
ListItemRecord, ListRecordsResponse, ModListRecord, PostRecord, RecordEmbed,
RecordWithMediaEmbed, ReplyRef, RepoRecordItem, SelfLabel, SelfLabels, StrongRef,
NSFW_SELF_LABEL_VALUES,
};
pub use repo::{
decode_tid_timestamp_secs, decode_tid_timestamp_us, extract_did_for_collection,
extract_did_from_at_uri, format_at_uri, generate_tid, parse_at_post_uri, split_at_uri,
validate_rkey, CreateRecordRequest, CreateRecordResult, DeleteRecordRequest, PdsRepoClient,
PutRecordRequest, PutRecordResult, RecordView, TidGenerator, XrpcErrorResponse,
MAX_PLAUSIBLE_TID_EPOCH_SECS, MIN_PLAUSIBLE_TID_EPOCH_SECS,
};
pub use skyauth;
pub use tokio_util::sync::CancellationToken;
#[cfg(feature = "storage")]
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct SkybaseConfig {
pub client_id: String,
pub redirect_uri: String,
pub app_name: String,
pub jetstream_endpoint: Option<String>,
pub storage_path: Option<PathBuf>,
pub in_memory_store: bool,
#[cfg(feature = "storage")]
pub store_config: Option<RecordStoreConfig>,
pub wanted_collections: Vec<String>,
pub wanted_dids: Vec<String>,
}
impl SkybaseConfig {
pub fn new(
client_id: impl Into<String>,
redirect_uri: impl Into<String>,
app_name: impl Into<String>,
) -> Self {
Self {
client_id: client_id.into(),
redirect_uri: redirect_uri.into(),
app_name: app_name.into(),
jetstream_endpoint: None,
storage_path: None,
in_memory_store: false,
#[cfg(feature = "storage")]
store_config: None,
wanted_collections: Vec::new(),
wanted_dids: Vec::new(),
}
}
pub fn with_jetstream_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.jetstream_endpoint = Some(endpoint.into());
self
}
pub fn with_storage_path(mut self, path: impl Into<PathBuf>) -> Self {
self.storage_path = Some(path.into());
self
}
pub fn with_in_memory_store(mut self) -> Self {
self.in_memory_store = true;
self
}
#[cfg(feature = "storage")]
pub fn with_store_config(mut self, config: RecordStoreConfig) -> Self {
self.store_config = Some(config);
self
}
pub fn with_wanted_collection(mut self, collection: impl Into<String>) -> Self {
self.wanted_collections.push(collection.into());
self
}
pub fn with_wanted_collections<I, S>(mut self, collections: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.wanted_collections = collections.into_iter().map(Into::into).collect();
self
}
pub fn with_wanted_did(mut self, did: impl Into<String>) -> Self {
self.wanted_dids.push(did.into());
self
}
pub fn with_wanted_dids<I, S>(mut self, dids: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.wanted_dids = dids.into_iter().map(Into::into).collect();
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LoginIdentityPreview {
pub oauth: skyauth::client::OAuthLoginPreview,
pub profile: Option<ActorProfile>,
}
#[derive(Clone)]
pub struct Skybase {
config: Arc<SkybaseConfig>,
auth_client: Arc<skyauth::client::AtprotoOAuthClient>,
#[cfg(feature = "storage")]
store: Option<RecordStore>,
}
impl Skybase {
pub fn new(config: SkybaseConfig) -> Result<Self> {
if config.client_id.trim().is_empty() {
return Err(SkybaseError::Config("client_id cannot be empty".into()));
}
if config.redirect_uri.trim().is_empty() {
return Err(SkybaseError::Config("redirect_uri cannot be empty".into()));
}
let metadata =
skyauth::client::OAuthClientMetadata::new(&config.client_id, &config.redirect_uri)
.with_client_name(&config.app_name);
let auth_client = skyauth::client::AtprotoOAuthClient::builder()
.client_metadata(metadata)
.build()
.map_err(SkybaseError::Auth)?;
#[cfg(feature = "storage")]
let store = if let Some(ref store_cfg) = config.store_config {
Some(RecordStore::with_config(store_cfg.clone())?)
} else if let Some(ref path) = config.storage_path {
Some(RecordStore::open(path)?)
} else if config.in_memory_store {
Some(RecordStore::open_in_memory()?)
} else {
None
};
Ok(Self {
config: Arc::new(config),
auth_client: Arc::new(auth_client),
#[cfg(feature = "storage")]
store,
})
}
#[cfg(feature = "storage")]
pub fn in_memory(config: SkybaseConfig) -> Result<Self> {
Self::new(config.with_in_memory_store())
}
#[cfg(feature = "storage")]
#[must_use]
pub fn with_store(mut self, store: RecordStore) -> Self {
self.store = Some(store);
self
}
#[cfg(feature = "storage")]
pub fn open_store(&mut self, path: impl AsRef<Path>) -> Result<RecordStore> {
let store = RecordStore::open(path)?;
self.store = Some(store.clone());
Ok(store)
}
#[cfg(feature = "storage")]
pub fn open_in_memory_store(&mut self) -> Result<RecordStore> {
let store = RecordStore::open_in_memory()?;
self.store = Some(store.clone());
Ok(store)
}
#[must_use]
pub fn config(&self) -> &SkybaseConfig {
&self.config
}
#[must_use]
pub fn auth(&self) -> &skyauth::client::AtprotoOAuthClient {
&self.auth_client
}
pub async fn preview_login(
&self,
appview: &AppViewClient,
handle_or_did: &str,
) -> Result<LoginIdentityPreview> {
let oauth = self
.auth_client
.preview_login(handle_or_did)
.await
.map_err(SkybaseError::Auth)?;
let profile = appview.fetch_profile(&oauth.did).await;
Ok(LoginIdentityPreview { oauth, profile })
}
#[cfg(feature = "storage")]
#[must_use]
pub fn store(&self) -> Option<&RecordStore> {
self.store.as_ref()
}
#[cfg(feature = "storage")]
pub fn require_store(&self) -> Result<&RecordStore> {
self.store.as_ref().ok_or_else(|| {
SkybaseError::Config("RecordStore is not initialized on this Skybase instance".into())
})
}
#[cfg(feature = "storage")]
pub fn query(&self, collection: impl Into<String>) -> Result<QueryBuilder<'_>> {
Ok(self.require_store()?.query(collection))
}
#[cfg(feature = "storage")]
pub fn collection(&self, collection: impl Into<String>) -> Result<QueryBuilder<'_>> {
self.query(collection)
}
#[cfg(feature = "storage")]
pub fn subscribe(&self) -> Result<tokio::sync::broadcast::Receiver<ChangeNotification>> {
Ok(self.require_store()?.subscribe())
}
#[cfg(feature = "storage")]
pub fn consumer(&self) -> Result<JetstreamConsumer> {
let endpoint = self.config.jetstream_endpoint.as_ref().ok_or_else(|| {
SkybaseError::Config(
"Cannot build JetstreamConsumer: 'jetstream_endpoint' is not configured".into(),
)
})?;
let mut config = IngesterConfig::new(endpoint);
if !self.config.wanted_collections.is_empty() {
config = config.with_collections(self.config.wanted_collections.clone());
}
if !self.config.wanted_dids.is_empty() {
config = config.with_dids(self.config.wanted_dids.clone());
}
self.consumer_with_config(config)
}
#[cfg(feature = "storage")]
pub fn consumer_with_config(&self, config: IngesterConfig) -> Result<JetstreamConsumer> {
let store = self.require_store()?.clone();
Ok(JetstreamConsumer::new(config, store))
}
#[cfg(feature = "storage")]
pub fn start_consumer(
&self,
cancel: tokio_util::sync::CancellationToken,
) -> Result<JetstreamConsumerHandle> {
let consumer = self.consumer()?;
Ok(consumer.start(cancel))
}
#[cfg(feature = "storage")]
pub fn start_consumer_with_config(
&self,
config: IngesterConfig,
cancel: tokio_util::sync::CancellationToken,
) -> Result<JetstreamConsumerHandle> {
let consumer = self.consumer_with_config(config)?;
Ok(consumer.start(cancel))
}
pub fn repo_client(
&self,
session: Arc<skyauth::session::OAuthSession>,
) -> Result<PdsRepoClient> {
PdsRepoClient::new(session, Arc::clone(&self.auth_client))
}
pub fn repo_client_from_credentials(
&self,
pds_endpoint: impl Into<String>,
repo_did: impl Into<String>,
access_token: impl Into<String>,
) -> Result<PdsRepoClient> {
let endpoint = pds_endpoint.into();
let did = repo_did.into();
let token = access_token.into();
let session = skyauth::session::OAuthSession::new(
did,
token,
None,
"DPoP",
None,
None,
skyauth::dpop::DPoPKey::generate(),
Some(endpoint),
None,
None,
)
.map_err(SkybaseError::Auth)?;
PdsRepoClient::new(Arc::new(session), Arc::clone(&self.auth_client))
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
use super::*;
#[test]
fn test_skybase_initialization() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"Test App",
);
let skybase = Skybase::new(config).expect("Skybase should initialize successfully");
assert_eq!(skybase.config().app_name, "Test App");
}
#[test]
fn test_skybase_config_validation() {
let invalid_config = SkybaseConfig::new("", "https://app.example.com/callback", "App");
let result = Skybase::new(invalid_config);
assert!(matches!(result, Err(SkybaseError::Config(_))));
}
#[test]
fn test_skybase_repo_client_creation() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"Test App",
);
let skybase = Skybase::new(config).expect("Skybase init failed");
let session = skyauth::session::OAuthSession::new(
"did:plc:alice",
"access_token",
None,
"DPoP",
None,
None,
skyauth::dpop::DPoPKey::generate(),
Some("https://pds.example.com".into()),
None,
None,
)
.expect("session creation failed");
let client = skybase.repo_client(Arc::new(session)).expect("repo_client");
assert_eq!(client.did(), "did:plc:alice");
assert_eq!(
client.pds_endpoint().expect("endpoint"),
"https://pds.example.com"
);
}
#[cfg(feature = "storage")]
#[test]
fn test_skybase_config_builders() {
let path = std::path::PathBuf::from("/tmp/skybase_test.db");
let store_cfg = RecordStoreConfig::in_memory();
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"Builder Test App",
)
.with_jetstream_endpoint("wss://jetstream.example.com/subscribe")
.with_storage_path(&path)
.with_in_memory_store()
.with_store_config(store_cfg)
.with_wanted_collections(vec!["app.bsky.actor.profile", "app.bsky.feed.like"])
.with_wanted_collection("app.bsky.feed.post")
.with_wanted_dids(vec!["did:plc:bob", "did:plc:charlie"])
.with_wanted_did("did:plc:alice");
assert_eq!(
config.jetstream_endpoint.as_deref(),
Some("wss://jetstream.example.com/subscribe")
);
assert_eq!(config.storage_path.as_deref(), Some(path.as_path()));
assert!(config.in_memory_store);
assert!(config.store_config.is_some());
assert_eq!(config.wanted_collections.len(), 3);
assert_eq!(config.wanted_dids.len(), 3);
}
#[cfg(feature = "storage")]
#[test]
fn test_skybase_in_memory_and_store_delegation() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"In Memory App",
);
let skybase = Skybase::in_memory(config).expect("in_memory init failed");
assert!(skybase.store().is_some());
assert!(skybase.require_store().is_ok());
let q1 = skybase.query("app.bsky.feed.post").expect("query failed");
let (sql1, _) = q1.build_sql().expect("sql build failed");
assert!(sql1.contains("WHERE collection = ?"));
let q2 = skybase
.collection("app.bsky.feed.post")
.expect("collection failed");
let (sql2, _) = q2.build_sql().expect("sql build failed");
assert_eq!(sql1, sql2);
let mut rx = skybase.subscribe().expect("subscribe failed");
let input = RecordInput::new(
"did:plc:alice",
"app.bsky.feed.post",
"post_facade_1",
"cid_1",
serde_json::json!({"text": "Hello facade"}),
1_000,
);
skybase
.require_store()
.unwrap()
.upsert_record(&input)
.expect("upsert failed");
let event = rx.try_recv().expect("broadcast event expected");
match event {
ChangeNotification::Upsert(row) => {
assert_eq!(row.uri, input.uri);
assert_eq!(row.record_json["text"], "Hello facade");
}
_ => panic!("Expected Upsert event"),
}
}
#[cfg(feature = "storage")]
#[test]
fn test_skybase_uninitialized_store_errors() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"No Store App",
);
let skybase = Skybase::new(config).expect("init failed");
assert!(skybase.store().is_none());
assert!(matches!(
skybase.require_store(),
Err(SkybaseError::Config(_))
));
assert!(matches!(skybase.query("col"), Err(SkybaseError::Config(_))));
assert!(matches!(
skybase.collection("col"),
Err(SkybaseError::Config(_))
));
assert!(matches!(skybase.subscribe(), Err(SkybaseError::Config(_))));
assert!(matches!(skybase.consumer(), Err(SkybaseError::Config(_))));
}
#[cfg(feature = "storage")]
#[test]
fn test_skybase_with_store_and_open_store() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"Attach Store App",
);
let mut skybase = Skybase::new(config).expect("init failed");
assert!(skybase.store().is_none());
let store = RecordStore::open_in_memory().expect("store init failed");
skybase = skybase.with_store(store);
assert!(skybase.store().is_some());
let store2 = skybase
.open_in_memory_store()
.expect("open_in_memory_store failed");
assert!(skybase.store().is_some());
let _ = store2;
let temp_dir = tempfile::tempdir().expect("tempdir failed");
let db_path = temp_dir.path().join("skybase_facade.db");
let store3 = skybase.open_store(&db_path).expect("open_store failed");
assert!(skybase.store().is_some());
let _ = store3;
}
#[cfg(feature = "storage")]
#[tokio::test]
async fn test_skybase_consumer_wiring() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"Consumer App",
)
.with_jetstream_endpoint("wss://jetstream.example.com/subscribe")
.with_in_memory_store()
.with_wanted_collection("app.bsky.feed.post")
.with_wanted_did("did:plc:alice");
let skybase = Skybase::new(config).expect("init failed");
let consumer = skybase.consumer().expect("consumer build failed");
assert_eq!(
consumer.config().wanted_collections,
vec!["app.bsky.feed.post"]
);
assert_eq!(consumer.config().wanted_dids, vec!["did:plc:alice"]);
let cancel = CancellationToken::new();
let handle = skybase
.start_consumer(cancel.clone())
.expect("start_consumer failed");
handle.stop();
let _ = handle.join().await;
}
#[test]
fn test_skybase_repo_client_from_credentials() {
let config = SkybaseConfig::new(
"https://app.example.com/oauth/client-metadata.json",
"https://app.example.com/oauth/callback",
"Repo App",
);
let skybase = Skybase::new(config).expect("init failed");
let client = skybase
.repo_client_from_credentials(
"https://pds.example.com",
"did:plc:bob",
"dpop_access_token_123",
)
.expect("repo_client_from_credentials failed");
assert_eq!(client.did(), "did:plc:bob");
assert_eq!(
client.pds_endpoint().expect("endpoint"),
"https://pds.example.com"
);
}
}