holochain_types/
activity.rsuse holo_hash::ActionHash;
use holo_hash::AgentPubKey;
use holochain_serialized_bytes::prelude::*;
use holochain_zome_types::prelude::*;
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, SerializedBytes)]
pub struct AgentActivityResponse {
pub agent: AgentPubKey,
pub valid_activity: ChainItems,
pub rejected_activity: ChainItems,
pub status: ChainStatus,
pub highest_observed: Option<HighestObserved>,
pub warrants: Vec<Warrant>,
}
impl AgentActivityResponse {
pub fn from_empty(other: AgentActivityResponse) -> Self {
let convert_activity = |items: &ChainItems| match items {
ChainItems::Full(_) => ChainItems::Full(Vec::with_capacity(0)),
ChainItems::Hashes(_) => ChainItems::Hashes(Vec::with_capacity(0)),
ChainItems::NotRequested => ChainItems::NotRequested,
};
AgentActivityResponse {
agent: other.agent,
valid_activity: convert_activity(&other.valid_activity),
rejected_activity: convert_activity(&other.rejected_activity),
status: ChainStatus::Empty,
highest_observed: other.highest_observed,
warrants: other.warrants,
}
}
pub fn status_only(other: AgentActivityResponse) -> Self {
AgentActivityResponse {
agent: other.agent,
valid_activity: ChainItems::NotRequested,
rejected_activity: ChainItems::NotRequested,
status: ChainStatus::Empty,
highest_observed: other.highest_observed,
warrants: other.warrants,
}
}
pub fn hashes_only(other: AgentActivityResponse) -> Self {
let convert_activity = |items: ChainItems| match items {
ChainItems::Full(records) => ChainItems::Hashes(
records
.into_iter()
.map(|r| (r.action().action_seq(), r.address().clone()))
.collect(),
),
ChainItems::Hashes(h) => ChainItems::Hashes(h),
ChainItems::NotRequested => ChainItems::NotRequested,
};
AgentActivityResponse {
agent: other.agent,
valid_activity: convert_activity(other.valid_activity),
rejected_activity: convert_activity(other.rejected_activity),
status: other.status,
highest_observed: other.highest_observed,
warrants: other.warrants,
}
}
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, SerializedBytes)]
pub enum ChainItems {
Full(Vec<Record>),
Hashes(Vec<(u32, ActionHash)>),
NotRequested,
}
impl From<AgentActivityResponse> for AgentActivity {
fn from(a: AgentActivityResponse) -> Self {
let valid_activity = match a.valid_activity {
ChainItems::Full(records) => records
.into_iter()
.map(|el| (el.action().action_seq(), el.action_address().clone()))
.collect(),
ChainItems::Hashes(h) => h,
ChainItems::NotRequested => Vec::new(),
};
let rejected_activity = match a.rejected_activity {
ChainItems::Full(records) => records
.into_iter()
.map(|el| (el.action().action_seq(), el.action_address().clone()))
.collect(),
ChainItems::Hashes(h) => h,
ChainItems::NotRequested => Vec::new(),
};
Self {
valid_activity,
rejected_activity,
status: a.status,
highest_observed: a.highest_observed,
warrants: a.warrants,
}
}
}
pub trait ChainItemsSource {
fn to_chain_items(self) -> ChainItems;
}
impl ChainItemsSource for Vec<Record> {
fn to_chain_items(self) -> ChainItems {
ChainItems::Full(self)
}
}
impl ChainItemsSource for Vec<ActionHashed> {
fn to_chain_items(self) -> ChainItems {
ChainItems::Hashes(
self.into_iter()
.map(|a| (a.action_seq(), a.as_hash().clone()))
.collect(),
)
}
}
impl ChainItemsSource for Vec<(u32, ActionHash)> {
fn to_chain_items(self) -> ChainItems {
ChainItems::Hashes(self)
}
}