aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/api/resource/NotificationResource.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/api/resource/NotificationResource.java')
-rw-r--r--src/main/java/org/traccar/api/resource/NotificationResource.java56
1 files changed, 42 insertions, 14 deletions
diff --git a/src/main/java/org/traccar/api/resource/NotificationResource.java b/src/main/java/org/traccar/api/resource/NotificationResource.java
index 2e4ad12f3..43dc1fa98 100644
--- a/src/main/java/org/traccar/api/resource/NotificationResource.java
+++ b/src/main/java/org/traccar/api/resource/NotificationResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,16 @@
*/
package org.traccar.api.resource;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.PathParam;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.QueryParam;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.api.ExtendedObjectResource;
@@ -23,20 +33,16 @@ import org.traccar.model.Notification;
import org.traccar.model.Typed;
import org.traccar.model.User;
import org.traccar.notification.MessageException;
+import org.traccar.notification.NotificationMessage;
import org.traccar.notification.NotificatorManager;
import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
-import javax.inject.Inject;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
@@ -80,10 +86,10 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
@POST
@Path("test")
- public Response testMessage() throws MessageException, InterruptedException, StorageException {
+ public Response testMessage() throws MessageException, StorageException {
User user = permissionsService.getUser(getUserId());
for (Typed method : notificatorManager.getAllNotificatorTypes()) {
- notificatorManager.getNotificator(method.getType()).send(user, new Event("test", 0), null);
+ notificatorManager.getNotificator(method.getType()).send(null, user, new Event("test", 0), null);
}
return Response.noContent().build();
}
@@ -91,9 +97,31 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
@POST
@Path("test/{notificator}")
public Response testMessage(@PathParam("notificator") String notificator)
- throws MessageException, InterruptedException, StorageException {
+ throws MessageException, StorageException {
User user = permissionsService.getUser(getUserId());
- notificatorManager.getNotificator(notificator).send(user, new Event("test", 0), null);
+ notificatorManager.getNotificator(notificator).send(null, user, new Event("test", 0), null);
+ return Response.noContent().build();
+ }
+
+ @POST
+ @Path("send/{notificator}")
+ public Response sendMessage(
+ @PathParam("notificator") String notificator, @QueryParam("userId") List<Long> userIds,
+ NotificationMessage message) throws MessageException, StorageException {
+ permissionsService.checkAdmin(getUserId());
+ List<User> users;
+ if (userIds.isEmpty()) {
+ users = storage.getObjects(User.class, new Request(new Columns.All()));
+ } else {
+ users = new ArrayList<>();
+ for (long userId : userIds) {
+ users.add(storage.getObject(
+ User.class, new Request(new Columns.All(), new Condition.Equals("id", userId))));
+ }
+ }
+ for (User user : users) {
+ notificatorManager.getNotificator(notificator).send(user, message, null, null);
+ }
return Response.noContent().build();
}