Search in sources :

Example 1 with Notification

use of com.willshex.blogwt.shared.api.datatype.Notification in project blogwt by billy1380.

the class NotificationService method addNotification.

@Override
public Notification addNotification(Notification notification) {
    if (notification.created == null) {
        notification.created = new Date();
    }
    if (notification.meta != null) {
        notification.metaKey = Key.create(notification.meta);
    }
    if (notification.target != null) {
        notification.targetKey = Key.create(notification.target);
    }
    if (notification.sender != null) {
        notification.senderKey = Key.create(notification.sender);
    }
    Key<Notification> key = provide().save().entity(notification).now();
    notification.id = Long.valueOf(key.getId());
    return notification;
}
Also used : Date(java.util.Date) Notification(com.willshex.blogwt.shared.api.datatype.Notification)

Example 2 with Notification

use of com.willshex.blogwt.shared.api.datatype.Notification in project blogwt by billy1380.

the class GetNotificationsResponse method fromJson.

@Override
public void fromJson(JsonObject jsonObject) {
    super.fromJson(jsonObject);
    if (jsonObject.has("notifications")) {
        JsonElement jsonNotifications = jsonObject.get("notifications");
        if (jsonNotifications != null) {
            notifications = new ArrayList<Notification>();
            Notification item = null;
            for (int i = 0; i < jsonNotifications.getAsJsonArray().size(); i++) {
                if (jsonNotifications.getAsJsonArray().get(i) != null) {
                    (item = new Notification()).fromJson(jsonNotifications.getAsJsonArray().get(i).getAsJsonObject());
                    notifications.add(item);
                }
            }
        }
    }
    if (jsonObject.has("pager")) {
        JsonElement jsonPager = jsonObject.get("pager");
        if (jsonPager != null) {
            pager = new Pager();
            pager.fromJson(jsonPager.getAsJsonObject());
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) Pager(com.willshex.blogwt.shared.api.Pager) Notification(com.willshex.blogwt.shared.api.datatype.Notification)

Example 3 with Notification

use of com.willshex.blogwt.shared.api.datatype.Notification in project blogwt by billy1380.

the class GetNotificationsActionHandler method handle.

@Override
public void handle(GetNotificationsRequest input, GetNotificationsResponse output) throws Exception {
    ApiValidator.request(input, GetNotificationsRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
    if (input.user == null) {
        input.user = input.session.user;
    } else {
        UserValidator.authorisation(input.session.user, Arrays.asList(PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_NOTIFICATIONS)), "input.session.user");
        input.user = UserValidator.lookup(input.user, "input.user");
    }
    if (input.pager == null) {
        input.pager = PagerHelper.createDefaultPager();
    }
    output.notifications = NotificationServiceProvider.provide().getUserNotifications(input.user, input.pager.start, input.pager.count, NotificationSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
    if (output.notifications != null) {
        for (Notification notification : output.notifications) {
            notification.meta = PersistenceHelper.type(MetaNotification.class, notification.metaKey);
            notification.target = PersistenceHelper.type(User.class, notification.targetKey);
            notification.sender = PersistenceHelper.type(User.class, notification.senderKey);
        }
    }
    output.pager = PagerHelper.moveForward(input.pager);
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification) Notification(com.willshex.blogwt.shared.api.datatype.Notification) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification)

Example 4 with Notification

use of com.willshex.blogwt.shared.api.datatype.Notification in project blogwt by billy1380.

the class NotificationHelper method sendNotification.

public static List<Notification> sendNotification(String code, User user, final Map<String, ?> values) {
    List<Notification> notifications = new ArrayList<>();
    final MetaNotification meta = MetaNotificationServiceProvider.provide().getCodeMetaNotification(code);
    if (meta == null) {
        if (LOG.isLoggable(Level.WARNING)) {
            LOG.warning("No meta notification found for code [" + code + "]");
        }
    } else {
        NotificationSetting setting = NotificationSettingServiceProvider.provide().getMetaUserNotificationSetting(meta, user);
        if (setting == null) {
            setting = NotificationSettingServiceProvider.provide().addNotificationSetting(new NotificationSetting().meta(meta).selected(meta.defaults).user(user));
        }
        String content;
        final Notification template = new Notification().content(content = InflatorHelper.inflate(values, meta.content)).meta(meta).target(user);
        Notification notification;
        String title = "Message from " + PropertyHelper.value(PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.TITLE));
        if (setting.selected == null || setting.selected.size() == 0) {
            if (LOG.isLoggable(Level.INFO)) {
                LOG.info("No notification modes found for [" + code + "] and user [" + user + "] adding template [" + template + "]");
            }
            notifications.add(NotificationServiceProvider.provide().addNotification(template));
        } else {
            for (NotificationModeType mode : setting.selected) {
                notification = JsonableHelper.copy(template, new Notification()).mode(mode);
                switch(mode) {
                    case NotificationModeTypeEmail:
                        EmailHelper.sendEmail(user.email, UserHelper.name(user), title, content, false);
                        break;
                    case NotificationModeTypePush:
                        List<PushToken> tokens = PushTokenServiceProvider.provide().getUserPushTokens(user);
                        Jsonable data = new Jsonable() {

                            @Override
                            public JsonObject toJson() {
                                JsonObject object = super.toJson();
                                object.addProperty("code", meta.code);
                                if (values != null) {
                                    Object value;
                                    DataType slim;
                                    for (String key : values.keySet()) {
                                        value = values.get(key);
                                        if (value instanceof DataType) {
                                            slim = PersistenceHelper.slim(DataType.class, (DataType) value, null);
                                            object.add(key, slim.toJson());
                                        }
                                    }
                                }
                                return object;
                            }
                        };
                        for (PushToken pushToken : tokens) {
                            PushHelper.push(pushToken, meta.name, content, data);
                        }
                        if (LOG.isLoggable(Level.FINE) && tokens.size() == 0) {
                            LOG.fine("Could not push because no tokens found [" + meta.name + "], [" + content + "] payload approximation [" + PushHelper.buildPayload(new PushToken().platform("android").user(user).value("approx-test-token"), new AndroidMessage().title(meta.name).body(content), data) + "]");
                        }
                        break;
                    case NotificationModeTypeSms:
                        break;
                }
                notifications.add(NotificationServiceProvider.provide().addNotification(notification));
            }
        }
    }
    return notifications;
}
Also used : AndroidMessage(com.willshex.blogwt.server.helper.push.AndroidMessage) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification) Jsonable(com.willshex.gson.shared.Jsonable) Notification(com.willshex.blogwt.shared.api.datatype.Notification) MetaNotification(com.willshex.blogwt.shared.api.datatype.MetaNotification) NotificationSetting(com.willshex.blogwt.shared.api.datatype.NotificationSetting) DataType(com.willshex.blogwt.shared.api.datatype.DataType) JsonObject(com.google.gson.JsonObject) NotificationModeType(com.willshex.blogwt.shared.api.datatype.NotificationModeType) PushToken(com.willshex.blogwt.shared.api.datatype.PushToken)

Aggregations

Notification (com.willshex.blogwt.shared.api.datatype.Notification)4 MetaNotification (com.willshex.blogwt.shared.api.datatype.MetaNotification)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 AndroidMessage (com.willshex.blogwt.server.helper.push.AndroidMessage)1 Pager (com.willshex.blogwt.shared.api.Pager)1 DataType (com.willshex.blogwt.shared.api.datatype.DataType)1 NotificationModeType (com.willshex.blogwt.shared.api.datatype.NotificationModeType)1 NotificationSetting (com.willshex.blogwt.shared.api.datatype.NotificationSetting)1 PushToken (com.willshex.blogwt.shared.api.datatype.PushToken)1 User (com.willshex.blogwt.shared.api.datatype.User)1 Jsonable (com.willshex.gson.shared.Jsonable)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1