#17552: Allowing multiple recipient when using IMessageManager

Work Item: 17552

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-12-05 12:20:37 -08:00
parent dfdc3596f6
commit 1b004bbc9d
9 changed files with 119 additions and 47 deletions

View File

@@ -0,0 +1,40 @@
using Autofac;
using Moq;
using NUnit.Framework;
using Orchard.ContentManagement.Records;
using Orchard.Messaging.Events;
using Orchard.Messaging.Services;
using Orchard.Tests.Messaging;
using Orchard.Tests.Utility;
namespace Orchard.Tests.Modules.Email {
[TestFixture]
public class EmailChannelTests {
private MessagingChannelStub _channel;
private IMessageManager _messageManager;
[SetUp]
public void Init() {
var builder = new ContainerBuilder();
builder.RegisterInstance(new Mock<IMessageEventHandler>().Object);
builder.RegisterType<DefaultMessageManager>().As<IMessageManager>();
builder.RegisterInstance(_channel = new MessagingChannelStub()).As<IMessagingChannel>();
var container = builder.Build();
_messageManager = container.Resolve<IMessageManager>();
}
[Test]
public void CanSendEmailUsingAddresses() {
_messageManager.Send(new []{ "steveb@microsoft.com" }, "test", "email");
Assert.That(_channel.Messages.Count, Is.EqualTo(1));
}
[Test]
public void OneMessageIsSentUsingMultipleRecipients() {
_messageManager.Send(new[] { "steveb@microsoft.com", "billg@microsoft.com" }, "test", "email");
Assert.That(_channel.Messages.Count, Is.EqualTo(1));
}
}
}

View File

