Accepting Friendship Requests

This section explains how to implement the functionality to allow a user to accept friendship requests. The functionality is yet again, provided by Struts framework actions. The Action declaration can be as follows:

<form-bean name="friendshipForm" type="com.ndc.community.struts.actions.forms.FriendshipForm"/>

<action path="/friendship/accept"
        name="friendshipForm"
        scope="request"
        type="com.ndc.community.struts.actions.friendship.AcceptFriendship">
        <forward name="error" path="/accept-friendship-error.jsp" />
        <forward name="success" path="/" redirect="true" />
</action>

The action definition and form properties for this feature is almost identical to that of the Request Friendship feature. But first, the user needs to see who has requested for friendship. An example JSP that will allow the user to see this is as follows:

<div>
  <community:user id="currentUser"/>
  <h3>Waiting Friendship Request(s)<h3/>
  <h3>(Wants to be friends with ${currentUser.article.fields.userName})</h3>
  <community:friends id="candidateFriends" name="currentUser" type="candidate"/>
  <logic:iterate id="candidate" name="candidateFriends" type="com.ndc.presentation.PresentationUser">
    <p>
      <util:valueof param="candidate.article.fieldElement(USERNAME)"/>
    </p>
  </logic:iterate>
</div>

Once the user can see who requested for his friendship, he can be presented a page where he can accept the request and perhaps send a response message back to user who requested his friendship. An example JSP template to accomplish this can be as follows:

<html:form action="/friendship/accept" method="post">
  <community:user id="currentUser"/>
  <community:user id="targetUser"/>

  <html:hidden property="friendId" value="${targetUser.id}"/>
  <html:hidden property="userId" value="${currentUser.id}"/>

	<html:hidden property="successUrl" value="${currentUser.section.url}"/>
	<html:hidden property="errorUrl" value="{targetUser.section.url}"/>

  <!-- Rest of the form fields related to textMessage content-type -->
  <html:hidden property="articleType" value="textMessage"/>
  Title: <html:text property="field(TITLE)"/><br/>
  Message: <html:text property="field(BODY)"/><br/>

  <html:submit>Accept</html:submit>
</html:form>

Almost all the properties are the same as Request Friendship form. The error properties are probably worth mentioning since they are different.

  • AcceptFriendship : if the message could not be send.

  • error : if any internal error occurs while accepting the friendship.