column: extract into_timeline logic into ColumnKind
I thought I needed this but maybe I don't. Anyways, it's a bit cleaner this way. Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use crate::{timeline::Timeline, Error};
|
||||
use enostr::Pubkey;
|
||||
use nostrdb::{Filter, Ndb, Transaction};
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -42,4 +44,50 @@ impl ColumnKind {
|
||||
pub fn contact_list(pk: PubkeySource) -> Self {
|
||||
ColumnKind::List(ListKind::Contact(pk))
|
||||
}
|
||||
|
||||
pub fn into_timeline(self, ndb: &Ndb, default_user: Option<&[u8; 32]>) -> Timeline {
|
||||
match self {
|
||||
ColumnKind::Universe => Timeline::new(ColumnKind::Universe, Some(vec![])),
|
||||
|
||||
ColumnKind::Generic => {
|
||||
panic!("you can't convert a ColumnKind::Generic to a Timeline")
|
||||
}
|
||||
|
||||
ColumnKind::List(ListKind::Contact(ref pk_src)) => {
|
||||
let pk = match pk_src {
|
||||
PubkeySource::DeckAuthor => {
|
||||
if let Some(user_pk) = default_user {
|
||||
user_pk
|
||||
} else {
|
||||
// No user loaded, so we have to return an unloaded
|
||||
// contact list columns
|
||||
return Timeline::new(
|
||||
ColumnKind::contact_list(PubkeySource::DeckAuthor),
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
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, vec![contact_filter], 1)
|
||||
.expect("contact query failed?");
|
||||
|
||||
if results.is_empty() {
|
||||
return Timeline::new(ColumnKind::contact_list(pk_src.to_owned()), None);
|
||||
}
|
||||
|
||||
match Timeline::contact_list(&results[0].note) {
|
||||
Err(Error::EmptyContactList) => {
|
||||
Timeline::new(ColumnKind::contact_list(pk_src.to_owned()), None)
|
||||
}
|
||||
Err(e) => panic!("Unexpected error: {e}"),
|
||||
Ok(tl) => tl,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user