diff options
author | Anton Tananaev <anton@traccar.org> | 2024-02-24 17:23:53 -0800 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2024-02-24 17:23:53 -0800 |
commit | 9e0c4c5504df985f7b0292042be9d58253450b10 (patch) | |
tree | a16c059da8b09f70cdecce54b56e1633bb7ecf87 /src/main/java/org | |
parent | b37c5aa05bdd903af998435e073638f42ae9e8d1 (diff) | |
download | trackermap-server-9e0c4c5504df985f7b0292042be9d58253450b10.tar.gz trackermap-server-9e0c4c5504df985f7b0292042be9d58253450b10.tar.bz2 trackermap-server-9e0c4c5504df985f7b0292042be9d58253450b10.zip |
Add API to send messages
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/traccar/api/resource/NotificationResource.java | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/src/main/java/org/traccar/api/resource/NotificationResource.java b/src/main/java/org/traccar/api/resource/NotificationResource.java index 2a209efb6..43dc1fa98 100644 --- a/src/main/java/org/traccar/api/resource/NotificationResource.java +++ b/src/main/java/org/traccar/api/resource/NotificationResource.java @@ -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 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.core.MediaType; -import jakarta.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; @@ -97,4 +103,26 @@ public class NotificationResource extends ExtendedObjectResource<Notification> { 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(); + } + } |