From 38d8c29065a72d7dafb94ab7e508b274e243da96 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Tue, 3 Jan 2017 17:48:49 +0100 Subject: [PATCH] Work checkpoint --- Backend/CommsTransmitter.cs | 73 ++++++++++++++++--- .../Types/Communications/BasicOutboundComm.cs | 6 +- Logic/Communications/OutboundComm.cs | 19 ++++- .../Payload/NotificationPayload.cs | 4 +- .../Payload/ParticipantMailPayload.cs | 13 ++++ .../Resolution/ParticipantResolver.cs | 13 +++- Logic/Logic.csproj | 1 + ...ansmission.NotificationPayload.Designer.cs | 4 +- ...ions.Transmission.NotificationPayload.resx | 2 +- 9 files changed, 113 insertions(+), 22 deletions(-) diff --git a/Backend/CommsTransmitter.cs b/Backend/CommsTransmitter.cs index 39618dfd..d5755c13 100644 --- a/Backend/CommsTransmitter.cs +++ b/Backend/CommsTransmitter.cs @@ -4,6 +4,8 @@ using Swarmops.Logic.Communications.Payload; using Swarmops.Logic.Communications.Resolution; using Swarmops.Utility.Communications; using System.Reflection; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Swarm; namespace Swarmops.Backend { @@ -17,21 +19,29 @@ namespace Swarmops.Backend { if (!comm.Resolved) { - // Resolve recipients - - ResolverEnvelope resolverEnvelope = ResolverEnvelope.FromXml(comm.ResolverDataXml); - - // Create the resolver via reflection of the static FromXml method - - Assembly assembly = typeof(ResolverEnvelope).Assembly; - - Type payloadType = assembly.GetType(resolverEnvelope.ResolverClass); - var methodInfo = payloadType.GetMethod("FromXml", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); - - ICommsResolver resolver = (ICommsResolver)(methodInfo.Invoke(null, new object[] { resolverEnvelope.ResolverDataXml })); + ICommsResolver resolver = FindResolver(comm); resolver.Resolve(comm); comm.Resolved = true; + if (comm.Recipients.Count > 1) + { + // "Your message has been queued for delivery and the recipients have been resolved. + // Your mail will be sent to, or be attempted to sent to, [RecipientCount] people in [Geography] in [OrganizationName]." + + NotificationStrings notifyStrings = new NotificationStrings(); + NotificationCustomStrings customStrings = new NotificationCustomStrings(); + notifyStrings[NotificationString.OrganizationName] = Organization.FromIdentity(comm.OrganizationId).Name; + customStrings["RecipientCount"] = comm.Recipients.Count.ToString("N0"); + if (resolver is IHasGeography) + { + customStrings["GeographyName"] = ((IHasGeography) resolver).Geography.Name; + } + OutboundComm.CreateNotification(Organization.FromIdentity(comm.OrganizationId), + NotificationResource.OutboundComm_Resolved, notifyStrings, + People.FromSingle(Person.FromIdentity(comm.SenderPersonId))); + } + + continue; // continue is not strictly necessary; could continue processing the same OutboundComm after resolution } @@ -61,7 +71,46 @@ namespace Swarmops.Backend } comm.Open = false; + + if (comm.RecipientsFail + comm.RecipientsSuccess > 1) + { + ICommsResolver resolver = FindResolver(comm); + + // "Your message to[GeographyName] has been sent to all scheduled recipients.Of the[RecipientCount] planned recipients, [RecipientsSuccess] succeeded + // from Swarmops' horizon. (These can fail later for a number of reasons, from broken computers to hospitalized recipients.) + // Time spent transmitting: [TransmissionTime]." + + // TODO: CONTINUE HERE + + NotificationStrings notifyStrings = new NotificationStrings(); + NotificationCustomStrings customStrings = new NotificationCustomStrings(); + notifyStrings[NotificationString.OrganizationName] = Organization.FromIdentity(comm.OrganizationId).Name; + customStrings["RecipientCount"] = comm.Recipients.Count.ToString("N0"); + if (resolver is IHasGeography) + { + customStrings["Geography"] = ((IHasGeography)resolver).Geography.Name; + } + OutboundComm.CreateNotification(Organization.FromIdentity(comm.OrganizationId), + NotificationResource.OutboundComm_Resolved, notifyStrings, + People.FromSingle(Person.FromIdentity(comm.SenderPersonId))); + } } } + + internal static ICommsResolver FindResolver(OutboundComm comm) + { + // Resolve recipients + + ResolverEnvelope resolverEnvelope = ResolverEnvelope.FromXml(comm.ResolverDataXml); + + // Create the resolver via reflection of the static FromXml method + + Assembly assembly = typeof(ResolverEnvelope).Assembly; + + Type payloadType = assembly.GetType(resolverEnvelope.ResolverClass); + var methodInfo = payloadType.GetMethod("FromXml", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); + + return (ICommsResolver)(methodInfo.Invoke(null, new object[] { resolverEnvelope.ResolverDataXml })); + } } } \ No newline at end of file diff --git a/Basic/Types/Communications/BasicOutboundComm.cs b/Basic/Types/Communications/BasicOutboundComm.cs index 0f973fa0..c827160c 100644 --- a/Basic/Types/Communications/BasicOutboundComm.cs +++ b/Basic/Types/Communications/BasicOutboundComm.cs @@ -29,7 +29,7 @@ namespace Swarmops.Basic.Types.Communications ClosedDateTime = closedDateTime; RecipientCount = recipientCount; RecipientsSuccess = recipientsSuccess; - RecipeintsFail = recipientsFail; + RecipientsFail = recipientsFail; } @@ -39,7 +39,7 @@ namespace Swarmops.Basic.Types.Communications original.CreatedDateTime, original.ResolverDataXml, original.RecipientDataXml, original.Resolved, original.ResolvedDateTime, original.Priority, original.TransmitterClass, original.PayloadXml, original.Open, original.StartTransmitDateTime, original.ClosedDateTime, original.RecipientCount, - original.RecipientsSuccess, original.RecipeintsFail) + original.RecipientsSuccess, original.RecipientsFail) { // copy ctor } @@ -62,7 +62,7 @@ namespace Swarmops.Basic.Types.Communications public DateTime ClosedDateTime { get; protected set; } public int RecipientCount { get; protected set; } public int RecipientsSuccess { get; protected set; } - public int RecipeintsFail { get; protected set; } + public int RecipientsFail { get; protected set; } #region Implementation of IHasIdentity diff --git a/Logic/Communications/OutboundComm.cs b/Logic/Communications/OutboundComm.cs index 1c54935c..76d79062 100644 --- a/Logic/Communications/OutboundComm.cs +++ b/Logic/Communications/OutboundComm.cs @@ -142,7 +142,7 @@ namespace Swarmops.Logic.Communications List recipients = People.FromSingle (participation.Person); OutboundComm comm = OutboundComm.Create (null, null, participation.Organization, - null, null, + null /* resolver */, null /*recipientdata*/, CommTransmitterClass.CommsTransmitterMail, new PayloadEnvelope (new ParticipantMailPayload (mailType, participation, actingPerson)).ToXml(), OutboundCommPriority.Low); @@ -158,6 +158,21 @@ namespace Swarmops.Logic.Communications } + public static OutboundComm CreateParticipantMail(string customSubject, string customBody, Person sender, Person from, + Organization organization, Geography geography) + { + ParticipantResolver resolver = new ParticipantResolver(organization, geography); + + OutboundComm comm = OutboundComm.Create (sender, from, organization, + resolver, null /*recipientdata*/, + CommTransmitterClass.CommsTransmitterMail, + new PayloadEnvelope(new ParticipantMailPayload(customSubject, customBody, organization, sender)).ToXml(), + OutboundCommPriority.Low); + + return comm; + } + + public static OutboundComm CreateParticipantMail(string customSubject, string customBody, Participation participation, Person actingPerson) { @@ -234,7 +249,7 @@ namespace Swarmops.Logic.Communications if (organization != null) { - strings[NotificationString.OrganizationName] = organization.Name; // since we're sending the org anyway, kind of pointless to require callers to set this + strings[NotificationString.OrganizationName] = organization.Name; //strings[NotificationString.OrganizationMailPrefix] = organization.MailPrefix; // TODO // TODO: Change to org admins diff --git a/Logic/Communications/Payload/NotificationPayload.cs b/Logic/Communications/Payload/NotificationPayload.cs index d9403774..ab0cd54c 100644 --- a/Logic/Communications/Payload/NotificationPayload.cs +++ b/Logic/Communications/Payload/NotificationPayload.cs @@ -192,7 +192,9 @@ namespace Swarmops.Logic.Communications.Payload BitcoinPayoutAddress_Changed, Salary_MonthlyStatement, Salary_Paid, - Salary_LastYearSummary + Salary_LastYearSummary, + OutboundComm_Resolved, + OutboundComm_Sent } // ReSharper restore InconsistentNaming diff --git a/Logic/Communications/Payload/ParticipantMailPayload.cs b/Logic/Communications/Payload/ParticipantMailPayload.cs index 39234c89..b1d143a8 100644 --- a/Logic/Communications/Payload/ParticipantMailPayload.cs +++ b/Logic/Communications/Payload/ParticipantMailPayload.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; //using Swarmops.Basic.Types.Common; using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; using Swarmops.Logic.Swarm; using Swarmops.Logic.Support; @@ -64,6 +65,18 @@ namespace Swarmops.Logic.Communications.Payload } + public ParticipantMailPayload(string customSubject, string customBody, Organization organization, + Person actingPerson) + { + Strings = new SerializableDictionary(); + + SubjectTemplate = customSubject; + BodyTemplate = customBody; + Strings[MailPayloadString.ActingPerson] = actingPerson.Name; + Strings[MailPayloadString.OrganizationName] = organization.Name; + } + + public RenderedComm RenderComm (Person person) { string body = BodyTemplate; diff --git a/Logic/Communications/Resolution/ParticipantResolver.cs b/Logic/Communications/Resolution/ParticipantResolver.cs index 53cc4d51..2c3d784d 100644 --- a/Logic/Communications/Resolution/ParticipantResolver.cs +++ b/Logic/Communications/Resolution/ParticipantResolver.cs @@ -6,11 +6,12 @@ using System.Threading.Tasks; using Swarmops.Logic.Support; using Swarmops.Logic.Swarm; using Swarmops.Logic.Structure; +using System.Xml.Serialization; namespace Swarmops.Logic.Communications.Resolution { [Serializable] - public class ParticipantResolver: PayloadBase, ICommsResolver + public class ParticipantResolver: PayloadBase, ICommsResolver, IHasGeography { [Obsolete("The parameterless ctor is provided for serializability and must not be called directly.", true)] public ParticipantResolver() @@ -18,6 +19,13 @@ namespace Swarmops.Logic.Communications.Resolution // Required for serialization. Never call directly. } + public ParticipantResolver(Organization organization, Geography geography, bool includeSuborganizations = true) + { + this.IncludeSuborganizations = includeSuborganizations; + this.GeographyId = geography.Identity; + this.OrganizationId = organization.Identity; + } + public void Resolve(OutboundComm comm) { Organization organization = Organization.FromIdentity (OrganizationId); @@ -36,5 +44,8 @@ namespace Swarmops.Logic.Communications.Resolution public bool IncludeSuborganizations { get; set; } public int OrganizationId { get; set; } public int GeographyId { get; set; } + + [XmlIgnore] + public Geography Geography { get { return Geography.FromIdentity(this.GeographyId); } } } } diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 157351e9..84c839b9 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -112,6 +112,7 @@ + diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index 2706e4cc..7cb6b65e 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34209 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -554,7 +554,7 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to Your message has been queued for delivery and the recipients have been resolved. Your mail will be sent to, or be attempted to sent to, [RecipientCount] people in [Geography] in [OrganizationName].. + /// Looks up a localized string similar to Your message has been queued for delivery and the recipients have been resolved. Your mail will be sent to, or be attempted to sent to, [RecipientCount] people in [GeographyName] in [OrganizationName].. /// internal static string OutboundComm_Resolved_Body { get { diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx index ff926333..e0573f5e 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -320,7 +320,7 @@ Please log on to [InstallationName] at [ExternalUrl]Financial/AttestCosts to att This mail was sent to [Addressee] from [OrganizationName]. If you do not desire to receive these notifications in the future, please log on to [InstallationName] at [ExternalUrl] and modify your notification settings. - Your message has been queued for delivery and the recipients have been resolved. Your mail will be sent to, or be attempted to sent to, [RecipientCount] people in [Geography] in [OrganizationName]. + Your message has been queued for delivery and the recipients have been resolved. Your mail will be sent to, or be attempted to sent to, [RecipientCount] people in [GeographyName] in [OrganizationName]. [[OrganizationName]] Comm recipients determined for [GeographyName]: [RecipientCount]