use of com.willshex.blogwt.shared.api.datatype.Relationship in project blogwt by billy1380.
the class BlockUsersActionHandler method handle.
/* (non-Javadoc)
*
* @see
* com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
* gson.web.service.shared.Request,
* com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(BlockUsersRequest input, BlockUsersResponse output) throws Exception {
PropertyValidator.ensureTrue(PropertyHelper.ALLOW_USER_REGISTRATION, PropertyHelper.ENABLE_USER_RELATIONSHIPS);
ApiValidator.request(input, BlockUsersRequest.class);
ApiValidator.accessCode(input.accessCode, "input.accessCode");
output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
User user = null;
if (input.user == null) {
user = input.session.user;
} else {
user = input.user;
UserValidator.validate(user, "input.user");
if (!DataTypeHelper.<User>same(user, input.session.user)) {
UserValidator.authorisation(input.session.user, Arrays.asList(PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_USERS)), "input.session.user");
}
}
ApiValidator.notNull(input.others, User.class, "input.others");
List<Relationship> added = null;
if (!Boolean.TRUE.equals(input.un)) {
added = new ArrayList<Relationship>();
}
for (User other : input.others) {
try {
// validate each user
other = UserValidator.validate(other, "input.other[n]");
// add a relationship for the validated users (skip failures)
if (Boolean.TRUE.equals(input.un)) {
// un-block
RelationshipServiceProvider.provide().deleteUsersRelationship(user, other, RelationshipTypeType.RelationshipTypeTypeBlock);
} else {
// block
added.add(RelationshipServiceProvider.provide().addUsersRelationship(user, other, RelationshipTypeType.RelationshipTypeTypeBlock));
}
} catch (InputValidationException inEx) {
// just skip that user if the user is not valid
}
}
}
use of com.willshex.blogwt.shared.api.datatype.Relationship in project blogwt by billy1380.
the class GetUsersActionHandler method getRelatedUsers.
/**
* @param input
* @return
* @throws DisallowedByPropertyException
* @throws InputValidationException
*/
private List<User> getRelatedUsers(GetUsersRequest input) throws DisallowedByPropertyException, InputValidationException {
List<User> relatedUsers = null;
User user = null;
PropertyValidator.ensureTrue(PropertyHelper.ALLOW_USER_REGISTRATION, PropertyHelper.ENABLE_USER_RELATIONSHIPS);
if (input.user == null) {
user = input.user = UserValidator.validate(input.user, "input.user");
} else {
user = input.session.user;
}
List<Relationship> relationships = null;
List<Long> userIds = null;
if (Boolean.TRUE.equals(input.userIsOther)) {
// Cannot reveal users blocking you
if (input.relationshipType == RelationshipTypeType.RelationshipTypeTypeBlock)
ApiValidator.throwServiceError(InputValidationException.class, ApiError.CannotRevealRelationshipUsers, "input.relationshipType");
relationships = RelationshipServiceProvider.provide().getWithUserRelationships(user, input.relationshipType, input.pager.start, input.pager.count, RelationshipSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
if (relationships != null && !relationships.isEmpty()) {
userIds = new ArrayList<Long>();
for (Relationship relationship : relationships) {
userIds.add(keyToId(relationship.oneKey));
}
}
} else {
relationships = RelationshipServiceProvider.provide().getUserRelationships(user, input.relationshipType, input.pager.start, input.pager.count, RelationshipSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
if (relationships != null && !relationships.isEmpty()) {
userIds = new ArrayList<Long>();
for (Relationship relationship : relationships) {
userIds.add(keyToId(relationship.anotherKey));
}
}
}
if (userIds != null) {
// order might become important later for now, it is not
relatedUsers = PersistenceHelper.batchLookup(UserServiceProvider.provide(), PersistenceHelper.idsToKeys(User.class, userIds));
}
return relatedUsers;
}
use of com.willshex.blogwt.shared.api.datatype.Relationship in project blogwt by billy1380.
the class FollowUsersActionHandler method handle.
/* (non-Javadoc)
*
* @see
* com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
* gson.web.service.shared.Request,
* com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(FollowUsersRequest input, FollowUsersResponse output) throws Exception {
PropertyValidator.ensureTrue(PropertyHelper.ALLOW_USER_REGISTRATION, PropertyHelper.ENABLE_USER_RELATIONSHIPS);
ApiValidator.request(input, FollowUsersRequest.class);
ApiValidator.accessCode(input.accessCode, "input.accessCode");
output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
User user = null;
if (input.user == null) {
user = input.session.user;
} else {
user = input.user;
UserValidator.validate(user, "input.user");
if (!DataTypeHelper.<User>same(user, input.session.user)) {
UserValidator.authorisation(input.session.user, Arrays.asList(PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_USERS)), "input.session.user");
}
}
ApiValidator.notNull(input.others, User.class, "input.others");
List<Relationship> added = null;
if (!Boolean.TRUE.equals(input.un)) {
added = new ArrayList<Relationship>();
}
for (User other : input.others) {
try {
// validate each user
other = UserValidator.validate(other, "input.other[n]");
// add a relationship for the validated users (skip failures)
if (Boolean.TRUE.equals(input.un)) {
// un-follow
RelationshipServiceProvider.provide().deleteUsersRelationship(user, other, RelationshipTypeType.RelationshipTypeTypeFollow);
} else {
// follow
added.add(RelationshipServiceProvider.provide().addUsersRelationship(user, other, RelationshipTypeType.RelationshipTypeTypeFollow));
}
} catch (InputValidationException inEx) {
// just skip that user if the user is not valid
}
}
}
use of com.willshex.blogwt.shared.api.datatype.Relationship in project blogwt by billy1380.
the class RelationshipService method addUsersRelationship.
/* (non-Javadoc)
*
* @see
* com.willshex.blogwt.server.service.relationship.IRelationshipService#
* addUsersRelationship(com.willshex.blogwt.shared.api.datatype.User,
* com.willshex.blogwt.shared.api.datatype.User,
* com.willshex.blogwt.shared.api.datatype.RelationshipTypeType) */
@Override
public Relationship addUsersRelationship(User user, User other, RelationshipTypeType type) {
Relationship relationship = getUsersRelationship(user, other, type);
if (relationship == null) {
relationship = new Relationship().one(user).another(other).type(type);
addRelationship(relationship);
}
return relationship;
}
use of com.willshex.blogwt.shared.api.datatype.Relationship in project blogwt by billy1380.
the class RelationshipService method addRelationship.
@Override
public Relationship addRelationship(Relationship relationship) {
if (relationship.created == null) {
relationship.created = new Date();
}
if (relationship.one != null) {
relationship.oneKey = Key.create(relationship.one);
}
if (relationship.another != null) {
relationship.anotherKey = Key.create(relationship.another);
}
Key<Relationship> key = provide().save().entity(relationship).now();
relationship.id = keyToId(key);
return relationship;
}
Aggregations