A notification record is associated with a user, and shows up in their
notification list. A notification indicates that something has happened that
the user should know about, like if a user's discussion was renamed by
someone else.
Each notification record has a *type*. The type determines how the record
looks in the notifications list, and what *subject* is associated with it.
For example, the 'discussionRenamed' notification type represents that
someone renamed a user's discussion. Its subject is a discussion, of which
the ID is stored in the subject_id column.
/** * @param ReadNotification $command * @return \Flarum\Core\Notification * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(ReadNotification $command) { $actor = $command->actor; $this->assertRegistered($actor); $notification = Notification::where('user_id', $actor->id)->findOrFail($command->notificationId); Notification::where(['user_id' => $actor->id, 'type' => $notification->type, 'subject_id' => $notification->subject_id])->update(['is_read' => true]); $notification->is_read = true; return $notification; }
/** * Register notification types. */ public function registerNotificationTypes() { $blueprints = ['Flarum\\Core\\Notification\\DiscussionRenamedBlueprint' => ['alert']]; $this->app->make('events')->fire(new ConfigureNotificationTypes($blueprints)); foreach ($blueprints as $blueprint => $enabled) { Notification::setSubjectModel($type = $blueprint::getType(), $blueprint::getSubjectModel()); User::addPreference(User::getNotificationPreferenceKey($type, 'alert'), 'boolval', in_array('alert', $enabled)); if ((new ReflectionClass($blueprint))->implementsInterface('Flarum\\Core\\Notification\\MailableInterface')) { User::addPreference(User::getNotificationPreferenceKey($type, 'email'), 'boolval', in_array('email', $enabled)); } } }
/** * Get the notification types that should be alerted to this user, according * to their preferences. * * @return array */ public function getAlertableNotificationTypes() { $types = array_keys(Notification::getSubjectModels()); return array_filter($types, [$this, 'shouldAlert']); }
/** * Set the deleted status of a list of notification records. * * @param int[] $ids * @param bool $isDeleted */ protected function setDeleted(array $ids, $isDeleted) { Notification::whereIn('id', $ids)->update(['is_deleted' => $isDeleted]); }
/** * Mark all of a user's notifications as read. * * @param User $user * * @return void */ public function markAllAsRead(User $user) { Notification::where('user_id', $user->id)->update(['is_read' => true]); }