use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.
the class AuthorDataUtil method createAuthorDataList.
public static List<AuthorData> createAuthorDataList(List<Long> authorIdList, boolean includeUserData) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
List<Author> authorList = dataAccessor.getAuthorList(authorIdList);
Map<Long, Page> authorPages = dataAccessor.getPages(PageType.AUTHOR, authorIdList);
List<AuthorData> authorDataList = new ArrayList<>(authorIdList.size());
if (includeUserData) {
List<Long> userIdList = new ArrayList<>(authorIdList.size());
for (Author author : authorList) if (author.getUserId() != null)
userIdList.add(author.getUserId());
List<User> userList = dataAccessor.getUserList(userIdList);
Map<Long, User> users = new HashMap<>(userIdList.size());
for (User user : userList) users.put(user.getId(), user);
for (Author author : authorList) authorDataList.add(createAuthorData(author, authorPages.get(author.getId()), users.get(author.getUserId())));
} else {
for (Author author : authorList) authorDataList.add(createAuthorData(author, authorPages.get(author.getId())));
}
return authorDataList;
}
use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.
the class UserDataUtil method updateUserAuthorStats.
public static void updateUserAuthorStats(Long userId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
User user = dataAccessor.getUser(userId);
UserFollowsDoc followsDoc = docAccessor.getUserFollowsDoc(userId);
if (user.getFollowCount().equals(followsDoc.getFollows(UserFollowState.FOLLOWING).size()))
return;
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
user.setFollowCount((long) followsDoc.getFollows(UserFollowState.FOLLOWING).size());
user = dataAccessor.createOrUpdateUser(user, auditLog);
}
use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.
the class UserDataUtil method updateUserPassword.
public static void updateUserPassword(String email, String verificationToken, String newPassword) throws InvalidArgumentException, InsufficientAccessException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
JsonObject errorMessages = new JsonObject();
if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
throw new InvalidArgumentException(errorMessages);
} else if (user.getState() == UserState.BLOCKED) {
throw new InvalidArgumentException(GenericRequest.ERR_ACCOUNT_BLOCKED);
}
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.setPassword(PasswordUtil.getSaltedHash(newPassword));
// Note: Do not clear VerificationToken
user = dataAccessor.createOrUpdateUser(user, auditLog);
}
use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.
the class UserDataUtil method _validateUserDataForSave.
private static void _validateUserDataForSave(UserData userData) throws InvalidArgumentException {
boolean isNew = userData.getId() == null;
JsonObject errorMessages = new JsonObject();
// New user profile must have name.
if (isNew && userData.getFirstName() == null)
errorMessages.addProperty("name", GenericRequest.ERR_NAME_REQUIRED);
// New user profile must have email.
if (isNew && (!userData.hasEmail() || userData.getEmail() == null))
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
else // Email can not be un-set or set to null.
if (!isNew && userData.hasEmail() && userData.getEmail() == null)
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
// For new user, user email should be not registered already.
if (isNew && DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail()) != null)
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
else // Email, if provided, must not be registered with some other user.
if (!isNew && userData.hasEmail() && userData.getEmail() != null) {
User user = DataAccessorFactory.getDataAccessor().getUserByEmail(userData.getEmail());
if (user != null && !user.getId().equals(userData.getId()))
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
}
if (errorMessages.entrySet().size() > 0)
throw new InvalidArgumentException(errorMessages);
}
use of com.pratilipi.data.type.User in project pratilipi by Pratilipi.
the class UserAuthorDataUtil method getUserFollowList.
public static DataListCursorTuple<AuthorData> getUserFollowList(Long userId, String cursor, Integer offset, Integer resultCount) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUser(userId);
if (user.getFollowCount() == 0L)
return new DataListCursorTuple<>(new ArrayList<AuthorData>(0), null, 0L);
DataListCursorTuple<Long> authorIdListCursorTuple = dataAccessor.getUserAuthorFollowList(userId, null, cursor, offset, resultCount);
List<Long> authorIdList = authorIdListCursorTuple.getDataList();
List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, true);
// Setting AuthorData.isFollowing flag
if (userId.equals(AccessTokenFilter.getAccessToken().getUserId())) {
for (AuthorData authorData : authorDataList) authorData.setFollowing(true);
} else {
List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(AccessTokenFilter.getAccessToken().getUserId(), authorIdList);
if (userAuthorList.size() != 0)
for (int i = 0; i < authorIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
authorDataList.get(i).setFollowing(true);
}
// Setting UserData.isFollowing flag
Author author = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
if (author != null) {
List<Long> userIdList = new ArrayList<>(authorDataList.size());
List<UserData> userDataList = new ArrayList<>(authorDataList.size());
for (AuthorData authorData : authorDataList) {
if (authorData.getUser().getId() != null) {
userIdList.add(authorData.getUser().getId());
userDataList.add(authorData.getUser());
}
}
List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(userIdList, author.getId());
if (userAuthorList != null) {
for (UserAuthor userAuthor : userAuthorList) if (userAuthor != null && userAuthor.getFollowState() == UserFollowState.FOLLOWING)
userDataList.get(userIdList.indexOf(userAuthor.getUserId())).setFollowing(true);
}
}
return new DataListCursorTuple<>(authorDataList, authorIdListCursorTuple.getCursor(), user.getFollowCount());
}
Aggregations