Switch to unified timeline cache via TimelineKinds

This is a fairly large rewrite which unifies our threads, timelines and
profiles. Now all timelines have a MultiSubscriber, and can be added
and removed to columns just like Threads and Profiles.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-01-22 15:59:21 -08:00
parent d46e526a45
commit 0cc1d8a600
39 changed files with 1395 additions and 2055 deletions

View File

@@ -1,23 +1,21 @@
use crate::{
actionbar::TimelineOpenResult,
error::Error,
multi_subscriber::MultiSubscriber,
profile::Profile,
thread::Thread,
//subscriptions::SubRefs,
timeline::{PubkeySource, Timeline},
timeline::{Timeline, TimelineKind},
};
use notedeck::{NoteCache, NoteRef, RootNoteId, RootNoteIdBuf};
use notedeck::{filter, FilterState, NoteCache, NoteRef};
use enostr::{Pubkey, PubkeyRef, RelayPool};
use nostrdb::{Filter, FilterBuilder, Ndb, Transaction};
use enostr::RelayPool;
use nostrdb::{Filter, Ndb, Transaction};
use std::collections::HashMap;
use tracing::{debug, info, warn};
use tracing::{debug, error, info, warn};
#[derive(Default)]
pub struct TimelineCache {
pub threads: HashMap<RootNoteIdBuf, Thread>,
pub profiles: HashMap<Pubkey, Profile>,
pub timelines: HashMap<TimelineKind, Timeline>,
}
pub enum Vitality<'a, M> {
@@ -41,102 +39,64 @@ impl<'a, M> Vitality<'a, M> {
}
}
#[derive(Hash, Debug, Copy, Clone)]
pub enum TimelineCacheKey<'a> {
Profile(PubkeyRef<'a>),
Thread(RootNoteId<'a>),
}
impl<'a> TimelineCacheKey<'a> {
pub fn profile(pubkey: PubkeyRef<'a>) -> Self {
Self::Profile(pubkey)
}
pub fn thread(root_id: RootNoteId<'a>) -> Self {
Self::Thread(root_id)
}
pub fn bytes(&self) -> &[u8; 32] {
match self {
Self::Profile(pk) => pk.bytes(),
Self::Thread(root_id) => root_id.bytes(),
}
}
/// The filters used to update our timeline cache
pub fn filters_raw(&self) -> Vec<FilterBuilder> {
match self {
TimelineCacheKey::Thread(root_id) => Thread::filters_raw(*root_id),
TimelineCacheKey::Profile(pubkey) => vec![Filter::new()
.authors([pubkey.bytes()])
.kinds([1])
.limit(notedeck::filter::default_limit())],
}
}
pub fn filters_since(&self, since: u64) -> Vec<Filter> {
self.filters_raw()
.into_iter()
.map(|fb| fb.since(since).build())
.collect()
}
pub fn filters(&self) -> Vec<Filter> {
self.filters_raw()
.into_iter()
.map(|mut fb| fb.build())
.collect()
}
}
impl TimelineCache {
fn contains_key(&self, key: TimelineCacheKey<'_>) -> bool {
match key {
TimelineCacheKey::Profile(pubkey) => self.profiles.contains_key(pubkey.bytes()),
TimelineCacheKey::Thread(root_id) => self.threads.contains_key(root_id.bytes()),
/// Pop a timeline from the timeline cache. This only removes the timeline
/// if it has reached 0 subscribers, meaning it was the last one to be
/// removed
pub fn pop(
&mut self,
id: &TimelineKind,
ndb: &mut Ndb,
pool: &mut RelayPool,
) -> Result<(), Error> {
let timeline = if let Some(timeline) = self.timelines.get_mut(id) {
timeline
} else {
return Err(Error::TimelineNotFound);
};
if let Some(sub) = &mut timeline.subscription {
// if this is the last subscriber, remove the timeline from cache
if sub.unsubscribe(ndb, pool) {
debug!(
"popped last timeline {:?}, removing from timeline cache",
id
);
self.timelines.remove(id);
}
Ok(())
} else {
Err(Error::MissingSubscription)
}
}
fn get_expected_mut(&mut self, key: TimelineCacheKey<'_>) -> &mut Timeline {
match key {
TimelineCacheKey::Profile(pubkey) => self
.profiles
.get_mut(pubkey.bytes())
.map(|p| &mut p.timeline),
TimelineCacheKey::Thread(root_id) => self
.threads
.get_mut(root_id.bytes())
.map(|t| &mut t.timeline),
}
.expect("expected notes in timline cache")
fn get_expected_mut(&mut self, key: &TimelineKind) -> &mut Timeline {
self.timelines
.get_mut(key)
.expect("expected notes in timline cache")
}
/// Insert a new profile or thread into the cache, based on the TimelineCacheKey
/// Insert a new timeline into the cache, based on the TimelineKind
#[allow(clippy::too_many_arguments)]
fn insert_new(
&mut self,
id: TimelineCacheKey<'_>,
id: TimelineKind,
txn: &Transaction,
ndb: &Ndb,
notes: &[NoteRef],
note_cache: &mut NoteCache,
filters: Vec<Filter>,
) {
match id {
TimelineCacheKey::Profile(pubkey) => {
let mut profile = Profile::new(PubkeySource::Explicit(pubkey.to_owned()), filters);
// insert initial notes into timeline
profile.timeline.insert_new(txn, ndb, note_cache, notes);
self.profiles.insert(pubkey.to_owned(), profile);
}
let mut timeline = if let Some(timeline) = id.clone().into_timeline(txn, ndb) {
timeline
} else {
error!("Error creating timeline from {:?}", &id);
return;
};
TimelineCacheKey::Thread(root_id) => {
let mut thread = Thread::new(root_id.to_owned());
thread.timeline.insert_new(txn, ndb, note_cache, notes);
self.threads.insert(root_id.to_owned(), thread);
}
}
// insert initial notes into timeline
timeline.insert_new(txn, ndb, note_cache, notes);
self.timelines.insert(id, timeline);
}
/// Get and/or update the notes associated with this timeline
@@ -145,24 +105,28 @@ impl TimelineCache {
ndb: &Ndb,
note_cache: &mut NoteCache,
txn: &Transaction,
id: TimelineCacheKey<'a>,
id: &TimelineKind,
) -> Vitality<'a, Timeline> {
// we can't use the naive hashmap entry API here because lookups
// require a copy, wait until we have a raw entry api. We could
// also use hashbrown?
if self.contains_key(id) {
if self.timelines.contains_key(id) {
return Vitality::Stale(self.get_expected_mut(id));
}
let filters = id.filters();
let notes = if let Ok(results) = ndb.query(txn, &filters, 1000) {
results
.into_iter()
.map(NoteRef::from_query_result)
.collect()
let notes = if let FilterState::Ready(filters) = id.filters(txn, ndb) {
if let Ok(results) = ndb.query(txn, &filters, 1000) {
results
.into_iter()
.map(NoteRef::from_query_result)
.collect()
} else {
debug!("got no results from TimelineCache lookup for {:?}", id);
vec![]
}
} else {
debug!("got no results from TimelineCache lookup for {:?}", id);
// filter is not ready yet
vec![]
};
@@ -172,44 +136,37 @@ impl TimelineCache {
info!("found NotesHolder with {} notes", notes.len());
}
self.insert_new(id, txn, ndb, &notes, note_cache, filters);
self.insert_new(id.to_owned(), txn, ndb, &notes, note_cache);
Vitality::Fresh(self.get_expected_mut(id))
}
pub fn subscription(
&mut self,
id: TimelineCacheKey<'_>,
) -> Option<&mut Option<MultiSubscriber>> {
match id {
TimelineCacheKey::Profile(pubkey) => self
.profiles
.get_mut(pubkey.bytes())
.map(|p| &mut p.subscription),
TimelineCacheKey::Thread(root_id) => self
.threads
.get_mut(root_id.bytes())
.map(|t| &mut t.subscription),
}
}
pub fn open<'a>(
/// Open a timeline, this is another way of saying insert a timeline
/// into the timeline cache. If there exists a timeline already, we
/// bump its subscription reference count. If it's new we start a new
/// subscription
pub fn open(
&mut self,
ndb: &Ndb,
note_cache: &mut NoteCache,
txn: &Transaction,
pool: &mut RelayPool,
id: TimelineCacheKey<'a>,
) -> Option<TimelineOpenResult<'a>> {
let result = match self.notes(ndb, note_cache, txn, id) {
id: &TimelineKind,
) -> Option<TimelineOpenResult> {
let (open_result, timeline) = match self.notes(ndb, note_cache, txn, id) {
Vitality::Stale(timeline) => {
// The timeline cache is stale, let's update it
let notes = find_new_notes(timeline.all_or_any_notes(), id, txn, ndb);
let cached_timeline_result = if notes.is_empty() {
let notes = find_new_notes(
timeline.all_or_any_notes(),
timeline.subscription.as_ref().map(|s| &s.filters)?,
txn,
ndb,
);
let open_result = if notes.is_empty() {
None
} else {
let new_notes = notes.iter().map(|n| n.key).collect();
Some(TimelineOpenResult::new_notes(new_notes, id))
Some(TimelineOpenResult::new_notes(new_notes, id.clone()))
};
// we can't insert and update the VirtualList now, because we
@@ -217,42 +174,36 @@ impl TimelineCache {
// result instead
//
// holder.get_view().insert(&notes); <-- no
cached_timeline_result
(open_result, timeline)
}
Vitality::Fresh(_timeline) => None,
Vitality::Fresh(timeline) => (None, timeline),
};
let sub_id = if let Some(sub) = self.subscription(id) {
if let Some(multi_subscriber) = sub {
multi_subscriber.subscribe(ndb, pool);
multi_subscriber.sub.as_ref().map(|s| s.local)
} else {
let mut multi_sub = MultiSubscriber::new(id.filters());
multi_sub.subscribe(ndb, pool);
let sub_id = multi_sub.sub.as_ref().map(|s| s.local);
*sub = Some(multi_sub);
sub_id
}
if let Some(multi_sub) = &mut timeline.subscription {
debug!("got open with *old* subscription for {:?}", &timeline.kind);
multi_sub.subscribe(ndb, pool);
} else if let Some(filter) = timeline.filter.get_any_ready() {
debug!("got open with *new* subscription for {:?}", &timeline.kind);
let mut multi_sub = MultiSubscriber::new(filter.clone());
multi_sub.subscribe(ndb, pool);
timeline.subscription = Some(multi_sub);
} else {
None
// This should never happen reasoning, self.notes would have
// failed above if the filter wasn't ready
error!(
"open: filter not ready, so could not setup subscription. this should never happen"
);
};
let timeline = self.get_expected_mut(id);
if let Some(sub_id) = sub_id {
timeline.subscription = Some(sub_id);
}
// TODO: We have subscription ids tracked in different places. Fix this
result
open_result
}
}
/// Look for new thread notes since our last fetch
fn find_new_notes(
notes: &[NoteRef],
id: TimelineCacheKey<'_>,
filters: &[Filter],
txn: &Transaction,
ndb: &Ndb,
) -> Vec<NoteRef> {
@@ -261,7 +212,7 @@ fn find_new_notes(
}
let last_note = notes[0];
let filters = id.filters_since(last_note.created_at + 1);
let filters = filter::make_filters_since(filters, last_note.created_at + 1);
if let Ok(results) = ndb.query(txn, &filters, 1000) {
debug!("got {} results from NotesHolder update", results.len());

View File

@@ -1,23 +1,35 @@
use crate::error::Error;
use crate::timeline::{Timeline, TimelineTab};
use enostr::{Filter, Pubkey};
use enostr::{Filter, NoteId, Pubkey};
use nostrdb::{Ndb, Transaction};
use notedeck::{filter::default_limit, FilterError, FilterState, RootNoteIdBuf};
use notedeck::{
filter::{self, default_limit},
FilterError, FilterState, NoteCache, RootIdError, RootNoteIdBuf,
};
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::{borrow::Cow, fmt::Display};
use tokenator::{ParseError, TokenParser, TokenSerializable, TokenWriter};
use tracing::{error, warn};
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Hash, Copy, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PubkeySource {
Explicit(Pubkey),
#[default]
DeckAuthor,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq)]
pub enum ListKind {
Contact(PubkeySource),
Contact(Pubkey),
}
impl ListKind {
pub fn pubkey(&self) -> Option<&Pubkey> {
match self {
Self::Contact(pk) => Some(pk),
}
}
}
impl PubkeySource {
@@ -31,13 +43,6 @@ impl PubkeySource {
PubkeySource::DeckAuthor => deck_author,
}
}
pub fn to_pubkey_bytes<'a>(&'a self, deck_author: &'a [u8; 32]) -> &'a [u8; 32] {
match self {
PubkeySource::Explicit(pk) => pk.bytes(),
PubkeySource::DeckAuthor => deck_author,
}
}
}
impl TokenSerializable for PubkeySource {
@@ -77,32 +82,18 @@ impl TokenSerializable for PubkeySource {
}
impl ListKind {
pub fn contact_list(pk_src: PubkeySource) -> Self {
ListKind::Contact(pk_src)
pub fn contact_list(pk: Pubkey) -> Self {
ListKind::Contact(pk)
}
pub fn pubkey_source(&self) -> Option<&PubkeySource> {
match self {
ListKind::Contact(pk_src) => Some(pk_src),
}
}
}
impl TokenSerializable for ListKind {
fn serialize_tokens(&self, writer: &mut TokenWriter) {
match self {
ListKind::Contact(pk_src) => {
writer.write_token("contact");
pk_src.serialize_tokens(writer);
}
}
}
fn parse_from_tokens<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
pub fn parse<'a>(
parser: &mut TokenParser<'a>,
deck_author: &Pubkey,
) -> Result<Self, ParseError<'a>> {
parser.parse_all(|p| {
p.parse_token("contact")?;
let pk_src = PubkeySource::parse_from_tokens(p)?;
Ok(ListKind::Contact(pk_src))
Ok(ListKind::Contact(*pk_src.to_pubkey(deck_author)))
})
/* here for u when you need more things to parse
@@ -120,8 +111,80 @@ impl TokenSerializable for ListKind {
)
*/
}
pub fn serialize_tokens(&self, writer: &mut TokenWriter) {
match self {
ListKind::Contact(pk) => {
writer.write_token("contact");
PubkeySource::pubkey(*pk).serialize_tokens(writer);
}
}
}
}
/// Thread selection hashing is done in a specific way. For TimelineCache
/// lookups, we want to only let the root_id influence thread selection.
/// This way Thread TimelineKinds always map to the same cached timeline
/// for now (we will likely have to rework this since threads aren't
/// *really* timelines)
#[derive(Debug, Clone)]
pub struct ThreadSelection {
pub root_id: RootNoteIdBuf,
/// The selected note, if different than the root_id. None here
/// means the root is selected
pub selected_note: Option<NoteId>,
}
impl ThreadSelection {
pub fn selected_or_root(&self) -> &[u8; 32] {
self.selected_note
.as_ref()
.map(|sn| sn.bytes())
.unwrap_or(self.root_id.bytes())
}
pub fn from_root_id(root_id: RootNoteIdBuf) -> Self {
Self {
root_id,
selected_note: None,
}
}
pub fn from_note_id(
ndb: &Ndb,
note_cache: &mut NoteCache,
txn: &Transaction,
note_id: NoteId,
) -> Result<Self, RootIdError> {
let root_id = RootNoteIdBuf::new(ndb, note_cache, txn, note_id.bytes())?;
Ok(if root_id.bytes() == note_id.bytes() {
Self::from_root_id(root_id)
} else {
Self {
root_id,
selected_note: Some(note_id),
}
})
}
}
impl Hash for ThreadSelection {
fn hash<H: Hasher>(&self, state: &mut H) {
// only hash the root id for thread selection
self.root_id.hash(state)
}
}
// need this to only match root_id or else hash lookups will fail
impl PartialEq for ThreadSelection {
fn eq(&self, other: &Self) -> bool {
self.root_id == other.root_id
}
}
impl Eq for ThreadSelection {}
///
/// What kind of timeline is it?
/// - Follow List
@@ -130,24 +193,23 @@ impl TokenSerializable for ListKind {
/// - filter
/// - ... etc
///
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TimelineKind {
List(ListKind),
/// The last not per pubkey
Algo(AlgoTimeline),
Notifications(PubkeySource),
Notifications(Pubkey),
Profile(PubkeySource),
Profile(Pubkey),
/// This could be any note id, doesn't need to be the root id
Thread(RootNoteIdBuf),
Thread(ThreadSelection),
Universe,
/// Generic filter
Generic,
/// Generic filter, references a hash of a filter
Generic(u64),
Hashtag(String),
}
@@ -155,86 +217,8 @@ pub enum TimelineKind {
const NOTIFS_TOKEN_DEPRECATED: &str = "notifs";
const NOTIFS_TOKEN: &str = "notifications";
fn parse_hex_id<'a>(parser: &mut TokenParser<'a>) -> Result<[u8; 32], ParseError<'a>> {
let hex = parser.pull_token()?;
hex::decode(hex)
.map_err(|_| ParseError::HexDecodeFailed)?
.as_slice()
.try_into()
.map_err(|_| ParseError::HexDecodeFailed)
}
impl TokenSerializable for TimelineKind {
fn serialize_tokens(&self, writer: &mut TokenWriter) {
match self {
TimelineKind::List(list_kind) => list_kind.serialize_tokens(writer),
TimelineKind::Algo(algo_timeline) => algo_timeline.serialize_tokens(writer),
TimelineKind::Notifications(pk_src) => {
writer.write_token(NOTIFS_TOKEN);
pk_src.serialize_tokens(writer);
}
TimelineKind::Profile(pk_src) => {
writer.write_token("profile");
pk_src.serialize_tokens(writer);
}
TimelineKind::Thread(root_note_id) => {
writer.write_token("thread");
writer.write_token(&root_note_id.hex());
}
TimelineKind::Universe => {
writer.write_token("universe");
}
TimelineKind::Generic => {
writer.write_token("generic");
}
TimelineKind::Hashtag(ht) => {
writer.write_token("hashtag");
writer.write_token(ht);
}
}
}
fn parse_from_tokens<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
TokenParser::alt(
parser,
&[
|p| Ok(TimelineKind::List(ListKind::parse_from_tokens(p)?)),
|p| Ok(TimelineKind::Algo(AlgoTimeline::parse_from_tokens(p)?)),
|p| {
// still handle deprecated form (notifs)
p.parse_any_token(&[NOTIFS_TOKEN, NOTIFS_TOKEN_DEPRECATED])?;
Ok(TimelineKind::Notifications(
PubkeySource::parse_from_tokens(p)?,
))
},
|p| {
p.parse_token("profile")?;
Ok(TimelineKind::Profile(PubkeySource::parse_from_tokens(p)?))
},
|p| {
p.parse_token("thread")?;
let note_id = RootNoteIdBuf::new_unsafe(parse_hex_id(p)?);
Ok(TimelineKind::Thread(note_id))
},
|p| {
p.parse_token("universe")?;
Ok(TimelineKind::Universe)
},
|p| {
p.parse_token("generic")?;
Ok(TimelineKind::Generic)
},
|p| {
p.parse_token("hashtag")?;
Ok(TimelineKind::Hashtag(p.pull_token()?.to_string()))
},
],
)
}
}
/// Hardcoded algo timelines
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub enum AlgoTimeline {
/// LastPerPubkey: a special nostr query that fetches the last N
/// notes for each pubkey on the list
@@ -244,8 +228,8 @@ pub enum AlgoTimeline {
/// The identifier for our last per pubkey algo
const LAST_PER_PUBKEY_TOKEN: &str = "last_per_pubkey";
impl TokenSerializable for AlgoTimeline {
fn serialize_tokens(&self, writer: &mut TokenWriter) {
impl AlgoTimeline {
pub fn serialize_tokens(&self, writer: &mut TokenWriter) {
match self {
AlgoTimeline::LastPerPubkey(list_kind) => {
writer.write_token(LAST_PER_PUBKEY_TOKEN);
@@ -254,16 +238,17 @@ impl TokenSerializable for AlgoTimeline {
}
}
fn parse_from_tokens<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
TokenParser::alt(
parser,
&[|p| {
p.parse_all(|p| {
p.parse_token(LAST_PER_PUBKEY_TOKEN)?;
Ok(AlgoTimeline::LastPerPubkey(ListKind::parse_from_tokens(p)?))
})
}],
)
pub fn parse<'a>(
parser: &mut TokenParser<'a>,
deck_author: &Pubkey,
) -> Result<Self, ParseError<'a>> {
parser.parse_all(|p| {
p.parse_token(LAST_PER_PUBKEY_TOKEN)?;
Ok(AlgoTimeline::LastPerPubkey(ListKind::parse(
p,
deck_author,
)?))
})
}
}
@@ -272,7 +257,7 @@ impl Display for TimelineKind {
match self {
TimelineKind::List(ListKind::Contact(_src)) => f.write_str("Contacts"),
TimelineKind::Algo(AlgoTimeline::LastPerPubkey(_lk)) => f.write_str("Last Notes"),
TimelineKind::Generic => f.write_str("Timeline"),
TimelineKind::Generic(_) => f.write_str("Timeline"),
TimelineKind::Notifications(_) => f.write_str("Notifications"),
TimelineKind::Profile(_) => f.write_str("Profile"),
TimelineKind::Universe => f.write_str("Universe"),
@@ -283,14 +268,14 @@ impl Display for TimelineKind {
}
impl TimelineKind {
pub fn pubkey_source(&self) -> Option<&PubkeySource> {
pub fn pubkey(&self) -> Option<&Pubkey> {
match self {
TimelineKind::List(list_kind) => list_kind.pubkey_source(),
TimelineKind::Algo(AlgoTimeline::LastPerPubkey(list_kind)) => list_kind.pubkey_source(),
TimelineKind::Notifications(pk_src) => Some(pk_src),
TimelineKind::Profile(pk_src) => Some(pk_src),
TimelineKind::List(list_kind) => list_kind.pubkey(),
TimelineKind::Algo(AlgoTimeline::LastPerPubkey(list_kind)) => list_kind.pubkey(),
TimelineKind::Notifications(pk) => Some(pk),
TimelineKind::Profile(pk) => Some(pk),
TimelineKind::Universe => None,
TimelineKind::Generic => None,
TimelineKind::Generic(_) => None,
TimelineKind::Hashtag(_ht) => None,
TimelineKind::Thread(_ht) => None,
}
@@ -305,17 +290,108 @@ impl TimelineKind {
TimelineKind::Notifications(_pk_src) => true,
TimelineKind::Profile(_pk_src) => true,
TimelineKind::Universe => true,
TimelineKind::Generic => true,
TimelineKind::Generic(_) => true,
TimelineKind::Hashtag(_ht) => true,
TimelineKind::Thread(_ht) => true,
}
}
pub fn serialize_tokens(&self, writer: &mut TokenWriter) {
match self {
TimelineKind::List(list_kind) => list_kind.serialize_tokens(writer),
TimelineKind::Algo(algo_timeline) => algo_timeline.serialize_tokens(writer),
TimelineKind::Notifications(pk) => {
writer.write_token(NOTIFS_TOKEN);
PubkeySource::pubkey(*pk).serialize_tokens(writer);
}
TimelineKind::Profile(pk) => {
writer.write_token("profile");
PubkeySource::pubkey(*pk).serialize_tokens(writer);
}
TimelineKind::Thread(root_note_id) => {
writer.write_token("thread");
writer.write_token(&root_note_id.root_id.hex());
}
TimelineKind::Universe => {
writer.write_token("universe");
}
TimelineKind::Generic(_usize) => {
// TODO: lookup filter and then serialize
writer.write_token("generic");
}
TimelineKind::Hashtag(ht) => {
writer.write_token("hashtag");
writer.write_token(ht);
}
}
}
pub fn parse<'a>(
parser: &mut TokenParser<'a>,
deck_author: &Pubkey,
) -> Result<Self, ParseError<'a>> {
let profile = parser.try_parse(|p| {
p.parse_token("profile")?;
let pk_src = PubkeySource::parse_from_tokens(p)?;
Ok(TimelineKind::Profile(*pk_src.to_pubkey(deck_author)))
});
if profile.is_ok() {
return profile;
}
let notifications = parser.try_parse(|p| {
// still handle deprecated form (notifs)
p.parse_any_token(&[NOTIFS_TOKEN, NOTIFS_TOKEN_DEPRECATED])?;
let pk_src = PubkeySource::parse_from_tokens(p)?;
Ok(TimelineKind::Notifications(*pk_src.to_pubkey(deck_author)))
});
if notifications.is_ok() {
return notifications;
}
let list_tl =
parser.try_parse(|p| Ok(TimelineKind::List(ListKind::parse(p, deck_author)?)));
if list_tl.is_ok() {
return list_tl;
}
let algo_tl =
parser.try_parse(|p| Ok(TimelineKind::Algo(AlgoTimeline::parse(p, deck_author)?)));
if algo_tl.is_ok() {
return algo_tl;
}
TokenParser::alt(
parser,
&[
|p| {
p.parse_token("thread")?;
Ok(TimelineKind::Thread(ThreadSelection::from_root_id(
RootNoteIdBuf::new_unsafe(tokenator::parse_hex_id(p)?),
)))
},
|p| {
p.parse_token("universe")?;
Ok(TimelineKind::Universe)
},
|p| {
p.parse_token("generic")?;
// TODO: generic filter serialization
Ok(TimelineKind::Generic(0))
},
|p| {
p.parse_token("hashtag")?;
Ok(TimelineKind::Hashtag(p.pull_token()?.to_string()))
},
],
)
}
pub fn last_per_pubkey(list_kind: ListKind) -> Self {
TimelineKind::Algo(AlgoTimeline::LastPerPubkey(list_kind))
}
pub fn contact_list(pk: PubkeySource) -> Self {
pub fn contact_list(pk: Pubkey) -> Self {
TimelineKind::List(ListKind::contact_list(pk))
}
@@ -323,51 +399,98 @@ impl TimelineKind {
matches!(self, TimelineKind::List(ListKind::Contact(_)))
}
pub fn profile(pk: PubkeySource) -> Self {
pub fn profile(pk: Pubkey) -> Self {
TimelineKind::Profile(pk)
}
pub fn thread(root_id: RootNoteIdBuf) -> Self {
TimelineKind::Thread(root_id)
pub fn thread(selected_note: ThreadSelection) -> Self {
TimelineKind::Thread(selected_note)
}
pub fn is_notifications(&self) -> bool {
matches!(self, TimelineKind::Notifications(_))
}
pub fn notifications(pk: PubkeySource) -> Self {
pub fn notifications(pk: Pubkey) -> Self {
TimelineKind::Notifications(pk)
}
pub fn into_timeline(self, ndb: &Ndb, default_user: Option<&[u8; 32]>) -> Option<Timeline> {
// TODO: probably should set default limit here
pub fn filters(&self, txn: &Transaction, ndb: &Ndb) -> FilterState {
match self {
TimelineKind::Universe => FilterState::ready(universe_filter()),
TimelineKind::List(list_k) => match list_k {
ListKind::Contact(pubkey) => contact_filter_state(txn, ndb, pubkey),
},
// TODO: still need to update this to fetch likes, zaps, etc
TimelineKind::Notifications(pubkey) => FilterState::ready(vec![Filter::new()
.pubkeys([pubkey.bytes()])
.kinds([1])
.limit(default_limit())
.build()]),
TimelineKind::Hashtag(hashtag) => FilterState::ready(vec![Filter::new()
.kinds([1])
.limit(filter::default_limit())
.tags([hashtag.clone()], 't')
.build()]),
TimelineKind::Algo(algo_timeline) => match algo_timeline {
AlgoTimeline::LastPerPubkey(list_k) => match list_k {
ListKind::Contact(pubkey) => last_per_pubkey_filter_state(ndb, pubkey),
},
},
TimelineKind::Generic(_) => {
todo!("implement generic filter lookups")
}
TimelineKind::Thread(selection) => FilterState::ready(vec![
nostrdb::Filter::new()
.kinds([1])
.event(selection.root_id.bytes())
.build(),
nostrdb::Filter::new()
.ids([selection.root_id.bytes()])
.limit(1)
.build(),
]),
TimelineKind::Profile(pk) => FilterState::ready(vec![Filter::new()
.authors([pk.bytes()])
.kinds([1])
.limit(default_limit())
.build()]),
}
}
pub fn into_timeline(self, txn: &Transaction, ndb: &Ndb) -> Option<Timeline> {
match self {
TimelineKind::Universe => Some(Timeline::new(
TimelineKind::Universe,
FilterState::ready(vec![Filter::new()
.kinds([1])
.limit(default_limit())
.build()]),
FilterState::ready(universe_filter()),
TimelineTab::no_replies(),
)),
TimelineKind::Thread(root_id) => Some(Timeline::thread(root_id)),
TimelineKind::Generic => {
TimelineKind::Generic(_filter_id) => {
warn!("you can't convert a TimelineKind::Generic to a Timeline");
// TODO: you actually can! just need to look up the filter id
None
}
TimelineKind::Algo(AlgoTimeline::LastPerPubkey(ListKind::Contact(pk_src))) => {
let pk = match &pk_src {
PubkeySource::DeckAuthor => default_user?,
PubkeySource::Explicit(pk) => pk.bytes(),
};
TimelineKind::Algo(AlgoTimeline::LastPerPubkey(ListKind::Contact(pk))) => {
let contact_filter = Filter::new()
.authors([pk.bytes()])
.kinds([3])
.limit(1)
.build();
let contact_filter = Filter::new().authors([pk]).kinds([3]).limit(1).build();
let txn = Transaction::new(ndb).expect("txn");
let results = ndb
.query(&txn, &[contact_filter.clone()], 1)
.query(txn, &[contact_filter.clone()], 1)
.expect("contact query failed?");
let kind_fn = TimelineKind::last_per_pubkey;
@@ -375,13 +498,13 @@ impl TimelineKind {
if results.is_empty() {
return Some(Timeline::new(
kind_fn(ListKind::contact_list(pk_src)),
kind_fn(ListKind::contact_list(pk)),
FilterState::needs_remote(vec![contact_filter.clone()]),
tabs,
));
}
let list_kind = ListKind::contact_list(pk_src);
let list_kind = ListKind::contact_list(pk);
match Timeline::last_per_pubkey(&results[0].note, &list_kind) {
Err(Error::App(notedeck::Error::Filter(FilterError::EmptyContactList))) => {
@@ -399,39 +522,29 @@ impl TimelineKind {
}
}
TimelineKind::Profile(pk_src) => {
let pk = match &pk_src {
PubkeySource::DeckAuthor => default_user?,
PubkeySource::Explicit(pk) => pk.bytes(),
};
TimelineKind::Profile(pk) => {
let filter = Filter::new()
.authors([pk])
.authors([pk.bytes()])
.kinds([1])
.limit(default_limit())
.build();
Some(Timeline::new(
TimelineKind::profile(pk_src),
TimelineKind::profile(pk),
FilterState::ready(vec![filter]),
TimelineTab::full_tabs(),
))
}
TimelineKind::Notifications(pk_src) => {
let pk = match &pk_src {
PubkeySource::DeckAuthor => default_user?,
PubkeySource::Explicit(pk) => pk.bytes(),
};
TimelineKind::Notifications(pk) => {
let notifications_filter = Filter::new()
.pubkeys([pk])
.pubkeys([pk.bytes()])
.kinds([1])
.limit(default_limit())
.build();
Some(Timeline::new(
TimelineKind::notifications(pk_src),
TimelineKind::notifications(pk),
FilterState::ready(vec![notifications_filter]),
TimelineTab::only_notes_and_replies(),
))
@@ -439,42 +552,11 @@ impl TimelineKind {
TimelineKind::Hashtag(hashtag) => Some(Timeline::hashtag(hashtag)),
TimelineKind::List(ListKind::Contact(pk_src)) => {
let pk = match &pk_src {
PubkeySource::DeckAuthor => default_user?,
PubkeySource::Explicit(pk) => pk.bytes(),
};
let contact_filter = Filter::new().authors([pk]).kinds([3]).limit(1).build();
let txn = Transaction::new(ndb).expect("txn");
let results = ndb
.query(&txn, &[contact_filter.clone()], 1)
.expect("contact query failed?");
if results.is_empty() {
return Some(Timeline::new(
TimelineKind::contact_list(pk_src),
FilterState::needs_remote(vec![contact_filter.clone()]),
TimelineTab::full_tabs(),
));
}
match Timeline::contact_list(&results[0].note, pk_src.clone(), default_user) {
Err(Error::App(notedeck::Error::Filter(FilterError::EmptyContactList))) => {
Some(Timeline::new(
TimelineKind::contact_list(pk_src),
FilterState::needs_remote(vec![contact_filter]),
TimelineTab::full_tabs(),
))
}
Err(e) => {
error!("Unexpected error: {e}");
None
}
Ok(tl) => Some(tl),
}
}
TimelineKind::List(ListKind::Contact(pk)) => Some(Timeline::new(
TimelineKind::contact_list(pk),
contact_filter_state(txn, ndb, &pk),
TimelineTab::full_tabs(),
)),
}
}
@@ -490,7 +572,7 @@ impl TimelineKind {
TimelineKind::Profile(_pubkey_source) => ColumnTitle::needs_db(self),
TimelineKind::Thread(_root_id) => ColumnTitle::simple("Thread"),
TimelineKind::Universe => ColumnTitle::simple("Universe"),
TimelineKind::Generic => ColumnTitle::simple("Custom"),
TimelineKind::Generic(_) => ColumnTitle::simple("Custom"),
TimelineKind::Hashtag(hashtag) => ColumnTitle::formatted(hashtag.to_string()),
}
}
@@ -506,26 +588,15 @@ impl<'a> TitleNeedsDb<'a> {
TitleNeedsDb { kind }
}
pub fn title<'txn>(
&self,
txn: &'txn Transaction,
ndb: &Ndb,
deck_author: Option<&Pubkey>,
) -> &'txn str {
if let TimelineKind::Profile(pubkey_source) = self.kind {
if let Some(deck_author) = deck_author {
let pubkey = pubkey_source.to_pubkey(deck_author);
let profile = ndb.get_profile_by_pubkey(txn, pubkey);
let m_name = profile
.as_ref()
.ok()
.map(|p| crate::profile::get_display_name(Some(p)).name());
pub fn title<'txn>(&self, txn: &'txn Transaction, ndb: &Ndb) -> &'txn str {
if let TimelineKind::Profile(pubkey) = self.kind {
let profile = ndb.get_profile_by_pubkey(txn, pubkey);
let m_name = profile
.as_ref()
.ok()
.map(|p| crate::profile::get_display_name(Some(p)).name());
m_name.unwrap_or("Profile")
} else {
// why would be there be no deck author? weird
"nostrich"
}
m_name.unwrap_or("Profile")
} else {
"Unknown"
}
@@ -553,3 +624,65 @@ impl<'a> ColumnTitle<'a> {
Self::NeedsDb(TitleNeedsDb::new(kind))
}
}
fn contact_filter_state(txn: &Transaction, ndb: &Ndb, pk: &Pubkey) -> FilterState {
let contact_filter = Filter::new()
.authors([pk.bytes()])
.kinds([3])
.limit(1)
.build();
let results = ndb
.query(txn, &[contact_filter.clone()], 1)
.expect("contact query failed?");
if results.is_empty() {
FilterState::needs_remote(vec![contact_filter.clone()])
} else {
let with_hashtags = false;
match filter::filter_from_tags(&results[0].note, Some(pk.bytes()), with_hashtags) {
Err(notedeck::Error::Filter(FilterError::EmptyContactList)) => {
FilterState::needs_remote(vec![contact_filter])
}
Err(err) => {
error!("Error getting contact filter state: {err}");
FilterState::Broken(FilterError::EmptyContactList)
}
Ok(filter) => FilterState::ready(filter.into_follow_filter()),
}
}
}
fn last_per_pubkey_filter_state(ndb: &Ndb, pk: &Pubkey) -> FilterState {
let contact_filter = Filter::new()
.authors([pk.bytes()])
.kinds([3])
.limit(1)
.build();
let txn = Transaction::new(ndb).expect("txn");
let results = ndb
.query(&txn, &[contact_filter.clone()], 1)
.expect("contact query failed?");
if results.is_empty() {
FilterState::needs_remote(vec![contact_filter])
} else {
let kind = 1;
let notes_per_pk = 1;
match filter::last_n_per_pubkey_from_tags(&results[0].note, kind, notes_per_pk) {
Err(notedeck::Error::Filter(FilterError::EmptyContactList)) => {
FilterState::needs_remote(vec![contact_filter])
}
Err(err) => {
error!("Error getting contact filter state: {err}");
FilterState::Broken(FilterError::EmptyContactList)
}
Ok(filter) => FilterState::ready(filter),
}
}
}
fn universe_filter() -> Vec<Filter> {
vec![Filter::new().kinds([1]).limit(default_limit()).build()]
}

View File

@@ -1,26 +1,19 @@
use crate::{
column::Columns,
decks::DecksCache,
error::Error,
multi_subscriber::MultiSubscriber,
subscriptions::{self, SubKind, Subscriptions},
thread::Thread,
timeline::kind::ListKind,
Result,
};
use notedeck::{
filter, CachedNote, FilterError, FilterState, FilterStates, NoteCache, NoteRef, RootNoteIdBuf,
UnknownIds,
filter, CachedNote, FilterError, FilterState, FilterStates, NoteCache, NoteRef, UnknownIds,
};
use std::fmt;
use std::sync::atomic::{AtomicU32, Ordering};
use egui_virtual_list::VirtualList;
use enostr::{PoolRelay, Pubkey, RelayPool};
use nostrdb::{Filter, Ndb, Note, NoteKey, Subscription, Transaction};
use nostrdb::{Filter, Ndb, Note, NoteKey, Transaction};
use std::cell::RefCell;
use std::hash::Hash;
use std::rc::Rc;
use tracing::{debug, error, info, warn};
@@ -29,17 +22,26 @@ pub mod cache;
pub mod kind;
pub mod route;
pub use cache::{TimelineCache, TimelineCacheKey};
pub use kind::{ColumnTitle, PubkeySource, TimelineKind};
pub use route::TimelineRoute;
pub use cache::TimelineCache;
pub use kind::{ColumnTitle, PubkeySource, ThreadSelection, TimelineKind};
#[derive(Debug, Hash, Copy, Clone, Eq, PartialEq)]
pub struct TimelineId(u32);
//#[derive(Debug, Hash, Clone, Eq, PartialEq)]
//pub type TimelineId = TimelineKind;
/*
impl TimelineId {
pub fn new(id: u32) -> Self {
pub fn kind(&self) -> &TimelineKind {
&self.kind
}
pub fn new(id: TimelineKind) -> Self {
TimelineId(id)
}
pub fn profile(pubkey: Pubkey) -> Self {
TimelineId::new(TimelineKind::Profile(PubkeySource::pubkey(pubkey)))
}
}
impl fmt::Display for TimelineId {
@@ -47,6 +49,7 @@ impl fmt::Display for TimelineId {
write!(f, "TimelineId({})", self.0)
}
}
*/
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub enum ViewFilter {
@@ -186,7 +189,6 @@ impl TimelineTab {
/// A column in a deck. Holds navigation state, loaded notes, column kind, etc.
#[derive(Debug)]
pub struct Timeline {
pub id: TimelineId,
pub kind: TimelineKind,
// We may not have the filter loaded yet, so let's make it an option so
// that codepaths have to explicitly handle it
@@ -194,35 +196,36 @@ pub struct Timeline {
pub views: Vec<TimelineTab>,
pub selected_view: usize,
pub subscription: Option<Subscription>,
pub subscription: Option<MultiSubscriber>,
}
impl Timeline {
/// Create a timeline from a contact list
pub fn contact_list(
contact_list: &Note,
pk_src: PubkeySource,
deck_author: Option<&[u8; 32]>,
) -> Result<Self> {
let our_pubkey = deck_author.map(|da| pk_src.to_pubkey_bytes(da));
pub fn contact_list(contact_list: &Note, pubkey: &[u8; 32]) -> Result<Self> {
let with_hashtags = false;
let filter =
filter::filter_from_tags(contact_list, our_pubkey, with_hashtags)?.into_follow_filter();
let filter = filter::filter_from_tags(contact_list, Some(pubkey), with_hashtags)?
.into_follow_filter();
Ok(Timeline::new(
TimelineKind::contact_list(pk_src),
TimelineKind::contact_list(Pubkey::new(*pubkey)),
FilterState::ready(filter),
TimelineTab::full_tabs(),
))
}
pub fn thread(note_id: RootNoteIdBuf) -> Self {
let filter = Thread::filters_raw(note_id.borrow())
.iter_mut()
.map(|fb| fb.build())
.collect();
pub fn thread(selection: ThreadSelection) -> Self {
let filter = vec![
nostrdb::Filter::new()
.kinds([1])
.event(selection.root_id.bytes())
.build(),
nostrdb::Filter::new()
.ids([selection.root_id.bytes()])
.limit(1)
.build(),
];
Timeline::new(
TimelineKind::Thread(note_id),
TimelineKind::Thread(selection),
FilterState::ready(filter),
TimelineTab::only_notes_and_replies(),
)
@@ -234,7 +237,7 @@ impl Timeline {
let filter = filter::last_n_per_pubkey_from_tags(list, kind, notes_per_pk)?;
Ok(Timeline::new(
TimelineKind::last_per_pubkey(list_kind.clone()),
TimelineKind::last_per_pubkey(*list_kind),
FilterState::ready(filter),
TimelineTab::only_notes_and_replies(),
))
@@ -254,25 +257,20 @@ impl Timeline {
)
}
pub fn make_view_id(id: TimelineId, selected_view: usize) -> egui::Id {
pub fn make_view_id(id: &TimelineKind, selected_view: usize) -> egui::Id {
egui::Id::new((id, selected_view))
}
pub fn view_id(&self) -> egui::Id {
Timeline::make_view_id(self.id, self.selected_view)
Timeline::make_view_id(&self.kind, self.selected_view)
}
pub fn new(kind: TimelineKind, filter_state: FilterState, views: Vec<TimelineTab>) -> Self {
// global unique id for all new timelines
static UIDS: AtomicU32 = AtomicU32::new(0);
let filter = FilterStates::new(filter_state);
let subscription: Option<Subscription> = None;
let subscription: Option<MultiSubscriber> = None;
let selected_view = 0;
let id = TimelineId::new(UIDS.fetch_add(1, Ordering::Relaxed));
Timeline {
id,
kind,
filter,
views,
@@ -417,6 +415,8 @@ impl Timeline {
let sub = self
.subscription
.as_ref()
.and_then(|s| s.local_subid)
.ok_or(Error::App(notedeck::Error::no_active_sub()))?;
let new_note_ids = ndb.poll_for_notes(sub, 500);
@@ -484,10 +484,9 @@ pub fn setup_new_timeline(
pool: &mut RelayPool,
note_cache: &mut NoteCache,
since_optimize: bool,
our_pk: Option<&Pubkey>,
) {
// if we're ready, setup local subs
if is_timeline_ready(ndb, pool, note_cache, timeline, our_pk) {
if is_timeline_ready(ndb, pool, note_cache, timeline) {
if let Err(err) = setup_timeline_nostrdb_sub(ndb, note_cache, timeline) {
error!("setup_new_timeline: {err}");
}
@@ -505,7 +504,7 @@ pub fn setup_new_timeline(
pub fn send_initial_timeline_filters(
ndb: &Ndb,
since_optimize: bool,
columns: &mut Columns,
timeline_cache: &mut TimelineCache,
subs: &mut Subscriptions,
pool: &mut RelayPool,
relay_id: &str,
@@ -513,7 +512,7 @@ pub fn send_initial_timeline_filters(
info!("Sending initial filters to {}", relay_id);
let relay = &mut pool.relays.iter_mut().find(|r| r.url() == relay_id)?;
for timeline in columns.timelines_mut() {
for (_kind, timeline) in timeline_cache.timelines.iter_mut() {
send_initial_timeline_filter(ndb, since_optimize, subs, relay, timeline);
}
@@ -527,7 +526,7 @@ pub fn send_initial_timeline_filter(
relay: &mut PoolRelay,
timeline: &mut Timeline,
) {
let filter_state = timeline.filter.get(relay.url());
let filter_state = timeline.filter.get_mut(relay.url());
match filter_state {
FilterState::Broken(err) => {
@@ -567,7 +566,7 @@ pub fn send_initial_timeline_filter(
if can_since_optimize && filter::should_since_optimize(lim, notes.len()) {
filter = filter::since_optimize_filter(filter, notes);
} else {
warn!("Skipping since optimization for {:?}: number of local notes is less than limit, attempting to backfill.", filter);
warn!("Skipping since optimization for {:?}: number of local notes is less than limit, attempting to backfill.", &timeline.kind);
}
filter
@@ -596,7 +595,7 @@ fn fetch_contact_list(
relay: &mut PoolRelay,
timeline: &mut Timeline,
) {
let sub_kind = SubKind::FetchingContactList(timeline.id);
let sub_kind = SubKind::FetchingContactList(timeline.kind.clone());
let sub_id = subscriptions::new_sub_id();
let local_sub = ndb.subscribe(&filter).expect("sub");
@@ -621,9 +620,21 @@ fn setup_initial_timeline(
) -> Result<()> {
// some timelines are one-shot and a refreshed, like last_per_pubkey algo feed
if timeline.kind.should_subscribe_locally() {
timeline.subscription = Some(ndb.subscribe(filters)?);
let local_sub = ndb.subscribe(filters)?;
match &mut timeline.subscription {
None => {
timeline.subscription = Some(MultiSubscriber::with_initial_local_sub(
local_sub,
filters.to_vec(),
));
}
Some(msub) => {
msub.local_subid = Some(local_sub);
}
};
}
let txn = Transaction::new(ndb)?;
debug!(
"querying nostrdb sub {:?} {:?}",
timeline.subscription, timeline.filter
@@ -634,6 +645,7 @@ fn setup_initial_timeline(
lim += filter.limit().unwrap_or(1) as i32;
}
let txn = Transaction::new(ndb)?;
let notes: Vec<NoteRef> = ndb
.query(&txn, filters, lim)?
.into_iter()
@@ -648,15 +660,11 @@ fn setup_initial_timeline(
pub fn setup_initial_nostrdb_subs(
ndb: &Ndb,
note_cache: &mut NoteCache,
decks_cache: &mut DecksCache,
timeline_cache: &mut TimelineCache,
) -> Result<()> {
for decks in decks_cache.get_all_decks_mut() {
for deck in decks.decks_mut() {
for timeline in deck.columns_mut().timelines_mut() {
if let Err(err) = setup_timeline_nostrdb_sub(ndb, note_cache, timeline) {
error!("setup_initial_nostrdb_subs: {err}");
}
}
for (_kind, timeline) in timeline_cache.timelines.iter_mut() {
if let Err(err) = setup_timeline_nostrdb_sub(ndb, note_cache, timeline) {
error!("setup_initial_nostrdb_subs: {err}");
}
}
@@ -688,7 +696,6 @@ pub fn is_timeline_ready(
pool: &mut RelayPool,
note_cache: &mut NoteCache,
timeline: &mut Timeline,
our_pk: Option<&Pubkey>,
) -> bool {
// TODO: we should debounce the filter states a bit to make sure we have
// seen all of the different contact lists from each relay
@@ -721,11 +728,7 @@ pub fn is_timeline_ready(
let filter = {
let txn = Transaction::new(ndb).expect("txn");
let note = ndb.get_note_by_key(&txn, note_key).expect("note");
let add_pk = timeline
.kind
.pubkey_source()
.as_ref()
.and_then(|pk_src| our_pk.map(|pk| pk_src.to_pubkey_bytes(pk)));
let add_pk = timeline.kind.pubkey().map(|pk| pk.bytes());
filter::filter_from_tags(&note, add_pk, with_hashtags).map(|f| f.into_follow_filter())
};

View File

@@ -1,124 +1,44 @@
use crate::{
column::Columns,
draft::Drafts,
nav::RenderNavAction,
profile::ProfileAction,
timeline::{TimelineCache, TimelineId, TimelineKind},
ui::{
self,
note::{NoteOptions, QuoteRepostView},
profile::ProfileView,
},
timeline::{TimelineCache, TimelineKind},
ui::{self, note::NoteOptions, profile::ProfileView},
};
use tokenator::{ParseError, TokenParser, TokenSerializable, TokenWriter};
use enostr::{NoteId, Pubkey};
use nostrdb::{Ndb, Transaction};
use enostr::Pubkey;
use nostrdb::Ndb;
use notedeck::{Accounts, ImageCache, MuteFun, NoteCache, UnknownIds};
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum TimelineRoute {
Timeline(TimelineId),
Thread(NoteId),
Profile(Pubkey),
Reply(NoteId),
Quote(NoteId),
}
fn parse_pubkey<'a>(parser: &mut TokenParser<'a>) -> Result<Pubkey, ParseError<'a>> {
let hex = parser.pull_token()?;
Pubkey::from_hex(hex).map_err(|_| ParseError::HexDecodeFailed)
}
fn parse_note_id<'a>(parser: &mut TokenParser<'a>) -> Result<NoteId, ParseError<'a>> {
let hex = parser.pull_token()?;
NoteId::from_hex(hex).map_err(|_| ParseError::HexDecodeFailed)
}
impl TokenSerializable for TimelineRoute {
fn serialize_tokens(&self, writer: &mut TokenWriter) {
match self {
TimelineRoute::Profile(pk) => {
writer.write_token("profile");
writer.write_token(&pk.hex());
}
TimelineRoute::Thread(note_id) => {
writer.write_token("thread");
writer.write_token(&note_id.hex());
}
TimelineRoute::Reply(note_id) => {
writer.write_token("reply");
writer.write_token(&note_id.hex());
}
TimelineRoute::Quote(note_id) => {
writer.write_token("quote");
writer.write_token(&note_id.hex());
}
TimelineRoute::Timeline(_tlid) => {
todo!("tlid")
}
}
}
fn parse_from_tokens<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
TokenParser::alt(
parser,
&[
|p| {
p.parse_token("profile")?;
Ok(TimelineRoute::Profile(parse_pubkey(p)?))
},
|p| {
p.parse_token("thread")?;
Ok(TimelineRoute::Thread(parse_note_id(p)?))
},
|p| {
p.parse_token("reply")?;
Ok(TimelineRoute::Reply(parse_note_id(p)?))
},
|p| {
p.parse_token("quote")?;
Ok(TimelineRoute::Quote(parse_note_id(p)?))
},
|_p| todo!("handle timeline parsing"),
],
)
}
}
#[allow(clippy::too_many_arguments)]
pub fn render_timeline_route(
ndb: &Ndb,
columns: &mut Columns,
drafts: &mut Drafts,
img_cache: &mut ImageCache,
unknown_ids: &mut UnknownIds,
note_cache: &mut NoteCache,
timeline_cache: &mut TimelineCache,
accounts: &mut Accounts,
route: TimelineRoute,
kind: &TimelineKind,
col: usize,
textmode: bool,
depth: usize,
ui: &mut egui::Ui,
) -> Option<RenderNavAction> {
match route {
TimelineRoute::Timeline(timeline_id) => {
let note_options = {
let is_universe = if let Some(timeline) = columns.find_timeline(timeline_id) {
timeline.kind == TimelineKind::Universe
} else {
false
};
let mut options = NoteOptions::new(is_universe);
options.set_textmode(textmode);
options
};
let note_options = {
let mut options = NoteOptions::new(kind == &TimelineKind::Universe);
options.set_textmode(textmode);
options
};
match kind {
TimelineKind::List(_)
| TimelineKind::Algo(_)
| TimelineKind::Notifications(_)
| TimelineKind::Universe
| TimelineKind::Hashtag(_)
| TimelineKind::Generic(_) => {
let note_action = ui::TimelineView::new(
timeline_id,
columns,
kind,
timeline_cache,
ndb,
note_cache,
img_cache,
@@ -130,89 +50,50 @@ pub fn render_timeline_route(
note_action.map(RenderNavAction::NoteAction)
}
TimelineRoute::Thread(id) => ui::ThreadView::new(
TimelineKind::Profile(pubkey) => {
if depth > 1 {
render_profile_route(
pubkey,
accounts,
ndb,
timeline_cache,
img_cache,
note_cache,
unknown_ids,
col,
ui,
&accounts.mutefun(),
)
} else {
// we render profiles like timelines if they are at the root
let note_action = ui::TimelineView::new(
kind,
timeline_cache,
ndb,
note_cache,
img_cache,
note_options,
&accounts.mutefun(),
)
.ui(ui);
note_action.map(RenderNavAction::NoteAction)
}
}
TimelineKind::Thread(id) => ui::ThreadView::new(
timeline_cache,
ndb,
note_cache,
unknown_ids,
img_cache,
id.bytes(),
id.selected_or_root(),
textmode,
&accounts.mutefun(),
)
.id_source(egui::Id::new(("threadscroll", col)))
.ui(ui)
.map(Into::into),
TimelineRoute::Reply(id) => {
let txn = if let Ok(txn) = Transaction::new(ndb) {
txn
} else {
ui.label("Reply to unknown note");
return None;
};
let note = if let Ok(note) = ndb.get_note_by_id(&txn, id.bytes()) {
note
} else {
ui.label("Reply to unknown note");
return None;
};
let id = egui::Id::new(("post", col, note.key().unwrap()));
let poster = accounts.selected_or_first_nsec()?;
let action = {
let draft = drafts.reply_mut(note.id());
let response = egui::ScrollArea::vertical().show(ui, |ui| {
ui::PostReplyView::new(ndb, poster, draft, note_cache, img_cache, &note)
.id_source(id)
.show(ui)
});
response.inner.action
};
action.map(Into::into)
}
TimelineRoute::Profile(pubkey) => render_profile_route(
&pubkey,
accounts,
ndb,
timeline_cache,
img_cache,
note_cache,
unknown_ids,
col,
ui,
&accounts.mutefun(),
),
TimelineRoute::Quote(id) => {
let txn = Transaction::new(ndb).expect("txn");
let note = if let Ok(note) = ndb.get_note_by_id(&txn, id.bytes()) {
note
} else {
ui.label("Quote of unknown note");
return None;
};
let id = egui::Id::new(("post", col, note.key().unwrap()));
let poster = accounts.selected_or_first_nsec()?;
let draft = drafts.quote_mut(note.id());
let response = egui::ScrollArea::vertical().show(ui, |ui| {
QuoteRepostView::new(ndb, poster, note_cache, img_cache, draft, &note)
.id_source(id)
.show(ui)
});
response.inner.action.map(Into::into)
}
}
}
@@ -262,22 +143,26 @@ mod tests {
use enostr::NoteId;
use tokenator::{TokenParser, TokenSerializable, TokenWriter};
use crate::timeline::{ThreadSelection, TimelineKind};
use enostr::Pubkey;
use notedeck::RootNoteIdBuf;
#[test]
fn test_timeline_route_serialize() {
use super::TimelineRoute;
use super::TimelineKind;
{
let note_id_hex = "1c54e5b0c386425f7e017d9e068ddef8962eb2ce1bb08ed27e24b93411c12e60";
let note_id = NoteId::from_hex(note_id_hex).unwrap();
let data_str = format!("thread:{}", note_id_hex);
let data = &data_str.split(":").collect::<Vec<&str>>();
let mut token_writer = TokenWriter::default();
let mut parser = TokenParser::new(&data);
let parsed = TimelineRoute::parse_from_tokens(&mut parser).unwrap();
let expected = TimelineRoute::Thread(note_id);
parsed.serialize_tokens(&mut token_writer);
assert_eq!(expected, parsed);
assert_eq!(token_writer.str(), data_str);
}
let note_id_hex = "1c54e5b0c386425f7e017d9e068ddef8962eb2ce1bb08ed27e24b93411c12e60";
let note_id = NoteId::from_hex(note_id_hex).unwrap();
let data_str = format!("thread:{}", note_id_hex);
let data = &data_str.split(":").collect::<Vec<&str>>();
let mut token_writer = TokenWriter::default();
let mut parser = TokenParser::new(&data);
let parsed = TimelineKind::parse(&mut parser, &Pubkey::new(*note_id.bytes())).unwrap();
let expected = TimelineKind::Thread(ThreadSelection::from_root_id(
RootNoteIdBuf::new_unsafe(*note_id.bytes()),
));
parsed.serialize_tokens(&mut token_writer);
assert_eq!(expected, parsed);
assert_eq!(token_writer.str(), data_str);
}
}