mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
#17552: Allowing multiple recipient when using IMessageManager
Work Item: 17552 --HG-- branch : 1.x
This commit is contained in:
40
src/Orchard.Tests.Modules/Email/EmailChannelTests.cs
Normal file
40
src/Orchard.Tests.Modules/Email/EmailChannelTests.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -143,6 +143,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="CodeGeneration\Commands\CodeGenerationCommandsTests.cs" />
|
<Compile Include="CodeGeneration\Commands\CodeGenerationCommandsTests.cs" />
|
||||||
<Compile Include="Comments\Services\CommentServiceTests.cs" />
|
<Compile Include="Comments\Services\CommentServiceTests.cs" />
|
||||||
|
<Compile Include="Email\EmailChannelTests.cs" />
|
||||||
<Compile Include="ImportExport\Services\ImportExportServiceTests.cs" />
|
<Compile Include="ImportExport\Services\ImportExportServiceTests.cs" />
|
||||||
<Compile Include="Indexing\IndexingTaskExecutorTests.cs" />
|
<Compile Include="Indexing\IndexingTaskExecutorTests.cs" />
|
||||||
<Compile Include="Indexing\LuceneIndexProviderTests.cs" />
|
<Compile Include="Indexing\LuceneIndexProviderTests.cs" />
|
||||||
@@ -298,6 +299,7 @@
|
|||||||
<EmbeddedResource Include="Recipes\Services\FoldersData\Sample1\Module.txt" />
|
<EmbeddedResource Include="Recipes\Services\FoldersData\Sample1\Module.txt" />
|
||||||
<EmbeddedResource Include="Recipes\Services\FoldersData\Sample1\Recipes\cms.recipe.xml" />
|
<EmbeddedResource Include="Recipes\Services\FoldersData\Sample1\Recipes\cms.recipe.xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- 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.
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Orchard.ContentManagement.Records;
|
||||||
using Orchard.Core.Common.Models;
|
using Orchard.Core.Common.Models;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.Events;
|
using Orchard.Events;
|
||||||
@@ -53,7 +55,6 @@ namespace Orchard.Email.Rules {
|
|||||||
if (owner != null && owner.Record != null) {
|
if (owner != null && owner.Record != null) {
|
||||||
_messageManager.Send(owner.Record, MessageType, "email", properties);
|
_messageManager.Send(owner.Record, MessageType, "email", properties);
|
||||||
}
|
}
|
||||||
_messageManager.Send(owner.As<IUser>().Email, MessageType, "email", properties);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (recipient == "author") {
|
else if (recipient == "author") {
|
||||||
@@ -91,14 +92,8 @@ namespace Orchard.Email.Rules {
|
|||||||
if (context.MessagePrepared)
|
if (context.MessagePrepared)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (context.Recipient != null) {
|
if (!context.Recipients.Any()) {
|
||||||
var contentItem = _contentManager.Get(context.Recipient.Id);
|
return;
|
||||||
if (contentItem == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var recipient = contentItem.As<IUser>();
|
|
||||||
if (recipient == null)
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (context.Type) {
|
switch (context.Type) {
|
||||||
|
@@ -12,8 +12,8 @@ namespace Orchard.Email.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Sending(MessageContext context) {
|
public void Sending(MessageContext context) {
|
||||||
if (context.Recipient != null) {
|
foreach(var rec in context.Recipients) {
|
||||||
var contentItem = _contentManager.Get(context.Recipient.Id);
|
var contentItem = _contentManager.Get(rec.Id);
|
||||||
if (contentItem == null)
|
if (contentItem == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -23,6 +23,10 @@ namespace Orchard.Email.Services {
|
|||||||
|
|
||||||
context.MailMessage.To.Add(recipient.Email);
|
context.MailMessage.To.Add(recipient.Email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach(var address in context.Addresses) {
|
||||||
|
context.MailMessage.To.Add(address);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Sent(MessageContext context) {
|
public void Sent(MessageContext context) {
|
||||||
|
@@ -34,7 +34,7 @@ namespace Orchard.Email.Services {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
using (SmtpClient smtpClient = new SmtpClient()) {
|
using (var smtpClient = new SmtpClient()) {
|
||||||
smtpClient.UseDefaultCredentials = !smtpSettings.RequireCredentials;
|
smtpClient.UseDefaultCredentials = !smtpSettings.RequireCredentials;
|
||||||
if (!smtpClient.UseDefaultCredentials && !String.IsNullOrWhiteSpace(smtpSettings.UserName)) {
|
if (!smtpClient.UseDefaultCredentials && !String.IsNullOrWhiteSpace(smtpSettings.UserName)) {
|
||||||
smtpClient.Credentials = new NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
|
smtpClient.Credentials = new NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Messaging.Events;
|
using Orchard.Messaging.Events;
|
||||||
using Orchard.Messaging.Models;
|
using Orchard.Messaging.Models;
|
||||||
@@ -23,7 +24,8 @@ namespace Orchard.Users.Handlers {
|
|||||||
if (context.MessagePrepared)
|
if (context.MessagePrepared)
|
||||||
return;
|
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 )
|
if ( contentItem == null )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
using Orchard.ContentManagement.Records;
|
using Orchard.ContentManagement.Records;
|
||||||
|
|
||||||
@@ -7,13 +9,17 @@ namespace Orchard.Messaging.Models {
|
|||||||
public MailMessage MailMessage { get; private set; }
|
public MailMessage MailMessage { get; private set; }
|
||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
public string Service { get; set; }
|
public string Service { get; set; }
|
||||||
|
[Obsolete("Use Recipients instead")]
|
||||||
public ContentItemRecord Recipient { get; set; }
|
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 Dictionary<string, string> Properties { get; private set; }
|
||||||
public bool MessagePrepared { get; set; }
|
public bool MessagePrepared { get; set; }
|
||||||
|
|
||||||
public MessageContext() {
|
public MessageContext() {
|
||||||
Properties = new Dictionary<string, string>();
|
Properties = new Dictionary<string, string>();
|
||||||
MailMessage = new MailMessage();
|
MailMessage = new MailMessage();
|
||||||
|
Addresses = Enumerable.Empty<string>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,13 +22,17 @@ namespace Orchard.Messaging.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null) {
|
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() )
|
if ( !HasChannels() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Logger.Information("Sending message {0}", type);
|
Logger.Information("Sending message {0}", type);
|
||||||
try {
|
try {
|
||||||
var context = new MessageContext {
|
var context = new MessageContext {
|
||||||
Recipient = recipient,
|
Recipients = recipients,
|
||||||
Type = type,
|
Type = type,
|
||||||
Service = service
|
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) {
|
private void PrepareAndSend(string type, Dictionary<string, string> properties, MessageContext context) {
|
||||||
try {
|
try {
|
||||||
if (properties != null) {
|
if (properties != null) {
|
||||||
@@ -61,33 +93,5 @@ namespace Orchard.Messaging.Services {
|
|||||||
|
|
||||||
Logger.Information("Message {0} sent", type);
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,17 +1,36 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Orchard.ContentManagement.Records;
|
using Orchard.ContentManagement.Records;
|
||||||
|
|
||||||
namespace Orchard.Messaging.Services {
|
namespace Orchard.Messaging.Services {
|
||||||
public interface IMessageManager : IDependency {
|
public interface IMessageManager : IDependency {
|
||||||
/// <summary>
|
/// <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>
|
/// </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);
|
void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </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>
|
/// <summary>
|
||||||
/// Whether at least one channel is active on the current site
|
/// Whether at least one channel is active on the current site
|
||||||
|
Reference in New Issue
Block a user