You are here

function activity_creator_activity_insert in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_creator/activity_creator.module \activity_creator_activity_insert()
  2. 8.8 modules/custom/activity_creator/activity_creator.module \activity_creator_activity_insert()
  3. 10.3.x modules/custom/activity_creator/activity_creator.module \activity_creator_activity_insert()
  4. 10.0.x modules/custom/activity_creator/activity_creator.module \activity_creator_activity_insert()
  5. 10.2.x modules/custom/activity_creator/activity_creator.module \activity_creator_activity_insert()

Implements hook_ENTITY_TYPE_insert().

File

modules/custom/activity_creator/activity_creator.module, line 153
Activity Creator module file.

Code

function activity_creator_activity_insert(ActivityInterface $activity) {

  // We only care about status for activities when we're dealing with
  // notifications.
  if (in_array('notifications', $activity
    ->getDestinations(), TRUE)) {

    // Get recipients.
    $recipients = $activity
      ->getRecipient();
    if (is_array($recipients)) {
      $uids = [];

      // Loop through the recipients and track the ID if the target is a user.
      foreach ($recipients as $recipient) {
        if ($recipient['target_type'] === 'user') {
          $uids[] = $recipient['target_id'];
        }
      }
      if (!empty($uids)) {

        // If we have users, we insert a status for each user for the activity.
        $connection = \Drupal::database();
        $query = $connection
          ->insert('activity_notification_status')
          ->fields([
          'uid',
          'aid',
        ]);
        foreach ($uids as $uid) {
          $query
            ->values([
            'uid' => $uid,
            'aid' => $activity
              ->id(),
          ]);
        }
        $query
          ->execute();
      }
    }
  }
}