PluginProbe
ActivityPub / 9.3.0
ActivityPub v9.3.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-event-stream.php

class-event-stream.php in ActivityPub 9.3.0, at includes/class-event-stream.php

62 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Event Stream signal writer.
4 *
5 * Sets lightweight transient signals when new items arrive in
6 * outbox or inbox collections, so the SSE polling loop can avoid
7 * unnecessary database queries.
8 *
9 * @package Activitypub
10 * @see https://swicg.github.io/activitypub-api/sse
11 * @since 8.1.0
12 */
13
14 namespace Activitypub;
15
16 /**
17 * Event_Stream class.
18 *
19 * Hooks into outbox and inbox actions to set transient signals
20 * that the SSE controller checks during its polling loop.
21 *
22 * @since 8.1.0
23 */
24 class Event_Stream {
25 /**
26 * Initialize the event stream signals.
27 */
28 public static function init() {
29 \add_action( 'post_activitypub_add_to_outbox', array( self::class, 'signal_outbox' ), 10, 3 );
30 \add_action( 'activitypub_handled_inbox', array( self::class, 'signal_inbox' ), 10, 2 );
31 }
32
33 /**
34 * Set a transient signal when a new item is added to the outbox.
35 *
36 * @param int $outbox_activity_id The outbox post ID.
37 * @param \Activitypub\Activity\Activity $activity The activity object.
38 * @param int $user_id The user ID.
39 */
40 public static function signal_outbox( $outbox_activity_id, $activity, $user_id ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
41 $signal_key = \sprintf( 'activitypub_sse_signal_%s_outbox', $user_id );
42 \set_transient( $signal_key, \time(), HOUR_IN_SECONDS );
43 }
44
45 /**
46 * Set transient signals when a new item is added to the inbox.
47 *
48 * @param array $data The activity data array.
49 * @param array $user_ids The user IDs that received the activity.
50 */
51 public static function signal_inbox( $data, $user_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
52 if ( ! \is_array( $user_ids ) ) {
53 $user_ids = array( $user_ids );
54 }
55
56 foreach ( $user_ids as $user_id ) {
57 $signal_key = \sprintf( 'activitypub_sse_signal_%s_inbox', $user_id );
58 \set_transient( $signal_key, \time(), HOUR_IN_SECONDS );
59 }
60 }
61 }
62