Browse Source

优化通知

zhou-hao 5 years ago
parent
commit
eafe167e4a

+ 2 - 0
jetlinks-manager/notify-manager/src/main/java/org/jetlinks/community/notify/manager/service/NotificationService.java

@@ -1,6 +1,7 @@
 package org.jetlinks.community.notify.manager.service;
 
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
 import org.hswebframework.web.api.crud.entity.QueryParamEntity;
 import org.hswebframework.web.crud.service.GenericReactiveCrudService;
 import org.jetlinks.core.utils.FluxUtils;
@@ -47,6 +48,7 @@ public class NotificationService extends GenericReactiveCrudService<Notification
     public Flux<NotificationEntity> findAndMarkRead(QueryParamEntity query) {
         return query(query)
             .collectList()
+            .filter(CollectionUtils::isNotEmpty)
             .flatMapMany(list -> createUpdate()
                 .set(NotificationEntity::getState, NotificationState.read)
                 .where()

+ 19 - 0
jetlinks-manager/notify-manager/src/main/java/org/jetlinks/community/notify/manager/web/NotificationController.java

@@ -2,11 +2,13 @@ package org.jetlinks.community.notify.manager.web;
 
 import lombok.Getter;
 import lombok.Setter;
+import org.apache.commons.collections4.CollectionUtils;
 import org.hswebframework.web.api.crud.entity.PagerResult;
 import org.hswebframework.web.api.crud.entity.QueryParamEntity;
 import org.hswebframework.web.authorization.Authentication;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.exception.UnAuthorizedException;
+import org.jetlinks.community.notify.manager.enums.NotificationState;
 import org.jetlinks.core.metadata.ConfigMetadata;
 import org.jetlinks.community.notify.manager.entity.NotificationEntity;
 import org.jetlinks.community.notify.manager.entity.NotifySubscriberEntity;
@@ -139,6 +141,23 @@ public class NotificationController {
             );
     }
 
+    @PostMapping("/_{state}")
+    @Authorize(ignore = true)
+    public Mono<Integer> readNotification(@RequestBody Mono<List<String>> idList,
+                                          @PathVariable NotificationState state) {
+        return Authentication
+            .currentReactive()
+            .switchIfEmpty(Mono.error(UnAuthorizedException::new))
+            .flatMap(auth -> idList
+                .filter(CollectionUtils::isNotEmpty)
+                .flatMap(list-> notificationService.createUpdate()
+                    .set(NotificationEntity::getState,state)
+                    .where(NotificationEntity::getSubscriberType, "user")
+                    .and(NotificationEntity::getSubscriber, auth.getUser().getId())
+                    .in(NotificationEntity::getId, list)
+                    .execute())
+            );
+    }
 
     @Getter
     @Setter