Skip to content
Browse files

Add failure message to sender if user isn't part of group. See #12.

If a user decides to manually leave a group, but decided to reply to an older RBE group forum item, this forum reply item will get blocked from posting, but a failure message isn't sent back to the sender to inform them of what happened.

This commit sends back a failure message to the sender to inform him/her that their message was not posted because they are no longer a member of the group.

In bp-rbe-classes.php:
* Separate group member check and group member banned check for failure message purposes.
* Drop support for super admins who aren't a member of a group to reply to group forum posts by email.  This didn't make sense to begin with.

In bp-rbe-functions.php:
* Introduce bp_rbe_get_group_member_info() to get all info about a group member in one DB query.
* In bp_rbe_imap_log_no_matches(), add internal log and failure messages for users who are not a part of a group anymore.
  • Loading branch information...
1 parent 9d0734b commit 72a969fa4970bbc6903e4139b782875a57fa463c @r-a-y committed Aug 23, 2012
Showing with 98 additions and 40 deletions.
  1. +58 −37 includes/bp-rbe-classes.php
  2. +40 −3 includes/bp-rbe-functions.php
View
95 includes/bp-rbe-classes.php
@@ -239,35 +239,43 @@ public function run() {
if ( bp_is_active( $bp->groups->id ) && bp_is_active( $bp->forums->id ) ) :
bp_rbe_log( 'Message #' . $i . ': this is a forum reply' );
- $can_post = apply_filters( 'bp_rbe_group_can_post', user_can( $user_id, 'edit_users' ), $user_id );
-
- // If user is a member of the group and not banned, then let's post the forum reply!
- if ( $can_post || ( groups_is_user_member( $user_id, $g ) && !groups_is_user_banned( $user_id, $g ) ) ) {
- $forum_post_id = bp_rbe_groups_new_group_forum_post( array(
- 'post_text' => $body,
- 'topic_id' => $t,
- 'user_id' => $user_id,
- 'group_id' => $g
- ) );
-
- if ( !$forum_post_id ) {
- do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'forum_reply_fail' );
- continue;
- }
+ // get all group member data for the user in one swoop!
+ $group_member_data = bp_rbe_get_group_member_info( $user_id, $g );
- bp_rbe_log( 'Message #' . $i . ': forum reply successfully posted!' );
+ // user is not a member of the group anymore
+ if ( empty( $group_member_data ) ) {
+ do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_not_group_member' );
+ continue;
+ }
- // could potentially add attachments
- do_action( 'bp_rbe_new_forum_post', $this->connection, $i, $forum_post_id, $g, $user_id );
+ // user is banned from group
+ if ( (int) $group_member_data->is_banned == 1 ) {
+ do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_banned_from_group' );
+ continue;
}
- // the user cannot post in this group b/c user is either banned or not part of the group
- else {
- do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_cannot_post_in_group' );
+ /* okay, we should be good to post now! */
+
+ $forum_post_id = bp_rbe_groups_new_group_forum_post( array(
+ 'post_text' => $body,
+ 'topic_id' => $t,
+ 'user_id' => $user_id,
+ 'group_id' => $g
+ ) );
+
+ if ( !$forum_post_id ) {
+ do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'forum_reply_fail' );
continue;
}
+ bp_rbe_log( 'Message #' . $i . ': forum reply successfully posted!' );
+
+ // could potentially add attachments
+ do_action( 'bp_rbe_new_forum_post', $this->connection, $forum_post_id, $user_id, $g, $headers );
+
unset( $t );
+ unset( $group_member_data );
+ unset( $forum_post_id );
endif;
// Private message reply
@@ -339,30 +347,43 @@ public function run() {
continue;
}
- $can_post = apply_filters( 'bp_rbe_group_can_post', user_can( $user_id, 'edit_users' ), $user_id );
+ // get all group member data for the user in one swoop!
+ $group_member_data = bp_rbe_get_group_member_info( $user_id, $g );
- // If user is a member of the group and not banned, then let's post the forum topic!
- if ( $can_post || ( groups_is_user_member( $user_id, $g ) && !groups_is_user_banned( $user_id, $g ) ) ) {
- $topic = bp_rbe_groups_new_group_forum_topic( array(
- 'topic_title' => $subject,
- 'topic_text' => $body,
- 'user_id' => $user_id,
- 'group_id' => $g
- ) );
+ // user is not a member of the group anymore
+ if ( empty( $group_member_data ) ) {
+ do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_not_group_member' );
+ continue;
+ }
- if ( !$topic ) {
- do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'new_topic_fail' );
- continue;
- }
+ // user is banned from group
+ if ( (int) $group_member_data->is_banned == 1 ) {
+ do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'user_banned_from_group' );
+ continue;
+ }
+
+ /* okay, we should be good to post now! */
- bp_rbe_log( 'Message #' . $i . ': forum topic successfully posted!' );
+ $topic = bp_rbe_groups_new_group_forum_topic( array(
+ 'topic_title' => $subject,
+ 'topic_text' => $body,
+ 'user_id' => $user_id,
+ 'group_id' => $g
+ ) );
- // could potentially add attachments
- do_action_ref_array( 'bp_rbe_new_forum_topic', array( $this->connection, $i, &$topic, $g, $user_id ) );
+ if ( !$topic ) {
+ do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'new_topic_fail' );
+ continue;
}
+ bp_rbe_log( 'Message #' . $i . ': forum topic successfully posted!' );
+
+ // could potentially add attachments
+ do_action( 'bp_rbe_new_forum_topic', $this->connection, $topic, $user_id, $g, $headers );
+
unset( $g );
unset( $subject );
+ unset( $group_member_data );
endif;
endif;
View
43 includes/bp-rbe-functions.php
@@ -711,10 +711,25 @@ function bp_rbe_imap_log_no_matches( $imap, $i, $headers, $type ) {
break;
- /** FORUMS *******************************************************/
+ /** GROUP FORUMS *************************************************/
- case 'user_cannot_post_in_group' :
- $log = __( 'notice - user cannot post in group because user is either banned or not a member of the group. reply not posted!', 'bp-rbe' );
+ case 'user_not_group_member' :
+ $log = __( 'error - user is not a member of the group. forum reply not posted.', 'bp-rbe' );
+
+ $message = sprintf( __( 'Hi there,
+
+Your forum reply:
+
+"%s"
+
+Could not be posted because you are no longer a member of this group. To comment on the forum thread, please rejoin the group.
+
+We apologize for any inconvenience this may have caused.', 'bp-rbe' ), BP_Reply_By_Email_IMAP::body_parser( $imap, $i ) );
+
+ break;
+
+ case 'user_banned_from_group' :
+ $log = __( 'notice - user is banned from group. forum reply not posted.', 'bp-rbe' );
break;
@@ -1182,6 +1197,28 @@ function bp_rbe_groups_new_group_forum_topic( $args = '' ) {
return false;
}
+/**
+ * Get a group member's info.
+ *
+ * Basically a copy of {@link BP_Groups_Member::populate()} without the
+ * extra {@link BP_Core_User()} call.
+ *
+ * @param int $user_id The user ID
+ * @param int $group_id The group ID
+ * @param mixed Object of group member data on success. NULL on failure.
+ * @since 1.0-beta2
+ */
+function bp_rbe_get_group_member_info( $user_id = false, $group_id = false ) {
+ global $bp, $wpdb;
+
+ if ( ! $user_id || ! $group_id )
+ return false;
+
+ $sql = $wpdb->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id );
+
+ return $wpdb->get_row( $sql );
+}
+
/** Template ************************************************************/
/**

0 comments on commit 72a969f

Please sign in to comment.
Something went wrong with that request. Please try again.