Skip to content

Commit

Permalink
WIP - Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
KartikSuthar committed Jan 2, 2025
1 parent 474b94f commit ceaed25
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/bp-messages/bp-messages-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,9 +1288,7 @@ function bp_messages_get_avatars( $thread_id, $user_id ) {
$avatar_urls = array( $group_avatar );
}
} else {
// Get the last two not deleted messages and message is not from a current user.
$qyery = "SELECT DISTINCT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d AND sender_id != %d AND is_deleted = 0 ORDER BY id DESC LIMIT 2";
$avatars_user_ids = $wpdb->get_col( $wpdb->prepare( $qyery, $thread_id, $user_id ) );
$avatars_user_ids = BP_Messages_Message::avatar_recipients( $thread_id, $user_id );

if ( empty( $avatars_user_ids ) ) {
$avatars_user_ids = array( $user_id );
Expand Down
31 changes: 31 additions & 0 deletions src/bp-messages/classes/class-bp-messages-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,35 @@ protected static function convert_orderby_to_order_by_term( $orderby, $order = '
protected static function strip_leading_and( $s ) {
return preg_replace( '/^\s*AND\s*/', '', $s );
}

/**
* Get the avatar recipient IDs for a given thread.
*
* @param int $thread_id Thread id.
* @param int $user_id User id.
*
* @return array
*/
public static function avatar_recipients( $thread_id, $user_id = 0 ) {
global $wpdb, $bp;
static $cache = array();

if ( empty( $user_id ) ) {
$user_id = bp_loggedin_user_id();
}

$key = 'thread_avatar_' . $thread_id . '_' . $user_id;

if ( isset( $cache[ $key ] ) ) {
return $cache[ $key ];
}

// Get the last two not deleted messages and message is not from a current user.
$query = "SELECT DISTINCT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d AND sender_id != %d AND is_deleted = 0 ORDER BY id DESC LIMIT 2";
$retval = $wpdb->get_col( $wpdb->prepare( $query, $thread_id, $user_id ) );

$cache[ $key ] = $retval;

return $retval;
}
}

0 comments on commit ceaed25

Please sign in to comment.