Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ impl Server {
})
}

/// Implementation of the GetLatessVersionId protocol transaction.
pub async fn latest_version_id(&self, client_id: ClientId) -> Result<VersionId, ServerError> {
let mut txn = self.txn(client_id).await?;
let client = txn.get_client().await?.ok_or(ServerError::NoSuchClient)?;
Ok(client.latest_version_id)
}

/// Convenience method to get a transaction for the embedded storage.
pub async fn txn(&self, client_id: Uuid) -> Result<Box<dyn StorageTxn + '_>, ServerError> {
Ok(self.storage.txn(client_id).await?)
Expand Down
64 changes: 64 additions & 0 deletions server/src/api/latest_version_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::api::{server_error_to_actix, ServerState, SNAPSHOT_CONTENT_TYPE, VERSION_ID_HEADER};

Check failure on line 1 in server/src/api/latest_version_id.rs

View workflow job for this annotation

GitHub Actions / Check & Clippy

unused import: `SNAPSHOT_CONTENT_TYPE`

Check warning on line 1 in server/src/api/latest_version_id.rs

View workflow job for this annotation

GitHub Actions / Check & Clippy

unused import: `SNAPSHOT_CONTENT_TYPE`

Check warning on line 1 in server/src/api/latest_version_id.rs

View workflow job for this annotation

GitHub Actions / Rustdoc

unused import: `SNAPSHOT_CONTENT_TYPE`

Check warning on line 1 in server/src/api/latest_version_id.rs

View workflow job for this annotation

GitHub Actions / Rustdoc

unused import: `SNAPSHOT_CONTENT_TYPE`
use actix_web::{get, web, HttpRequest, HttpResponse, Result};
use std::sync::Arc;

/// Get the latest version ID.
#[get("/v1/client/latest-version-id")]
pub(crate) async fn service(
req: HttpRequest,
server_state: web::Data<Arc<ServerState>>,
) -> Result<HttpResponse> {
let client_id = server_state.client_id_header(&req)?;

let latest_version_id = server_state
.server
.latest_version_id(client_id)
.await
.map_err(server_error_to_actix)?;
Ok(HttpResponse::Ok()
.append_header((VERSION_ID_HEADER, latest_version_id.to_string()))
.body(()))
}

#[cfg(test)]
mod test {
use crate::{
api::CLIENT_ID_HEADER,
web::{WebConfig, WebServer},
};
use actix_web::{http::StatusCode, test, App};
use pretty_assertions::assert_eq;
use taskchampion_sync_server_core::{InMemoryStorage, ServerConfig, Storage};
use uuid::Uuid;

#[actix_rt::test]
async fn test_success() {
let client_id = Uuid::new_v4();
let latest_version_id = Uuid::new_v4();
let storage = InMemoryStorage::new();

// set up the storage contents..
{
let mut txn = storage.txn(client_id).await.unwrap();
txn.new_client(latest_version_id).await.unwrap();
txn.commit().await.unwrap();
}

let server = WebServer::new(ServerConfig::default(), WebConfig::default(), storage);
let app = App::new().configure(|sc| server.config(sc));
let app = test::init_service(app).await;

let uri = "/v1/client/latest-version-id";
let req = test::TestRequest::get()
.uri(uri)
.append_header((CLIENT_ID_HEADER, client_id.to_string()))
.to_request();
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);

assert_eq!(
resp.headers().get("X-Version-Id").unwrap(),
&latest_version_id.to_string()
);
}
}
2 changes: 2 additions & 0 deletions server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod add_version;
mod events;
mod get_child_version;
mod get_snapshot;
mod latest_version_id;

pub(crate) use events::ChangeNotifier;

Expand Down Expand Up @@ -64,6 +65,7 @@ pub(crate) fn api_scope() -> Scope {
.service(add_version::service)
.service(get_snapshot::service)
.service(add_snapshot::service)
.service(latest_version_id::service)
.service(events::service)
}

Expand Down
Loading