* @param communityIdStr
* @return
*/
public ResponsePojo inviteCommunity(String userIdStr, String personIdStr, String communityIdStr, String skipInvitation)
{
ResponsePojo rp = new ResponsePojo();
try {
communityIdStr = allowCommunityRegex(userIdStr, communityIdStr);
}
catch (Exception e) {
rp.setResponse(new ResponseObject("Invite Community", false, "Error returning community info: " + e.getMessage()));
return rp;
}
boolean skipInvite = ((null != skipInvitation) && (skipInvitation.equalsIgnoreCase("true"))) ? true : false;
/////////////////////////////////////////////////////////////////////////////////////////////////
// Note: Only Sys Admins, Community Owner, and Community Moderators can invite users to
// private communities however any member can be able to invite someone to a public community
boolean isOwnerOrModerator = SocialUtils.isOwnerOrModerator(communityIdStr, userIdStr);
boolean isSysAdmin = RESTTools.adminLookup(userIdStr);
boolean canInvite = (isOwnerOrModerator || isSysAdmin) ? true : false;
try
{
BasicDBObject query = new BasicDBObject("_id",new ObjectId(communityIdStr));
DBObject dboComm = DbManager.getSocial().getCommunity().findOne(query);
if ( dboComm != null )
{
CommunityPojo cp = CommunityPojo.fromDb(dboComm, CommunityPojo.class);
// Make sure this isn't a personal community
if ( !cp.getIsPersonalCommunity() )
{
// Check to see if the user has permissions to invite or selfregister
boolean selfRegister = canSelfRegister(cp);
if ( canInvite || cp.getOwnerId().toString().equalsIgnoreCase(userIdStr) || selfRegister )
{
BasicDBObject dboPerson = (BasicDBObject) DbManager.getSocial().getPerson().findOne(new BasicDBObject("email", personIdStr));
if (null == dboPerson) { // (ie personId isn't an email address... convert to ObjectId and try again)
dboPerson = (BasicDBObject) DbManager.getSocial().getPerson().findOne(new BasicDBObject("_id", new ObjectId(personIdStr)));
}
else {
personIdStr = dboPerson.getString("_id");
}
// OK from here on, personId is the object Id...
if ( dboPerson != null )
{
PersonPojo pp = PersonPojo.fromDb(dboPerson,PersonPojo.class);
//need to check for if a person is pending, and skipInvite and isSysAdmin, otherwise
//they would just get sent an email again, so leave it be
boolean isPending = false;
if ( isSysAdmin && skipInvite )
{
isPending = isMemberPending(cp, pp);
}
if ( selfRegister )
{
//If the comm allows for self registering, just call join community
//instead of invite, it will handle registration
return this.joinCommunity(pp.get_id().toString(), communityIdStr, isSysAdmin);
}
else if ( !cp.isMember(pp.get_id()) || isPending )
{
if (isSysAdmin && skipInvite) // Can only skip invite if user is Admin
{
// Update community with new member
cp.addMember(pp, false); // Member status set to Active
cp.setNumberOfMembers(cp.getNumberOfMembers() + 1); // Increment number of members
/////////////////////////////////////////////////////////////////////////////////////////////////
// TODO (INF-1214): Make this code more robust to handle changes to the community that need to
// Caleb: this means change update to $set
/////////////////////////////////////////////////////////////////////////////////////////////////
DbManager.getSocial().getCommunity().update(query, cp.toDb());
// Add community to persons object and save to db
pp.addCommunity(cp);
/////////////////////////////////////////////////////////////////////////////////////////////////
// TODO (INF-1214): Make this code more robust to handle changes to the community that need to
// Caleb: this means change update to $set
/////////////////////////////////////////////////////////////////////////////////////////////////
DbManager.getSocial().getPerson().update(new BasicDBObject("_id", pp.get_id()), pp.toDb());
rp.setResponse(new ResponseObject("Invite Community",true,"User added to community successfully."));
}
else
{
cp.addMember(pp, true); // Member status set to Pending
/////////////////////////////////////////////////////////////////////////////////////////////////
// TODO (INF-1214): Make this code more robust to handle changes to the community that need to
// Caleb: this means change update to $set
/////////////////////////////////////////////////////////////////////////////////////////////////
DbManager.getSocial().getCommunity().update(query, cp.toDb());
//send email out inviting user
CommunityApprovePojo cap = cp.createCommunityApprove(personIdStr,communityIdStr,"invite",userIdStr);
DbManager.getSocial().getCommunityApprove().insert(cap.toDb());
PropertiesManager propManager = new PropertiesManager();
String rootUrl = propManager.getUrlRoot();
if (null == rootUrl) {
rp.setResponse(new ResponseObject("Invite Community",false,"The system was unable to email the invite because an invite was required and root.url is not set up."));
return rp;
}
String subject = "Invite to join infinit.e community: " + cp.getName();
String body = "You have been invited to join the community " + cp.getName() +
"<br/><a href=\"" + rootUrl + "social/community/requestresponse/"+cap.get_id().toString() + "/true\">Accept</a> " +
"<a href=\"" + rootUrl + "social/community/requestresponse/"+cap.get_id().toString() + "/false\">Deny</a>";
SendMail mail = new SendMail(new PropertiesManager().getAdminEmailAddress(), pp.getEmail(), subject, body);
if (mail.send("text/html"))
{
if (isSysAdmin) {
rp.setResponse(new ResponseObject("Invite Community",true,"Invited user to community successfully: " + cap.get_id().toString()));
}
else {
rp.setResponse(new ResponseObject("Invite Community",true,"Invited user to community successfully"));
}
}
else
{
rp.setResponse(new ResponseObject("Invite Community",false,"The system was unable to email the invite for an unknown reason (eg an invite was required and the mail server is not setup)."));
}
}
}
else
{
//otherwise just return we failed
rp.setResponse(new ResponseObject("Invite Community",false,"The user is already a member of this community."));
}
}
else
{
rp.setResponse(new ResponseObject("Invite Community",false,"Person does not exist"));
}
}
else
{
rp.setResponse(new ResponseObject("Invite Community",false,"You must be owner to invite other members, if you received an invite, you must accept it through that"));
}
}
else
{
rp.setResponse(new ResponseObject("Invite Community",false,"Cannot invite members to personal community"));
}
}
else
{
rp.setResponse(new ResponseObject("Invite Community",false,"Community does not exist"));
}
}
catch(Exception ex)
{
rp.setResponse(new ResponseObject("Invite Community",false,"General Error, bad params maybe? " + ex.getMessage()));
}
return rp;
}