@@ -28,14 +28,23 @@ class Posts {
2828	/** 
2929	 * Add an object to the collection. 
3030	 * 
31- 	 * @param array $activity The activity object data. 
32- 	 * @param int   $user_id  The  local user ID . 
31+ 	 * @param array      $activity    The activity object data. 
32+ 	 * @param int|int[] $recipients The id(s) of the  local blog- user(s) . 
3333	 * 
3434	 * @return \WP_Post|\WP_Error The object post or WP_Error on failure. 
3535	 */ 
36- 	public  static  function  add ( $ activity$ user_id
36+ 	public  static  function  add ( $ activity$ recipients
37+ 		$ recipientsarray ) $ recipients
3738		$ activity_object$ activity'object ' ];
38- 		$ actorfetch_by_uri ( object_to_uri ( $ activity_object'attributedTo ' ] ) );
39+ 
40+ 		$ existingself ::get_by_guid ( $ activity_object'id ' ] );
41+ 		// If post exists, call update instead. 
42+ 		if  ( ! \is_wp_error ( $ existing
43+ 			return  self ::update ( $ activity$ recipients
44+ 		}
45+ 
46+ 		// Post doesn't exist, create new post. 
47+ 		$ actorfetch_by_uri ( object_to_uri ( $ activity_object'attributedTo ' ] ) );
3948
4049		if  ( \is_wp_error ( $ actor
4150			return  $ actor
@@ -49,7 +58,11 @@ public static function add( $activity, $user_id ) {
4958		}
5059
5160		\add_post_meta ( $ post_id'_activitypub_remote_actor_id ' , $ actorID  );
52- 		\add_post_meta ( $ post_id'_activitypub_user_id ' , $ user_id
61+ 
62+ 		// Add recipients as separate meta entries after post is created. 
63+ 		foreach  ( $ recipientsas  $ user_id
64+ 			self ::add_recipient ( $ post_id$ user_id
65+ 		}
5366
5467		self ::add_taxonomies ( $ post_id$ activity_object
5568
@@ -104,12 +117,14 @@ public static function get_by_guid( $guid ) {
104117	/** 
105118	 * Update an object in the collection. 
106119	 * 
107- 	 * @param array $activity The activity object data. 
108- 	 * @param int   $user_id  The  local user ID . 
120+ 	 * @param array      $activity    The activity object data. 
121+ 	 * @param int|int[] $recipients The id(s) of the  local blog- user(s) . 
109122	 * 
110123	 * @return \WP_Post|\WP_Error The updated object post or WP_Error on failure. 
111124	 */ 
112- 	public  static  function  update ( $ activity$ user_id
125+ 	public  static  function  update ( $ activity$ recipients
126+ 		$ recipientsarray ) $ recipients
127+ 
113128		$ postself ::get_by_guid ( $ activity'object ' ]['id ' ] );
114129		if  ( \is_wp_error ( $ post
115130			return  $ post
@@ -123,9 +138,9 @@ public static function update( $activity, $user_id ) {
123138			return  $ post_id
124139		}
125140
126- 		$ post_meta  =  \get_post_meta (  $ post_id ,  ' _activitypub_user_id ' ,  false  ); 
127- 		if  ( \is_array (  $ post_meta  ) && !  \in_array ( ( string )  $ user_id,  $ post_meta ,  true  )  ) {
128- 			\add_post_meta ( $ post_id,  ' _activitypub_user_id ' $ user_id
141+ 		// Add new recipients using add_recipient (handles deduplication). 
142+ 		foreach  ( $ recipients   as   $ user_id
143+ 			self :: add_recipient ( $ post_id$ user_id
129144		}
130145
131146		self ::add_taxonomies ( $ post_id$ activity'object ' ] );
@@ -250,4 +265,89 @@ public static function get_by_remote_actor_id( $actor_id ) {
250265
251266		return  $ queryposts ;
252267	}
268+ 
269+ 	/** 
270+ 	 * Get all recipients for a post. 
271+ 	 * 
272+ 	 * @param int $post_id The post ID. 
273+ 	 * 
274+ 	 * @return int[] Array of user IDs who are recipients. 
275+ 	 */ 
276+ 	public  static  function  get_recipients ( $ post_id
277+ 		// Get all meta values with key '_activitypub_user_id' (single => false). 
278+ 		$ recipients\get_post_meta ( $ post_id'_activitypub_user_id ' , false  );
279+ 		$ recipients\array_map ( 'intval ' , $ recipients
280+ 
281+ 		return  $ recipients
282+ 	}
283+ 
284+ 	/** 
285+ 	 * Check if a user is a recipient of a post. 
286+ 	 * 
287+ 	 * @param int $post_id The post ID. 
288+ 	 * @param int $user_id The user ID to check. 
289+ 	 * 
290+ 	 * @return bool True if user is a recipient, false otherwise. 
291+ 	 */ 
292+ 	public  static  function  has_recipient ( $ post_id$ user_id
293+ 		$ recipientsself ::get_recipients ( $ post_id
294+ 
295+ 		return  \in_array ( (int ) $ user_id$ recipientstrue  );
296+ 	}
297+ 
298+ 	/** 
299+ 	 * Add a recipient to an existing post. 
300+ 	 * 
301+ 	 * @param int $post_id The post ID. 
302+ 	 * @param int $user_id The user ID to add. 
303+ 	 * 
304+ 	 * @return bool True on success, false on failure. 
305+ 	 */ 
306+ 	public  static  function  add_recipient ( $ post_id$ user_id
307+ 		$ user_idint ) $ user_id
308+ 		// Allow 0 for blog user, but reject negative values. 
309+ 		if  ( $ user_id0  ) {
310+ 			return  false ;
311+ 		}
312+ 
313+ 		// Check if already a recipient. 
314+ 		if  ( self ::has_recipient ( $ post_id$ user_id
315+ 			return  true ;
316+ 		}
317+ 
318+ 		// Add new recipient as separate meta entry. 
319+ 		return  (bool ) \add_post_meta ( $ post_id'_activitypub_user_id ' , $ user_idfalse  );
320+ 	}
321+ 
322+ 	/** 
323+ 	 * Add multiple recipients to an existing post. 
324+ 	 * 
325+ 	 * @param int   $post_id  The post ID. 
326+ 	 * @param int[] $user_ids The user ID or array of user IDs to add. 
327+ 	 */ 
328+ 	public  static  function  add_recipients ( $ post_id$ user_ids
329+ 		foreach  ( $ user_idsas  $ user_id
330+ 			self ::add_recipient ( $ post_id$ user_id
331+ 		}
332+ 	}
333+ 
334+ 	/** 
335+ 	 * Remove a recipient from a post. 
336+ 	 * 
337+ 	 * @param int $post_id The post ID. 
338+ 	 * @param int $user_id The user ID to remove. 
339+ 	 * 
340+ 	 * @return bool True on success, false on failure. 
341+ 	 */ 
342+ 	public  static  function  remove_recipient ( $ post_id$ user_id
343+ 		$ user_idint ) $ user_id
344+ 
345+ 		// Allow 0 for blog user, but reject negative values. 
346+ 		if  ( $ user_id0  ) {
347+ 			return  false ;
348+ 		}
349+ 
350+ 		// Delete the specific meta entry with this value. 
351+ 		return  \delete_post_meta ( $ post_id'_activitypub_user_id ' , $ user_id
352+ 	}
253353}
0 commit comments