Skip to content

Commit

Permalink
Merge pull request #2056 from gtalha07/1924_compose-draft-state
Browse files Browse the repository at this point in the history
compose draft state apis for the room message
  • Loading branch information
gtalha07 authored Aug 14, 2024
2 parents 5ea1758 + 53cdef2 commit 0335321
Show file tree
Hide file tree
Showing 5 changed files with 891 additions and 24 deletions.
30 changes: 29 additions & 1 deletion native/acter/api.rsh
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ object OptionRsvpStatus {
fn status_str() -> Option<string>;
}

object OptionComposeDraft {
/// get compose draft object
fn draft() -> Option<ComposeDraft>;
}

object UserProfile {
/// get user id
fn user_id() -> UserId;
Expand Down Expand Up @@ -330,6 +335,21 @@ object MxcUri {
fn to_string() -> string;
}

object ComposeDraft {
/// plain body text, always available
fn plain_text() -> string;

/// formatted text
fn html_text() -> Option<string>;

/// event id, only valid for edit and reply states
fn event_id() -> Option<string>;

/// compose message state type.
/// One of `new`, `edit`, `reply`.
fn draft_type() -> string;
}

object RoomId {
fn to_string() -> string;
}
Expand Down Expand Up @@ -1098,7 +1118,6 @@ object Room {

/// leave this room
fn leave() -> Future<Result<bool>>;

}


Expand Down Expand Up @@ -1356,6 +1375,15 @@ object Convo {
fn redact_content(event_id: string, reason: Option<string>) -> Future<Result<EventId>>;

fn is_joined() -> bool;

/// compose message state of the room
fn msg_draft() -> Future<Result<OptionComposeDraft>>;

/// save composed message state of the room
fn save_msg_draft(text: string, html: Option<string>, draft_type: string, event_id: Option<string>) -> Future<Result<bool>>;

/// clear composed message state of the room
fn clear_msg_draft() -> Future<Result<bool>>;
}


Expand Down
4 changes: 2 additions & 2 deletions native/acter/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub use comments::{Comment, CommentDraft, CommentsManager};
pub use common::{
duration_from_secs, new_calendar_event_ref_builder, new_colorize_builder, new_link_ref_builder,
new_obj_ref_builder, new_task_list_ref_builder, new_task_ref_builder, new_thumb_size,
DeviceRecord, MediaSource, MsgContent, OptionBuffer, OptionRsvpStatus, OptionString,
ReactionRecord, ThumbnailInfo, ThumbnailSize,
ComposeDraft, DeviceRecord, MediaSource, MsgContent, OptionBuffer, OptionComposeDraft,
OptionRsvpStatus, OptionString, ReactionRecord, ThumbnailInfo, ThumbnailSize,
};
pub use convo::{
new_convo_settings_builder, Convo, ConvoDiff, CreateConvoSettings, CreateConvoSettingsBuilder,
Expand Down
82 changes: 80 additions & 2 deletions native/acter/src/api/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ use acter_core::events::{
};
use anyhow::{Context, Result};
use core::time::Duration;
use matrix_sdk::media::{MediaFormat, MediaThumbnailSettings, MediaThumbnailSize};
use matrix_sdk::{
media::{MediaFormat, MediaThumbnailSettings, MediaThumbnailSize},
ComposerDraft, ComposerDraftType,
};
use ruma::UInt;
use ruma_client_api::media::get_content_thumbnail;
use ruma_common::{EventId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedMxcUri, OwnedUserId};
use ruma_common::{
EventId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedMxcUri, OwnedUserId,
};
use ruma_events::room::{
message::{
AudioInfo, AudioMessageEventContent, EmoteMessageEventContent, FileInfo,
Expand Down Expand Up @@ -73,6 +78,20 @@ impl OptionRsvpStatus {
self.status.as_ref().map(|x| x.to_string())
}
}
#[derive(Clone)]
pub struct OptionComposeDraft {
draft: Option<ComposeDraft>,
}

impl OptionComposeDraft {
pub(crate) fn new(draft: Option<ComposeDraft>) -> Self {
OptionComposeDraft { draft }
}

pub fn draft(&self) -> Option<ComposeDraft> {
self.draft.clone()
}
}

pub struct MediaSource {
inner: SdkMediaSource,
Expand Down Expand Up @@ -468,6 +487,65 @@ impl MsgContent {
}
}

#[derive(Clone)]
pub struct ComposeDraft {
inner: ComposerDraft,
}

impl ComposeDraft {
pub fn new(
plain_text: String,
html_text: Option<String>,
msg_type: String,
event_id: Option<OwnedEventId>,
) -> Self {
let m_type = msg_type.clone();
let draft_type = match (m_type.as_str(), event_id) {
("new", None) => ComposerDraftType::NewMessage,
("edit", Some(id)) => ComposerDraftType::Edit { event_id: id },
("reply", Some(id)) => ComposerDraftType::Reply { event_id: id },
_ => ComposerDraftType::NewMessage,
};

ComposeDraft {
inner: ComposerDraft {
plain_text,
html_text,
draft_type,
},
}
}

pub fn inner(&self) -> ComposerDraft {
self.inner.clone()
}

pub fn plain_text(&self) -> String {
self.inner.plain_text.clone()
}

pub fn html_text(&self) -> Option<String> {
self.inner.html_text.clone()
}

// only valid for reply and edit drafts
pub fn event_id(&self) -> Option<String> {
match &(self.inner.draft_type) {
ComposerDraftType::Edit { event_id } => Some(event_id.to_string()),
ComposerDraftType::Reply { event_id } => Some(event_id.to_string()),
ComposerDraftType::NewMessage => None,
}
}

pub fn draft_type(&self) -> String {
match &(self.inner.draft_type) {
ComposerDraftType::NewMessage => "new".to_string(),
ComposerDraftType::Edit { event_id } => "edit".to_string(),
ComposerDraftType::Reply { event_id } => "reply".to_string(),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ReactionRecord {
sender_id: OwnedUserId,
Expand Down
92 changes: 88 additions & 4 deletions native/acter/src/api/convo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use acter_core::{statics::default_acter_convo_states, Error};
use anyhow::{bail, Context, Result};
use derive_builder::Builder;
use futures::stream::{Stream, StreamExt};
use matrix_sdk::{executor::JoinHandle, RoomMemberships};
use matrix_sdk::{executor::JoinHandle, ComposerDraft, ComposerDraftType, RoomMemberships};
use matrix_sdk_ui::{timeline::RoomExt, Timeline};
use ruma::assign;
use ruma_client_api::room::{create_room, Visibility};
use ruma_common::{
serde::Raw, MxcUri, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomAliasId, RoomId,
RoomOrAliasId, ServerName, UserId,
serde::Raw, MxcUri, OwnedEventId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomAliasId,
RoomId, RoomOrAliasId, ServerName, UserId,
};
use ruma_events::{
receipt::{ReceiptThread, ReceiptType},
Expand All @@ -35,7 +35,7 @@ use super::{
receipt::ReceiptRecord,
room::Room,
utils::{remap_for_diff, ApiVectorDiff},
RUNTIME,
ComposeDraft, OptionComposeDraft, RUNTIME,
};

pub type ConvoDiff = ApiVectorDiff<Convo>;
Expand Down Expand Up @@ -313,6 +313,90 @@ impl Convo {
})
.await?
}

pub async fn msg_draft(&self) -> Result<OptionComposeDraft> {
if !self.is_joined() {
bail!("Unable to fetch composer draft of a room we are not in");
}
let room = self.room.clone();
RUNTIME
.spawn(async move {
let draft = room.load_composer_draft().await?;

Ok(OptionComposeDraft::new(draft.map(|composer_draft| {
let (msg_type, event_id) = match composer_draft.draft_type {
ComposerDraftType::NewMessage => ("new".to_string(), None),
ComposerDraftType::Edit { event_id } => {
("edit".to_string(), Some(event_id))
}
ComposerDraftType::Reply { event_id } => {
("reply".to_string(), Some(event_id))
}
};
ComposeDraft::new(
composer_draft.plain_text,
composer_draft.html_text,
msg_type,
event_id,
)
})))
})
.await?
}

pub async fn save_msg_draft(
&self,
text: String,
html: Option<String>,
draft_type: String,
event_id: Option<String>,
) -> Result<bool> {
if !self.is_joined() {
bail!("Unable to save composer draft of a room we are not in");
}
let room = self.room.clone();

let draft_type = match (draft_type.as_str(), event_id) {
("new", None) => ComposerDraftType::NewMessage,
("edit", Some(id)) => ComposerDraftType::Edit {
event_id: OwnedEventId::try_from(id)?,
},

("reply", Some(id)) => ComposerDraftType::Reply {
event_id: OwnedEventId::try_from(id)?,
},

("reply", _) | ("edit", _) => bail!("Invalid event id"),

(draft_type, _) => bail!("Invalid draft type {draft_type}"),
};

let msg_draft = ComposerDraft {
plain_text: text,
html_text: html,
draft_type,
};

RUNTIME
.spawn(async move {
room.save_composer_draft(msg_draft).await?;
Ok(true)
})
.await?
}

pub async fn clear_msg_draft(&self) -> Result<bool> {
if !self.is_joined() {
bail!("Unable to remove composer draft of a room we are not in");
}
let room = self.room.clone();
RUNTIME
.spawn(async move {
let draft = room.clear_composer_draft();
Ok(true)
})
.await?
}
}

impl Deref for Convo {
Expand Down
Loading

0 comments on commit 0335321

Please sign in to comment.