Search in sources :

Example 1 with NotificationData

use of com.pratilipi.data.client.NotificationData in project pratilipi by Pratilipi.

the class NotificationDataUtil method updateFirebaseDb.

public static void updateFirebaseDb(final Long userId, List<Notification> notifList, Async async) throws UnexpectedServerException {
    List<NotificationData> notifDataList = createNotificationDataList(notifList, null, true);
    final List<Long> notifIdListToAdd = new LinkedList<>();
    final List<Long> notifIdListToRemove = new LinkedList<>();
    for (NotificationData notifData : notifDataList) {
        if (notifData.getMessage() != null && notifData.getState() == NotificationState.UNREAD)
            notifIdListToAdd.add(notifData.getId());
        else
            notifIdListToRemove.add(notifData.getId());
    }
    FirebaseApi.updateUserNotificationData(userId, notifIdListToAdd, notifIdListToRemove, async);
}
Also used : LinkedList(java.util.LinkedList) NotificationData(com.pratilipi.data.client.NotificationData)

Example 2 with NotificationData

use of com.pratilipi.data.client.NotificationData in project pratilipi by Pratilipi.

the class NotificationDataUtil method sendFcm.

public static void sendFcm(Long notifId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Notification notif = dataAccessor.getNotification(notifId);
    if (!notif.isFcmPending())
        return;
    if (notif.getState() == NotificationState.UNREAD) {
        List<Notification> notifList = new ArrayList<>(1);
        notifList.add(notif);
        // TODO: Implement createNotificationData
        NotificationData notifData = createNotificationDataList(notifList, null, true).get(0);
        if (notifData.getMessage() != null) {
            // TODO: Cancel previously sent FCM if notifData.getMessage() == null
            List<String> fcmTokenList = dataAccessor.getFcmTokenList(notifData.getUserId());
            if (fcmTokenList.size() != 0) {
                String fcmResponse = FirebaseApi.sendCloudMessage2(fcmTokenList, notifData.getMessage(), notifData.getId().toString(), notifData.getNotificationType().getAndroidHandler(), notifData.getSourceId(), notifData.getSourceImageUrl(), notifData.getDisplayImageUrl());
                if (notif.getFcmResponse() == null)
                    notif.setFcmResponse(fcmResponse);
                else
                    notif.setFcmResponse(notif.getFcmResponse() + "\n" + fcmResponse);
            }
        }
    }
    notif.setFcmPending(false);
    notif = dataAccessor.createOrUpdateNotification(notif);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Notification(com.pratilipi.data.type.Notification) NotificationData(com.pratilipi.data.client.NotificationData)

Example 3 with NotificationData

use of com.pratilipi.data.client.NotificationData in project pratilipi by Pratilipi.

the class NotificationDataUtil method createNotificationDataList.

public static List<NotificationData> createNotificationDataList(List<Notification> notificationList, Language language, boolean plainText) throws UnexpectedServerException {
    // Pre-fetching required User and Pratilipi Entities
    List<Long> userIdList = new LinkedList<>();
    List<Long> pratilipiIdList = new LinkedList<>();
    for (Notification notification : notificationList) {
        userIdList.add(notification.getUserId());
        if (notification.getType() == NotificationType.PRATILIPI) {
            pratilipiIdList.add(notification.getSourceIdLong());
        } else if (notification.getType() == NotificationType.PRATILIPI_PUBLISHED_FOLLOWER) {
            pratilipiIdList.add(notification.getSourceIdLong());
        } else if (notification.getType() == NotificationType.AUTHOR_FOLLOW) {
            if (notification.getDataIds().size() <= 3)
                userIdList.addAll(notification.getDataIds());
            else
                userIdList.addAll(notification.getDataIds().subList(notification.getDataIds().size() - 3, notification.getDataIds().size()));
        }
    }
    Map<Long, UserData> users = UserDataUtil.createUserDataList(userIdList, true);
    List<PratilipiData> pratilipiDataList = PratilipiDataUtil.createPratilipiDataList(pratilipiIdList, true);
    Map<Long, PratilipiData> pratilipis = new HashMap<>(pratilipiDataList.size());
    for (PratilipiData pratilipiData : pratilipiDataList) pratilipis.put(pratilipiData.getId(), pratilipiData);
    // Creating Notification Data list
    List<NotificationData> notificationDataList = new ArrayList<>(notificationList.size());
    for (Notification notification : notificationList) {
        NotificationData notificationData = new NotificationData(notification.getId());
        notificationData.setUserId(notification.getUserId());
        Language notificationLanguage = language == null ? users.get(notification.getUserId()).getLanguage() : language;
        if (notificationLanguage == null)
            notificationLanguage = Language.ENGLISH;
        if (notification.getType() == NotificationType.PRATILIPI) {
            String createdBy = notification.getCreatedBy();
            if (createdBy.startsWith("BATCH_PROCESS::")) {
                Long batchProcessId = Long.parseLong(createdBy.substring(15));
                BatchProcess batchProcess = DataAccessorFactory.getDataAccessor().getBatchProcess(batchProcessId);
                JsonObject execDoc = new Gson().fromJson(batchProcess.getExecDoc(), JsonElement.class).getAsJsonObject();
                notificationData.setMessage(execDoc.get("message").getAsString());
            }
            PratilipiData pratilipiData = pratilipis.get(notification.getSourceIdLong());
            notificationData.setSourceUrl(pratilipiData.getPageUrl() + "?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
            notificationData.setSourceImageUrl(pratilipiData.getCoverImageUrl());
        } else if (notification.getType() == NotificationType.PRATILIPI_PUBLISHED_FOLLOWER) {
            PratilipiData pratilipiData = pratilipis.get(notification.getSourceIdLong());
            if (pratilipiData.getState() == PratilipiState.PUBLISHED) {
                notificationData.setMessage(createNotificationMessage(pratilipiData, notificationLanguage, plainText));
                notificationData.setSourceUrl(pratilipiData.getPageUrl() + "?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
                notificationData.setSourceImageUrl(pratilipiData.getCoverImageUrl());
                notificationData.setDisplayImageUrl(pratilipiData.getAuthor().getImageUrl());
            }
        } else if (notification.getType() == NotificationType.AUTHOR_FOLLOW) {
            notificationData.setMessage(createNotificationMessage(notification.getDataIds(), users, notificationLanguage, plainText));
            notificationData.setSourceUrl("/followers?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
            if (notification.getDataIds().size() != 0)
                notificationData.setDisplayImageUrl(users.get(notification.getDataIds().get(notification.getDataIds().size() - 1)).getProfileImageUrl());
        }
        notificationData.setSourceId(notification.getSourceId());
        notificationData.setState(notification.getState());
        notificationData.setNotificationType(notification.getType());
        notificationData.setLastUpdatedDate(notification.getLastUpdated());
        notificationDataList.add(notificationData);
    }
    return notificationDataList;
}
Also used : UserData(com.pratilipi.data.client.UserData) HashMap(java.util.HashMap) BatchProcess(com.pratilipi.data.type.BatchProcess) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Notification(com.pratilipi.data.type.Notification) Language(com.pratilipi.common.type.Language) PratilipiData(com.pratilipi.data.client.PratilipiData) JsonElement(com.google.gson.JsonElement) NotificationData(com.pratilipi.data.client.NotificationData)

Aggregations

NotificationData (com.pratilipi.data.client.NotificationData)3 Notification (com.pratilipi.data.type.Notification)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Language (com.pratilipi.common.type.Language)1 DataAccessor (com.pratilipi.data.DataAccessor)1 PratilipiData (com.pratilipi.data.client.PratilipiData)1 UserData (com.pratilipi.data.client.UserData)1 BatchProcess (com.pratilipi.data.type.BatchProcess)1 HashMap (java.util.HashMap)1