public ProblemController(ProblemAppService problemAppService, ArticleController articleController, NotificationSubscriptionManager notificationSubscriptionManager, NotificationPublisher notificationPublisher) { _problemAppService = problemAppService; _articleController = articleController; _notificationSubscriptionManager = notificationSubscriptionManager; _notificationPublisher = notificationPublisher; }
public ArticleController(ArticleAppService articleAppService, CommentManager commentManager, NotificationPublisher notificationPublisher, NotificationSubscriptionManager notificationSubscriptionManager) { _articleAppService = articleAppService; _commentManager = commentManager; _notificationPublisher = notificationPublisher; _notificationSubscriptionManager = notificationSubscriptionManager; }
/// <summary> /// 指定提供者发布通知 /// </summary> /// <param name="providers">提供者列表</param> /// <param name="notificationInfo">通知信息</param> /// <returns></returns> protected async Task PublishFromProvidersAsync( IEnumerable <INotificationPublishProvider> providers, IEnumerable <UserIdentifier> users, NotificationInfo notificationInfo) { // 检查是够已订阅消息 Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}"); List <NotificationSubscriptionInfo> userSubscriptions; if (users == null) { // 获取用户订阅列表 userSubscriptions = await NotificationSubscriptionManager .GetUserSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name); } else { // 过滤未订阅的用户 userSubscriptions = await NotificationSubscriptionManager .GetUsersSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name, users); } users = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName)); if (users.Count() > 0) { // 持久化用户通知 Logger.LogDebug($"Persistent user notifications {notificationInfo.Name}"); await NotificationStore .InsertUserNotificationsAsync( notificationInfo, users.Select(u => u.UserId)); // 2020-11-02 fix bug, 多个发送提供者处于同一个工作单元之下,不能把删除用户订阅写入到单个通知提供者完成事件中 // 而且为了确保一致性,删除订阅移动到发布通知之前 if (notificationInfo.Lifetime == NotificationLifetime.OnlyOne) { // 一次性通知在发送完成后就取消用户订阅 await NotificationStore .DeleteUserSubscriptionAsync( notificationInfo.TenantId, users, notificationInfo.Name); } // 发布通知 foreach (var provider in providers) { await PublishAsync(provider, notificationInfo, users); } } }
/// <summary> /// 指定提供者发布通知 /// </summary> /// <param name="providers">提供者列表</param> /// <param name="notificationInfo">通知信息</param> /// <returns></returns> protected async Task PublishFromProvidersAsync(IEnumerable <INotificationPublishProvider> providers, NotificationInfo notificationInfo) { Logger.LogDebug($"Persistent notification {notificationInfo.Name}"); // 持久化通知 await NotificationStore.InsertNotificationAsync(notificationInfo); // TODO: 某些情况下,不能直接在服务内订阅消息,目前只能通过将订阅内容放进消息内部,需要重构通知系统设计了 if (notificationInfo.Data.HasUserNotification(out Guid userId, out string userName)) { await NotificationSubscriptionManager.SubscribeAsync(notificationInfo.TenantId, new UserIdentifier(userId, userName), notificationInfo.Name); } Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}"); // 获取用户订阅列表 var userSubscriptions = await NotificationSubscriptionManager.GetSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name); Logger.LogDebug($"Persistent user notifications {notificationInfo.Name}"); // 持久化用户通知 var subscriptionUserIdentifiers = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName)); await NotificationStore.InsertUserNotificationsAsync(notificationInfo, subscriptionUserIdentifiers.Select(u => u.UserId)); // 发布通知 foreach (var provider in providers) { await PublishAsync(provider, notificationInfo, subscriptionUserIdentifiers); } if (notificationInfo.Lifetime == NotificationLifetime.OnlyOne) { // 一次性通知在发送完成后就取消用户订阅 await NotificationStore.DeleteAllUserSubscriptionAsync(notificationInfo.TenantId, notificationInfo.Name); } }
public override async Task PublishAsync(NotificationInfo notification, IEnumerable <UserIdentifier> identifiers) { // step1 默认微信openid绑定的就是username, // 如果不是,需要自行处理openid获取逻辑 // step2 调用微信消息推送接口 // 微信不支持推送到所有用户,需要获取订阅列表再发送 // 在小程序里用户订阅消息后通过 api/subscribes/subscribe 接口订阅对应模板消息 if (identifiers == null) { var userSubscriptions = await NotificationSubscriptionManager .GetSubscriptionsAsync(notification.TenantId, notification.Name); identifiers = userSubscriptions .Select(us => new UserIdentifier(us.UserId, us.UserName)); } foreach (var identifier in identifiers) { await SendWeChatTemplateMessagAsync(notification, identifier); } }