Search in sources :

Example 6 with UserNotification

use of io.lumeer.api.model.UserNotification in project engine by Lumeer.

the class PusherFacade method removeUserNotification.

public void removeUserNotification(@Observes final RemoveUserNotification removeUserNotification) {
    if (isEnabled()) {
        try {
            UserNotification notification = removeUserNotification.getUserNotification();
            Event event = createEventForRemove(notification.getClass().getSimpleName(), new ResourceId(getAppId(), notification.getId()), notification.getUserId());
            sendNotificationsBatch(Collections.singletonList(event));
        } catch (Exception e) {
            log.log(Level.WARNING, "Unable to send push notification: ", e);
        }
    }
}
Also used : CreateOrUpdateUserNotification(io.lumeer.engine.api.event.CreateOrUpdateUserNotification) RemoveUserNotification(io.lumeer.engine.api.event.RemoveUserNotification) UserNotification(io.lumeer.api.model.UserNotification) OrganizationUserEvent(io.lumeer.engine.api.event.OrganizationUserEvent) ResourceVariableEvent(io.lumeer.engine.api.event.ResourceVariableEvent) SelectionListEvent(io.lumeer.engine.api.event.SelectionListEvent) Event(org.marvec.pusher.data.Event)

Example 7 with UserNotification

use of io.lumeer.api.model.UserNotification in project engine by Lumeer.

the class PusherFacade method createUserNotification.

public void createUserNotification(@Observes final CreateOrUpdateUserNotification createOrUpdateUserNotification) {
    if (isEnabled()) {
        try {
            UserNotification notification = createOrUpdateUserNotification.getUserNotification();
            Event event = createEventForObject(notification, CREATE_EVENT_SUFFIX, notification.getUserId());
            sendNotificationsBatch(Collections.singletonList(event));
        } catch (Exception e) {
            log.log(Level.WARNING, "Unable to send push notification: ", e);
        }
    }
}
Also used : CreateOrUpdateUserNotification(io.lumeer.engine.api.event.CreateOrUpdateUserNotification) RemoveUserNotification(io.lumeer.engine.api.event.RemoveUserNotification) UserNotification(io.lumeer.api.model.UserNotification) OrganizationUserEvent(io.lumeer.engine.api.event.OrganizationUserEvent) ResourceVariableEvent(io.lumeer.engine.api.event.ResourceVariableEvent) SelectionListEvent(io.lumeer.engine.api.event.SelectionListEvent) Event(org.marvec.pusher.data.Event)

Example 8 with UserNotification

use of io.lumeer.api.model.UserNotification in project engine by Lumeer.

the class DelayedActionProcessor method executeActions.

private void executeActions(final List<DelayedAction> actions) {
    // org id -> users
    final Map<String, List<User>> userCache = new HashMap<>();
    clearCache();
    aggregateActions(actions).forEach(action -> {
        final String organizationId = action.getData().getString(DelayedAction.DATA_ORGANIZATION_ID);
        final List<User> allUsers = userCache.computeIfAbsent(organizationId, orgId -> userDao.getAllUsers(orgId));
        // mix in users from actions
        allUsers.addAll(getUsersFromActions(actions, allUsers));
        // id -> user
        final Map<String, User> users = getUsers(allUsers);
        final Map<String, Language> userLanguages = initializeLanguages(users.values());
        // email -> id
        final Map<String, String> userIds = getUserIds(users.values());
        final Language lang = userLanguages.getOrDefault(action.getReceiver(), Language.EN);
        final User receiverUser = userIds.containsKey(action.getReceiver()) ? users.get(userIds.get(action.getReceiver())) : null;
        final Collection collection = checkActionResourceExistsAndFillData(action, receiverUser);
        if (collection != null) {
            // if we do not know anything about the user, make sure to send the notification; otherwise check the user settings
            if (receiverUser == null || isNotificationEnabled(action, receiverUser)) {
                if (action.getNotificationChannel() == NotificationChannel.Email) {
                    final User user = userIds.containsKey(action.getInitiator()) ? users.get(userIds.get(action.getInitiator())) : null;
                    final String sender = user != null ? emailSenderFacade.formatUserReference(user) : "";
                    final String from = user != null ? emailSenderFacade.formatFrom(user) : "";
                    final String recipient = action.getReceiver();
                    final Map<String, Object> additionalData = processData(action.getData(), lang, receiverUser);
                    emailSenderFacade.sendEmailFromTemplate(getEmailTemplate(action), lang, sender, from, recipient, getEmailSubjectPart(action, additionalData, lang), additionalData);
                } else if (action.getNotificationChannel() == NotificationChannel.Internal && userIds.containsKey(action.getReceiver())) {
                    UserNotification notification = createUserNotification(users.get(userIds.get(action.getReceiver())), action, lang);
                    notification = userNotificationDao.createNotification(notification);
                    if (pusherClient != null) {
                        pusherClient.trigger(List.of(createUserNotificationEvent(notification, PusherFacade.CREATE_EVENT_SUFFIX, userIds.get(action.getReceiver()))));
                    }
                }
            }
            // reschedule past due actions
            if (!rescheduleDueDateAction(actions, action, receiverUser, collection)) {
                markActionAsCompleted(actions, action);
            }
        } else {
            markActionAsCompleted(actions, action);
        }
    });
}
Also used : User(io.lumeer.api.model.User) HashMap(java.util.HashMap) UserNotification(io.lumeer.api.model.UserNotification) Language(io.lumeer.api.model.Language) Collection(io.lumeer.api.model.Collection) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

UserNotification (io.lumeer.api.model.UserNotification)8 CreateOrUpdateUserNotification (io.lumeer.engine.api.event.CreateOrUpdateUserNotification)4 RemoveUserNotification (io.lumeer.engine.api.event.RemoveUserNotification)4 List (java.util.List)4 NotificationType (io.lumeer.api.model.NotificationType)3 User (io.lumeer.api.model.User)3 DataDocument (io.lumeer.engine.api.data.DataDocument)3 UserNotificationDao (io.lumeer.storage.api.dao.UserNotificationDao)3 ZonedDateTime (java.time.ZonedDateTime)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 DeleteResult (com.mongodb.client.result.DeleteResult)2 Collection (io.lumeer.api.model.Collection)2 NotificationChannel (io.lumeer.api.model.NotificationChannel)2 Organization (io.lumeer.api.model.Organization)2 Permissions (io.lumeer.api.model.Permissions)2 Project (io.lumeer.api.model.Project)2