include decoded address in /api/obj

feat/vaults
Tomáš Mládek 2022-02-07 20:46:17 +01:00
parent 25bc14e3b4
commit 686020e579
No known key found for this signature in database
GPG Key ID: ED21612889E75EC5
3 changed files with 47 additions and 11 deletions

View File

@ -308,12 +308,12 @@ impl UpEndConnection {
}
}
pub fn retrieve_object(&self, object_address: Address) -> Result<Vec<Entry>> {
pub fn retrieve_object(&self, object_address: &Address) -> Result<Vec<Entry>> {
use crate::database::inner::schema::data::dsl::*;
let primary = data
.filter(entity.eq(object_address.encode()?))
.or_filter(value_str.eq(EntryValue::Address(object_address).to_string()?))
.or_filter(value_str.eq(EntryValue::Address(object_address.clone()).to_string()?))
.load::<models::Entry>(&self.conn)?;
let entries = primary

View File

@ -207,18 +207,30 @@ impl EntriesAsHash for Vec<Entry> {
#[get("/api/obj/{address_str}")]
pub async fn get_object(
state: web::Data<State>,
address_str: web::Path<String>,
address: web::Path<Address>,
) -> Result<HttpResponse, Error> {
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let address = address.into_inner();
let result: Vec<Entry> = connection
.retrieve_object(
Address::decode(&b58_decode(address_str.into_inner()).map_err(ErrorBadRequest)?)
.map_err(ErrorBadRequest)?,
)
.retrieve_object(&address)
.map_err(ErrorInternalServerError)?;
debug!("{:?}", result);
Ok(HttpResponse::Ok().json(result.as_hash().map_err(ErrorInternalServerError)?))
let (entity_type, entity_content) = match address {
Address::Hash(_) => ("Hash", None),
Address::Uuid(_) => ("Uuid", None),
Address::Attribute(attribute) => ("Attribute", Some(attribute)),
Address::Url(url) => ("Url", Some(url)),
};
Ok(HttpResponse::Ok().json(json!({
"entity": {
"t": entity_type,
"c": entity_content
},
"entries": result.as_hash().map_err(ErrorInternalServerError)?
})))
}
#[put("/api/obj")]
@ -417,7 +429,7 @@ pub async fn list_hier_roots(state: web::Data<State>) -> Result<HttpResponse, Er
let result = list_roots(&connection)
.map_err(ErrorInternalServerError)?
.into_iter()
.map(|root| connection.retrieve_object(root))
.map(|root| connection.retrieve_object(&root))
.collect::<Result<Vec<Vec<Entry>>>>()
.map_err(ErrorInternalServerError)?
.concat();

View File

@ -5,23 +5,47 @@ import { UpListing, UpObject } from "upend";
import type { ListingResult } from "upend/types";
import { useSWR } from "../util/fetch";
export type EntityInfo =
| {
t: "Hash" | "Uuid";
}
| {
t: "Attribute" | "Url";
c: string;
};
export interface EntityListing {
entity: EntityInfo;
entries: ListingResult;
}
const queryOnceLRU = new LRU<string, UpListing>(128);
const inFlightRequests: { [key: string]: Promise<UpListing> } = {};
export function useEntity(address: string) {
const { data, error, revalidate } = useSWR<ListingResult, unknown>(
const { data, error, revalidate } = useSWR<EntityListing, unknown>(
`api/obj/${address}`
);
const entity: Readable<UpObject | undefined> = derived(data, ($listing) => {
if ($listing) {
const listing = new UpListing($listing);
const listing = new UpListing($listing.entries);
return listing.getObject(address);
}
});
const entityInfo: Readable<EntityInfo | undefined> = derived(
data,
($listing) => {
if ($listing) {
return $listing.entity;
}
}
);
return {
entity,
entityInfo,
error,
revalidate,
};