Skip to content

Commit

Permalink
addressing pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed May 31, 2024
1 parent 3d212c9 commit 4b88fe1
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 114 deletions.
9 changes: 3 additions & 6 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,20 +696,17 @@ impl Room {
/// Stores the given `ComposerDraft` in the state store using the current
/// room id, as identifier.
pub async fn save_composer_draft(&self, draft: ComposerDraft) -> Result<(), ClientError> {
self.inner.save_composer_draft(draft).await?;
Ok(())
Ok(self.inner.save_composer_draft(draft).await?)
}

/// Retrieves the `ComposerDraft` stored in the state store for this room.
pub async fn restore_composer_draft(&self) -> Result<Option<ComposerDraft>, ClientError> {
let draft = self.inner.restore_composer_draft().await?;
Ok(draft)
Ok(self.inner.restore_composer_draft().await?)
}

/// Removes the `ComposerDraft` stored in the state store for this room.
pub async fn clear_composer_draft(&self) -> Result<(), ClientError> {
self.inner.clear_composer_draft().await?;
Ok(())
Ok(self.inner.clear_composer_draft().await?)
}

/// Loads the reply details for the given event id.
Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,13 @@ impl Timeline {
event_id: String,
) -> Result<(), ClientError> {
let event_id = EventId::parse(event_id)?;
let editing_info = self
let edit_info = self
.inner
.get_edit_info_from_event_id(&event_id)
.await
.map_err(|err| anyhow::anyhow!(err))?;
self.inner
.edit((*new_content).clone(), editing_info)
.edit((*new_content).clone(), edit_info)
.await
.map_err(|err| anyhow::anyhow!(err))?;
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-base/src/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::{
#[derive(Debug)]
pub struct MemoryStore {
recently_visited_rooms: StdRwLock<HashMap<String, Vec<String>>>,
composer_drafts: StdRwLock<HashMap<String, ComposerDraft>>,
composer_drafts: StdRwLock<HashMap<OwnedRoomId, ComposerDraft>>,
user_avatar_url: StdRwLock<HashMap<String, String>>,
sync_token: StdRwLock<Option<String>>,
filters: StdRwLock<HashMap<String, String>>,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl StateStore for MemoryStore {
.composer_drafts
.read()
.unwrap()
.get(room_id.as_str())
.get(room_id)
.cloned()
.map(StateStoreDataValue::ComposerDraft),
})
Expand Down Expand Up @@ -230,7 +230,7 @@ impl StateStore for MemoryStore {
}
StateStoreDataKey::ComposerDraft(room_id) => {
self.composer_drafts.write().unwrap().insert(
room_id.to_string(),
room_id.to_owned(),
value.into_composer_draft().expect("Session data not a composer draft"),
);
}
Expand All @@ -252,7 +252,7 @@ impl StateStore for MemoryStore {
self.recently_visited_rooms.write().unwrap().remove(user_id.as_str());
}
StateStoreDataKey::ComposerDraft(room_id) => {
self.composer_drafts.write().unwrap().remove(room_id.as_str());
self.composer_drafts.write().unwrap().remove(room_id);
}
}
Ok(())
Expand Down
15 changes: 9 additions & 6 deletions crates/matrix-sdk-base/src/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,13 +811,13 @@ pub enum StateStoreDataValue {
RecentlyVisitedRooms(Vec<String>),

/// A composer draft for the room.
/// /// To learn more, see [`ComposerDraft`].
/// To learn more, see [`ComposerDraft`].
///
/// [`ComposerDraft`]: self::ComposerDraft
/// [`ComposerDraft`]: Self::ComposerDraft
ComposerDraft(ComposerDraft),
}

/// Struct that represents the current draft of the composer for the room
/// Current draft of the composer for the room.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct ComposerDraft {
Expand All @@ -830,7 +830,7 @@ pub struct ComposerDraft {
draft_type: DraftType,
}

/// Struct that represents the type of draft the composer is composing.
/// The type of draft of the composer.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum DraftType {
Expand Down Expand Up @@ -884,7 +884,10 @@ pub enum StateStoreDataKey<'a> {
/// Recently visited room identifiers
RecentlyVisitedRooms(&'a UserId),

/// Composer draft for the room
/// A composer draft for the room.
/// To learn more, see [`ComposerDraft`].
///
/// [`ComposerDraft`]: Self::ComposerDraft
ComposerDraft(&'a RoomId),
}

Expand All @@ -901,7 +904,7 @@ impl StateStoreDataKey<'_> {
/// [`RecentlyVisitedRooms`][Self::RecentlyVisitedRooms] variant.
pub const RECENTLY_VISITED_ROOMS: &'static str = "recently_visited_rooms";

/// Key prefix to user for the [`ComposerDraft`][Self::ComposerDraft]
/// Key prefix to use for the [`ComposerDraft`][Self::ComposerDraft]
/// variant.
pub const COMPOSER_DRAFT: &'static str = "composer_draft";
}
4 changes: 2 additions & 2 deletions crates/matrix-sdk-sqlite/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ impl SqliteStateStore {
StateStoreDataKey::RecentlyVisitedRooms(b) => {
Cow::Owned(format!("{}:{b}", StateStoreDataKey::RECENTLY_VISITED_ROOMS))
}
StateStoreDataKey::ComposerDraft(r) => {
Cow::Owned(format!("{}:{r}", StateStoreDataKey::COMPOSER_DRAFT))
StateStoreDataKey::ComposerDraft(room_id) => {
Cow::Owned(format!("{}:{room_id}", StateStoreDataKey::COMPOSER_DRAFT))
}
};

Expand Down
10 changes: 5 additions & 5 deletions crates/matrix-sdk-ui/src/timeline/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ enum UnsupportedReplyItemInner {
MissingEvent,
#[error("error deserializing event")]
DeserializationError,
#[error("could not get content")]
#[error("could not get event content")]
MissingContent,
}

Expand All @@ -120,8 +120,8 @@ impl UnsupportedEditItem {
pub(super) const MISSING_EVENT: Self = Self(UnsupportedEditItemInner::MissingEvent);
pub(super) const NOT_ROOM_MESSAGE: Self = Self(UnsupportedEditItemInner::NotRoomMessage);
pub(super) const NOT_POLL_EVENT: Self = Self(UnsupportedEditItemInner::NotPollEvent);
pub(super) const DESERIALIZATION_ERROR: Self =
Self(UnsupportedEditItemInner::DeserializationError);
pub(super) const FAILED_TO_DESERIALIZE_EVENT: Self =
Self(UnsupportedEditItemInner::FailedToDeserializeEvent);
pub(super) const MISSING_CONTENT: Self = Self(UnsupportedEditItemInner::MissingContent);
}

Expand All @@ -143,7 +143,7 @@ enum UnsupportedEditItemInner {
#[error("event could not be obtained")]
MissingEvent,
#[error("error deserializing event")]
DeserializationError,
#[error("could not get content")]
FailedToDeserializeEvent,
#[error("could not get event content")]
MissingContent,
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ impl InReplyToDetails {
InReplyToDetails { event_id, event: TimelineDetails::from_initial_value(event) }
}

/// Create a new `InReplyToDetails` with the given event ID and the event
/// timeline details.
pub fn new_with_timeline_details(
event_id: OwnedEventId,
event: TimelineDetails<Box<RepliedToEvent>>,
Expand Down
39 changes: 39 additions & 0 deletions crates/matrix-sdk-ui/src/timeline/event_item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(super) use self::{
local::LocalEventTimelineItem,
remote::{RemoteEventOrigin, RemoteEventTimelineItem},
};
use super::{EditInfo, RepliedToInfo, ReplyContent, UnsupportedEditItem, UnsupportedReplyItem};

/// An item in the timeline that represents at least one event.
///
Expand Down Expand Up @@ -417,6 +418,44 @@ impl EventTimelineItem {
kind,
}
}

/// Gives the information needed to reply to the event of the item.
pub fn get_replied_to_info(&self) -> Result<RepliedToInfo, UnsupportedReplyItem> {
let reply_content = match self.content() {
TimelineItemContent::Message(msg) => ReplyContent::Message(msg.to_owned()),
_ => {
let Some(raw_event) = self.latest_json() else {
return Err(UnsupportedReplyItem::MISSING_JSON);
};

ReplyContent::Raw(raw_event.clone())
}
};

let Some(event_id) = self.event_id() else {
return Err(UnsupportedReplyItem::MISSING_EVENT_ID);
};

Ok(RepliedToInfo {
event_id: event_id.to_owned(),
sender: self.sender().to_owned(),
timestamp: self.timestamp(),
content: reply_content,
})
}

/// Gives the information needed to edit the event of the item.
pub fn get_edit_info(&self) -> Result<EditInfo, UnsupportedEditItem> {
// Early returns here must be in sync with
// `EventTimelineItem::can_be_edited`
let Some(event_id) = self.event_id() else {
return Err(UnsupportedEditItem::MISSING_EVENT_ID);
};
let TimelineItemContent::Message(original_content) = self.content() else {
return Err(UnsupportedEditItem::NOT_ROOM_MESSAGE);
};
Ok(EditInfo { event_id: event_id.to_owned(), original_message: original_content.clone() })
}
}

impl From<LocalEventTimelineItem> for EventTimelineItemKind {
Expand Down
Loading

0 comments on commit 4b88fe1

Please sign in to comment.