@@ -143,6 +143,7 @@
<ItemGroup>
<Compile Include="CodeGeneration\Commands\CodeGenerationCommandsTests.cs" />
<Compile Include="Comments\Services\CommentServiceTests.cs" />
<Compile Include="Email\EmailChannelTests.cs" />
<Compile Include="ImportExport\Services\ImportExportServiceTests.cs" />
<Compile Include="Indexing\IndexingTaskExecutorTests.cs" />
<Compile Include="Indexing\LuceneIndexProviderTests.cs" />
@@ -298,6 +299,7 @@
<EmbeddedResource Include="Recipes\Services\FoldersData\Sample1\Module.txt" />
<EmbeddedResource Include="Recipes\Services\FoldersData\Sample1\Recipes\cms.recipe.xml" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement.Records;
using Orchard.Core.Common.Models;
using Orchard.Environment.Extensions;
using Orchard.Events;
@@ -53,7 +55,6 @@ namespace Orchard.Email.Rules {
if (owner != null && owner.Record != null) {
_messageManager.Send(owner.Record, MessageType, "email", properties);
}
_messageManager.Send(owner.As<IUser>().Email, MessageType, "email", properties);
}
}
else if (recipient == "author") {
@@ -91,14 +92,8 @@ namespace Orchard.Email.Rules {
if (context.MessagePrepared)
return;
if (context.Recipient != null) {
var contentItem = _contentManager.Get(context.Recipient.Id);
if (contentItem == null)
return;
var recipient = contentItem.As<IUser>();
if (recipient == null)
return;
if (!context.Recipients.Any()) {
return;
}
switch (context.Type) {

View File

@@ -12,8 +12,8 @@ namespace Orchard.Email.Services {
}
public void Sending(MessageContext context) {
if (context.Recipient != null) {
var contentItem = _contentManager.Get(context.Recipient.Id);
foreach(var rec in context.Recipients) {
var contentItem = _contentManager.Get(rec.Id);
if (contentItem == null)
return;
@@ -23,6 +23,10 @@ namespace Orchard.Email.Services {
context.MailMessage.To.Add(recipient.Email);
}
foreach(var address in context.Addresses) {
context.MailMessage.To.Add(address);
}
}
public void Sent(MessageContext context) {

View File

@@ -34,7 +34,7 @@ namespace Orchard.Email.Services {
return;
}
using (SmtpClient smtpClient = new SmtpClient()) {
using (var smtpClient = new SmtpClient()) {
smtpClient.UseDefaultCredentials = !smtpSettings.RequireCredentials;
if (!smtpClient.UseDefaultCredentials && !String.IsNullOrWhiteSpace(smtpSettings.UserName)) {
smtpClient.Credentials = new NetworkCredential(smtpSettings.UserName, smtpSettings.Password);

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Orchard.Localization;
using Orchard.Messaging.Events;
using Orchard.Messaging.Models;
@@ -23,7 +24,8 @@ namespace Orchard.Users.Handlers {
if (context.MessagePrepared)
return;
var contentItem = _contentManager.Get(context.Recipient.Id);
// we expect a single account to be created
var contentItem = _contentManager.Get(context.Recipients.Single().Id);
if ( contentItem == null )
return;

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using Orchard.ContentManagement.Records;
@@ -7,13 +9,17 @@ namespace Orchard.Messaging.Models {
public MailMessage MailMessage { get; private set; }
public string Type { get; set; }
public string Service { get; set; }
[Obsolete("Use Recipients instead")]
public ContentItemRecord Recipient { get; set; }
public IEnumerable<ContentItemRecord> Recipients { get; set; }
public IEnumerable<string> Addresses { get; set; }
public Dictionary<string, string> Properties { get; private set; }
public bool MessagePrepared { get; set; }
public MessageContext() {
Properties = new Dictionary<string, string>();
MailMessage = new MailMessage();
Addresses = Enumerable.Empty<string>();
}
}
}

View File

@@ -22,13 +22,17 @@ namespace Orchard.Messaging.Services {
}
public void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null) {
Send(new [] { recipient }, service, type, properties);
}
public void Send(IEnumerable<ContentItemRecord> recipients, string type, string service, Dictionary<string, string> properties = null) {
if ( !HasChannels() )
return;
Logger.Information("Sending message {0}", type);
try {
var context = new MessageContext {
Recipient = recipient,
Recipients = recipients,
Type = type,
Service = service
};
@@ -40,6 +44,34 @@ namespace Orchard.Messaging.Services {
}
}
public void Send(IEnumerable<string> recipientAddresses, string type, string service, Dictionary<string, string> properties = null) {
if (!HasChannels())
return;
Logger.Information("Sending message {0}", type);
try {
var context = new MessageContext {
Type = type,
Service = service,
Addresses = recipientAddresses
};
PrepareAndSend(type, properties, context);
}
catch (Exception e) {
Logger.Error(e, "An error occured while sending the message {0}", type);
}
}
public bool HasChannels() {
return _channels.Any();
}
public IEnumerable<string> GetAvailableChannelServices() {
return _channels.SelectMany(c => c.GetAvailableServices());
}
private void PrepareAndSend(string type, Dictionary<string, string> properties, MessageContext context) {
try {
if (properties != null) {
@@ -61,33 +93,5 @@ namespace Orchard.Messaging.Services {
Logger.Information("Message {0} sent", type);
}
public void Send(string recipientAddresses, string type, string service, Dictionary<string, string> properties = null) {
if (!HasChannels())
return;
Logger.Information("Sending message {0}", type);
try {
var context = new MessageContext {
Type = type,
Service = service
};
context.MailMessage.To.Add(recipientAddresses);
PrepareAndSend(type, properties, context);
}
catch (Exception e) {
Logger.Error(e, "An error occured while sending the message {0}", type);
}
}
public bool HasChannels() {
return _channels.Any();
}
public IEnumerable<string> GetAvailableChannelServices() {
return _channels.SelectMany(c => c.GetAvailableServices());
}
}
}

View File

@@ -1,17 +1,36 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Orchard.ContentManagement.Records;
namespace Orchard.Messaging.Services {
public interface IMessageManager : IDependency {
/// <summary>
/// Sends a message to a channel using a user content item as the recipient
/// Sends a message to a channel using a content item as the recipient
/// </summary>
/// <param name="recipient">A content item to send the message to.</param>
/// <param name="type">A custom string specifying what type of message is sent. Used in even handlers to define the message.</param>
/// <param name="service">The name of the channel to use, e.g. "email"</param>
/// <param name="properties">A set of specific properties for the channel.</param>
void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null);
/// <summary>
/// Sends a message to a channel using a comma-separated list of recipient addresses
/// Sends a message to a channel using a set of content items as the recipients
/// </summary>
void Send(string recipientAddresses, string type, string service, Dictionary<string, string> properties = null);
/// <param name="recipients">A set of content items to send the message to. Only one message may be sent if the channel manages it.</param>
/// <param name="type">A custom string specifying what type of message is sent. Used in even handlers to define the message.</param>
/// <param name="service">The name of the channel to use, e.g. "email"</param>
/// <param name="properties">A set of specific properties for the channel.</param>
void Send(IEnumerable<ContentItemRecord> recipients, string type, string service, Dictionary<string, string> properties = null);
/// <summary>
/// Sends a message to a channel using a list of recipient addresses
/// </summary>
/// <param name="recipientAddresses">A list of addresses that the channel can process.</param>
/// <param name="type">A custom string specifying what type of message is sent. Used in even handlers to define the message.</param>
/// <param name="service">The name of the channel to use, e.g. "email"</param>
/// <param name="properties">A set of specific properties for the channel.</param>
void Send(IEnumerable<string> recipientAddresses, string type, string service, Dictionary<string, string> properties = null);
/// <summary>
/// Whether at least one channel is active on the current site