Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/03/2014 08:52:05 PM (10 years ago)
Author:
djpaul
Message:

Activity: introducing @mentions auto-suggestions.

Activity has long-supported @mentions, where you can mention the name of another user to get their attention in a conversation. You've always had to know the exact username of the person you're trying to mention, or you've been forced to go look it up. Now, when leaving an activity update or reply, press @ and then start typing someone's diaply name (or user_nicename), and username suggestions will automatically appear below where you're typing.

If the Friends component is active, and if the logged-in user has any friends, those friends' details are used to pre-prime the username suggestions for super-fast snappy suggestions. Suggestions are also provided when writing a blog post comment.

This change has mostly been implemented using two third-party libraries, At.js (https://github.com/ichord/At.js) and Caret.js (https://github.com/ichord/Caret.js). Big thanks to those projects' authors for their fine work, and also to Automattic's O2 team, who made a number of CSS+JS tweaks/adaptions to At.js, which we've adopted. Props also to karmatosed for design suggestions on the earliest implementation of this feature, three years ago. :)

Fixes #3278

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-friends/bp-friends-functions.php

    r8514 r8754  
    567567add_action( 'delete_user',       'friends_remove_data' );
    568568add_action( 'bp_make_spam_user', 'friends_remove_data' );
     569
     570/**
     571 * Used by the Activity component's @mentions to print a JSON list of the current user's friends.
     572 *
     573 * This is intended to speed up @mentions lookups for a majority of use cases.
     574 *
     575 * @see bp_activity_mentions_script()
     576 */
     577function bp_friends_prime_mentions_results() {
     578    if ( ! bp_activity_maybe_load_mentions_scripts() ) {
     579        return;
     580    }
     581
     582    $friends_query = array(
     583        'count_total'     => '',                    // Prevents total count
     584        'populate_extras' => false,
     585
     586        'type'            => 'alphabetical',
     587        'user_id'         => get_current_user_id(),
     588    );
     589
     590    $friends_query = new BP_User_Query( $friends_query );
     591    $results       = array();
     592
     593    foreach ( $friends_query->results as $user ) {
     594        $result        = new stdClass();
     595        $result->ID    = $user->user_nicename;
     596        $result->image = bp_core_fetch_avatar( array( 'html' => false, 'item_id' => $user->ID ) );
     597        $result->name  = bp_core_get_user_displayname( $user->ID );
     598
     599        $results[] = $result;
     600    }
     601
     602    wp_localize_script( 'bp-mentions', 'BP_Suggestions', array(
     603        'friends' => $results,
     604    ) );
     605}
     606add_action( 'bp_activity_mentions_prime_results', 'bp_friends_prime_mentions_results' );
Note: See TracChangeset for help on using the changeset viewer.