Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

WIP: Inline Moderation #120

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3152bf1
Basic moderation architecture
wpillar Apr 8, 2015
0ba1740
Tidying up how inline moderation bar is rendered
wpillar Apr 12, 2015
817031f
Frontend handling for inline moderation
wpillar Apr 12, 2015
3377358
Making ModerationController more generic
wpillar Apr 12, 2015
86dd8a1
Add ModerationRequest to handle lots of stuff for us
wpillar Apr 12, 2015
3f6e73f
MovePost moderation and support for moderation options and forms
wpillar Apr 12, 2015
03bf647
Add ModerationInterface::visible()
wpillar Apr 13, 2015
c419ead
Adding MergePosts moderation
wpillar Apr 13, 2015
718fb8d
Make array moderations only appear when more than one post is selected
wpillar Apr 13, 2015
54d5a55
Delete post moderation
wpillar Apr 13, 2015
7bdd0ef
Adding approval of topic from forum show view
wpillar Apr 13, 2015
1fbfa32
When approving a topic, do the same to the first post too
wpillar Apr 13, 2015
f676994
Dynamic name of what is being selected
wpillar Apr 13, 2015
d85f7c9
DeleteTopic moderation
wpillar Apr 13, 2015
d150f4a
Make sure only checkboxes in topic-lists are counted
wpillar Apr 18, 2015
655c8d5
Support for multiple fields to be specified by a moderation presenter
wpillar Apr 18, 2015
a87af60
Adding close/open moderation to topics
wpillar Apr 18, 2015
a6707d2
MoveTopic moderation
wpillar Apr 18, 2015
b714ab7
Fixing a few issues and compiling the front-end
wpillar Apr 27, 2015
09f7c4d
Default posts and topics to being approved:
wpillar Apr 28, 2015
2c6423e
Forget the caches when moving a topic to a forum
wpillar Apr 28, 2015
16b6026
Update the last post of a forum when moving a topic to a forum
wpillar Apr 28, 2015
a877c66
Add new lines around the horizontal rule separating merged posts
wpillar Apr 28, 2015
54aea10
Inject a forum repository rather that invoking app()
wpillar Apr 28, 2015
5388323
Ensure first post approval/unapproval applies to the topic and vice v…
wpillar Apr 28, 2015
1101f68
Fixing the clear selection link in the moderation bar
wpillar Apr 28, 2015
d0ee30f
Don't allow replies to a closed topic
wpillar Apr 28, 2015
2b272cf
Adding mybb/standards as a dev dependency
wpillar Apr 28, 2015
0a09a04
Fixing standards issues
wpillar Apr 28, 2015
1c32f7f
Adding composer commands for the sniffer and sniff fixer
wpillar Apr 28, 2015
b44309a
Adding Licence headers
wpillar May 10, 2015
e83893f
Updating mybb/standards dependency
wpillar May 10, 2015
cda476b
Fixing some standards issues and the sniffer
wpillar May 10, 2015
94a1840
Add default attribute to closed column on the forums and topics tables:
wpillar May 10, 2015
7d9c386
Removing phpcs and standards install from cicle config
wpillar May 10, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion app/Database/Models/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

namespace MyBB\Core\Database\Models;

use MyBB\Core\Moderation\Moderations\CloseableInterface;
use MyBB\Core\Permissions\Interfaces\InheritPermissionInterface;
use MyBB\Core\Permissions\Traits\InheritPermissionableTrait;
use Kalnoy\Nestedset\Node;
use McCool\LaravelAutoPresenter\HasPresenter;

class Forum extends Node implements HasPresenter, InheritPermissionInterface
/**
* @property int id
*/
class Forum extends Node implements HasPresenter, InheritPermissionInterface, CloseableInterface
{
use InheritPermissionableTrait;

Expand Down Expand Up @@ -55,6 +59,13 @@ class Forum extends Node implements HasPresenter, InheritPermissionInterface
*/
protected $guarded = ['left_id', 'right_id', 'parent_id'];

/**
* @var array
*/
protected $casts = [
'id' => 'int'
];

/**
* Get the presenter class.
*
Expand Down Expand Up @@ -104,4 +115,20 @@ public function lastPostAuthor()
{
return $this->hasOne('MyBB\\Core\\Database\\Models\\User', 'id', 'last_post_user_id');
}

/**
* @return bool|int
*/
public function close()
{
return $this->update(['closed' => 1]);
}

/**
* @return bool|int
*/
public function open()
{
return $this->update(['closed' => 0]);
}
}
41 changes: 40 additions & 1 deletion app/Database/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
use MyBB\Core\Likes\Traits\LikeableTrait;
use MyBB\Core\Moderation\Moderations\ApprovableInterface;

