private SendResponse CreateNoticeMessageFromNotifyRequest(NotifyRequest request, string sender, IServiceScope serviceScope, out NoticeMessage noticeMessage) { if (request == null) { throw new ArgumentNullException("request"); } var recipientProvider = request.GetRecipientsProvider(serviceScope); var recipient = request.Recipient as IDirectRecipient; var addresses = recipient.Addresses; if (addresses == null || !addresses.Any()) { addresses = recipientProvider.GetRecipientAddresses(request.Recipient as IDirectRecipient, sender); recipient = new DirectRecipient(request.Recipient.ID, request.Recipient.Name, addresses); } recipient = recipientProvider.FilterRecipientAddresses(recipient); noticeMessage = request.CreateMessage(recipient); addresses = recipient.Addresses; if (addresses == null || !addresses.Any(a => !string.IsNullOrEmpty(a))) { //checking addresses return(new SendResponse(request.NotifyAction, sender, recipient, new NotifyException(string.Format("For recipient {0} by sender {1} no one addresses getted.", recipient, sender)))); } var pattern = request.GetSenderPattern(sender); if (pattern == null) { return(new SendResponse(request.NotifyAction, sender, recipient, new NotifyException(string.Format("For action \"{0}\" by sender \"{1}\" no one patterns getted.", request.NotifyAction, sender)))); } noticeMessage.Pattern = pattern; noticeMessage.ContentType = pattern.ContentType; noticeMessage.AddArgument(request.Arguments.ToArray()); var patternProvider = request.GetPatternProvider(serviceScope); var formatter = patternProvider.GetFormatter(pattern); try { if (formatter != null) { formatter.FormatMessage(noticeMessage, noticeMessage.Arguments); } sysTagFormatter.FormatMessage( noticeMessage, new[] { new TagValue(Context._SYS_RECIPIENT_ID, request.Recipient.ID), new TagValue(Context._SYS_RECIPIENT_NAME, request.Recipient.Name), new TagValue(Context._SYS_RECIPIENT_ADDRESS, addresses != null && addresses.Length > 0 ? addresses[0] : null) } ); //Do styling here if (!string.IsNullOrEmpty(pattern.Styler)) { //We need to run through styler before templating StyleMessage(serviceScope, noticeMessage); } } catch (Exception exc) { return(new SendResponse(request.NotifyAction, sender, recipient, exc)); } return(null); }
private void SendGroupNotify(NotifyRequest request, List <SendResponse> responces, IServiceScope serviceScope) { if (request.Recipient is IDirectRecipient) { var subscriptionSource = request.GetSubscriptionProvider(serviceScope); if (!request.IsNeedCheckSubscriptions || !subscriptionSource.IsUnsubscribe(request.Recipient as IDirectRecipient, request.NotifyAction, request.ObjectID)) { var directresponses = new List <SendResponse>(1); try { directresponses = SendDirectNotify(request, serviceScope); } catch (Exception exc) { directresponses.Add(new SendResponse(request.NotifyAction, request.Recipient, exc)); } responces.AddRange(directresponses); } } else { if (request.Recipient is IRecipientsGroup) { var checkresp = CheckPreventInterceptors(request, InterceptorPlace.GroupSend, serviceScope, null); if (checkresp != null) { responces.Add(checkresp); } else { var recipientProvider = request.GetRecipientsProvider(serviceScope); try { var recipients = recipientProvider.GetGroupEntries(request.Recipient as IRecipientsGroup) ?? new IRecipient[0]; foreach (var recipient in recipients) { try { var newRequest = request.Split(recipient); SendGroupNotify(newRequest, responces, serviceScope); } catch (Exception exc) { responces.Add(new SendResponse(request.NotifyAction, request.Recipient, exc)); } } } catch (Exception exc) { responces.Add(new SendResponse(request.NotifyAction, request.Recipient, exc) { Result = SendResult.IncorrectRecipient }); } } } else { responces.Add(new SendResponse(request.NotifyAction, request.Recipient, null) { Result = SendResult.IncorrectRecipient, Exception = new NotifyException("recipient may be IRecipientsGroup or IDirectRecipient") }); } } }