Search in sources :

Example 6 with Notification

use of com.pratilipi.data.type.Notification in project pratilipi by Pratilipi.

the class NotificationDataUtil method saveNotificationState.

public static void saveNotificationState(Map<Long, NotificationState> notificationStates) throws InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Map<Long, Notification> notifications = dataAccessor.getNotifications(notificationStates.keySet());
    for (Map.Entry<Long, NotificationState> entry : notificationStates.entrySet()) {
        Notification notification = notifications.get(entry.getKey());
        if (notification.getState() == entry.getValue())
            continue;
        if (!hasAccessToUpdateData(notification))
            throw new InsufficientAccessException();
        notification.setState(entry.getValue());
    }
    dataAccessor.createOrUpdateNotificationList(new ArrayList<Notification>(notifications.values()));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) HashMap(java.util.HashMap) Map(java.util.Map) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Notification(com.pratilipi.data.type.Notification) NotificationState(com.pratilipi.common.type.NotificationState)

Example 7 with Notification

use of com.pratilipi.data.type.Notification in project pratilipi by Pratilipi.

the class NotificationProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Notification> notifList = dataAccessor.getNotificationListWithFcmPending(1000);
    logger.log(Level.INFO, "Total pending notifications = " + notifList.size());
    Map<Long, List<Notification>> userIdNotifListMap = new HashMap<>();
    for (Notification notif : notifList) {
        List<Notification> userNotifList = userIdNotifListMap.get(notif.getUserId());
        if (userNotifList == null) {
            userNotifList = new LinkedList<>();
            userIdNotifListMap.put(notif.getUserId(), userNotifList);
        }
        userNotifList.add(notif);
    }
    for (final Entry<Long, List<Notification>> entry : userIdNotifListMap.entrySet()) {
        Async async = new Async() {

            @Override
            public void exec() {
                for (Notification notif : entry.getValue()) {
                    Task task = TaskQueueFactory.newTask().setUrl("/notification/process").addParam("notificationId", notif.getId().toString());
                    TaskQueueFactory.getNotificationTaskQueue().add(task);
                    logger.log(Level.INFO, "Task created for notification id " + notif.getId());
                }
            }
        };
        NotificationDataUtil.updateFirebaseDb(entry.getKey(), entry.getValue(), async);
    }
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) HashMap(java.util.HashMap) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) Async(com.pratilipi.common.util.Async) List(java.util.List) LinkedList(java.util.LinkedList) Notification(com.pratilipi.data.type.Notification) Get(com.pratilipi.api.annotation.Get)

Example 8 with Notification

use of com.pratilipi.data.type.Notification in project pratilipi by Pratilipi.

the class AuditLogProcessApi method _createUserAuthorFollowingNotifications.

private void _createUserAuthorFollowingNotifications(UserAuthor userAuthor, Author author) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    if (// Followed
    author.getUserId() == null)
        return;
    Notification notification = dataAccessor.getNotification(author.getUserId(), NotificationType.AUTHOR_FOLLOW, author.getId());
    if (notification == null || (userAuthor.getFollowState() == UserFollowState.FOLLOWING && !_isToday(notification.getCreationDate())))
        notification = dataAccessor.newNotification(author.getUserId(), NotificationType.AUTHOR_FOLLOW, author.getId());
    if (userAuthor.getFollowState() == UserFollowState.FOLLOWING && notification.addDataId(userAuthor.getUserId())) {
        if (notification.getState() == NotificationState.READ)
            notification.setState(NotificationState.UNREAD);
        notification.setFcmPending(true);
    } else if (userAuthor.getFollowState() != UserFollowState.FOLLOWING && notification.removeDataId(userAuthor.getUserId())) {
    // Do nothing
    } else {
        return;
    }
    notification.setLastUpdated(new Date());
    notification = dataAccessor.createOrUpdateNotification(notification);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Notification(com.pratilipi.data.type.Notification) Date(java.util.Date)

Example 9 with Notification

use of com.pratilipi.data.type.Notification in project pratilipi by Pratilipi.

the class AuditLogProcessApi method _createPratilipiPublishedNotification.

private void _createPratilipiPublishedNotification(Pratilipi pratilipi, Author author) {
    if (author.getUserId() == null)
        return;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Notification notification = dataAccessor.getNotification(author.getUserId(), NotificationType.PRATILIPI_PUBLISHED_AUTHOR, pratilipi.getId());
    if (notification != null)
        return;
    notification = dataAccessor.newNotification(author.getUserId(), NotificationType.PRATILIPI_PUBLISHED_AUTHOR, pratilipi.getId());
    notification = dataAccessor.createOrUpdateNotification(notification);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Notification(com.pratilipi.data.type.Notification)

Example 10 with Notification

use of com.pratilipi.data.type.Notification 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

Notification (com.pratilipi.data.type.Notification)11 DataAccessor (com.pratilipi.data.DataAccessor)10 ArrayList (java.util.ArrayList)4 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)3 HashMap (java.util.HashMap)3 Gson (com.google.gson.Gson)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 NotificationData (com.pratilipi.data.client.NotificationData)2 LinkedList (java.util.LinkedList)2 TypeToken (com.google.gson.reflect.TypeToken)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 Language (com.pratilipi.common.type.Language)1 NotificationState (com.pratilipi.common.type.NotificationState)1 NotificationType (com.pratilipi.common.type.NotificationType)1 Async (com.pratilipi.common.util.Async)1 DataListCursorTuple (com.pratilipi.data.DataListCursorTuple)1 DocAccessor (com.pratilipi.data.DocAccessor)1 PratilipiData (com.pratilipi.data.client.PratilipiData)1