class Post extends Model implements HasPresenter
/**
* @property int topic_id
*/
class Post extends Model implements HasPresenter, ApprovableInterface
{
use SoftDeletes;
use LikeableTrait;
Expand Down Expand Up @@ -57,6 +61,13 @@ class Post extends Model implements HasPresenter
*/
protected $dates = ['deleted_at', 'created_at', 'updated_at'];

/**
* @var array
*/
protected $casts = [
'topic_id' => 'int'
];

/**
* Get the presenter class.
*
Expand Down Expand Up @@ -86,4 +97,32 @@ public function author()
{
return $this->belongsTo('MyBB\\Core\\Database\\Models\\User', 'user_id');
}

/**
* @return bool|int
*/
public function approve()
{
$result = $this->update(['approved' => 1]);

if ($result && ! $this->topic->approved) {
$this->topic->approve();
}

return $result;
}

/**
* @return bool|int
*/
public function unapprove()
{
$result = $this->update(['approved' => 0]);

if ($result && $this->topic->approved) {
$this->topic->unapprove();
}

return $result;
}
}
61 changes: 60 additions & 1 deletion app/Database/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
use MyBB\Core\Moderation\Moderations\ApprovableInterface;
use MyBB\Core\Moderation\Moderations\CloseableInterface;

class Topic extends Model implements HasPresenter
/**
* @property int id
* @property Forum forum
* @property int forum_id
*/
class Topic extends Model implements HasPresenter, ApprovableInterface, CloseableInterface
{
use SoftDeletes;

Expand Down Expand Up @@ -55,6 +62,14 @@ class Topic extends Model implements HasPresenter
*/
protected $dates = ['deleted_at', 'created_at', 'updated_at'];

/**
* @var array
*/
protected $casts = [
'id' => 'int',
'forum_id' => 'int'
];

/**
* Get the presenter class.
*
Expand Down Expand Up @@ -137,4 +152,48 @@ public function lastPost()
{
return $this->hasOne('MyBB\\Core\\Database\\Models\\Post', 'id', 'last_post_id');
}

/**
* @return bool|int
*/
public function approve()
{
$result = $this->update(['approved' => 1]);

if ($result && ! $this->firstPost->approved) {
$this->firstPost->approve();
}

return $result;
}

/**
* @return bool|int
*/
public function unapprove()
{
$result = $this->update(['approved' => 0]);

if ($result && $this->firstPost->approved) {
$this->firstPost->unapprove();
}

return $result;
}

/**
* @return bool|int
*/
public function close()
{
return $this->update(['closed' => 1]);
}

/**
* @return bool|int
*/
public function open()
{
return $this->update(['closed' => 0]);
}
}
12 changes: 12 additions & 0 deletions app/Database/Repositories/Decorators/Forum/CachingDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use MyBB\Auth\Contracts\Guard;
use MyBB\Core\Database\Models\Forum;
use MyBB\Core\Database\Models\Post;
use MyBB\Core\Database\Models\Topic;
use MyBB\Core\Database\Repositories\ForumRepositoryInterface;
use MyBB\Core\Permissions\PermissionChecker;

Expand Down Expand Up @@ -184,4 +185,15 @@ private function filterUnviewableForums(Collection $forums)
);
});
}

/**
* @param Topic $topic
* @param Forum $forum
*/
public function moveTopicToForum(Topic $topic, Forum $forum)
{
$this->cache->forget('forums.index_tree');
$this->cache->forget('forums.all');
return $this->decoratedRepository->moveTopicToForum($topic, $forum);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to update the cache (simply call forget for now), otherwise the other functions will return invalid data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, thanks.

}
}
17 changes: 17 additions & 0 deletions app/Database/Repositories/Eloquent/ForumRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use MyBB\Core\Database\Models\Forum;
use MyBB\Core\Database\Models\Post;
use MyBB\Core\Database\Models\Topic;
use MyBB\Core\Database\Repositories\ForumRepositoryInterface;
use MyBB\Core\Permissions\PermissionChecker;

