Home Forums BP Better Messages many threads with one user Reply To: many threads with one user

#6131
add_action( 'bp_better_messages_before_new_thread', 'disable_start_thread_if_thread_exist', 10, 2 );

function disable_start_thread_if_thread_exist(&$args, &$errors){
            global $wpdb;

            $recipients = $args['recipients'];

            $threadExists = array();
            foreach($recipients as $recipient){
                $user = get_user_by('login', $recipient);

                $from = get_current_user_id();
                $to   = $user->ID;

                $query = $wpdb->prepare("SELECT
                  {$wpdb->base_prefix}bp_messages_recipients.thread_id
                FROM {$wpdb->base_prefix}bp_messages_recipients
                  INNER JOIN {$wpdb->base_prefix}bp_messages_recipients {$wpdb->base_prefix}bp_messages_recipients_1
                    ON {$wpdb->base_prefix}bp_messages_recipients.thread_id = {$wpdb->base_prefix}bp_messages_recipients_1.thread_id
                WHERE {$wpdb->base_prefix}bp_messages_recipients.user_id = %d
                AND {$wpdb->base_prefix}bp_messages_recipients.is_deleted = 0
                AND {$wpdb->base_prefix}bp_messages_recipients_1.user_id = %d", $from, $to);

                $threads = $wpdb->get_col($query);

                if( count($threads) > 0) {
                    $threadExists[] = $recipient;
                }
            }

            if(count($threadExists) > 0){
                $message = sprintf(__('You already have threads with %s', 'bp-better-messages'), implode(', ', $threadExists));
                $errors[] = $message;
            }
}