Skip to content

Commit

Permalink
Show references in the search UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Feb 23, 2024
1 parent abac562 commit 33e7304
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
11 changes: 7 additions & 4 deletions dnas/trusted/zomes/coordinator/trusted/src/gpg_key_dist.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::convert_to_app_entry_type;
use hdk::prelude::*;
use trusted_integrity::prelude::*;

Expand Down Expand Up @@ -57,8 +58,8 @@ pub struct SearchKeysRequest {

#[derive(Serialize, Deserialize, Debug, Clone, SerializedBytes)]
pub struct SearchKeysResponse {
pub key: Record,
pub key_collection_count: usize,
pub gpg_key_dist: GpgKeyDist,
pub reference_count: usize,
}

#[hdk_extern]
Expand Down Expand Up @@ -95,13 +96,15 @@ pub fn search_keys(request: SearchKeysRequest) -> ExternResult<Vec<SearchKeysRes
{
match get(target.clone(), GetOptions::default())? {
Some(r) => {
// TODO permissible to have a key in multiple collections so this count needs to be a
// get + filter to unique agents
let link_count = count_links(LinkQuery::new(
target,
LinkTypes::GpgKeyDistToKeyCollection.try_into_filter()?,
))?;
out.push(SearchKeysResponse {
key: r,
key_collection_count: link_count,
gpg_key_dist: convert_to_app_entry_type(r)?,
reference_count: link_count,
});
}
_ => {
Expand Down
15 changes: 15 additions & 0 deletions dnas/trusted/zomes/coordinator/trusted/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ fn get_entry_for_action(action_hash: &ActionHash) -> ExternResult<Option<EntryTy

EntryTypes::deserialize_from_type(*zome_index, *entry_index, entry)
}

pub fn convert_to_app_entry_type<T>(record: Record) -> ExternResult<T>
where
T: TryFrom<SerializedBytes>,
{
record
.entry()
.as_option()
.and_then(|e| e.as_app_entry())
.and_then(|e| -> Option<T> { e.clone().into_sb().try_into().ok() })
.ok_or(wasm_error!(WasmErrorInner::Guest(format!(
"Could not convert record to: {:?}",
std::any::type_name::<T>()
))))
}
10 changes: 4 additions & 6 deletions tests/src/trusted/trusted/gpg-key-dist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ test('Search for a key', async () => {
},
});
assert.equal(1, responses.length);
const decoded = (decode((responses[0].key.entry as any).Present.entry) as any);
assert.equal("Alice", decoded.name);
assert.equal(sampleGpgKey().trim(), decoded.public_key);
assert.equal("Alice", responses[0].gpg_key_dist.name);
assert.equal(sampleGpgKey().trim(), responses[0].gpg_key_dist.public_key);
});
});

Expand Down Expand Up @@ -109,8 +108,7 @@ test('Search for a key which is in another agent collection', async () => {
},
});
assert.equal(1, responses.length);
const decoded = (decode((responses[0].key.entry as any).Present.entry) as any);
assert.equal("Alice", decoded.name);
assert.equal(1, responses[0].key_collection_count);
assert.equal("Alice", responses[0].gpg_key_dist.name);
assert.equal(1, responses[0].reference_count);
});
});
6 changes: 6 additions & 0 deletions ui/src/component/KeyList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const copyFingerprint = (keyDist: GpgKeyDist) => {
<th>Name</th>
<th>Email</th>
<th>Expiry</th>
<th v-if="keys.some((k) => k.reference_count !== undefined)">
References
</th>
<th>Fingerprint</th>
<th v-if="!readonly">Action</th>
</tr>
Expand All @@ -48,6 +51,9 @@ const copyFingerprint = (keyDist: GpgKeyDist) => {
<td>{{ k.name }}</td>
<td>{{ k.email ?? "-" }}</td>
<td>{{ k.expires_at ? formatDistanceToNow(k.expires_at) : "-" }}</td>
<td v-if="keys.some((k) => k.reference_count !== undefined)">
{{ k.reference_count ?? "unknown" }}
</td>

<td class="cursor-pointer font-mono" @click="copyFingerprint(k)">
<GpgFingerprint :fingerprint="k.fingerprint" />
Expand Down
17 changes: 10 additions & 7 deletions ui/src/trusted/trusted/SearchKeys.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script setup lang="ts">
import { AppAgentClient, Record } from "@holochain/client";
import { AppAgentClient } from "@holochain/client";
import { ComputedRef, inject, ref } from "vue";
import { GpgKeyDist, SearchKeysRequest } from "./types";
import { decode } from "@msgpack/msgpack";
import { GpgKeyDist, SearchKeysRequest, SearchKeysResponse } from "./types";
import { useNotificationsStore } from "../../store/notifications-store";
import KeyList from "../../component/KeyList.vue";
import AddKeyToCollection from "./AddKeyToCollection.vue";
Expand All @@ -26,17 +25,21 @@ const searchKeys = async () => {
query: searchQuery.value,
};
const r: Record[] = await client.value.callZome({
const r: SearchKeysResponse[] = await client.value.callZome({
role_name: "trusted",
zome_name: "trusted",
fn_name: "search_keys",
payload: request,
cap_secret: null,
});
results.value = r.map(
(record) => decode((record.entry as any).Present.entry) as GpgKeyDist,
);
results.value = r.map((r) => {
console.log("ref count is", r.reference_count);
return {
...r.gpg_key_dist,
reference_count: r.reference_count,
};
});
} catch (e) {
notifications.pushNotification({
message: `Error searching for keys - ${e}`,
Expand Down
6 changes: 6 additions & 0 deletions ui/src/trusted/trusted/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ export interface GpgKeyDist {
name: string;
email?: string;
expires_at?: Date;
reference_count?: number;
}

export interface SearchKeysRequest {
query: string;
}

export interface SearchKeysResponse {
gpg_key_dist: GpgKeyDist;
reference_count: number;
}

export interface KeyCollection {
name: string;
}

0 comments on commit 33e7304

Please sign in to comment.