diff --git a/CHANGELOG.md b/CHANGELOG.md index 6933a351..d1bdffbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] +* `Template::Message` accepts `mime_content` (Base64 MIME, with `mime_content_charset`, default `UTF-8`) and an explicit `message_disposition`; the builder now emits `t:MimeContent` for items, and `IncludeMimeContent` is built by `include_mime_content!` * Replace the `logging` gem with stdlib `logger` * `EWSClient.new` and `Viewpoint::EWS::Connection.new` now take an `auth` hash (`endpoint:`, `type:`, `user:`, `password:`, `token:`) instead of positional `endpoint, username, password` arguments; OAuth bearer-token authentication is supported via `type: 'oauth'` with a `token:`, which is sent as an `Authorization: Bearer` header diff --git a/lib/ews/soap/builders/ews_builder.rb b/lib/ews/soap/builders/ews_builder.rb index 547143b2..4f77d0a2 100644 --- a/lib/ews/soap/builders/ews_builder.rb +++ b/lib/ews/soap/builders/ews_builder.rb @@ -131,7 +131,7 @@ def item_shape!(item_shape) @nbuild[NS_EWS_MESSAGES].ItemShape { @nbuild.parent.default_namespace = @default_ns base_shape!(item_shape[:base_shape]) - mime_content!(item_shape[:include_mime_content]) if item_shape.key?(:include_mime_content) + include_mime_content!(item_shape[:include_mime_content]) if item_shape.key?(:include_mime_content) body_type!(item_shape[:body_type]) if item_shape[:body_type] additional_properties!(item_shape[:additional_properties]) if item_shape[:additional_properties] } @@ -152,7 +152,7 @@ def base_shape!(base_shape) @nbuild[NS_EWS_TYPES].BaseShape(camel_case(base_shape)) end - def mime_content!(include_mime_content) + def include_mime_content!(include_mime_content) @nbuild[NS_EWS_TYPES].IncludeMimeContent(include_mime_content.to_s.downcase) end @@ -948,6 +948,13 @@ def body!(body) } end + # @see https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/mimecontent + def mime_content!(mime_content) + nbuild[NS_EWS_TYPES].MimeContent(mime_content[:text]) { |x| + x.parent['CharacterSet'] = mime_content[:character_set] if mime_content[:character_set] + } + end + def new_body_content!(body) nbuild[NS_EWS_TYPES].NewBodyContent(body[:text]) { |x| x.parent['BodyType'] = body[:body_type] if body[:body_type] diff --git a/lib/ews/templates/message.rb b/lib/ews/templates/message.rb index cbd1adb4..ac04f0ea 100644 --- a/lib/ews/templates/message.rb +++ b/lib/ews/templates/message.rb @@ -36,11 +36,12 @@ def init_defaults! self.item_attachments ||= [] self.inline_attachments ||= [] self.extended_properties ||= [] + self.mime_content_charset ||= 'UTF-8' end def to_ews_basic ews_opts = {} - ews_opts[:message_disposition] = (draft ? 'SaveOnly' : 'SendAndSaveCopy') + ews_opts[:message_disposition] = message_disposition || (draft ? 'SaveOnly' : 'SendAndSaveCopy') if saved_item_folder_id ews_opts[:saved_item_folder_id] = if saved_item_folder_id.is_a?(Hash) @@ -51,6 +52,7 @@ def to_ews_basic end msg = {} + msg[:mime_content] = { text: mime_content, character_set: mime_content_charset } if mime_content msg[:subject] = subject if subject msg[:body] = { text: body, body_type: body_type } if body diff --git a/spec/unit/soap/builders/ews_builder_spec.rb b/spec/unit/soap/builders/ews_builder_spec.rb index 5fde0f72..bd8e42a3 100644 --- a/spec/unit/soap/builders/ews_builder_spec.rb +++ b/spec/unit/soap/builders/ews_builder_spec.rb @@ -24,6 +24,22 @@ expect(doc.xpath('//t:BodyType[3]').text).to eq('Best') end + it 'should build MimeContent for a message item' do + doc = builder.build! { |section| + builder.message!(mime_content: { text: 'TUlNRQ==', character_set: 'UTF-8' }) if section == :body + } + mime_content = doc.at_xpath('//t:Message/t:MimeContent', namespaces) + expect(mime_content.text).to eq('TUlNRQ==') + expect(mime_content['CharacterSet']).to eq('UTF-8') + end + + it 'should build IncludeMimeContent' do + doc = builder.build! { |section| + builder.include_mime_content!(true) if section == :body + } + expect(doc.at_xpath('//t:IncludeMimeContent', namespaces).text).to eq('true') + end + it 'should format various Time inputs' do expect(builder.send(:format_time, Date.iso8601('2013-01-01'))).to eq('2013-01-01T00:00:00+00:00') expect(builder.send(:format_time, DateTime.iso8601('2013-01-01T23:01:00-05:00'))).to eq('2013-01-02T04:01:00+00:00') diff --git a/spec/unit/templates/message_spec.rb b/spec/unit/templates/message_spec.rb new file mode 100644 index 00000000..ad683ed8 --- /dev/null +++ b/spec/unit/templates/message_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative '../../spec_helper' + +describe Viewpoint::EWS::Template::Message do + describe '#to_ews' do + it 'defaults the message disposition from the draft flag' do + expect(described_class.new.to_ews[:message_disposition]).to eq('SendAndSaveCopy') + expect(described_class.new(draft: true).to_ews[:message_disposition]).to eq('SaveOnly') + end + + it 'uses an explicit message disposition' do + msg = described_class.new(draft: true, message_disposition: 'SendOnly') + expect(msg.to_ews[:message_disposition]).to eq('SendOnly') + end + + it 'omits MimeContent unless given' do + expect(described_class.new.to_ews[:items].first[:message]).not_to have_key(:mime_content) + end + + it 'includes MimeContent with a UTF-8 character set by default' do + msg = described_class.new(mime_content: 'TUlNRQ==') + expect(msg.to_ews[:items].first[:message][:mime_content]).to eq(text: 'TUlNRQ==', character_set: 'UTF-8') + end + + it 'includes MimeContent set in a block-style assignment with a custom character set' do + msg = described_class.new + msg.mime_content = 'TUlNRQ==' + msg.mime_content_charset = 'ISO-8859-1' + expect(msg.to_ews[:items].first[:message][:mime_content]).to eq(text: 'TUlNRQ==', character_set: 'ISO-8859-1') + end + end +end