forked from matrix-org/matrix-rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
731 additions
and
238 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{ | ||
fs, | ||
future::{Future, IntoFuture}, | ||
path::Path, | ||
pin::Pin, | ||
}; | ||
|
||
use eyeball::{shared::Observable as SharedObservable, Subscriber}; | ||
use matrix_sdk::{attachment::AttachmentConfig, room::Room, TransmissionProgress}; | ||
use mime::Mime; | ||
|
||
use super::{Error, Timeline}; | ||
|
||
pub struct SendAttachment<'a> { | ||
timeline: &'a Timeline, | ||
url: String, | ||
mime_type: Mime, | ||
config: AttachmentConfig, | ||
pub(crate) send_progress: SharedObservable<TransmissionProgress>, | ||
} | ||
|
||
impl<'a> SendAttachment<'a> { | ||
pub(crate) fn new( | ||
timeline: &'a Timeline, | ||
url: String, | ||
mime_type: Mime, | ||
config: AttachmentConfig, | ||
) -> Self { | ||
Self { timeline, url, mime_type, config, send_progress: Default::default() } | ||
} | ||
|
||
/// Get a subscriber to observe the progress of sending the request | ||
/// body. | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub fn subscribe_to_send_progress(&self) -> Subscriber<TransmissionProgress> { | ||
self.send_progress.subscribe() | ||
} | ||
} | ||
|
||
impl<'a> IntoFuture for SendAttachment<'a> { | ||
type Output = Result<(), Error>; | ||
#[cfg(target_arch = "wasm32")] | ||
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>; | ||
|
||
fn into_future(self) -> Self::IntoFuture { | ||
let Self { timeline, url, mime_type, config, send_progress } = self; | ||
Box::pin(async move { | ||
let Room::Joined(room) = Room::from(timeline.room().clone()) else { | ||
return Err(Error::RoomNotJoined); | ||
}; | ||
|
||
let body = Path::new(&url) | ||
.file_name() | ||
.ok_or(Error::InvalidAttachmentFileName)? | ||
.to_str() | ||
.expect("path was created from UTF-8 string, hence filename part is UTF-8 too"); | ||
let data = fs::read(&url).map_err(|_| Error::InvalidAttachmentData)?; | ||
|
||
room.send_attachment(body, &mime_type, data, config) | ||
.with_send_progress_observable(send_progress) | ||
.await | ||
.map_err(|_| Error::FailedSendingAttachment)?; | ||
|
||
Ok(()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::{ | ||
fmt::Debug, | ||
future::{Future, IntoFuture}, | ||
pin::Pin, | ||
}; | ||
|
||
use cfg_vis::cfg_vis; | ||
use eyeball::shared::Observable as SharedObservable; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
use eyeball::Subscriber; | ||
use ruma::api::{client::error::ErrorKind, error::FromHttpResponseError, OutgoingRequest}; | ||
|
||
use super::super::Client; | ||
use crate::{ | ||
config::RequestConfig, | ||
error::{HttpError, HttpResult}, | ||
RefreshTokenError, TransmissionProgress, | ||
}; | ||
|
||
/// `IntoFuture` returned by [`Client::send`]. | ||
#[allow(missing_debug_implementations)] | ||
pub struct SendRequest<R> { | ||
pub(crate) client: Client, | ||
pub(crate) request: R, | ||
pub(crate) config: Option<RequestConfig>, | ||
pub(crate) send_progress: SharedObservable<TransmissionProgress>, | ||
} | ||
|
||
impl<R> SendRequest<R> { | ||
/// Replace the default `SharedObservable` used for tracking upload | ||
/// progress. | ||
/// | ||
/// Note that any subscribers obtained from | ||
/// [`subscribe_to_send_progress`][Self::subscribe_to_send_progress] | ||
/// will be invalidated by this. | ||
#[cfg_vis(target_arch = "wasm32", pub(crate))] | ||
pub fn with_send_progress_observable( | ||
mut self, | ||
send_progress: SharedObservable<TransmissionProgress>, | ||
) -> Self { | ||
self.send_progress = send_progress; | ||
self | ||
} | ||
|
||
/// Get a subscriber to observe the progress of sending the request | ||
/// body. | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub fn subscribe_to_send_progress(&self) -> Subscriber<TransmissionProgress> { | ||
self.send_progress.subscribe() | ||
} | ||
} | ||
|
||
impl<R> IntoFuture for SendRequest<R> | ||
where | ||
R: OutgoingRequest + Clone + Debug + Send + Sync + 'static, | ||
R::IncomingResponse: Send + Sync, | ||
HttpError: From<FromHttpResponseError<R::EndpointError>>, | ||
{ | ||
type Output = HttpResult<R::IncomingResponse>; | ||
#[cfg(target_arch = "wasm32")] | ||
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>; | ||
|
||
fn into_future(self) -> Self::IntoFuture { | ||
let Self { client, request, config, send_progress } = self; | ||
Box::pin(async move { | ||
let res = | ||
Box::pin(client.send_inner(request.clone(), config, None, send_progress.clone())) | ||
.await; | ||
|
||
// If this is an `M_UNKNOWN_TOKEN` error and refresh token handling is active, | ||
// try to refresh the token and retry the request. | ||
if client.inner.handle_refresh_tokens { | ||
if let Err(Some(ErrorKind::UnknownToken { .. })) = | ||
res.as_ref().map_err(HttpError::client_api_error_kind) | ||
{ | ||
if let Err(refresh_error) = client.refresh_access_token().await { | ||
match &refresh_error { | ||
HttpError::RefreshToken(RefreshTokenError::RefreshTokenRequired) => { | ||
// Refreshing access tokens is not supported | ||
// by | ||
// this `Session`, ignore. | ||
} | ||
_ => { | ||
return Err(refresh_error); | ||
} | ||
} | ||
} else { | ||
return Box::pin(client.send_inner(request, config, None, send_progress)) | ||
.await; | ||
} | ||
} | ||
} | ||
|
||
res | ||
}) | ||
} | ||
} |
Oops, something went wrong.