Inheritance: INotifyPropertyChanging, INotifyPropertyChanged
Beispiel #1
0
        public static void Subscribe(User subscriber, Node target, NotificationFrequency frequency, string language, string sitePath, string siteUrl, bool isActive)
        {
            if (subscriber.Email == null)
                throw new InvalidOperationException("Subscriber's email cannot be null.");
            if (subscriber.Email.Length == 0)
                throw new InvalidOperationException("Subscriber's email cannot be empty.");

            var userName = subscriber.FullName;
            if (String.IsNullOrEmpty(userName))
                userName = subscriber.Username;

            var subscription = new Subscription
            {
                UserEmail = subscriber.Email,
                UserId = subscriber.Id,
                UserPath = subscriber.Path,
                UserName = userName,
                ContentPath = target.Path,
                Frequency = frequency,
                Language = language,
                IsActive = true,
                SitePath = sitePath,
                SiteUrl = siteUrl,
            };
            subscription.Save();
        }
        private static Message GenerateMessage(Subscription subscription, Dictionary<string, NotificationConfig> configs)
        {
            if (subscription.RelatedEvents.Count == 0)
                return null;

            // we will process those content only for which no config is available. if there is no such content, we are done.
            if (!subscription.RelatedEvents.Any(e => configs[e.ContentPath] == null))
                return null;

            var template = new MessageTemplate(subscription.Language);
            var cultureInfo = CultureInfo.CreateSpecificCulture(subscription.Language);

            string subjectTemplate = null;
            switch (subscription.Frequency)
            {
                case NotificationFrequency.Immediately: subjectTemplate = template.ImmediatelySubject; break;
                case NotificationFrequency.Daily: subjectTemplate = template.DailySubject; break;
                case NotificationFrequency.Weekly: subjectTemplate = template.WeeklySubject; break;
                case NotificationFrequency.Monthly: subjectTemplate = template.MonthlySubject; break;
                default: throw GetUnknownFrequencyException(subscription.Frequency);
            }
            var subject = ReplaceParameters(subjectTemplate, subscription);

            var body = new StringBuilder();
            string headTemplate = null;
            switch (subscription.Frequency)
            {
                case NotificationFrequency.Immediately: headTemplate = template.ImmediatelyHeader; break;
                case NotificationFrequency.Daily: headTemplate = template.DailyHeader; break;
                case NotificationFrequency.Weekly: headTemplate = template.WeeklyHeader; break;
                case NotificationFrequency.Monthly: headTemplate = template.MonthlyHeader; break;
                default: throw GetUnknownFrequencyException(subscription.Frequency);
            }
            body.Append(ReplaceParameters(headTemplate, subscription));

            foreach (var @event in subscription.RelatedEvents)
            {
                // a custom config exists for this contentpath, email for that has already been processed previously
                if (configs[@event.ContentPath] != null)
                    continue;

                body.Append(ReplaceParameters(GetEventTemplate(template, @event.NotificationType), subscription.SitePath, subscription.SiteUrl, @event, cultureInfo));
            }

            string footTemplate = null;
            switch (subscription.Frequency)
            {
                case NotificationFrequency.Immediately: footTemplate = template.ImmediatelyFooter; break;
                case NotificationFrequency.Daily: footTemplate = template.DailyFooter; break;
                case NotificationFrequency.Weekly: footTemplate = template.WeeklyFooter; break;
                case NotificationFrequency.Monthly: footTemplate = template.MonthlyFooter; break;
                default: throw GetUnknownFrequencyException(subscription.Frequency);
            }
            body.Append(ReplaceParameters(footTemplate, subscription));

            return new Message
            {
                Address = subscription.UserEmail,
                Subject = subject,
                Body = body.ToString()
            };

        }
 private static string ReplaceParameters(string template, Subscription subscription)
 {
     return template
         .Replace("{Address}", subscription.UserEmail)
         .Replace("{Addressee}", subscription.UserName);
 }
        private static void GatherConfigsForSubscription(Subscription subscription, Dictionary<string, NotificationConfig> configs)
        {
            // go through related events, and gather available configs
            foreach (var @event in subscription.RelatedEvents)
            {
                // config already resolved for this contentpath
                if (configs.ContainsKey(@event.ContentPath))
                    continue;

                // check custom config
                var query = new ApplicationQuery(NotificationConfig.CONFIGFOLDERNAME, false, false, HierarchyOption.Path);
                var configHeads = query.ResolveApplications(NotificationConfig.NOTIFICATIONCONFIGCONTENTNAME, @event.ContentPath, NotificationConfig.NOTIFICATIONCONFIGTYPENAME);
                var targetConfigHead = configHeads.FirstOrDefault();

                NotificationConfig configNode = null;
                if (targetConfigHead != null)
                {
                    configNode = Node.LoadNode(targetConfigHead.Id) as NotificationConfig;
                }
                configs.Add(@event.ContentPath, configNode);
            }
        }
        private static IEnumerable<Message> GenerateMessagesForConfig(Subscription subscription, Dictionary<string, NotificationConfig> configs)
        {
            if (subscription.RelatedEvents.Count == 0)
                yield break;

            foreach (var @event in subscription.RelatedEvents)
            {
                // we will process those content only for which config is available.
                var config = configs[@event.ContentPath];
                if (config == null)
                    continue;

                var contextNode = Node.LoadNode(@event.ContentPath);
                if (contextNode == null)
                    continue;

                var allowed = config.IsNotificationAllowedForContent(contextNode);
                if (!allowed)
                    continue;

                var subject = config.GetSubject(contextNode);
                var body = config.GetBody(contextNode);

                yield return new Message
                {
                    Address = subscription.UserEmail,
                    Subject = subject,
                    Body = body.ToString()
                };
            }
        }
 partial void DeleteSubscription(Subscription instance);
 private static bool HasPermission(Subscription subscription, Event @event)
 {
     if (@event.NotificationType == NotificationType.MinorVersionModified)
         return SecurityHandler.HasPermission(subscription.User, @event.ContentPath, @event.CreatorId, @event.LastModifierId, PermissionType.OpenMinor);
     else
         return SecurityHandler.HasPermission(subscription.User, @event.ContentPath, @event.CreatorId, @event.LastModifierId, PermissionType.Open);
 }
 partial void UpdateSubscription(Subscription instance);
 partial void InsertSubscription(Subscription instance);
