Email/copy fixes.
parent
603395566d
commit
867a19d211
|
@ -2,6 +2,8 @@ use std::collections::HashMap;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
use super::{
|
||||
set::{SetError, SetObject},
|
||||
RequestParams,
|
||||
|
@ -83,12 +85,10 @@ impl<T: SetObject> CopyRequest<T> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn create(&mut self) -> &mut T {
|
||||
let create_id = self.create.len();
|
||||
let create_id_str = format!("c{}", create_id);
|
||||
self.create
|
||||
.insert(create_id_str.clone(), T::new(create_id.into()));
|
||||
self.create.get_mut(&create_id_str).unwrap()
|
||||
pub fn create(&mut self, id: impl Into<String>) -> &mut T {
|
||||
let id = id.into();
|
||||
self.create.insert(id.clone(), T::new(None));
|
||||
self.create.get_mut(&id).unwrap()
|
||||
}
|
||||
|
||||
pub fn on_success_destroy_original(&mut self, on_success_destroy_original: bool) -> &mut Self {
|
||||
|
@ -122,13 +122,21 @@ impl<O: SetObject> CopyResponse<O> {
|
|||
&self.new_state
|
||||
}
|
||||
|
||||
pub fn created(&self, id: &str) -> Option<&O> {
|
||||
self.created.as_ref().and_then(|created| created.get(id))
|
||||
pub fn created(&mut self, id: &str) -> crate::Result<O> {
|
||||
if let Some(result) = self.created.as_mut().and_then(|r| r.remove(id)) {
|
||||
Ok(result)
|
||||
} else if let Some(error) = self.not_created.as_mut().and_then(|r| r.remove(id)) {
|
||||
Err(error.to_string_error().into())
|
||||
} else {
|
||||
Err(Error::Internal(format!("Id {} not found.", id)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn not_created(&self, id: &str) -> Option<&SetError<O::Property>> {
|
||||
self.not_created
|
||||
.as_ref()
|
||||
.and_then(|not_created| not_created.get(id))
|
||||
pub fn created_ids(&self) -> Option<impl Iterator<Item = &String>> {
|
||||
self.created.as_ref().map(|map| map.keys())
|
||||
}
|
||||
|
||||
pub fn not_created_ids(&self) -> Option<impl Iterator<Item = &String>> {
|
||||
self.not_created.as_ref().map(|map| map.keys())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,11 @@ impl<T> Response<T> {
|
|||
self.method_responses
|
||||
}
|
||||
|
||||
pub fn unwrap_method_response(mut self) -> T {
|
||||
pub fn method_response_by_pos(&mut self, index: usize) -> T {
|
||||
self.method_responses.remove(index)
|
||||
}
|
||||
|
||||
pub fn pop_method_response(&mut self) -> T {
|
||||
self.method_responses.pop().unwrap()
|
||||
}
|
||||
|
||||
|
@ -74,7 +78,7 @@ impl<T> Response<T> {
|
|||
}
|
||||
|
||||
impl Response<TaggedMethodResponse> {
|
||||
pub fn method_response(&self, id: &str) -> Option<&TaggedMethodResponse> {
|
||||
pub fn method_response_by_id(&self, id: &str) -> Option<&TaggedMethodResponse> {
|
||||
self.method_responses
|
||||
.iter()
|
||||
.find(|response| response.call_id() == id)
|
||||
|
@ -90,6 +94,7 @@ pub enum SingleMethodResponse<T> {
|
|||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub enum Error {
|
||||
#[serde(rename = "error")]
|
||||
Error,
|
||||
}
|
||||
|
||||
|
|
|
@ -50,8 +50,8 @@ impl Email<Get> {
|
|||
self.size.unwrap()
|
||||
}
|
||||
|
||||
pub fn received_at(&self) -> i64 {
|
||||
self.received_at.as_ref().unwrap().timestamp()
|
||||
pub fn received_at(&self) -> Option<i64> {
|
||||
self.received_at.as_ref().map(|r| r.timestamp())
|
||||
}
|
||||
|
||||
pub fn message_id(&self) -> Option<&[String]> {
|
||||
|
@ -126,6 +126,10 @@ impl Email<Get> {
|
|||
self.headers.contains_key(id)
|
||||
}
|
||||
|
||||
pub fn preview(&self) -> Option<&str> {
|
||||
self.preview.as_deref()
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
pub fn into_test(self) -> super::TestEmail {
|
||||
self.into()
|
||||
|
|
|
@ -184,6 +184,41 @@ impl Client {
|
|||
.await
|
||||
.and_then(|mut r| r.parsed(blob_id))
|
||||
}
|
||||
|
||||
pub async fn email_copy<T, U, V, W>(
|
||||
&mut self,
|
||||
from_account_id: impl Into<String>,
|
||||
id: impl Into<String>,
|
||||
mailbox_ids: T,
|
||||
keywords: Option<V>,
|
||||
received_at: Option<i64>,
|
||||
) -> crate::Result<Email>
|
||||
where
|
||||
T: IntoIterator<Item = U>,
|
||||
U: Into<String>,
|
||||
V: IntoIterator<Item = W>,
|
||||
W: Into<String>,
|
||||
{
|
||||
let id = id.into();
|
||||
let mut request = self.build();
|
||||
let email = request
|
||||
.copy_email(from_account_id)
|
||||
.create(id.clone())
|
||||
.mailbox_ids(mailbox_ids);
|
||||
|
||||
if let Some(keywords) = keywords {
|
||||
email.keywords(keywords);
|
||||
}
|
||||
|
||||
if let Some(received_at) = received_at {
|
||||
email.received_at(received_at);
|
||||
}
|
||||
|
||||
request
|
||||
.send_single::<EmailCopyResponse>()
|
||||
.await?
|
||||
.created(&id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Request<'_> {
|
||||
|
|
|
@ -175,6 +175,11 @@ impl Email<Set> {
|
|||
self.headers.insert(header, Some(value.into()));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn received_at(&mut self, received_at: i64) -> &mut Self {
|
||||
self.received_at = Some(from_timestamp(received_at));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl SetObject for Email<Set> {
|
||||
|
|
Loading…
Reference in New Issue