diff --git a/zomes/chat/src/entries/channel/handlers.rs b/zomes/chat/src/entries/channel/handlers.rs index 7bfb649..29243ba 100644 --- a/zomes/chat/src/entries/channel/handlers.rs +++ b/zomes/chat/src/entries/channel/handlers.rs @@ -1,3 +1,4 @@ +use super::{ChannelData, ChannelInfo, ChannelInfoTag, ChannelList, ChannelListInput}; use crate::{ channel::{Channel, ChannelInput}, error::ChatResult, @@ -5,8 +6,7 @@ use crate::{ }; use hdk::prelude::*; use link::Link; - -use super::{ChannelData, ChannelInfo, ChannelInfoTag, ChannelList, ChannelListInput}; +use std::collections::HashMap; /// Create a new channel /// This effectively just stores channel info on the @@ -45,7 +45,11 @@ pub(crate) fn list_channels(list_channels_input: ChannelListInput) -> ChatResult // Get any channels on this path let links = path.children()?.into_inner(); let mut channels = Vec::with_capacity(links.len()); - + struct ChannelPayload { + entry: Channel, + latest_chunk: u32, + } + let mut channel_data: HashMap = HashMap::new(); // For each channel get the channel info links and choose the latest for tag in links.into_iter().map(|link| link.tag) { // Path links have their full path as the tag so @@ -97,18 +101,38 @@ pub(crate) fn list_channels(list_channels_input: ChannelListInput) -> ChatResult } } - // Get the actual channel info entry - if let Some(element) = get(latest_info.target, GetOptions::content())? { - if let Some(info) = element.into_inner().1.to_app_option()? { - // Construct the channel data from the channel and info - channels.push(ChannelData { - entry: channel, - info, - latest_chunk: chunk, - }); + channel_data.insert( + latest_info.target, + ChannelPayload { + entry: channel, + latest_chunk: chunk, + }, + ); + } + let chan_results_input = channel_data + .keys() + .cloned() + .into_iter() + .map(|t| GetInput::new(t.into(), GetOptions::default())) + .collect(); + let all_channel_results_elements = HDK.with(|hdk| hdk.borrow().get(chan_results_input))?; + // Get the actual channel info entry + for ele in all_channel_results_elements.into_iter() { + if let Some(element) = ele { + if let Some(info) = element.into_inner().1.to_app_option::()? { + // Turn the entry into a ChannelInfo + let info_hash = hash_entry(&info)?; + if let Some(d) = channel_data.get(&info_hash) { + channels.push(ChannelData { + entry: d.entry.clone(), + info, + latest_chunk: d.latest_chunk.clone(), + }) + } } } } + // Return all the channels data to the UI Ok(channels.into()) } diff --git a/zomes/chat/src/entries/message/handlers.rs b/zomes/chat/src/entries/message/handlers.rs index dd69e73..ce54fca 100644 --- a/zomes/chat/src/entries/message/handlers.rs +++ b/zomes/chat/src/entries/message/handlers.rs @@ -149,7 +149,10 @@ fn get_messages(links: Vec) -> ChatResult> { let signed_header = match headers.pop() { Some(h) => h, // Ignoring missing messages - None => continue, + None => { + debug!("Ignoring missing messages"); + continue; + } }; // Create the message type for the UI diff --git a/zomes/chat/src/pagination_helper.rs b/zomes/chat/src/pagination_helper.rs index d580207..4d7b9c2 100644 --- a/zomes/chat/src/pagination_helper.rs +++ b/zomes/chat/src/pagination_helper.rs @@ -37,7 +37,10 @@ pub fn get_page_links( // set next path match get_next_path(&base, next_path)? { Some(p) => next_path = p, - None => break, + None => { + debug!("Crawled through the entire tree"); + break; + } } } Ok(links) @@ -45,15 +48,15 @@ pub fn get_page_links( /// pub fn get_next_path(channel: &Path, last_seen: Path) -> ChatResult> { - let (_, _, l_day, l_hour) = path_spread(&last_seen)?; + let (ly, lm, l_day, mut l_hour) = path_spread(&last_seen)?; + debug!("Crawling through year {} and month {}", ly, lm); // get base that starts with year and month let base_ym = get_year_month_path(&channel, &last_seen)?; // ROOT->Y->M let days = base_ym.children()?.into_inner(); - // let mut msg_links: Vec = Vec::new(); for tag in days.clone().into_iter().rev().map(|link| link.tag) { let day = Path::try_from(&tag)?; // ROOT->Y->M->D let dp: &Vec<_> = day.as_ref(); - // debug!("CHECK days: {:?}", String::try_from(&dp[dp.len() - 1])?); + debug!("Checking day {:?}", String::try_from(&dp[dp.len() - 1])?); // check if latest day is less than path day if format!("{}", l_day) >= String::try_from(&dp[dp.len() - 1])? { let hours = day.children()?.into_inner(); @@ -61,11 +64,14 @@ pub fn get_next_path(channel: &Path, last_seen: Path) -> ChatResult let hour_path = Path::try_from(&tag)?; // ROOT->Y->M->D->H let hp: &Vec<_> = hour_path.as_ref(); let hour = String::try_from(&hp[hp.len() - 1])?; - debug!("CHECK hour/seconds: {:?}", hour); + debug!("Checking hour: {:?}", hour); if hour < format!("{}", l_hour) { + debug!("Next hour selected: {:?}", hour); return Ok(Some(hour_path)); } } + debug!("Resetting the hour to 23:00"); + l_hour = "23".to_string() } }