[WIP] Further implementing notifications

This commit is contained in:
Robert von Burg 2024-03-01 16:22:11 +01:00
parent f02b541848
commit 15b2788b9a
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
4 changed files with 27 additions and 4 deletions

View File

@ -20,12 +20,15 @@ public class DefaultNotificationsPolicy extends NotificationsPolicy {
return tx().streamResources(StrolchModelConstants.TYPE_NOTIFICATION).filter(this::isForUser).toList();
}
@Override
public boolean canView(Resource notification) {
return isForAll(notification) || isForRole(notification) || isForGroup(notification);
}
protected boolean isForUser(Resource notification) {
if (!isActive(notification))
return false;
if (isForAll(notification))
return true;
return isForRole(notification) || isForGroup(notification);
return isForAll(notification) || isForRole(notification) || isForGroup(notification);
}
protected boolean isActive(Resource notification) {

View File

@ -18,6 +18,7 @@ public abstract class NotificationsPolicy extends StrolchPolicy {
}
public abstract List<Resource> findUserNotifications();
public abstract boolean canView(Resource notification);
public static NotificationsPolicy getDefaultPolicy(StrolchTransaction tx) {
PolicyDef defaultDef = getKeyPolicy(NotificationsPolicy.class, POLICY_DEFAULT);

View File

@ -86,6 +86,7 @@ public class StrolchModelConstants {
public static final String TYPE_NOTIFICATION = "Notification";
public static final String TYPE_VISIBILITY = "Visibility";
public static final String TYPE_TEXT = "Text";
public static final String TYPE_LOCATION = "Location";
public static final String RES_CONFIGURATION = "configuration";
@ -110,6 +111,7 @@ public class StrolchModelConstants {
public static final String PARAM_FOR_ALL = "forAll";
public static final String PARAM_ROLES = "roles";
public static final String PARAM_LOCATIONS = "locations";
public static final String PARAM_LOCATION_NAMES = "locationNames";
public static final String PARAM_LANGUAGES = "languages";
public static final String PARAM_GROUPS = "groups";

View File

@ -15,6 +15,7 @@
*/
package li.strolch.rest.endpoint;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import jakarta.servlet.http.HttpServletRequest;
@ -49,6 +50,7 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.Optional.ofNullable;
import static li.strolch.model.StrolchModelConstants.*;
@ -122,7 +124,8 @@ public class NotificationResource {
.withoutPolicies()
.withoutVersion()
.withoutStateVariables()
.flatBagsByType(TYPE_TEXT, TYPE_VISIBILITY);
.flatBagsByType(TYPE_TEXT, TYPE_VISIBILITY)
.resourceHook((notification, notificationJ) -> addLocationNames(notification, notificationJ, tx));
return toResponse(DATA, tx.streamResources(TYPE_NOTIFICATION).map(a -> a.accept(visitor)).toList());
} catch (Exception e) {
logger.error(e.getMessage(), e);
@ -130,6 +133,20 @@ public class NotificationResource {
}
}
private static void addLocationNames(Resource notification, JsonObject notificationJ, StrolchTransaction tx) {
if (!notification.hasParameter(BAG_VISIBILITY, PARAM_LOCATIONS))
return;
JsonArray locationNamesJ = notification
.getStringList(BAG_VISIBILITY, PARAM_LOCATIONS)
.stream()
.map(locationId -> {
Resource location = tx.getResourceBy(TYPE_LOCATION, locationId);
return location == null ? locationId : location.getName();
})
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
notificationJ.get(BAG_VISIBILITY).getAsJsonObject().add(PARAM_LOCATION_NAMES, locationNamesJ);
}
private static Function<Resource, JsonObject> notificationToJson(StrolchAgent agent, Certificate cert) {
return notification -> {
JsonObject notificationJ = new JsonObject();