BuddyDev

Search

[Resolved] Hook to activate at the timing of receiving the message

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20621

    Hello everyone.

    Do you know the hooks that let you take action when receiving private messages?

    I want to rewrite “user_message” when I receive a message as follows.

    For that, I would like to know 【➀➁➂➃】.

    // Upon receiving a private message, update "user_message"
    function action() {
    	// Message information
    	$receiver_id =【②】;
    	$sender_id =【➂】;
    	$message_link = 'http://example.com/members/smith/bp-messages/?thread_id=【➃】;
    
    	// Rewriting user_meta("user_message")
    	$timestamp = date("Y/m/d H:i:s");
    	$notification_add_array = array(array('action'=>'message','time'=>''.$timestamp.'','actor'=>''.$sender_id.'','actee'=>''.$receiver_id.'','read'=>'yet', link => ''.$message_link.''));
    	$notification_current_txt = get_user_meta( $receiver_id, 'user_notification', true );
    	$notification_current_array = unserialize($notification_current_txt);
    	$notification_new_array = array_merge($notification_add_array, $notification_current_array);
    	$notification_new_txt = serialize($notification_new_array);
    	update_user_meta( $receiver_id, "user_message", $notification_new_txt );
    }
    add_filter( '【➀】', 'action' );

    Best regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20625

    Hi maimaimemain,
    Thank you for asking.
    For you, the appropriate action is

    
    messages_message_before_save
    or 
    messages_message_after_save
    
    

    You can hook to it like

    
    
    function your_message_action_handler( $message ) {
    }
    add_action('messages_message_after_save', 'your_message_action_handler');
    
    

    $message is ‘BP_Messages_Message’ object and you can access the details

    Please do note that hook does not allow to differentiate between a new thread and an existing one.

    To do that, you might need to hook to

    
    
    messages_message_before_save
    
    

    and check for empty thread id.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20627

    Hi Brajesh,
    Thank you as always.

    Do you know a site listing how to use “$message”?
    I got “$sender_id” but I can not get “$recipients_id” and “$message_thread”.

    function your_message_action_handler($message) {
    
    	$sender_id = $message->sender_id;
    	$recipients_id = ???;
    	$message_thread = ???;
    	$message_link = 'http://example.com/members/smith/bp-messages/?thread_id='.$message_thread.'';
    
    }
    add_filter( 'messages_message_before_save', 'your_message_action_handler' );

    I did not understand the meaning of talking about threads.
    What mean check for empty thread id?

    I look forward to your response.

    Regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20628

    Hi ,
    You are welcome.

    Try this one

    
    $recipients_ids = wp_list_pluck( $message->recipients, 'user_id' );
    

    It should give an array of user ids.

    About the thread:- In BuddyPress, it is possible to have multiple people involved in a common message. It is accomplished via thread. All messages have a thread id(think of it as a topic id) which BuddyPress uses to relate the replies.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20630

    Hi ,

    I had no idea.
    That means you can rewrite “user_meta(“notification_message”)” of everyone by arranging “user ids” as an array, right!?
    You’re so knowledgeable!

    function your_message_action_handler($message) {
    
    	// I did it
    	$sender_id = $message->sender_id;
    	$recipients_ids = wp_list_pluck( $message->recipients, 'user_id' );
    	update_user_meta( $recipients_ids, "notification_message", $sender_id );
    
    	// Is there a way to get the ID of that thread?
    	$message_thread = ???;
    	$message_link = 'http://example.com/members/smith/bp-messages/?thread_id='.$message_thread.'';
    
    }
    add_filter( 'messages_message_before_save', 'your_message_action_handler' );

    Is there a way to get the ID of that thread?
    I would like to use that thread ID and create a link to a thread.

    Regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20633

    Hi maimaimemain,
    Thank you. I had looked at the code and assisted you(with a few time working with Buddyress, you will be able to do it easily yourself. Nothing special on my part 🙂 )

    1. If you need thread id, my suggestion is hook to

    
    messages_message_after_save
    

    Then, you can access thread id like

    
    
    $thread_id = $message->thread_id;
    
    

    Also, ‘update_user_meta()’ works with individual users. You should loop on the user ids and then update for each user.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20638

    Hi Brajesh,

    I couldn’t have done it without your help.

    function your_message_action_handler($message) {
    	// Message information
    	$time_micro = date("Y-m-d H:i:s") . "." . substr(explode(".", (microtime(true) . ""))[1], 0, 3);
    	$sender_id = $message->sender_id;
    	$thread_id = $message->thread_id;
    	$recipients_ids = wp_list_pluck( $message->recipients, 'user_id' );
    
    	// Update user_meta("notification_message")
    	foreach ($recipients_ids as $recipient_id) {
    		$notification_new_array = array(array('action'=>'message','time'=>''.$time_micro.'','senderID'=>''.$sender_id.'','recipientID'=>''.$recipient_id.'','threadID'=>''.$thread_id.'','read'=>'yet'));
    		$notification_current_txt = get_user_meta( $recipient_id, 'notification_message', true );
    		if( $notification_current_txt !== '' ){
    			$notification_current_array = unserialize($notification_current_txt);
    			$notification_update_array = array_merge($notification_new_array, $notification_current_array);
    			$notification_update_txt = serialize($notification_update_array);
    			update_user_meta( $recipient_id, "notification_message", $notification_update_txt );
    		}else{
    			$notification_new_txt = serialize($notification_new_array);
    			update_user_meta( $recipient_id, "notification_message", $notification_new_txt );	
    		}
    	}
    }
    add_filter( 'messages_message_after_save', 'your_message_action_handler' );

    Thank you so, so much!❤

    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20644

    Glad it is working 🙂

    A few advice’s for future
    1. You do not need to append empty spaces like ”. $some_value as it is not needed in notification_new_array.

    2. instead of checking for $notification_current_txt !==”” you can simply do if( ! $notification_current_txt )
    That ill take care of empty array.

    Best regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 17
    maimaimemain on #20655

    Thank you. I appreciate what you have said.
    I will keep doing my best!

    Best regards
    maimaimemain

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #20662

    You are welcome.
    All the best 🙂

    Regards
    Brajesh

The topic ‘ [Resolved] Hook to activate at the timing of receiving the message’ is closed to new replies.

This topic is: resolved