Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
fix: prevent unwrap panic from dioxus-core (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-avb authored Feb 28, 2024
1 parent 6e710f6 commit 5546827
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/components/atoms/messages/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct ThreadPreview {
#[derive(PartialEq, Props, Debug, Clone)]
pub struct Message {
pub id: i64,
pub event_id: Option<String>,
pub event_id: String,
pub content: TimelineMessageType,
pub display_name: String,
pub avatar_uri: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/input_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
message: Message {
id: 1,
display_name: replying.display_name.clone(),
event_id: None,
event_id: String::from(""),
avatar_uri: replying.avatar_uri.clone(),
content: replying.content.clone(),
reply: None,
Expand Down
75 changes: 32 additions & 43 deletions src/components/molecules/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
TimelineRelation::None(message) => {
let message = message.clone();
let event_id = message.event_id.clone();

cx.render(rsx!(
MessageView {
key: "{i}",
key: "{event_id}",
message: Message {
id: i as i64,
event_id: message.event_id,
Expand All @@ -126,17 +125,15 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
info!("TODO: handle download")
}
MenuOption::Reply => {
if let Some(eid) = &event_id {
let replying = ReplyingTo {
event_id: eid.clone(),
content: message.body.clone(),
display_name: message.sender.name.clone(),
avatar_uri: message.sender.avatar_uri.clone(),
origin: message.origin.clone()
};

replying_to.set(Some(replying));
}
let replying = ReplyingTo {
event_id: event_id.clone(),
content: message.body.clone(),
display_name: message.sender.name.clone(),
avatar_uri: message.sender.avatar_uri.clone(),
origin: message.origin.clone()
};

replying_to.set(Some(replying));
}
MenuOption::Close => {
info!("close");
Expand All @@ -147,9 +144,7 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
MenuOption::CreateThread => {
info!("create thread");
let thread = vec![TimelineMessage { event_id: event_id.clone(), sender: message.sender.clone(), body: message.body.clone(), origin: message.origin.clone(), time: message.time.clone() }];
if let Some(e) = &event_id {
threading_to.set(Some(TimelineThread { event_id: e.clone(), thread, count: 0, latest_event: e.clone() }));
}
threading_to.set(Some(TimelineThread { event_id: event_id.clone(), thread, count: 0, latest_event: event_id.clone() }));
}

}
Expand Down Expand Up @@ -178,7 +173,7 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {

cx.render(rsx!(
MessageView {
key: "{i}",
key: "{event_id}",
message: Message {
id: i as i64,
event_id: message.event_id,
Expand All @@ -199,17 +194,15 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
info!("TODO: handle download")
}
MenuOption::Reply => {
if let Some(eid) = &event_id {
let replying = ReplyingTo {
event_id: eid.clone(),
content: message.body.clone(),
display_name: message.sender.name.clone(),
avatar_uri: message.sender.avatar_uri.clone(),
origin: message.origin.clone()
};

replying_to.set(Some(replying));
}
let replying = ReplyingTo {
event_id: event_id.clone(),
content: message.body.clone(),
display_name: message.sender.name.clone(),
avatar_uri: message.sender.avatar_uri.clone(),
origin: message.origin.clone()
};

replying_to.set(Some(replying));
}
MenuOption::Close => {
info!("close");
Expand Down Expand Up @@ -254,10 +247,9 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
thread_avatars.push(Sender{avatar_uri: t.sender.avatar_uri.clone(), display_name: t.sender.name.clone()})
}


cx.render(rsx!(
MessageView {
key: "{i}",
key: "{event_id}",
message: Message {
id: i as i64,
event_id: head_message.event_id.clone(),
Expand Down Expand Up @@ -313,10 +305,9 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
messages.iter().enumerate().map(|(i, m)| {
let message = m.clone();
let event_id = message.event_id.clone();

cx.render(rsx!(
MessageView {
key: "{i}",
key: "{event_id}",
message: Message {
id: i as i64,
event_id: message.event_id,
Expand All @@ -337,17 +328,15 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
info!("TODO: handle download")
}
MenuOption::Reply => {
if let Some(eid) = &event_id {
let replying = ReplyingTo {
event_id: eid.clone(),
content: message.body.clone(),
display_name: message.sender.name.clone(),
avatar_uri: message.sender.avatar_uri.clone(),
origin: message.origin.clone()
};

replying_to.set(Some(replying));
}
let replying = ReplyingTo {
event_id: event_id.clone(),
content: message.body.clone(),
display_name: message.sender.name.clone(),
avatar_uri: message.sender.avatar_uri.clone(),
origin: message.origin.clone()
};

replying_to.set(Some(replying));
}
MenuOption::Close => {
info!("close");
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/factory/message_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl MessageFactory for TextMessageFactory {
) -> TimelineRelation {
TimelineRelation::None(TimelineMessage {
body: content.clone(),
event_id: Some(uuid.to_string()),
event_id: uuid.to_string(),
sender: RoomMember {
id: session.user_id.clone(),
name: String::from("x"),
Expand All @@ -81,7 +81,7 @@ impl MessageFactory for ReplyMessageFactory {
TimelineRelation::Reply(TimelineMessageReply {
event: TimelineMessage {
body: content.clone(),
event_id: Some(uuid.to_string()),
event_id: uuid.to_string(),
sender: RoomMember {
id: session.user_id.clone(),
name: String::from("x"),
Expand All @@ -91,7 +91,7 @@ impl MessageFactory for ReplyMessageFactory {
time: time.to_string(),
},
reply: Some(TimelineMessage {
event_id: Some(self.relation.event_id.clone()),
event_id: self.relation.event_id.clone(),
sender: RoomMember {
id: String::from(""),
name: self.relation.display_name.clone(),
Expand Down Expand Up @@ -121,7 +121,7 @@ impl MessageFactory for CustomThreadMessageFactory {

t.thread.push(TimelineMessage {
body: content.clone(),
event_id: Some(uuid.to_string()),
event_id: uuid.to_string(),
sender: RoomMember {
id: session.user_id.clone(),
name: String::from("x"),
Expand Down
66 changes: 23 additions & 43 deletions src/hooks/use_listen_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,10 @@ pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {
TimelineRelation::CustomThread(TimelineThread {
event_id: timeline_thread.event_id.clone(),
thread: timeline_thread.thread.clone(),
latest_event: match timeline_thread.thread
latest_event: timeline_thread.thread
[timeline_thread.thread.len() - 1]
.clone()
.event_id
{
Some(id) => id,
None => {
notification.handle_error(
&key_common_error_thread_id,
);
return;
}
},
.event_id,
count: timeline_thread.thread.len(),
});

Expand All @@ -123,12 +114,7 @@ pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {
return false;
};

let Some(ref id) = timeline_message.event_id else {
notification.handle_error(&key_common_error_event_id);
return false;
};

t.event_id.eq(id)
t.event_id.eq(&timeline_message.event_id)
});

match position {
Expand Down Expand Up @@ -196,20 +182,18 @@ pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {
}));
}
} else if let TimelineRelation::None(t) = m {
if let Some(event_id) = &t.event_id {
if event_id.eq(&thread.event_id) {
let mut new_thread = thread.clone();

new_thread.thread.push(t.clone());

threading_to.set(Some(TimelineThread {
event_id: event_id.clone(),
thread: new_thread.thread.clone(),
count: new_thread.count.clone(),
latest_event: new_thread.latest_event.clone(),
}));
}
};
if t.event_id.eq(&thread.event_id) {
let mut new_thread = thread.clone();

new_thread.thread.push(t.clone());

threading_to.set(Some(TimelineThread {
event_id: t.event_id.clone(),
thread: new_thread.thread.clone(),
count: new_thread.count.clone(),
latest_event: new_thread.latest_event.clone(),
}));
}
}
});
}
Expand Down Expand Up @@ -320,20 +304,16 @@ pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {

if let Some((uuid, event_id)) = to_find {
position = back_messages.iter().position(|m| match m {
TimelineRelation::None(relation) => {
relation.event_id.as_ref() == Some(&uuid)
}
TimelineRelation::None(relation) => relation.event_id == uuid,
TimelineRelation::Reply(relation) => {
relation.event.event_id.as_ref() == Some(&uuid)
relation.event.event_id == uuid
}
TimelineRelation::CustomThread(relation) => {
relation.thread.iter().any(|rm| rm.event_id == uuid)
}
TimelineRelation::Thread(relation) => {
relation.thread.iter().any(|rm| rm.event_id == uuid)
}
TimelineRelation::CustomThread(relation) => relation
.thread
.iter()
.any(|rm| rm.event_id.as_ref() == Some(&uuid)),
TimelineRelation::Thread(relation) => relation
.thread
.iter()
.any(|rm| rm.event_id.as_ref() == Some(&uuid)),
});

info!("position {:?}", position);
Expand Down
Loading

0 comments on commit 5546827

Please sign in to comment.