Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 173 additions & 16 deletions gossip-bin/src/ui/dm_chat_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use gossip_lib::FeedKind;
use gossip_lib::Person;
use gossip_lib::GLOBALS;
use gossip_lib::{PersonTable, Table};
use nostr_types::PublicKey;
use std::time::{Duration, Instant};

pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) {
Expand Down Expand Up @@ -38,8 +39,21 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra

let mut channels = app.dm_channel_cache.clone();

let is_signer_ready = GLOBALS.identity.is_unlocked();

widgets::page_header(ui, "Direct Messages", |ui| {
ui.add_space(16.0);
if is_signer_ready {
if widgets::Button::bordered(&app.theme, "New message")
.small(true)
.show(ui)
.clicked()
{
app.dm_new_message = true;
app.dm_new_message_error = None;
}
}
ui.add_space(8.0);
if widgets::Button::bordered(&app.theme, "Mark all read")
.small(true)
.show(ui)
Expand All @@ -49,7 +63,9 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra
}
});

let is_signer_ready = GLOBALS.identity.is_unlocked();
if app.dm_new_message {
render_new_message_popup(app, ctx);
}

app.vert_scroll_area()
.id_salt("dm_chat_list")
Expand Down Expand Up @@ -173,23 +189,164 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
app.set_page(
ctx,
Page::Feed(FeedKind::DmChat(channeldata.dm_channel.clone())),
);
app.draft_needs_focus = true;
open_dm_channel(app, ctx, channeldata.dm_channel.clone());
}
}
});
}

// Maybe clear the draft, if we are going into a different channel than last
// time
if let Some(oldtarget) = &app.dm_draft_data_target {
if *oldtarget != channeldata.dm_channel {
app.dm_draft_data.clear();
}
} else {
app.dm_draft_data.clear();
}
app.dm_draft_data_target = Some(channeldata.dm_channel.clone());
fn open_dm_channel(app: &mut GossipUi, ctx: &Context, channel: gossip_lib::DmChannel) {
app.set_page(ctx, Page::Feed(FeedKind::DmChat(channel.clone())));
app.draft_needs_focus = true;

// Maybe clear the draft, if we are going into a different channel than last time.
if let Some(oldtarget) = &app.dm_draft_data_target {
if *oldtarget != channel {
app.request_dm_draft_states_save();
app.dm_draft_data.clear();
app.load_dm_draft_state(&channel);
}
} else {
app.dm_draft_data.clear();
app.load_dm_draft_state(&channel);
}
app.dm_draft_data_target = Some(channel);
}

fn render_new_message_popup(app: &mut GossipUi, ctx: &Context) {
const DLG_SIZE: eframe::egui::Vec2 = vec2(420.0, 320.0);

let ret = widgets::modal_popup(ctx, DLG_SIZE, DLG_SIZE, true, |ui| {
ui.vertical(|ui| {
ui.heading("New direct message");
ui.add_space(8.0);

if let Some(err) = &app.dm_new_message_error {
ui.label(RichText::new(err).color(app.theme.warning_marker_text_color()));
ui.add_space(8.0);
}

ui.label("Search for a known contact");
let mut output = widgets::TextEdit::search(
&app.theme,
&app.assets,
&mut app.dm_new_message_search,
)
.desired_width(f32::INFINITY)
.show(ui);

let mut selected = app.dm_new_message_search_selected;
let mut enter_key = false;
if app.dm_new_message_search_results.is_empty() {
selected = None;
} else {
(selected, enter_key) = widgets::capture_keyboard_for_search(
ui,
app.dm_new_message_search_results.len(),
selected,
);
}

if app.dm_new_message_search.len() > 2 {
if Some(&app.dm_new_message_search) != app.dm_new_message_searched.as_ref()
&& output.cursor_range.is_some()
{
let mut pairs = GLOBALS
.people
.search_people_to_tag(app.dm_new_message_search.as_str())
.unwrap_or_default();
pairs.sort_by(|(_, ak), (_, bk)| {
let af = GLOBALS
.db()
.is_person_in_list(ak, gossip_lib::PersonList::Followed)
.unwrap_or(false);
let bf = GLOBALS
.db()
.is_person_in_list(bk, gossip_lib::PersonList::Followed)
.unwrap_or(false);
bf.cmp(&af).then(std::cmp::Ordering::Greater)
});
app.dm_new_message_searched = Some(app.dm_new_message_search.clone());
app.dm_new_message_search_results = pairs.to_owned();
}
} else {
app.dm_new_message_searched = None;
app.dm_new_message_search_results.clear();
}

widgets::show_contact_search(
ui,
app,
egui::AboveOrBelow::Below,
&mut output,
&mut selected,
app.dm_new_message_search_results.clone(),
enter_key,
|_, app, _, pair| {
app.dm_new_message_search = pair.0.clone();
app.dm_new_message_search_results.clear();
app.dm_new_message_search_selected = None;
app.dm_new_message_address = pair.1.as_bech32_string();
},
);
app.dm_new_message_search_selected = selected;

ui.add_space(10.0);
ui.label("Or enter an npub, hex key, or nprofile address");
ui.add(
text_edit_line!(app, app.dm_new_message_address)
.desired_width(f32::INFINITY)
.hint_text("npub1, hex key, or nprofile1"),
);

ui.add_space(12.0);
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
ui.horizontal(|ui| {
if widgets::Button::secondary(&app.theme, "Cancel")
.show(ui)
.clicked()
{
app.clear_new_message_dialog();
}

ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
if widgets::Button::primary(&app.theme, "Start chat")
.show(ui)
.clicked()
{
if let Some(pubkey) = parse_new_message_target(
app.dm_new_message_address.trim(),
) {
open_dm_channel(
app,
ctx,
gossip_lib::DmChannel::new(&[pubkey]),
);
app.clear_new_message_dialog();
} else {
app.dm_new_message_error =
Some("Enter a valid recipient to start a chat.".to_owned());
}
}
});
});
});
});
});

