---
# System prepended metadata

title: Aggregator Zome
tags: [Kizuna-architecture]

---

# Aggregator Zome
###### tags: `Kizuna-architecture`
<style> .ui-infobar, #doc.markdown-body { max-width: 1000px; } </style>

## Data Structure
```rust=
// for contacts
pub struct AgentPubKeys(pub Vec<AgentPubKey>);

// for username
pub struct UsernameInfo {
    username: String,
    agent_id: AgentPubKey,
    created_at: Timestamp,
    entry_header_hash: HeaderHash,
}

// for group
pub struct GroupOutput {
    group_id: EntryHash,
    group_revision_id: HeaderHash,
    latest_name: String,
    members: Vec<AgentPubKey>,
    creator: AgentPubKey,
    created: Timestamp,
    // group_versions: Vec<Group>,
}

pub struct MyGroupListWrapper(pub Vec<GroupOutput>);

// for group chat
pub struct BatchSize(pub u8);

pub struct GroupMessageHash(pub EntryHash);

pub struct ReadList(pub HashMap<String, SystemTime>);

pub struct GroupMessageElement(pub Element);

pub struct GroupMessageContent(pub GroupMessageElement, pub ReadList);

pub struct MessagesByGroup(pub HashMap<String, Vec<GroupMessageHash>>);

pub struct GroupMessagesContents(pub HashMap<String, GroupMessageContent>);

pub struct GroupMessagesOutput {
    messages_by_group: MessagesByGroup,
    group_messages_contents: GroupMessagesContents,
}

// for p2p chat
pub struct P2PMessage {
    author: AgentPubKey,
    receiver: AgentPubKey,
    payload: Payload,
    time_sent: Timestamp,
    reply_to: Option<EntryHash>,
}

pub struct P2PMessageReceipt {
    id: EntryHash,
    status: Status,
}

pub enum Payload {
    Text {
        payload: String,
    },
    File {
        metadata: FileMetadata,
        file_type: FileType,
    },
}

pub struct FileMetadata {
    file_name: String,
    file_size: u8,
    file_type: FileType,
    file_hash: String,
}

pub enum FileType {
    Image { thumbnail: SerializedBytes },
    Video { thumbnail: SerializedBytes },
    Others,
}

pub enum Status {
    Sent,
    Delivered { timestamp: Timestamp },
    Read { timestamp: Timestamp },
}

pub struct AgentMessages(HashMap<String, Vec<String>>);
pub struct MessageBundle(P2PMessage, Vec<String>);
pub struct MessageContents(HashMap<String, MessageBundle>);

pub struct ReceiptContents(HashMap<String, P2PMessageReceipt>);

pub struct P2PMessageHashTables(AgentMessages, MessageContents, ReceiptContents);

// for preference
pub struct Preference {
    typing_indicator: bool,
    read_receipt: bool,
}

pub struct PerAgentPreference {
    typing_indicator: Vec<AgentPubKey>,
    read_receipt: Vec<AgentPubKey>,
}

pub struct PerGroupPreference {
    typing_indicator: Vec<String>,
    read_receipt: Vec<String>,
}

// the aggregated data from other zomes
pub struct AggregatedLatestData {
    pub user_info: UsernameInfo,
    // retrieved from contacts zome
    pub added_contacts: Vec<AgentPubKey>,
    pub blocked_contacts: Vec<AgentPubKey>,
    // from group
    pub groups: MyGroupListWrapper,
    pub latest_group_messages: GroupMessagesOutput,
    // from p2pmessage
    pub latest_p2p_messages: P2PMessageHashTables,
    // from preference
    pub global_preference: Preference,
    pub per_agent_preference: PerAgentPreference,
    pub per_group_preference: PerGroupPreference,
}
```

## Zome Fn
```rust=
fn retrieve_latest_data() -> ExternResult<AggregatedLatestData> 
```


```mermaid
sequenceDiagram

participant UI as user_interface
participant UC as user_cell

UI-->>UC: retrieve_latest_data()
UC-->>UC: call() get_my_username()
UC-->>UC: call() list_added()
UC-->>UC: call() list_blocked()
UC-->>UC: call() get_latest_p2p_messages()
UC-->>UC: call() get_latest_group_messages
UC-->>UC: call() get_preference
UC-->>UC: call() get_per_agent_preference
UC-->>UC: call() get_per_group_preference
UC-->>UC: construct AggregatedLatestData
UC-->>UI: AggregatedLatestData
```
