Flarum\Core\Notification\NotificationSyncer::sync PHP Method

sync() public method

Sync a notification so that it is visible to the specified users, and not visible to anyone else. If it is being made visible for the first time, attempt to send the user an email.
public sync ( Flarum\Core\Notification\BlueprintInterface $blueprint, array $users ) : void
$blueprint Flarum\Core\Notification\BlueprintInterface
$users array
return void
    public function sync(BlueprintInterface $blueprint, array $users)
    {
        $attributes = $this->getAttributes($blueprint);
        // Find all existing notification records in the database matching this
        // blueprint. We will begin by assuming that they all need to be
        // deleted in order to match the provided list of users.
        $toDelete = Notification::where($attributes)->get();
        $toUndelete = [];
        $newRecipients = [];
        // For each of the provided users, check to see if they already have
        // a notification record in the database. If they do, we will make sure
        // it isn't marked as deleted. If they don't, we will want to create a
        // new record for them.
        foreach ($users as $user) {
            if (!$user instanceof User) {
                continue;
            }
            $existing = $toDelete->first(function ($i, $notification) use($user) {
                return $notification->user_id === $user->id;
            });
            if ($existing) {
                $toUndelete[] = $existing->id;
                $toDelete->forget($toDelete->search($existing));
            } elseif (!static::$onePerUser || !in_array($user->id, static::$sentTo)) {
                $newRecipients[] = $user;
                static::$sentTo[] = $user->id;
            }
        }
        // Delete all of the remaining notification records which weren't
        // removed from this collection by the above loop. Un-delete the
        // existing records that we want to keep.
        if (count($toDelete)) {
            $this->setDeleted($toDelete->lists('id')->all(), true);
        }
        if (count($toUndelete)) {
            $this->setDeleted($toUndelete, false);
        }
        // Create a notification record, and send an email, for all users
        // receiving this notification for the first time (we know because they
        // didn't have a record in the database).
        if (count($newRecipients)) {
            $this->sendNotifications($blueprint, $newRecipients);
        }
    }

Usage Example

 /**
  * @param PostWasPosted $event
  */
 public function whenPostWasPosted(PostWasPosted $event)
 {
     $post = $event->post;
     $discussion = $post->discussion;
     $notify = $discussion->readers()->where('users.id', '!=', $post->user_id)->where('users_discussions.subscription', 'follow')->where('users_discussions.read_number', $discussion->last_post_number)->get();
     $this->notifications->sync($this->getNotification($event->post), $notify->all());
 }
All Usage Examples Of Flarum\Core\Notification\NotificationSyncer::sync