1
0
mirror of https://github.com/flarum/core.git synced 2025-08-01 06:00:24 +02:00

Allow author to delete discussion if there are no replies

Also disallow the first post in a discussion to be deleted or hidden
(thus preventing discussions with zero posts)

closes flarum/core#90 closes flarum/core#92
This commit is contained in:
Toby Zerner
2015-06-23 10:34:33 +09:30
parent 3a7efe202e
commit 64e5d50533
8 changed files with 80 additions and 30 deletions

View File

@@ -20,15 +20,16 @@ class Discussion extends Model
* @var array
*/
public static $rules = [
'title' => 'required',
'start_time' => 'required|date',
'comments_count' => 'integer',
'start_user_id' => 'integer',
'start_post_id' => 'integer',
'last_time' => 'date',
'last_user_id' => 'integer',
'last_post_id' => 'integer',
'last_post_number' => 'integer'
'title' => 'required',
'start_time' => 'required|date',
'comments_count' => 'integer',
'participants_count' => 'integer',
'start_user_id' => 'integer',
'start_post_id' => 'integer',
'last_time' => 'date',
'last_user_id' => 'integer',
'last_post_id' => 'integer',
'last_post_number' => 'integer'
];
protected static $relationships = [];
@@ -175,6 +176,18 @@ class Discussion extends Model
return $this;
}
/**
* Refresh the discussion's participants count.
*
* @return $this
*/
public function refreshParticipantsCount()
{
$this->participants_count = $this->participants()->count('users.id');
return $this;
}
/**
* Specify that a post was added to this discussion during this request
* for later retrieval.
@@ -255,6 +268,16 @@ class Discussion extends Model
return $this->posts()->where('type', 'comment')->whereNull('hide_time');
}
/**
* Define the relationship with the discussion's participants.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function participants()
{
return User::join('posts', 'posts.user_id', '=', 'users.id')->where('posts.discussion_id', $this->id)->select('users.*')->distinct();
}
/**
* Define the relationship with the discussion's first post.
*