using System; using System.Collections.Generic; using Abp.Domain.Entities; using Abp.Threading; namespace Abp.Notifications { /// /// Extension methods for /// , /// , /// . /// public static class NotificationExtensions { #region INotificationSubscriptionManager /// /// Subscribes to a notification. /// /// Notification subscription manager /// User. /// Name of the notification. /// entity identifier public static void Subscribe(this INotificationSubscriptionManager notificationSubscriptionManager, UserIdentifier user, string notificationName, EntityIdentifier entityIdentifier = null) { AsyncHelper.RunSync(() => notificationSubscriptionManager.SubscribeAsync(user, notificationName, entityIdentifier)); } /// /// Subscribes to all available notifications for given user. /// It does not subscribe entity related notifications. /// /// Notification subscription manager /// User. public static void SubscribeToAllAvailableNotifications(this INotificationSubscriptionManager notificationSubscriptionManager, UserIdentifier user) { AsyncHelper.RunSync(() => notificationSubscriptionManager.SubscribeToAllAvailableNotificationsAsync(user)); } /// /// Unsubscribes from a notification. /// /// Notification subscription manager /// User. /// Name of the notification. /// entity identifier public static void Unsubscribe(this INotificationSubscriptionManager notificationSubscriptionManager, UserIdentifier user, string notificationName, EntityIdentifier entityIdentifier = null) { AsyncHelper.RunSync(() => notificationSubscriptionManager.UnsubscribeAsync(user, notificationName, entityIdentifier)); } /// /// Gets all subscribtions for given notification. /// TODO: Can work only for single database approach! /// /// Notification subscription manager /// Name of the notification. /// entity identifier public static List GetSubscriptions(this INotificationSubscriptionManager notificationSubscriptionManager, string notificationName, EntityIdentifier entityIdentifier = null) { return AsyncHelper.RunSync(() => notificationSubscriptionManager.GetSubscriptionsAsync(notificationName, entityIdentifier)); } /// /// Gets all subscribtions for given notification. /// /// Notification subscription manager /// Tenant id. Null for the host. /// Name of the notification. /// entity identifier public static List GetSubscriptions(this INotificationSubscriptionManager notificationSubscriptionManager, int? tenantId, string notificationName, EntityIdentifier entityIdentifier = null) { return AsyncHelper.RunSync(() => notificationSubscriptionManager.GetSubscriptionsAsync(tenantId, notificationName, entityIdentifier)); } /// /// Gets subscribed notifications for a user. /// /// Notification subscription manager /// User. public static List GetSubscribedNotifications(this INotificationSubscriptionManager notificationSubscriptionManager, UserIdentifier user) { return AsyncHelper.RunSync(() => notificationSubscriptionManager.GetSubscribedNotificationsAsync(user)); } /// /// Checks if a user subscribed for a notification. /// /// Notification subscription manager /// User. /// Name of the notification. /// entity identifier public static bool IsSubscribed(this INotificationSubscriptionManager notificationSubscriptionManager, UserIdentifier user, string notificationName, EntityIdentifier entityIdentifier = null) { return AsyncHelper.RunSync(() => notificationSubscriptionManager.IsSubscribedAsync(user, notificationName, entityIdentifier)); } #endregion #region INotificationPublisher /// /// Publishes a new notification. /// /// Notification publisher /// Unique notification name /// Notification data (optional) /// The entity identifier if this notification is related to an entity /// Notification severity /// Target user id(s). Used to send notification to specific user(s). If this is null/empty, the notification is sent to all subscribed users public static void Publish(this INotificationPublisher notificationPublisher, string notificationName, NotificationData data = null, EntityIdentifier entityIdentifier = null, NotificationSeverity severity = NotificationSeverity.Info, UserIdentifier[] userIds = null) { AsyncHelper.RunSync(() => notificationPublisher.PublishAsync(notificationName, data, entityIdentifier, severity, userIds)); } #endregion #region IUserNotificationManager /// /// Gets notifications for a user. /// /// User notificaiton manager /// User. /// State /// Skip count. /// Maximum result count. public static List GetUserNotifications(this IUserNotificationManager userNotificationManager, UserIdentifier user, UserNotificationState? state = null, int skipCount = 0, int maxResultCount = int.MaxValue) { return AsyncHelper.RunSync(() => userNotificationManager.GetUserNotificationsAsync(user, state, skipCount: skipCount, maxResultCount: maxResultCount)); } /// /// Gets user notification count. /// /// User notificaiton manager /// User. /// State. public static int GetUserNotificationCount(this IUserNotificationManager userNotificationManager, UserIdentifier user, UserNotificationState? state = null) { return AsyncHelper.RunSync(() => userNotificationManager.GetUserNotificationCountAsync(user, state)); } /// /// Gets a user notification by given id. /// /// User notificaiton manager /// Tenant Id /// The user notification id. public static UserNotification GetUserNotification(this IUserNotificationManager userNotificationManager, int? tenantId, Guid userNotificationId) { return AsyncHelper.RunSync(() => userNotificationManager.GetUserNotificationAsync(tenantId, userNotificationId)); } /// /// Updates a user notification state. /// /// User notificaiton manager /// Tenant Id /// The user notification id. /// New state. public static void UpdateUserNotificationState(this IUserNotificationManager userNotificationManager, int? tenantId, Guid userNotificationId, UserNotificationState state) { AsyncHelper.RunSync(() => userNotificationManager.UpdateUserNotificationStateAsync(tenantId, userNotificationId, state)); } /// /// Updates all notification states for a user. /// /// User notificaiton manager /// User. /// New state. public static void UpdateAllUserNotificationStates(this IUserNotificationManager userNotificationManager, UserIdentifier user, UserNotificationState state) { AsyncHelper.RunSync(() => userNotificationManager.UpdateAllUserNotificationStatesAsync(user, state)); } /// /// Deletes a user notification. /// /// User notificaiton manager /// Tenant Id /// The user notification id. public static void DeleteUserNotification(this IUserNotificationManager userNotificationManager, int? tenantId, Guid userNotificationId) { AsyncHelper.RunSync(() => userNotificationManager.DeleteUserNotificationAsync(tenantId, userNotificationId)); } /// /// Deletes all notifications of a user. /// /// User notificaiton manager /// The user id. public static void DeleteAllUserNotifications(this IUserNotificationManager userNotificationManager, UserIdentifier user) { AsyncHelper.RunSync(() => userNotificationManager.DeleteAllUserNotificationsAsync(user)); } #endregion } }