/// <summary> /// Set the note as Watched or Unwatched by the current person /// </summary> /// <param name="noteId">The note identifier.</param> /// <param name="watching">if set to <c>true</c> [watching].</param> private void WatchNote(int?noteId, bool watching) { var rockPage = this.Page as RockPage; if (rockPage != null) { var currentPerson = rockPage.CurrentPerson; var rockContext = new RockContext(); var noteService = new NoteService(rockContext); var noteWatchService = new NoteWatchService(rockContext); if (noteId.HasValue) { var note = noteService.Get(noteId.Value); if (note != null && note.IsAuthorized(Authorization.VIEW, currentPerson)) { var noteWatch = noteWatchService.Queryable().Where(a => a.NoteId == noteId.Value && a.WatcherPersonAlias.PersonId == currentPerson.Id).FirstOrDefault(); if (noteWatch == null) { noteWatch = new NoteWatch { NoteId = noteId.Value, WatcherPersonAliasId = rockPage.CurrentPersonAliasId }; noteWatchService.Add(noteWatch); } noteWatch.IsWatching = watching; rockContext.SaveChanges(); } } } }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { RockContext rockContext = new RockContext(); NoteWatchService noteWatchService = new NoteWatchService(rockContext); var qry = noteWatchService.Queryable().Include(a => a.WatcherPersonAlias.Person).Include(a => a.WatcherGroup); Guid?blockEntityTypeGuid = this.GetAttributeValue("EntityType").AsGuidOrNull(); Guid?blockNoteTypeGuid = this.GetAttributeValue("NoteType").AsGuidOrNull(); if (blockNoteTypeGuid.HasValue) { // if a NoteType was specified in block settings, only list note watches for the specified note type int noteTypeId = EntityTypeCache.Get(blockNoteTypeGuid.Value).Id; qry = qry.Where(a => a.NoteTypeId.HasValue && a.NoteTypeId == noteTypeId); } else if (blockEntityTypeGuid.HasValue) { // if an EntityType was specific in block settings, only list note watches for the specified entity type (or for NoteTypes of the specified EntityType) int entityTypeId = EntityTypeCache.Get(blockEntityTypeGuid.Value).Id; qry = qry.Where(a => (a.EntityTypeId.HasValue && a.EntityTypeId.Value == entityTypeId) || (a.NoteTypeId.HasValue && a.NoteType.EntityTypeId == entityTypeId)); } var contextPerson = ContextEntity <Person>(); var contextGroup = ContextEntity <Group>(); if (contextPerson != null) { // if there is a Person context, only list note watches that where the watcher is the person context qry = qry.Where(a => a.WatcherPersonAliasId.HasValue && a.WatcherPersonAlias.PersonId == contextPerson.Id); } else if (contextGroup != null) { // if there is a Group context, only list note watches that where the watcher is the group context qry = qry.Where(a => a.WatcherGroupId.HasValue && a.WatcherGroupId == contextGroup.Id); } var sortProperty = gList.SortProperty; if (sortProperty != null) { qry = qry.Sort(sortProperty); } else { qry = qry.OrderBy(d => d.EntityType.Name).ThenBy(a => a.NoteType.Name); } gList.SetLinqDataSource(qry); gList.DataBind(); }
/// <summary> /// Sends the note watch notifications. /// </summary> /// <param name="context">The context.</param> private List <string> SendNoteWatchNotifications(IJobExecutionContext context) { var errors = new List <string>(); List <int> noteIdsToProcessNoteWatchesList = new List <int>(); using (var rockContext = new RockContext()) { var noteService = new NoteService(rockContext); var noteWatchService = new NoteWatchService(rockContext); var noteWatchQuery = noteWatchService.Queryable(); if (!noteWatchQuery.Any()) { // there aren't any note watches, so there is nothing to do return(errors); } // get all notes that haven't processed notifications yet var notesToNotifyQuery = noteService.Queryable().Where(a => a.NotificationsSent == false && a.NoteType.AllowsWatching == true && a.EditedDateTime > _cutoffNoteEditDateTime); // limit to notes that don't require approval or are approved notesToNotifyQuery = notesToNotifyQuery.Where(a => a.NoteType.RequiresApprovals == false || a.ApprovalStatus == NoteApprovalStatus.Approved); if (!notesToNotifyQuery.Any()) { // there aren't any notes that haven't had notifications processed yet return(errors); } noteIdsToProcessNoteWatchesList = notesToNotifyQuery.Select(a => a.Id).ToList(); } // make a list of notifications to send to each personId Dictionary <int, NoteWatchPersonToNotifyList> personNotificationDigestList = new Dictionary <int, NoteWatchPersonToNotifyList>(); using (var rockContext = new RockContext()) { foreach (int noteId in noteIdsToProcessNoteWatchesList) { this.UpdateNoteWatchNotificationDigest(personNotificationDigestList, rockContext, noteId); } // Send NoteWatch notifications if (personNotificationDigestList.Any()) { foreach (var personNotificationDigest in personNotificationDigestList) { var recipients = new List <RecipientData>(); Person personToNotify = personNotificationDigest.Value.Person; List <Note> noteList = personNotificationDigest.Value.Select(a => a.Note).OrderBy(a => a.EditedDateTime).ToList(); // make sure a person doesn't get a notification on a note that they wrote noteList = noteList.Where(a => a.EditedByPersonAlias?.PersonId != personToNotify.Id).ToList(); if (!string.IsNullOrEmpty(personToNotify.Email) && personToNotify.IsEmailActive && personToNotify.EmailPreference != EmailPreference.DoNotEmail && noteList.Any()) { var mergeFields = new Dictionary <string, object>(_defaultMergeFields); mergeFields.Add("Person", personToNotify); mergeFields.Add("NoteList", noteList); recipients.Add(new RecipientData(personToNotify.Email, mergeFields)); } if (_noteWatchNotificationEmailGuid.HasValue) { var emailMessage = new RockEmailMessage(_noteWatchNotificationEmailGuid.Value); emailMessage.SetRecipients(recipients); emailMessage.Send(out errors); _noteWatchNotificationsSent += recipients.Count(); } } } } using (var rockUpdateContext = new RockContext()) { var notesToMarkNotified = new NoteService(rockUpdateContext).Queryable().Where(a => noteIdsToProcessNoteWatchesList.Contains(a.Id)); // use BulkUpdate to mark all the notes that we processed to NotificationsSent = true rockUpdateContext.BulkUpdate(notesToMarkNotified, n => new Note { NotificationsSent = true }); } return(errors); }