Skip to content

Commit 416442d

Browse files
committed
Created UpdateItem and added necessary trait implementations
1 parent c080aad commit 416442d

File tree

3 files changed

+273
-5
lines changed

3 files changed

+273
-5
lines changed

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pub mod get_folder;
1414
pub mod get_item;
1515
pub mod sync_folder_hierarchy;
1616
pub mod sync_folder_items;
17+
pub mod update_item;

src/types/common.rs

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct ItemShape {
4949
}
5050

5151
/// An identifier for a property on an Exchange entity.
52-
#[derive(Debug, XmlSerialize)]
52+
#[derive(Debug, Deserialize, XmlSerialize)]
5353
#[xml_struct(variant_ns_prefix = "t")]
5454
pub enum PathToElement {
5555
/// An identifier for an extended MAPI property.
@@ -162,7 +162,7 @@ pub struct ExtendedFieldURI {
162162
/// A well-known MAPI property set identifier.
163163
///
164164
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/extendedfielduri#distinguishedpropertysetid-attribute>
165-
#[derive(Clone, Copy, Debug, XmlSerialize)]
165+
#[derive(Clone, Copy, Debug, Deserialize, XmlSerialize)]
166166
#[xml_struct(text)]
167167
pub enum DistinguishedPropertySet {
168168
Address,
@@ -177,10 +177,32 @@ pub enum DistinguishedPropertySet {
177177
UnifiedMessaging,
178178
}
179179

180+
/// Identifies the type of conflict resolution to try during an update. The default value is AutoResolve.
181+
///
182+
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem#conflictresolution-attribute>
183+
#[derive(Clone, Copy, Debug, Deserialize, XmlSerialize)]
184+
#[xml_struct(text)]
185+
pub enum ConflictResolution {
186+
NeverOverwrite,
187+
AutoResolve,
188+
AlwaysOverwrite,
189+
}
190+
191+
/// The action an Exchange server will take upon creating or updating a `Message` item.
192+
///
193+
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem#messagedisposition-attribute>
194+
#[derive(Clone, Copy, Debug, Deserialize, XmlSerialize)]
195+
#[xml_struct(text)]
196+
pub enum MessageDisposition {
197+
SaveOnly,
198+
SendOnly,
199+
SendAndSaveCopy,
200+
}
201+
180202
/// The type of the value of a MAPI property.
181203
///
182204
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/extendedfielduri#propertytype-attribute>
183-
#[derive(Clone, Copy, Debug, XmlSerialize)]
205+
#[derive(Clone, Copy, Debug, Deserialize, XmlSerialize)]
184206
#[xml_struct(text)]
185207
pub enum PropertyType {
186208
ApplicationTime,
@@ -297,7 +319,7 @@ pub struct FolderId {
297319
///
298320
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemids>
299321
// N.B.: Commented-out variants are not yet implemented.
300-
#[derive(Debug, XmlSerialize)]
322+
#[derive(Debug, Deserialize, XmlSerialize)]
301323
#[xml_struct(variant_ns_prefix = "t")]
302324
pub enum BaseItemId {
303325
/// An identifier for a standard Exchange item.
@@ -458,7 +480,7 @@ impl XmlSerialize for DateTime {
458480
/// An email message.
459481
///
460482
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/message-ex15websvcsotherref>
461-
#[derive(Debug, Deserialize)]
483+
#[derive(Debug, Deserialize, XmlSerialize)]
462484
#[serde(rename_all = "PascalCase")]
463485
pub struct Message {
464486
/// The MIME content of the item.
@@ -528,6 +550,65 @@ pub struct Message {
528550
pub conversation_id: Option<ItemId>,
529551
}
530552

553+
554+
// Add this Default implementation
555+
impl Default for Message {
556+
fn default() -> Self {
557+
Self {
558+
is_read: None,
559+
mime_content: None,
560+
item_id: ItemId {
561+
id: "".to_string(),
562+
change_key: None,
563+
},
564+
parent_folder_id: None,
565+
item_class: None,
566+
subject: None,
567+
sensitivity: None,
568+
body: None,
569+
attachments: None,
570+
date_time_received: None,
571+
size: None,
572+
categories: None,
573+
importance: None,
574+
in_reply_to: None,
575+
is_submitted: None,
576+
is_draft: None,
577+
is_from_me: None,
578+
is_resend: None,
579+
is_unmodified: None,
580+
internet_message_headers: None,
581+
date_time_sent: None,
582+
date_time_created: None,
583+
reminder_due_by: None,
584+
reminder_is_set: None,
585+
reminder_minutes_before_start: None,
586+
display_cc: None,
587+
display_to: None,
588+
has_attachments: None,
589+
culture: None,
590+
sender: None,
591+
to_recipients: None,
592+
cc_recipients: None,
593+
bcc_recipients: None,
594+
is_read_receipt_requested: None,
595+
is_delivery_receipt_requested: None,
596+
conversation_index: None,
597+
conversation_topic: None,
598+
from: None,
599+
internet_message_id: None,
600+
is_response_requested: None,
601+
reply_to: None,
602+
received_by: None,
603+
received_representing: None,
604+
last_modified_name: None,
605+
last_modified_time: None,
606+
is_associated: None,
607+
conversation_id: None,
608+
}
609+
}
610+
}
611+
531612
/// A list of attachments.
532613
///
533614
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/attachments-ex15websvcsotherref>
@@ -917,6 +998,43 @@ pub struct MessageXml {
917998
pub back_off_milliseconds: Option<usize>,
918999
}
9191000

1001+
/// Represents a change to an individual item, including the item ID and updates.
1002+
#[derive(Debug, Deserialize, XmlSerialize)]
1003+
pub struct ItemChange {
1004+
#[serde(rename = "t:ItemId")]
1005+
pub item_id: BaseItemId, // Represents the <t:ItemId> element with Id and ChangeKey.
1006+
1007+
#[serde(rename = "t:Updates")]
1008+
pub updates: Updates, // Represents the <t:Updates> element containing the changes.
1009+
}
1010+
1011+
/// Represents a list of item changes without an explicit container tag.
1012+
#[derive(Debug, Deserialize, XmlSerialize)]
1013+
pub struct ItemChanges {
1014+
#[serde(rename = "t:ItemChange")]
1015+
pub item_changes: Vec<ItemChange>,
1016+
}
1017+
1018+
/// Struct representing the field update operation.
1019+
///
1020+
/// This struct contains details of the field that needs to be updated (in this case, marking a message as read/unread).
1021+
#[derive(Debug, Deserialize, XmlSerialize)]
1022+
pub struct SetItemField {
1023+
#[serde(rename = "t:FieldURI")]
1024+
pub field_uri: PathToElement, // Reference to the field being updated.
1025+
#[serde(rename = "t:Message")]
1026+
pub message: Message, // The new value for the specified field.
1027+
}
1028+
1029+
/// Struct representing updates to be applied to an item.
1030+
///
1031+
/// This struct is used to create an UpdateItem request to mark emails as read or unread.
1032+
#[derive(Debug, Deserialize, XmlSerialize)]
1033+
pub struct Updates {
1034+
#[serde(rename = "t:SetItemField")]
1035+
pub set_item_field: SetItemField,
1036+
}
1037+
9201038
#[cfg(test)]
9211039
mod tests {
9221040
use quick_xml::Writer;

src/types/update_item.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
use crate::types::common::{BaseItemId, Updates, ItemChange, ItemChanges, ConflictResolution, MessageDisposition, PathToElement, Importance};
2+
use crate::{
3+
types::sealed::EnvelopeBodyContents, Items, Operation,
4+
OperationResponse, ResponseClass, ResponseCode,
5+
};
6+
use serde::Deserialize;
7+
use xml_struct::XmlSerialize;
8+
9+
/// Represents the UpdateItem operation for interacting with the EWS server.
10+
#[derive(Debug, Deserialize, XmlSerialize)]
11+
#[serde(rename_all = "PascalCase")]
12+
pub struct UpdateItem {
13+
/// Describes how the item will be handled after it is updated.
14+
/// The MessageDisposition attribute is required for message items, including meeting
15+
/// messages such as meeting cancellations, meeting requests, and meeting responses.
16+
///
17+
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemshape>
18+
pub message_disposition: MessageDisposition,
19+
20+
/// Identifies the type of conflict resolution to try during an update.
21+
/// The default value is AutoResolve.
22+
///
23+
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem#conflictresolution-attribute>
24+
pub conflict_resolution: ConflictResolution,
25+
26+
/// Contains an array of ItemChange elements that identify items and
27+
/// the updates to apply to the items.
28+
///
29+
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemchanges>
30+
pub item_changes: ItemChanges, // Represents the list of item changes to be included in the request.
31+
}
32+
33+
impl UpdateItem {
34+
/// Creates a new UpdateItem instance for marking a message as read/unread.
35+
pub fn new_is_read(item_id: BaseItemId, is_read: bool) -> Self {
36+
let item_change = ItemChange {
37+
item_id,
38+
updates: Updates::new_is_read(is_read),
39+
};
40+
41+
UpdateItem {
42+
message_disposition: MessageDisposition::SaveOnly,
43+
conflict_resolution: ConflictResolution::AutoResolve,
44+
item_changes: ItemChanges { item_changes: vec![item_change] },
45+
}
46+
}
47+
48+
/// Creates a new UpdateItem instance for changing the importance of a message.
49+
pub fn new_importance(item_id: BaseItemId, importance: Importance) -> Self {
50+
let item_change = ItemChange {
51+
item_id,
52+
updates: Updates::new_importance(importance),
53+
};
54+
55+
UpdateItem {
56+
message_disposition: MessageDisposition::SaveOnly,
57+
conflict_resolution: ConflictResolution::AutoResolve,
58+
item_changes: ItemChanges { item_changes: vec![item_change] },
59+
}
60+
}
61+
62+
/// Adds another `ItemChange` to the `UpdateItem` request.
63+
pub fn add_item_change(&mut self, item_change: ItemChange) {
64+
self.item_changes.item_changes.push(item_change);
65+
}
66+
}
67+
68+
impl Updates {
69+
/// Creates a new `Updates` instance for marking a message as read/unread.
70+
pub fn new_is_read(is_read: bool) -> Self {
71+
Updates {
72+
set_item_field: crate::types::common::SetItemField {
73+
field_uri: PathToElement::FieldURI {
74+
field_URI: "message:IsRead".to_string(),
75+
},
76+
message: crate::types::common::Message {
77+
is_read: Some(is_read),
78+
..Default::default()
79+
},
80+
},
81+
}
82+
}
83+
84+
/// Creates a new `Updates` instance for changing the importance of a message.
85+
pub fn new_importance(importance: Importance) -> Self {
86+
Updates {
87+
set_item_field: crate::types::common::SetItemField {
88+
field_uri: PathToElement::FieldURI {
89+
field_URI: "message:Importance".to_string(),
90+
},
91+
message: crate::types::common::Message {
92+
importance: Some(importance),
93+
..Default::default()
94+
},
95+
},
96+
}
97+
}
98+
}
99+
100+
101+
102+
impl Operation for UpdateItem {
103+
type Response = UpdateItemResponse; // Define the appropriate response type if needed.
104+
}
105+
106+
impl EnvelopeBodyContents for UpdateItem {
107+
fn name() -> &'static str {
108+
"UpdateItem"
109+
}
110+
}
111+
112+
/// A response to an [`UpdateItem`] request.
113+
///
114+
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem>
115+
#[derive(Debug, Deserialize)]
116+
#[serde(rename_all = "PascalCase")]
117+
pub struct UpdateItemResponse {
118+
pub response_messages: ResponseMessages,
119+
}
120+
121+
122+
impl OperationResponse for UpdateItemResponse {}
123+
124+
impl EnvelopeBodyContents for UpdateItemResponse {
125+
fn name() -> &'static str {
126+
"UpdateItemResponse"
127+
}
128+
}
129+
130+
#[derive(Debug, Deserialize)]
131+
#[serde(rename_all = "PascalCase")]
132+
pub struct ResponseMessages {
133+
pub update_item_response_message: Vec<UpdateItemResponseMessage>,
134+
}
135+
136+
#[derive(Debug, Deserialize)]
137+
#[serde(rename_all = "PascalCase")]
138+
pub struct UpdateItemResponseMessage {
139+
/// The status of the corresponding request, i.e. whether it succeeded or
140+
/// resulted in an error.
141+
#[serde(rename = "@ResponseClass")]
142+
pub response_class: ResponseClass,
143+
144+
pub response_code: Option<ResponseCode>,
145+
146+
pub message_text: Option<String>,
147+
148+
pub items: Items,
149+
}

0 commit comments

Comments
 (0)