use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method registerUser.
public static UserData registerUser(String firstName, String lastName, String email, String password, UserSignUpSource signUpSource) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null || user.getState() == UserState.DELETED) {
user = dataAccessor.newUser();
} else if (user.getState() != UserState.REFERRAL) {
JsonObject errorMessages = new JsonObject();
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
throw new InvalidArgumentException(errorMessages);
}
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
user.setPassword(PasswordUtil.getSaltedHash(password));
user.setEmail(email.toLowerCase());
user.setState(UserState.REGISTERED);
user.setSignUpDate(new Date());
user.setSignUpSource(signUpSource);
user = dataAccessor.createOrUpdateUser(user, auditLog);
UserData userData = createUserData(user);
userData.setFirstName(firstName);
userData.setLastName(lastName);
return userData;
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method _sendEmailVerificationMail.
private static void _sendEmailVerificationMail(User user, Language language) throws InvalidArgumentException, UnexpectedServerException {
String verificationToken = _getNextToken(user.getVerificationToken());
if (!verificationToken.equals(user.getVerificationToken())) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
user.setVerificationToken(verificationToken);
user = dataAccessor.createOrUpdateUser(user);
}
Map<String, String> dataModel = new HashMap<>();
String verificationLink = "/" + "?" + "email=" + user.getEmail() + "&" + "token=" + verificationToken.substring(0, verificationToken.indexOf("|")) + "&" + "verifyUser=" + Boolean.TRUE;
dataModel.put("emailVerificationUrl", verificationLink);
EmailUtil.sendMail(createUserData(user).getDisplayName(), user.getEmail(), "verification", language, dataModel);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method verifyUserEmail.
public static void verifyUserEmail(String email, String verificationToken) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null || user.getState() != UserState.REGISTERED)
return;
if (!_verifyToken(user, verificationToken))
throw new InvalidArgumentException(GenericRequest.ERR_VERIFICATION_TOKEN_INVALID_OR_EXPIRED);
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
user.setState(UserState.ACTIVE);
// Note: Do not clear VerificationToken
user = dataAccessor.createOrUpdateUser(user, auditLog);
}
use of com.pratilipi.data.DataAccessor 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.DataAccessor 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);
}
Aggregations