Skip to content

Commit

Permalink
resetting hours for each day change
Browse files Browse the repository at this point in the history
  • Loading branch information
zo-el committed Nov 8, 2021
1 parent a741dbf commit 452aa9e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
48 changes: 36 additions & 12 deletions zomes/chat/src/entries/channel/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{ChannelData, ChannelInfo, ChannelInfoTag, ChannelList, ChannelListInput};
use crate::{
channel::{Channel, ChannelInput},
error::ChatResult,
message::handlers::add_chunk_path,
};
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
Expand Down Expand Up @@ -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<EntryHash, ChannelPayload> = 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
Expand Down Expand Up @@ -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::<ChannelInfo>()? {
// 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())
}
Expand Down
5 changes: 4 additions & 1 deletion zomes/chat/src/entries/message/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ fn get_messages(links: Vec<Link>) -> ChatResult<Vec<MessageData>> {
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
Expand Down
16 changes: 11 additions & 5 deletions zomes/chat/src/pagination_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,41 @@ 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)
}

///
pub fn get_next_path(channel: &Path, last_seen: Path) -> ChatResult<Option<Path>> {
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<Link> = 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();
for tag in hours.clone().into_iter().rev().map(|link| link.tag) {
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()
}
}

Expand Down

0 comments on commit 452aa9e

Please sign in to comment.