/** * @param PostReply $command * @return CommentPost * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(PostReply $command) { $actor = $command->actor; // Make sure the user has permission to reply to this discussion. First, // make sure the discussion exists and that the user has permission to // view it; if not, fail with a ModelNotFound exception so we don't give // away the existence of the discussion. If the user is allowed to view // it, check if they have permission to reply. $discussion = $this->discussions->findOrFail($command->discussionId, $actor); $this->assertCan($actor, 'reply', $discussion); // Create a new Post entity, persist it, and dispatch domain events. // Before persistence, though, fire an event to give plugins an // opportunity to alter the post entity based on data in the command. $post = CommentPost::reply($discussion->id, array_get($command->data, 'attributes.content'), $actor->id, $command->ipAddress); if ($actor->isAdmin() && ($time = array_get($command->data, 'attributes.time'))) { $post->time = new DateTime($time); } $this->events->fire(new PostWillBeSaved($post, $actor, $command->data)); $this->validator->assertValid($post->getAttributes()); $post->save(); $this->notifications->onePerUser(function () use($post, $actor) { $this->dispatchEventsFor($post, $actor); }); return $post; }
/** * @param Discussion $discussion * @param User $user * @param $isLocked */ protected function lockedChanged(Discussion $discussion, User $user, $isLocked) { $post = DiscussionLockedPost::reply($discussion->id, $user->id, $isLocked); $post = $discussion->mergePost($post); if ($discussion->start_user_id !== $user->id) { $notification = new DiscussionLockedBlueprint($post); $this->notifications->sync($notification, $post->exists ? [$discussion->startUser] : []); } }
/** * @param \Flarum\Event\DiscussionWasRenamed $event */ public function whenDiscussionWasRenamed(DiscussionWasRenamed $event) { $post = DiscussionRenamedPost::reply($event->discussion->id, $event->actor->id, $event->oldTitle, $event->discussion->title); $post = $event->discussion->mergePost($post); if ($event->discussion->start_user_id !== $event->actor->id) { $blueprint = new DiscussionRenamedBlueprint($post); if ($post->exists) { $this->notifications->sync($blueprint, [$event->discussion->startUser]); } else { $this->notifications->delete($blueprint); } } }
/** * @param Post $post * @param array $mentioned */ protected function sync(Post $post, array $mentioned) { $post->mentionsUsers()->sync($mentioned); $users = User::whereIn('id', $mentioned)->get()->filter(function ($user) use($post) { return $post->isVisibleTo($user) && $user->id !== $post->user->id; })->all(); $this->notifications->sync(new UserMentionedBlueprint($post), $users); }
/** * @param Post $reply * @param array $mentioned */ protected function sync(Post $reply, array $mentioned) { $reply->mentionsPosts()->sync($mentioned); $posts = Post::with('user')->whereIn('id', $mentioned)->get()->filter(function ($post) use($reply) { return $post->user && $post->user->id !== $reply->user_id; })->all(); foreach ($posts as $post) { $this->notifications->sync(new PostMentionedBlueprint($post, $reply), [$post->user]); } }
/** * @param Post $post * @param User $user * @param array $recipients */ public function sync(Post $post, User $user, array $recipients) { if ($post->user->id != $user->id) { $this->notifications->sync(new PostLikedBlueprint($post, $user), $recipients); } }
/** * @param PostWasDeleted $event */ public function whenPostWasDeleted(PostWasDeleted $event) { $this->notifications->delete($this->getNotification($event->post)); }