Beispiel #10
0
        public void Notification_SubscriptionSave()
        {
            var user = Subscriber1;

            var subscription = new Subscription
            {
                UserEmail = user.Email,
                UserId = user.Id,
                UserPath = user.Path,
                UserName = user.FullName,
                ContentPath = "/Root/IMS",
                Frequency = NotificationFrequency.Weekly,
                Language = "en",
                IsActive = true
            };
            subscription.Save();

            subscription = new Subscription
            {
                UserEmail = user.Email,
                UserId = user.Id,
                UserPath = user.Path,
                UserName = user.FullName,
                ContentPath = "/Root/IMS/BuiltIn",
                Frequency = NotificationFrequency.Monthly,
                Language = "en",
                IsActive = true
            };
            subscription.Save();

            subscription = new Subscription
            {
                UserEmail = user.Email,
                UserId = user.Id,
                UserPath = user.Path,
                UserName = user.FullName,
                ContentPath = "/Root/IMS",
                Frequency = NotificationFrequency.Daily,
                Language = "en",
                IsActive = true
            };
            subscription.Save();

            var result = Subscription.GetAllSubscriptions();

            Assert.IsTrue(2 == result.Count(), "#1");
        }
        protected void BtnSave_Click(object sender, EventArgs args)
        {
            _contentView.UpdateContent();
            var content = _contentView.Content;

            if (!_contentView.IsUserInputValid || !content.IsValid)
                return;

            if (!this.HasPermission)
            {
                _contentView.ContentException = new SenseNetSecurityException(HttpContext.GetGlobalResourceObject("Notification", "NotEnoughPermissions") as string);
                return;
            }

            Subscription subscription;
            if (IsSubscriptionNew){
                subscription = new Subscription
                {
                    IsActive = true,
                    ContentPath = (string)content["ContentPath"],
                    Frequency = (NotificationFrequency)Enum.Parse(typeof(NotificationFrequency),
                                                                  (content["Frequency"] as List<String>)[0]),
                    UserEmail = (string)content["UserEmail"],
                    UserId = (int)(decimal)content["UserId"],
                    UserName = (string)content["UserName"],
                    UserPath = (string)content["UserPath"],
                    Language = (content["Language"] as List<String>)[0],
                    SitePath = PortalContext.Current.Site.Path,
                    SiteUrl = PortalContext.Current.SiteUrl
                };
            } else
            {
                subscription = new Subscription
                {
                    IsActive = (bool)content["IsActive"],
                    ContentPath = (string)content["ContentPath"],
                    Frequency = (NotificationFrequency)Enum.Parse(typeof(NotificationFrequency),
                                                                  (content["Frequency"] as List<String>)[0]),
                    UserEmail = (string)content["UserEmail"],
                    UserId = (int)(decimal)content["UserId"],
                    UserName = (string)content["UserName"],
                    UserPath = (string)content["UserPath"],
                    Language = (content["Language"] as List<String>)[0],
                    SitePath = PortalContext.Current.Site.Path,
                    SiteUrl = PortalContext.Current.SiteUrl
                };
            }

            try
            {
                subscription.Save();

                CallDone();
            }
            catch (Exception ex) //logged
            {
                Logger.WriteException(ex);
                _contentView.ContentException = ex;
            }
        }
        protected override object GetModel()
        {
            var ctdFile = Node.Load<ContentType>("/Root/System/Schema/ContentTypes/GenericContent/Subscription");
            var ctdStream = ctdFile.Binary.GetStream();
            var subscriptionCtd = Tools.GetStreamString(ctdStream);
            var currentSite = PortalContext.Current.Site;
            var siteUrl = PortalContext.Current.SiteUrl;

            if (this.User == null)
                return null;

            //edit subscription or create new if not exists
            var subscription = Subscription.GetSubscriptionByUser(this.User.Path, this.ContentPath);
            if (subscription != null){
                IsSubscriptionNew = false;
            }
            else
            {
                subscription = new Subscription()
                                    {
                                        ContentPath = this.ContentPath,
                                        Frequency = NotificationFrequency.Immediately,
                                        IsActive = true,
                                        UserEmail = this.User.Email,
                                        UserPath = this.User.Path,
                                        UserId = this.User.Id,
                                        UserName = this.User.Name,
                                        Language = "en",
                                        SitePath = currentSite.Path,
                                        SiteUrl = siteUrl
                                    };

                IsSubscriptionNew = true;
            }

            var sct = SN.Content.Create(subscription, subscriptionCtd);
            var rch = sct.ContentHandler as Content.RuntimeContentHandler;
            if (rch != null)
                rch.SetIsNew(IsSubscriptionNew);

            return sct;
        }