/** * 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. * * @param \Flarum\Core\Notifications\NotificationInterface $notification * @param \Flarum\Core\Models\User[] $users * @return void */ public function sync(NotificationInterface $notification, array $users) { $attributes = ['type' => $notification::getType(), 'sender_id' => $notification->getSender()->id, 'subject_id' => $notification->getSubject()->id, 'data' => ($data = $notification->getData()) ? json_encode($data) : null]; $toDelete = Notification::where($attributes)->get(); $toUndelete = []; $newRecipients = []; foreach ($users as $user) { $existing = $toDelete->where('user_id', $user->id)->first(); if (($k = $toDelete->search($existing)) !== false) { $toUndelete[] = $existing->id; $toDelete->pull($k); } elseif (!$this->onePerUser || !in_array($user->id, $this->sentTo)) { $newRecipients[] = $user; $this->sentTo[] = $user->id; } } if (count($toDelete)) { Notification::whereIn('id', $toDelete->lists('id'))->update(['is_deleted' => true]); } if (count($toUndelete)) { Notification::whereIn('id', $toUndelete)->update(['is_deleted' => false]); } if (count($newRecipients)) { $now = Carbon::now('utc')->toDateTimeString(); Notification::insert(array_map(function ($user) use($attributes, $notification, $now) { return $attributes + ['user_id' => $user->id, 'time' => $now]; }, $newRecipients)); foreach ($newRecipients as $user) { if ($user->shouldEmail($notification::getType())) { $this->mailer->send($notification, $user); } } } }
public function extend(Application $app) { $class = $this->class; Notification::registerType($class); User::registerPreference(User::notificationPreferenceKey($class::getType(), 'alert'), 'boolval', in_array('alert', $this->enabled)); if ($class::isEmailable()) { User::registerPreference(User::notificationPreferenceKey($class::getType(), 'email'), 'boolval', in_array('email', $this->enabled)); } NotificationSerializer::$subjects[$class::getType()] = $this->serializer; }
public function handle($command) { $user = $command->user; if (!$user->exists) { throw new PermissionDeniedException(); } $notification = Notification::where('user_id', $user->id)->findOrFail($command->notificationId); $notification->read(); $notification->save(); $this->dispatchEventsFor($notification); return $notification; }
public function findByUser(User $user, $limit = null, $offset = 0) { $primaries = Notification::select(DB::raw('MAX(id) AS id'), DB::raw('SUM(is_read = 0) AS unread_count'))->where('user_id', $user->id)->whereIn('type', array_filter(array_keys(Notification::getTypes()), [$user, 'shouldAlert']))->where('is_deleted', false)->groupBy('type', 'subject_id')->orderBy('time', 'desc')->skip($offset)->take($limit); return Notification::with('subject')->select('notifications.*', 'p.unread_count')->mergeBindings($primaries->getQuery())->join(DB::raw('(' . $primaries->toSql() . ') p'), 'notifications.id', '=', 'p.id')->orderBy('time', 'desc')->get(); }
public function getUnreadNotificationsCount() { $types = array_keys(Notification::getTypes()); return $this->notifications()->whereIn('type', array_filter($types, [$this, 'shouldAlert']))->where('time', '>', $this->notification_read_time ?: 0)->where('is_read', 0)->count(\DB::raw('DISTINCT type, subject_id')); }