/** * @param ReadNotification $command * @return Notification * @throws \Flarum\Core\Exceptions\PermissionDeniedException */ public function handle(ReadNotification $command) { $actor = $command->actor; if ($actor->isGuest()) { throw new PermissionDeniedException(); } $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]); return $notification; }
/** * @param ReadNotification $command * @return Notification * @throws \Flarum\Core\Exceptions\PermissionDeniedException */ public function handle(ReadNotification $command) { $actor = $command->actor; if ($actor->isGuest()) { throw new PermissionDeniedException(); } $notification = Notification::where('user_id', $actor->id)->findOrFail($command->notificationId); $notification->read(); $notification->save(); return $notification; }
/** * Register notification types. * * @return void */ public function registerNotificationTypes() { $blueprints = ['Flarum\\Core\\Notifications\\DiscussionRenamedBlueprint' => ['alert']]; event(new RegisterNotificationTypes($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\\Notifications\\MailableBlueprint')) { User::addPreference(User::getNotificationPreferenceKey($type, 'email'), 'boolval', in_array('email', $enabled)); } } }
/** * Find a user's notifications. * * @param User $user * @param int|null $limit * @param int $offset * @return \Illuminate\Database\Eloquent\Collection */ public function findByUser(User $user, $limit = null, $offset = 0) { $primaries = Notification::select(app('flarum.db')->raw('MAX(id) AS id'), app('flarum.db')->raw('SUM(is_read = 0) AS unread_count'))->where('user_id', $user->id)->whereIn('type', $user->getAlertableNotificationTypes())->where('is_deleted', false)->groupBy('type', 'subject_id')->orderByRaw('MAX(time) DESC')->skip($offset)->take($limit); return Notification::select('notifications.*', app('flarum.db')->raw('p.unread_count'))->mergeBindings($primaries->getQuery())->join(app('flarum.db')->raw('(' . $primaries->toSql() . ') p'), 'notifications.id', '=', app('flarum.db')->raw('p.id'))->latest('time')->get(); }
/** * 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]); }
/** * 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']); }
/** * 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]); }