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()));
}
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();
}
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);
}
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);
}
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;
}
Aggregations