Expand Down Expand Up @@ -169,4 +170,20 @@ public function updateLastPost(Forum $forum, Post $post = null)

return $forum;
}

/**
* @param Topic $topic
* @param Forum $forum
*/
public function moveTopicToForum(Topic $topic, Forum $forum)
{
$topic->forum->decrement('num_posts');

$topic->forum_id = $forum->id;
$topic->save();

$topic->forum->increment('num_posts');

$this->updateLastPost($forum);
}
}
34 changes: 34 additions & 0 deletions app/Database/Repositories/Eloquent/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace MyBB\Core\Database\Repositories\Eloquent;

use Illuminate\Support\Collection;
use MyBB\Auth\Contracts\Guard;
use MyBB\Core\Database\Models\Post;
use MyBB\Core\Database\Models\Topic;
Expand Down Expand Up @@ -357,4 +358,37 @@ public function getPostsByIds(array $postIds)
$query->whereNotIn('forum_id', $unviewableForums);
})->get();
}

/**
* @param Post[] $posts
*
* @return Post
*/
public function mergePosts(array $posts)
{
if (! is_array_of($posts, 'MyBB\Core\Database\Models\Post')) {
throw new \InvalidArgumentException('$posts must be an array of Post objects');
}

$collection = new Collection($posts);
$collection = $collection->sortBy('created_at');

$firstPost = $collection->shift();
$firstPostContent = $firstPost->content;

foreach ($collection as $post) {
if ($post->author->id !== $firstPost->author->id) {
throw new \InvalidArgumentException("All posts being merged must have the same author");
}

$firstPostContent .= "\n[hr]\n". $post->content;
$this->deletePost($post);
}

$firstPost->content = $firstPostContent;
$firstPost->content_parsed = $this->formatter->parse($firstPost->content);
$firstPost->save();

return $firstPost;
}
}
23 changes: 23 additions & 0 deletions app/Database/Repositories/Eloquent/TopicRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,27 @@ public function updateLastPost(Topic $topic, Post $post = null)

return $topic;
}

/**
* @param Post $post
* @param Topic $topic
*/
public function movePostToTopic(Post $post, Topic $topic)
{
$post->topic->decrement('num_posts');
$post->topic->forum->decrement('num_posts');

$post->topic_id = $topic->id;
$post->save();

$topic->increment('num_posts');
$topic->forum->increment('num_posts');
// $topic->update([
// 'last_post_id' => $post->id
// ]);
// $topic->forum->update([
// 'last_post_id' => $post->id,
// 'last_post_user_id' => $post->author->id
// ]);
}
}
7 changes: 7 additions & 0 deletions app/Database/Repositories/ForumRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use MyBB\Core\Database\Models\Forum;
use MyBB\Core\Database\Models\Post;
use MyBB\Core\Database\Models\Topic;

interface ForumRepositoryInterface
{
Expand Down Expand Up @@ -76,4 +77,10 @@ public function incrementTopicCount($id = 0);
* @return mixed
*/
public function updateLastPost(Forum $forum, Post $post = null);

/**
* @param Topic $topic
* @param Forum $forum
*/
public function moveTopicToForum(Topic $topic, Forum $forum);
}
7 changes: 7 additions & 0 deletions app/Database/Repositories/PostRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ public function deletePostsForTopic(Topic $topic);
* @return mixed
*/
public function getPostsByIds(array $postIds);

/**
* @param Post[] $posts
*
* @return Post
*/
public function mergePosts(array $posts);
}
15 changes: 15 additions & 0 deletions app/Database/Repositories/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MyBB\Core\Database\Repositories;

use Illuminate\Database\Eloquent\Model;

interface RepositoryInterface
{
/**
* @param int $id
*
* @return Model
*/
public function find($id);
}
6 changes: 6 additions & 0 deletions app/Database/Repositories/TopicRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,10 @@ public function restoreTopic(Topic $topic);
* @return mixed
*/
public function updateLastPost(Topic $topic, Post $post = null);

/**
* @param Post $post
* @param Topic $topic
*/
public function movePostToTopic(Post $post, Topic $topic);
}
Loading