Inheritance: extends Flarum\Database\AbstractModel, use trait Flarum\Core\Support\EventGeneratorTrait, use trait Flarum\Core\Support\ScopeVisibilityTrait
Example #1
0
 public function registerPostTypes()
 {
     $models = ['Flarum\\Core\\Post\\CommentPost', 'Flarum\\Core\\Post\\DiscussionRenamedPost'];
     $this->app->make('events')->fire(new ConfigurePostTypes($models));
     foreach ($models as $model) {
         Post::setModel($model::$type, $model);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function saveAfter(Post $previous = null)
 {
     // If the previous post is another 'discussion locked' post, and it's
     // by the same user, then we can merge this post into it. If we find
     // that we've in fact reverted the locked status, delete it. Otherwise,
     // update its content.
     if ($previous instanceof static && $this->user_id === $previous->user_id) {
         if ($previous->content['locked'] != $this->content['locked']) {
             $previous->delete();
         } else {
             $previous->content = $this->content;
             $previous->save();
         }
         return $previous;
     }
     $this->save();
     return $this;
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function match($string)
 {
     $discussionIds = Post::where('type', 'comment')->whereRaw('MATCH (`content`) AGAINST (? IN BOOLEAN MODE)', [$string])->orderByRaw('MATCH (`content`) AGAINST (?) DESC', [$string])->lists('discussion_id', 'id');
     $relevantPostIds = [];
     foreach ($discussionIds as $postId => $discussionId) {
         $relevantPostIds[$discussionId][] = $postId;
     }
     return $relevantPostIds;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function saveAfter(Post $previous = null)
 {
     // If the previous post is another 'discussion renamed' post, and it's
     // by the same user, then we can merge this post into it. If we find
     // that we've in fact reverted the title, delete it. Otherwise, update
     // its content.
     if ($previous instanceof static && $this->user_id === $previous->user_id) {
         if ($previous->content[0] == $this->content[1]) {
             $previous->delete();
         } else {
             $previous->content = static::buildContent($previous->content[0], $this->content[1]);
             $previous->save();
         }
         return $previous;
     }
     $this->save();
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function match($string)
 {
     $discussionIds = Post::where('type', 'comment')->where('content', 'like', "%{$string}%")->lists('discussion_id', 'id');
     $relevantPostIds = [];
     foreach ($discussionIds as $postId => $discussionId) {
         $relevantPostIds[$discussionId][] = $postId;
     }
     return $relevantPostIds;
 }
Example #6
0
 /**
  * Find posts by their IDs, optionally making sure they are visible to a
  * certain user.
  *
  * @param array $ids
  * @param \Flarum\Core\User|null $actor
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function findByIds(array $ids, User $actor = null)
 {
     $discussions = $this->getDiscussionsForPosts($ids, $actor);
     $posts = Post::whereIn('id', $ids)->where(function ($query) use($discussions, $actor) {
         foreach ($discussions as $discussion) {
             $query->orWhere(function ($query) use($discussion, $actor) {
                 $query->where('discussion_id', $discussion->id);
                 event(new ScopePostVisibility($discussion, $query, $actor));
             });
         }
     })->get();
     $posts = $posts->sort(function ($a, $b) use($ids) {
         $aPos = array_search($a->id, $ids);
         $bPos = array_search($b->id, $ids);
         if ($aPos === $bPos) {
             return 0;
         }
         return $aPos < $bPos ? -1 : 1;
     });
     return $posts;
 }
Example #7
0
 /**
  * @param array $ids
  * @param User|null $actor
  * @return mixed
  */
 protected function queryIds(array $ids, User $actor = null)
 {
     $discussions = $this->getDiscussionsForPosts($ids, $actor);
     return Post::whereIn('id', $ids)->where(function ($query) use($discussions, $actor) {
         foreach ($discussions as $discussion) {
             $query->orWhere(function ($query) use($discussion, $actor) {
                 $query->where('discussion_id', $discussion->id);
                 event(new ScopePostVisibility($discussion, $query, $actor));
             });
         }
         $query->orWhereRaw('FALSE');
     });
 }
 /**
  * @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 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);
 }
Example #10
0
 /**
  * @param User $actor
  * @return bool
  */
 protected function isFlooding(User $actor)
 {
     return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
 }