if ret.inner.clicked() {
app.clear_new_message_dialog();
}
}

fn parse_new_message_target(value: &str) -> Option<PublicKey> {
if let Ok(pubkey) = PublicKey::try_from_bech32_string(value, true) {
Some(pubkey)
} else if let Ok(pubkey) = PublicKey::try_from_hex_string(value, true) {
Some(pubkey)
} else if let Ok(profile) = nostr_types::Profile::try_from_bech32_string(value, true) {
Some(profile.pubkey)
} else {
None
}
}
22 changes: 21 additions & 1 deletion gossip-bin/src/ui/feed/note/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ pub struct NoteRenderData {

/// Should hide nameline
pub hide_nameline: bool,

/// Is this note being rendered in the DM feed?
pub is_dm_feed: bool,

/// Was this note authored by us?
pub is_our_event: bool,
}

pub(super) fn render_note(
Expand Down Expand Up @@ -119,6 +125,11 @@ pub(super) fn render_note(
thread_position: indent as i32,
hide_footer: as_reply_to,
hide_nameline: false,
is_dm_feed: matches!(app.page, Page::Feed(FeedKind::DmChat(_))),
is_our_event: GLOBALS
.identity
.public_key()
.is_some_and(|our_pubkey| note_data.event.pubkey == our_pubkey),
};

let top = ui.next_widget_position();
Expand Down Expand Up @@ -231,6 +242,11 @@ pub fn render_dm_note(app: &mut GossipUi, ui: &mut Ui, feed_note_params: FeedNot

if let Some(note_ref) = app.notecache.try_update_and_get(&id) {
if let Ok(note_data) = note_ref.try_borrow() {
let is_our_event = GLOBALS
.identity
.public_key()
.is_some_and(|our_pubkey| note_data.event.pubkey == our_pubkey);

let viewed = GLOBALS
.db()
.is_event_viewed(note_data.event.id)
Expand All @@ -252,6 +268,8 @@ pub fn render_dm_note(app: &mut GossipUi, ui: &mut Ui, feed_note_params: FeedNot
thread_position: indent as i32,
hide_footer: false,
hide_nameline: true,
is_dm_feed: true,
is_our_event,
};

let inner_response =
Expand Down Expand Up @@ -385,7 +403,7 @@ pub fn render_note_inside_framing(
EncryptionType::Nip04 => Some(EncryptionIndicator {
color: app.theme.amber_400(),
tooltip_ui: Box::new(|ui: &mut Ui| {
ui.label("NIP-04 encryption. It is recomended to upgrade [link to help page] to Giftwrap (NIP-44) encryption.");
ui.label("NIP-04 encryption. It is recommended to upgrade [link to help page] to Giftwrap (NIP-44) encryption.");
}),
}),
EncryptionType::Giftwrap => None, // Giftwrap is the new good default, we won't show an indicator
Expand Down Expand Up @@ -1412,6 +1430,8 @@ fn render_repost(
thread_position: 0,
hide_footer: false,
hide_nameline: false,
is_dm_feed: false,
is_our_event: false,
};

let row_height = ui.cursor().height();
Expand Down
Loading