* clean up * wip * clean up * remove unused picture field
This commit is contained in:
@@ -44,8 +44,8 @@ pub struct Registry {
|
||||
/// Status of the unwrapping process
|
||||
pub unwrapping_status: Entity<UnwrappingStatus>,
|
||||
|
||||
/// Public Key of the current user
|
||||
pub identity: Option<PublicKey>,
|
||||
/// Public key of the currently activated signer
|
||||
signer_pubkey: Option<PublicKey>,
|
||||
|
||||
/// Tasks for asynchronous operations
|
||||
_tasks: SmallVec<[Task<()>; 1]>,
|
||||
@@ -106,21 +106,19 @@ impl Registry {
|
||||
unwrapping_status,
|
||||
rooms: vec![],
|
||||
persons: HashMap::new(),
|
||||
identity: None,
|
||||
signer_pubkey: None,
|
||||
_tasks: tasks,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the identity of the user.
|
||||
///
|
||||
/// WARNING: This method will panic if user is not logged in.
|
||||
pub fn identity(&self, cx: &App) -> Profile {
|
||||
self.get_person(&self.identity.unwrap(), cx)
|
||||
/// Returns the public key of the currently activated signer.
|
||||
pub fn signer_pubkey(&self) -> Option<PublicKey> {
|
||||
self.signer_pubkey
|
||||
}
|
||||
|
||||
/// Sets the identity of the user.
|
||||
pub fn set_identity(&mut self, identity: PublicKey, cx: &mut Context<Self>) {
|
||||
self.identity = Some(identity);
|
||||
/// Update the public key of the currently activated signer.
|
||||
pub fn set_signer_pubkey(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
|
||||
self.signer_pubkey = Some(public_key);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
@@ -254,7 +252,7 @@ impl Registry {
|
||||
self.set_unwrapping_status(UnwrappingStatus::default(), cx);
|
||||
|
||||
// Clear the current identity
|
||||
self.identity = None;
|
||||
self.signer_pubkey = None;
|
||||
|
||||
// Clear all current rooms
|
||||
self.rooms.clear();
|
||||
@@ -276,7 +274,7 @@ impl Registry {
|
||||
let contacts = client.database().contacts_public_keys(public_key).await?;
|
||||
|
||||
// Get messages sent by the user
|
||||
let send = Filter::new()
|
||||
let sent = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(public_key);
|
||||
|
||||
@@ -285,9 +283,9 @@ impl Registry {
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
|
||||
let send_events = client.database().query(send).await?;
|
||||
let sent_events = client.database().query(sent).await?;
|
||||
let recv_events = client.database().query(recv).await?;
|
||||
let events = send_events.merge(recv_events);
|
||||
let events = sent_events.merge(recv_events);
|
||||
|
||||
let mut rooms: HashSet<Room> = HashSet::new();
|
||||
|
||||
@@ -297,12 +295,16 @@ impl Registry {
|
||||
.sorted_by_key(|event| Reverse(event.created_at))
|
||||
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
|
||||
{
|
||||
if rooms.iter().any(|room| room.id == event.uniq_id()) {
|
||||
// Parse the room from the nostr event
|
||||
let room = Room::from(&event);
|
||||
|
||||
// Skip if the room is already in the set
|
||||
if rooms.iter().any(|r| r.id == room.id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get all public keys from the event's tags
|
||||
let mut public_keys = event.all_pubkeys();
|
||||
let mut public_keys: Vec<PublicKey> = room.members().to_vec();
|
||||
public_keys.retain(|pk| pk != &public_key);
|
||||
|
||||
// Bypass screening flag
|
||||
@@ -323,9 +325,6 @@ impl Registry {
|
||||
// If current user has sent a message at least once, mark as ongoing
|
||||
let is_ongoing = client.database().count(filter).await.unwrap_or(1) >= 1;
|
||||
|
||||
// Create a new room
|
||||
let room = Room::from(&event).current_user(public_key);
|
||||
|
||||
if is_ongoing || bypassed {
|
||||
rooms.insert(room.kind(RoomKind::Ongoing));
|
||||
} else {
|
||||
@@ -419,7 +418,7 @@ impl Registry {
|
||||
/// Updates room ordering based on the most recent messages.
|
||||
pub fn event_to_message(
|
||||
&mut self,
|
||||
gift_wrap_id: EventId,
|
||||
gift_wrap: EventId,
|
||||
event: Event,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
@@ -427,7 +426,7 @@ impl Registry {
|
||||
let id = event.uniq_id();
|
||||
let author = event.pubkey;
|
||||
|
||||
let Some(identity) = self.identity else {
|
||||
let Some(public_key) = self.signer_pubkey else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -441,13 +440,13 @@ impl Registry {
|
||||
}
|
||||
|
||||
// Set this room is ongoing if the new message is from current user
|
||||
if author == identity {
|
||||
if author == public_key {
|
||||
this.set_ongoing(cx);
|
||||
}
|
||||
|
||||
// Emit the new message to the room
|
||||
cx.defer_in(window, move |this, _window, cx| {
|
||||
this.emit_message(gift_wrap_id, event, cx);
|
||||
this.emit_message(gift_wrap, event, cx);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -458,10 +457,8 @@ impl Registry {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
let room = Room::from(&event).current_user(identity);
|
||||
|
||||
// Push the new room to the front of the list
|
||||
self.add_room(cx.new(|_| room), cx);
|
||||
self.add_room(cx.new(|_| Room::from(&event)), cx);
|
||||
|
||||
// Notify the UI about the new room
|
||||
cx.defer_in(window, move |_this, _window, cx| {
|
||||
|
||||
@@ -88,8 +88,6 @@ pub struct Room {
|
||||
pub created_at: Timestamp,
|
||||
/// Subject of the room
|
||||
pub subject: Option<String>,
|
||||
/// Picture of the room
|
||||
pub picture: Option<String>,
|
||||
/// All members of the room
|
||||
pub members: Vec<PublicKey>,
|
||||
/// Kind
|
||||
@@ -130,32 +128,18 @@ impl From<&Event> for Room {
|
||||
let created_at = val.created_at;
|
||||
|
||||
// Get the members from the event's tags and event's pubkey
|
||||
let members = val
|
||||
.all_pubkeys()
|
||||
.into_iter()
|
||||
.unique()
|
||||
.sorted()
|
||||
.collect_vec();
|
||||
let members = val.all_pubkeys();
|
||||
|
||||
// Get the subject from the event's tags
|
||||
let subject = if let Some(tag) = val.tags.find(TagKind::Subject) {
|
||||
tag.content().map(|s| s.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Get the picture from the event's tags
|
||||
let picture = if let Some(tag) = val.tags.find(TagKind::custom("picture")) {
|
||||
tag.content().map(|s| s.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Get subject from tags
|
||||
let subject = val
|
||||
.tags
|
||||
.find(TagKind::Subject)
|
||||
.and_then(|tag| tag.content().map(|s| s.to_owned()));
|
||||
|
||||
Room {
|
||||
id,
|
||||
created_at,
|
||||
subject,
|
||||
picture,
|
||||
members,
|
||||
kind: RoomKind::default(),
|
||||
}
|
||||
@@ -168,32 +152,18 @@ impl From<&UnsignedEvent> for Room {
|
||||
let created_at = val.created_at;
|
||||
|
||||
// Get the members from the event's tags and event's pubkey
|
||||
let members = val
|
||||
.all_pubkeys()
|
||||
.into_iter()
|
||||
.unique()
|
||||
.sorted()
|
||||
.collect_vec();
|
||||
let members = val.all_pubkeys();
|
||||
|
||||
// Get the subject from the event's tags
|
||||
let subject = if let Some(tag) = val.tags.find(TagKind::Subject) {
|
||||
tag.content().map(|s| s.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Get the picture from the event's tags
|
||||
let picture = if let Some(tag) = val.tags.find(TagKind::custom("picture")) {
|
||||
tag.content().map(|s| s.to_owned())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Get subject from tags
|
||||
let subject = val
|
||||
.tags
|
||||
.find(TagKind::Subject)
|
||||
.and_then(|tag| tag.content().map(|s| s.to_owned()));
|
||||
|
||||
Room {
|
||||
id,
|
||||
created_at,
|
||||
subject,
|
||||
picture,
|
||||
members,
|
||||
kind: RoomKind::default(),
|
||||
}
|
||||
@@ -201,32 +171,39 @@ impl From<&UnsignedEvent> for Room {
|
||||
}
|
||||
|
||||
impl Room {
|
||||
/// Constructs a new room instance with a given receiver.
|
||||
pub fn new(receiver: PublicKey, tags: Tags, cx: &App) -> Self {
|
||||
let identity = Registry::read_global(cx).identity(cx);
|
||||
/// Constructs a new room instance for a private message with the given receiver and tags.
|
||||
pub async fn new(subject: Option<String>, receivers: Vec<PublicKey>) -> Result<Self, Error> {
|
||||
let client = nostr_client();
|
||||
let signer = client.signer().await?;
|
||||
let public_key = signer.get_public_key().await?;
|
||||
|
||||
let mut event = EventBuilder::private_msg_rumor(receiver, "")
|
||||
if receivers.is_empty() {
|
||||
return Err(anyhow!("You need to add at least one receiver"));
|
||||
};
|
||||
|
||||
// Convert receiver's public keys into tags
|
||||
let mut tags: Tags = Tags::from_list(
|
||||
receivers
|
||||
.iter()
|
||||
.map(|pubkey| Tag::public_key(pubkey.to_owned()))
|
||||
.collect(),
|
||||
);
|
||||
|
||||
// Add subject if it is present
|
||||
if let Some(subject) = subject {
|
||||
tags.push(Tag::from_standardized_without_cell(TagStandard::Subject(
|
||||
subject,
|
||||
)));
|
||||
}
|
||||
|
||||
let mut event = EventBuilder::new(Kind::PrivateDirectMessage, "")
|
||||
.tags(tags)
|
||||
.build(identity.public_key());
|
||||
.build(public_key);
|
||||
|
||||
// Ensure event ID is generated
|
||||
// Generate event ID
|
||||
event.ensure_id();
|
||||
|
||||
Room::from(&event).current_user(identity.public_key())
|
||||
}
|
||||
|
||||
/// Constructs a new room instance from an nostr event.
|
||||
pub fn from(event: impl Into<Room>) -> Self {
|
||||
event.into()
|
||||
}
|
||||
|
||||
/// Call this function to ensure the current user is always at the bottom of the members list
|
||||
pub fn current_user(mut self, public_key: PublicKey) -> Self {
|
||||
let (not_match, matches): (Vec<PublicKey>, Vec<PublicKey>) =
|
||||
self.members.iter().partition(|&key| key != &public_key);
|
||||
self.members = not_match;
|
||||
self.members.extend(matches);
|
||||
self
|
||||
Ok(Room::from(&event))
|
||||
}
|
||||
|
||||
/// Sets the kind of the room and returns the modified room
|
||||
@@ -255,13 +232,12 @@ impl Room {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Updates the picture of the room
|
||||
pub fn set_picture(&mut self, picture: String, cx: &mut Context<Self>) {
|
||||
self.picture = Some(picture);
|
||||
cx.notify();
|
||||
/// Returns the members of the room
|
||||
pub fn members(&self) -> &Vec<PublicKey> {
|
||||
&self.members
|
||||
}
|
||||
|
||||
/// Checks if the room is a group chat
|
||||
/// Checks if the room has more than two members (group)
|
||||
pub fn is_group(&self) -> bool {
|
||||
self.members.len() > 2
|
||||
}
|
||||
@@ -277,20 +253,27 @@ impl Room {
|
||||
|
||||
/// Gets the display image for the room
|
||||
pub fn display_image(&self, proxy: bool, cx: &App) -> SharedUri {
|
||||
if let Some(picture) = self.picture.as_ref() {
|
||||
SharedUri::from(picture)
|
||||
} else if !self.is_group() {
|
||||
self.first_member(cx).avatar(proxy)
|
||||
if !self.is_group() {
|
||||
self.display_member(cx).avatar(proxy)
|
||||
} else {
|
||||
SharedUri::from("brand/group.png")
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the first member of the room.
|
||||
/// Get a single member to represent the room
|
||||
///
|
||||
/// First member is always different from the current user.
|
||||
fn first_member(&self, cx: &App) -> Profile {
|
||||
/// This member is always different from the current user.
|
||||
fn display_member(&self, cx: &App) -> Profile {
|
||||
let registry = Registry::read_global(cx);
|
||||
|
||||
if let Some(public_key) = registry.signer_pubkey() {
|
||||
for member in self.members() {
|
||||
if member != &public_key {
|
||||
return registry.get_person(member, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registry.get_person(&self.members[0], cx)
|
||||
}
|
||||
|
||||
@@ -299,11 +282,11 @@ impl Room {
|
||||
let registry = Registry::read_global(cx);
|
||||
|
||||
if self.is_group() {
|
||||
let profiles = self
|
||||
let profiles: Vec<Profile> = self
|
||||
.members
|
||||
.iter()
|
||||
.map(|pk| registry.get_person(pk, cx))
|
||||
.collect::<Vec<_>>();
|
||||
.map(|public_key| registry.get_person(public_key, cx))
|
||||
.collect();
|
||||
|
||||
let mut name = profiles
|
||||
.iter()
|
||||
@@ -318,7 +301,7 @@ impl Room {
|
||||
|
||||
SharedString::from(name)
|
||||
} else {
|
||||
self.first_member(cx).display_name()
|
||||
self.display_member(cx).display_name()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -455,9 +438,8 @@ impl Room {
|
||||
|
||||
/// Create a new message event (unsigned)
|
||||
pub fn create_message(&self, content: &str, replies: &[EventId], cx: &App) -> UnsignedEvent {
|
||||
let public_key = Registry::read_global(cx).identity(cx).public_key();
|
||||
let public_key = Registry::read_global(cx).signer_pubkey().unwrap();
|
||||
let subject = self.subject.clone();
|
||||
let picture = self.picture.clone();
|
||||
|
||||
let mut tags = vec![];
|
||||
|
||||
@@ -470,22 +452,17 @@ impl Room {
|
||||
|
||||
// Add subject tag if it's present
|
||||
if let Some(subject) = subject {
|
||||
tags.push(Tag::from_standardized(TagStandard::Subject(
|
||||
subject.to_string(),
|
||||
tags.push(Tag::from_standardized_without_cell(TagStandard::Subject(
|
||||
subject,
|
||||
)));
|
||||
}
|
||||
|
||||
// Add picture tag if it's present
|
||||
if let Some(picture) = picture {
|
||||
tags.push(Tag::custom(TagKind::custom("picture"), vec![picture]));
|
||||
}
|
||||
|
||||
// Add reply/quote tag
|
||||
if replies.len() == 1 {
|
||||
tags.push(Tag::event(replies[0]))
|
||||
} else {
|
||||
for id in replies {
|
||||
tags.push(Tag::from_standardized(TagStandard::Quote {
|
||||
tags.push(Tag::from_standardized_without_cell(TagStandard::Quote {
|
||||
event_id: id.to_owned(),
|
||||
relay_url: None,
|
||||
public_key: None,
|
||||
@@ -677,7 +654,7 @@ impl Room {
|
||||
client.connect_relay(url).await?;
|
||||
}
|
||||
|
||||
relay_urls.extend(urls);
|
||||
relay_urls.extend(urls.into_iter().take(3).unique());
|
||||
} else {
|
||||
return Err(anyhow!("Not found"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user