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; using Telligent.Evolution.Extensibility.Email.Version1; namespace Samples { public class SampleNotificationType : INotificationType, IEmailNotificationType, IScriptedEmail { private INotificationController _notificationController; #region IPlugin public string Name => "Sample Notification"; public string Description => "This plugin demonstrates the INotificationType plugin type"; public void Initialize() { Apis.Get().Events.AfterCreate += CommentEvents_AfterCreate; } #endregion #region INotificationType public string NotificationTypeName => "Sample"; public string NotificationTypeDescription => "Sends a sample notification."; public string NotificationTypeCategory => "Samples"; public Guid NotificationTypeId => new Guid("3C0542FD-5AB0-4386-911C-86CCF530BAEE"); public bool IsCacheable => true; public bool VaryCacheByUser => 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 $"The following comment was added by {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 }); } #region IEmailNotificationType public string Get(EmailTarget target, Notification notification) { 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 _scriptedEmailController.Render(target, new ScriptedEmailRenderOptions { Context = new Dictionary { { "Content", notification.Content }, { "Comment", comment } }, ApplicationId = notification?.Content?.Application?.ApplicationId, ApplicationTypeId = notification?.Content?.Application?.ApplicationTypeId }); } #endregion #region IScriptedEmail IScriptedEmailController _scriptedEmailController; public Guid ScriptedEmailId => new Guid("efed29fa-e94e-41f4-b36d-74f9f1df29a0"); public void SetController(IScriptedEmailController controller) => _scriptedEmailController = controller; public IDictionary ContextualData => new Dictionary { { "Content", typeof(Content) }, { "Comment", typeof(Comment) } }; public IDictionary GetSampleContext() => new Dictionary { { "Content", null }, { "Comment", null } }; #endregion } }