Skip to content

Commit

Permalink
move compose draft fns over to convo object
Browse files Browse the repository at this point in the history
  • Loading branch information
gtalha07 committed Aug 14, 2024
1 parent c5b7eb0 commit 5eff47e
Show file tree
Hide file tree
Showing 4 changed files with 516 additions and 518 deletions.
19 changes: 9 additions & 10 deletions native/acter/api.rsh
Original file line number Diff line number Diff line change
Expand Up @@ -1118,16 +1118,6 @@ object Room {

/// leave this room
fn leave() -> Future<Result<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 Expand Up @@ -1385,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
101 changes: 97 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,99 @@ 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", id) => {
if let Some(id) = id {
ComposerDraftType::Edit {
event_id: OwnedEventId::try_from(id)?,
}
} else {
bail!("Invalid event id or not found");
}
}

("reply", id) => {
if let Some(id) = id {
ComposerDraftType::Reply {
event_id: OwnedEventId::try_from(id)?,
}
} else {
bail!("Invalid event id or not found");
}
}
_ => bail!("Invalid 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
98 changes: 2 additions & 96 deletions native/acter/src/api/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use matrix_sdk::{
media::{MediaFormat, MediaRequest},
notification_settings::{IsEncrypted, IsOneToOne},
room::{Room as SdkRoom, RoomMember},
ComposerDraft, ComposerDraftType, DisplayName, RoomMemberships, RoomState,
DisplayName, RoomMemberships, RoomState,
};
use ruma::{assign, Int};
use ruma_client_api::{
Expand All @@ -44,7 +44,7 @@ use ruma_events::{
space::{child::HierarchySpaceChildEvent, parent::SpaceParentEventContent},
MessageLikeEventType, StateEvent, StateEventType, StaticEventContent,
};
use std::{io::Write, ops::Deref, path::PathBuf, str::FromStr};
use std::{io::Write, ops::Deref, path::PathBuf};
use tokio_stream::{wrappers::BroadcastStream, StreamExt};
use tracing::{info, warn};

Expand All @@ -54,7 +54,6 @@ use crate::{OptionBuffer, OptionString, RoomMessage, ThumbnailSize, UserProfile,

use super::{
api::FfiBuffer,
common::{ComposeDraft, OptionComposeDraft},
push::{notification_mode_from_input, room_notification_mode_name},
};

Expand Down Expand Up @@ -1107,99 +1106,6 @@ impl Room {
.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", id) => {
if let Some(id) = id {
ComposerDraftType::Edit {
event_id: OwnedEventId::try_from(id)?,
}
} else {
bail!("Invalid event id or not found");
}
}

("reply", id) => {
if let Some(id) = id {
ComposerDraftType::Reply {
event_id: OwnedEventId::try_from(id)?,
}
} else {
bail!("Invalid event id or not found");
}
}
_ => bail!("Invalid 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?
}

pub async fn media_binary(
&self,
event_id: String,
Expand Down
Loading

0 comments on commit 5eff47e

Please sign in to comment.