using System; using System.Collections.Generic; using System.Linq; using Telligent.Evolution.Extensibility; using Telligent.Evolution.Extensibility.Api.Entities.Version1; using Telligent.Evolution.Extensibility.Api.Version1; using Telligent.Evolution.Extensibility.Content.Version1; namespace Samples { public class MyNotifications : INotificationType { private INotificationController _notificationController; #region IPlugin public string Name { get { return "My Notifications Plugin"; } } public string Description { get { return "This plugin will demo how the INotificationType plugin works"; } } public void Initialize() { Apis.Get().Events.AfterCreate += CommentEvents_AfterCreate; } #endregion #region INotificationType public string NotificationTypeName { get { return "Notification Sample"; } } public string NotificationTypeDescription { get { return "Sends a sample notification."; } } public string NotificationTypeCategory { get { return "Samples"; } } public Guid NotificationTypeId { get { return new Guid("3C0542FD-5AB0-4386-911C-86CCF530BAEE"); } } public bool IsCacheable { get { return true; } } public bool VaryCacheByUser { get { return true; } } public bool CanDeleteNotification(Guid notificationId, int userId) { var notification = Apis.Get().Get(notificationId); return notification != null && notification.UserId == userId; } public string GetMessage(Guid notificationId, string target) { var notification = Apis.Get().Get(notificationId); if (notification == null || notification.ExtendedAttributes == null) { return null; } var commentIdAttribute = notification.ExtendedAttributes.FirstOrDefault(ea => ea.Key == "TargetCommentId"); if (commentIdAttribute == null) { return null; } var commentId = Guid.Parse(commentIdAttribute.Value); var comment = Apis.Get().Get(commentId); if (comment == null) { return null; } var user = Apis.Get().Get(new UsersGetOptions { Id = notification.UserId }); if (user == null) { return null; } return string.Format("The following comment was added by {0}: \"{1}\"", user.DisplayName, comment.Body()); } public string GetTargetUrl(Guid notificationId) { var notification = Apis.Get().Get(notificationId); return notification != null && notification.Content != null ? notification.Content.Url : null; } public void SetController(INotificationController controller) { _notificationController = controller; } #endregion private void CommentEvents_AfterCreate(CommentAfterCreateEventArgs e) { if (e == null || e.Content == null || e.User == null || !e.IsApproved || !e.Content.CreatedByUserId.HasValue || e.CommentTypeId.GetValueOrDefault(Guid.Empty) != Guid.Empty) { return; } var comment = Apis.Get().Get(e.CommentId); if (comment != null && comment.IsApproved) { AddNotifications(e.Content.ContentId, e.Content.ContentTypeId, e.CommentId, e.Content.CreatedByUserId.Value, e.UserId); } } private void AddNotifications(Guid contentId, Guid contentTypeId, Guid commentId, int contentAuthor, int actorId) { var comment = Apis.Get().Get(commentId); if (comment == null) { return; } var attributes = new List { new ExtendedAttribute {Key = "TargetCommentId", Value = comment.CommentId.ToString("N")} }; _notificationController.CreateUpdate(new NotificationCreateUpdateOptions { ContentId = contentId, ContentTypeId = contentTypeId, LastUpdate = DateTime.UtcNow, UserId = contentAuthor, ActorIdToAdd = actorId, ExtendedAttributes = attributes }); } } }