/** * @param PostWillBeSaved $event */ public function unapproveNewPosts(PostWillBeSaved $event) { $post = $event->post; if (!$post->exists) { $ability = $post->discussion->number_index == 0 ? 'startWithoutApproval' : 'replyWithoutApproval'; if ($event->actor->can($ability, $post->discussion)) { if ($post->is_approved === null) { $post->is_approved = true; } return; } $post->is_approved = false; $post->afterSave(function ($post) { if ($post->number == 1) { $post->discussion->is_approved = false; $post->discussion->save(); } $flag = new Flag(); $flag->post_id = $post->id; $flag->type = 'approval'; $flag->time = time(); $flag->save(); }); } }
/** * {@inheritdoc} */ protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); $actor->flags_read_time = time(); $actor->save(); return Flag::whereVisibleTo($actor)->with($this->extractInclude($request))->latest('flags.time')->groupBy('post_id')->get(); }
protected function data(JsonApiRequest $request, Document $document) { $actor = $request->actor; $actor->flags_read_time = time(); $actor->save(); return Flag::whereVisibleTo($actor)->with($request->include)->latest('flags.time')->groupBy('post_id')->get(); }
/** * @param PostWillBeSaved $event */ public function validatePost(PostWillBeSaved $event) { $post = $event->post; if ($post->exists || $post->user->groups()->count()) { return; } $isSpam = $this->getAkismet()->isSpam($post->content, $post->user->username, $post->user->email, null, 'comment'); if ($isSpam) { $post->is_approved = false; $post->is_spam = true; $post->afterSave(function ($post) { $flag = new Flag(); $flag->post_id = $post->id; $flag->type = 'akismet'; $flag->time = time(); $flag->save(); }); } }
/** * @param CreateFlag $command * @return Flag * @throws InvalidParameterException */ public function handle(CreateFlag $command) { $actor = $command->actor; $data = $command->data; $postId = array_get($data, 'relationships.post.data.id'); $post = $this->posts->findOrFail($postId, $actor); if (!$post instanceof CommentPost) { throw new InvalidParameterException(); } $this->assertCan($actor, 'flag', $post); Flag::unguard(); $flag = Flag::firstOrNew(['post_id' => $post->id, 'user_id' => $actor->id]); $flag->post_id = $post->id; $flag->user_id = $actor->id; $flag->type = 'user'; $flag->reason = array_get($data, 'attributes.reason'); $flag->reason_detail = array_get($data, 'attributes.reasonDetail'); $flag->time = time(); $flag->save(); return $flag; }
public function addAttributes(ApiAttributes $event) { if ($event->serializer instanceof ForumSerializer) { $event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags'); if ($event->attributes['canViewFlags']) { $query = Flag::whereVisibleTo($event->actor); if ($time = $event->actor->flags_read_time) { $query->where('flags.time', '>', $time); } $event->attributes['unreadFlagsCount'] = $query->distinct('flags.post_id')->count(); } } if ($event->serializer instanceof PostSerializer) { $event->attributes['canFlag'] = $event->model->can($event->actor, 'flag'); } }
/** * @param User $actor * @return int */ protected function getNewFlagsCount(User $actor) { $query = Flag::whereVisibleTo($actor); if ($time = $actor->flags_read_time) { $query->where('flags.time', '>', $time); } return $query->distinct()->count('flags.post_id'); }