Skip to content

Commit

Permalink
Use Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jan 13, 2024
1 parent 61ed045 commit 274fd1a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ impl App {
action_tx.send(Action::SystemMessage(format!("[Reposted] {note1}")))?;
}
Action::SendTextNote(ref content, ref tags) => {
let event = EventBuilder::new_text_note(content, tags.iter().cloned()).to_event(&keys)?;
let event = EventBuilder::new_text_note(content, tags.iter().cloned())
.to_event(&keys)?;
log::info!("Send text note: {event:?}");
event_tx.send(event)?;
action_tx.send(Action::SystemMessage(format!("[Posted] {content}")))?;
Expand Down
32 changes: 14 additions & 18 deletions src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ use std::collections::HashSet;
use std::collections::{hash_map::Entry, HashMap};

use color_eyre::eyre::Result;
use nostr_sdk::prelude::{Metadata as NostrMetadata, *};
use nostr_sdk::prelude::*;
use ratatui::{prelude::*, widgets, widgets::*};
use sorted_vec::ReverseSortedSet;
use tokio::sync::mpsc::UnboundedSender;
use tui_textarea::TextArea;
use tui_widget_list::List;

use super::{Component, Frame};
use crate::text::shorten_hex;
use crate::{
action::Action,
config::Config,
nostr::{nip10::ReplyTagsBuilder, Metadata, SortableEvent},
nostr::{nip10::ReplyTagsBuilder, Metadata, Profile, SortableEvent},
widgets::ScrollableList,
widgets::TextNote,
};
Expand All @@ -25,7 +26,7 @@ pub struct Home<'a> {
config: Config,
list_state: tui_widget_list::ListState,
notes: ReverseSortedSet<SortableEvent>,
profiles: HashMap<XOnlyPublicKey, NostrMetadata>,
profiles: HashMap<XOnlyPublicKey, Profile>,
reactions: HashMap<EventId, HashSet<Event>>,
reposts: HashMap<EventId, HashSet<Event>>,
zap_receipts: HashMap<EventId, HashSet<Event>>,
Expand Down Expand Up @@ -58,8 +59,14 @@ impl<'a> Home<'a> {

fn add_profile(&mut self, event: Event) {
if let Ok(metadata) = Metadata::from_json(event.content.clone()) {
self.profiles
.insert(event.pubkey, NostrMetadata::from(metadata));
let profile = Profile::new(event.pubkey, event.created_at, metadata);
if let Some(existing_profile) = self.profiles.get(&event.pubkey) {
if existing_profile.created_at > profile.created_at {
return;
}
}

self.profiles.insert(event.pubkey, profile);
}
}

Expand Down Expand Up @@ -264,20 +271,9 @@ impl<'a> Component for Home<'a> {

let block = if let Some(ref reply_to) = self.reply_to {
let name = if let Some(profile) = self.profiles.get(&reply_to.pubkey) {
match (
profile.display_name.clone(),
profile.name.clone(),
reply_to.pubkey.to_bech32(),
) {
(Some(display_name), _, _) if !display_name.is_empty() => display_name,
(_, Some(name), _) if !name.is_empty() => format!("@{name}"),
(_, _, Ok(npub)) => npub,
_ => reply_to.pubkey.to_string(),
}
} else if let Ok(npub) = reply_to.pubkey.to_bech32() {
npub.to_string()
profile.name()
} else {
reply_to.pubkey.to_string()
shorten_hex(&reply_to.pubkey.to_string())
};

widgets::Block::default()
Expand Down
31 changes: 17 additions & 14 deletions src/components/status_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,40 @@ use ratatui::{prelude::*, widgets::*};
use crate::action::Action;
use crate::components::Component;
use crate::nostr::Metadata;
use crate::nostr::Profile;
use crate::tui::Frame;
use crate::widgets::PublicKey;

pub struct StatusBar {
pubkey: XOnlyPublicKey,
metadata: Option<Metadata>,
profile: Option<Profile>,
message: Option<String>,
is_loading: bool,
}

impl StatusBar {
pub fn new(
pubkey: XOnlyPublicKey,
metadata: Option<Metadata>,
profile: Option<Profile>,
message: Option<String>,
is_loading: bool,
) -> Self {
Self {
pubkey,
metadata,
profile,
message,
is_loading,
}
}

pub fn set_metadata(&mut self, metadata: Option<Metadata>) {
self.metadata = metadata;
pub fn set_profile(&mut self, profile: Option<Profile>) {
self.profile = profile;
}

pub fn name(&self) -> String {
self.metadata
self.profile
.clone()
.and_then(|metadata| match (metadata.name, metadata.display_name) {
(Some(name), _) if !name.is_empty() => Some(format!("@{name}")),
(_, Some(display_name)) if !display_name.is_empty() => Some(display_name),
(_, _) => None,
})
.map(|profile| profile.name())
.unwrap_or(PublicKey::new(self.pubkey).shortened())
}
}
Expand All @@ -54,9 +51,15 @@ impl Component for StatusBar {

match ev.kind {
Kind::Metadata if ev.pubkey == self.pubkey => {
let maybe_metadata = Metadata::from_json(ev.content);
if let Ok(metadata) = maybe_metadata {
self.set_metadata(Some(metadata));
if let Ok(metadata) = Metadata::from_json(ev.content.clone()) {
let profile = Profile::new(ev.pubkey, ev.created_at, metadata);
if let Some(existing_profile) = &self.profile {
if existing_profile.created_at > profile.created_at {
// TODO
}
}

self.set_profile(Some(profile));
}
}
_ => {}
Expand Down
1 change: 1 addition & 0 deletions src/nostr/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use nostr_sdk::prelude::*;
use crate::nostr::Metadata;
use crate::text::shorten_hex;

#[derive(Clone, Debug)]
pub struct Profile {
pub pubkey: XOnlyPublicKey,
pub created_at: Timestamp,
Expand Down
42 changes: 34 additions & 8 deletions src/widgets/text_note.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::collections::HashSet;

use chrono::{DateTime, Local};
use nostr_sdk::{Event, Metadata, Tag, ToBech32};
use nostr_sdk::prelude::*;
use ratatui::{prelude::*, widgets::*};
use thousands::Separable;
use tui_widget_list::Listable;

use crate::nostr::Profile;
use crate::widgets::{PublicKey, ShrinkText};

#[derive(Clone, Debug)]
pub struct TextNote {
pub event: Event,
pub profile: Option<Metadata>,
pub profile: Option<Profile>,
pub reactions: HashSet<Event>,
pub reposts: HashSet<Event>,
pub zap_receipts: HashSet<Event>,
Expand All @@ -24,7 +25,7 @@ pub struct TextNote {
impl TextNote {
pub fn new(
event: Event,
profile: Option<Metadata>,
profile: Option<Profile>,
reactions: HashSet<Event>,
reposts: HashSet<Event>,
zap_receipts: HashSet<Event>,
Expand All @@ -46,7 +47,7 @@ impl TextNote {

pub fn display_name(&self) -> Option<String> {
if let Some(profile) = self.profile.clone() {
if let Some(display_name) = profile.display_name {
if let Some(display_name) = profile.metadata.display_name {
if !display_name.is_empty() {
return Some(display_name);
}
Expand All @@ -58,7 +59,7 @@ impl TextNote {

pub fn name(&self) -> Option<String> {
if let Some(profile) = self.profile.clone() {
if let Some(name) = profile.name {
if let Some(name) = profile.metadata.name {
if !name.is_empty() {
match self.display_name() {
Some(display_name) if name == display_name => return None,
Expand Down Expand Up @@ -247,11 +248,14 @@ impl Listable for TextNote {

#[cfg(test)]
mod tests {
use nostr_sdk::JsonUtil;
use std::str::FromStr;

use nostr_sdk::{secp256k1::XOnlyPublicKey, JsonUtil};
use pretty_assertions::assert_eq;
use rstest::*;

use super::*;
use crate::nostr::Metadata;

#[fixture]
fn event() -> Event {
Expand Down Expand Up @@ -293,9 +297,20 @@ mod tests {
area: Rect,
padding: Padding,
) {
let profile = metadata.map(|metadata| {
Profile::new(
XOnlyPublicKey::from_str(
"4d39c23b3b03bf99494df5f3a149c7908ae1bc7416807fdd6b34a31886eaae25",
)
.unwrap(),
Timestamp::now(),
metadata,
)
});

let note = TextNote::new(
event,
metadata,
profile,
HashSet::new(),
HashSet::new(),
HashSet::new(),
Expand All @@ -319,9 +334,20 @@ mod tests {
area: Rect,
padding: Padding,
) {
let profile = metadata.map(|metadata| {
Profile::new(
XOnlyPublicKey::from_str(
"4d39c23b3b03bf99494df5f3a149c7908ae1bc7416807fdd6b34a31886eaae25",
)
.unwrap(),
Timestamp::now(),
metadata,
)
});

let note = TextNote::new(
event,
metadata,
profile,
HashSet::new(),
HashSet::new(),
HashSet::new(),
Expand Down

0 comments on commit 274fd1a

Please sign in to comment.