went over most of the changed attributes from the other pr

This commit is contained in:
Daniel Klabbers 2018-04-17 14:22:38 +02:00
parent c30e19de1c
commit f04eaa1549
21 changed files with 122 additions and 106 deletions

View File

@ -47,7 +47,7 @@ class ListDiscussionsController extends AbstractListController
/**
* {@inheritdoc}
*/
public $sortFields = ['lastTime', 'commentsCount', 'startTime'];
public $sortFields = ['lastPostedAt', 'commentCount', 'createdAt'];
/**
* @var DiscussionSearcher

View File

@ -37,10 +37,10 @@ class DiscussionSerializer extends BasicDiscussionSerializer
$gate = $this->gate->forUser($this->actor);
$attributes = parent::getDefaultAttributes($discussion) + [
'commentsCount' => (int) $discussion->comments_count,
'participantsCount' => (int) $discussion->participants_count,
'startTime' => $this->formatDate($discussion->start_time),
'lastTime' => $this->formatDate($discussion->last_time),
'commentsCount' => (int) $discussion->comment_count,
'participantsCount' => (int) $discussion->participant_count,
'startTime' => $this->formatDate($discussion->created_at),
'lastTime' => $this->formatDate($discussion->last_posted_at),
'lastPostNumber' => (int) $discussion->last_post_number,
'canReply' => $gate->allows('reply', $discussion),
'canRename' => $gate->allows('rename', $discussion),
@ -48,9 +48,9 @@ class DiscussionSerializer extends BasicDiscussionSerializer
'canHide' => $gate->allows('hide', $discussion)
];
if ($discussion->hide_time) {
if ($discussion->hidden_at) {
$attributes['isHidden'] = true;
$attributes['hideTime'] = $this->formatDate($discussion->hide_time);
$attributes['hideTime'] = $this->formatDate($discussion->hidden_at);
}
Discussion::setStateUser($this->actor);

View File

@ -30,18 +30,18 @@ use Flarum\Util\Str;
* @property int $id
* @property string $title
* @property string $slug
* @property int $comments_count
* @property int $participants_count
* @property int $number_index
* @property \Carbon\Carbon $start_time
* @property int|null $start_user_id
* @property int|null $start_post_id
* @property \Carbon\Carbon|null $last_time
* @property int|null $last_user_id
* @property int $comment_count
* @property int $participant_count
* @property int $post_number_index
* @property \Carbon\Carbon $created_at
* @property int|null $user_id
* @property int|null $first_post_id
* @property \Carbon\Carbon|null $last_posted_at
* @property int|null $last_posted_user_id
* @property int|null $last_post_id
* @property int|null $last_post_number
* @property \Carbon\Carbon|null $hide_time
* @property int|null $hide_user_id
* @property \Carbon\Carbon|null $hidden_at
* @property int|null $hidden_user_id
* @property UserState|null $state
* @property \Illuminate\Database\Eloquent\Collection $posts
* @property \Illuminate\Database\Eloquent\Collection $comments
@ -73,7 +73,7 @@ class Discussion extends AbstractModel
/**
* {@inheritdoc}
*/
protected $dates = ['start_time', 'last_time', 'hide_time'];
protected $dates = ['created_at', 'last_posted_at', 'hidden_at'];
/**
* Casts properties to a specific type.
@ -138,8 +138,8 @@ class Discussion extends AbstractModel
$discussion = new static;
$discussion->title = $title;
$discussion->start_time = time();
$discussion->start_user_id = $user->id;
$discussion->created_at = time();
$discussion->user_id = $user->id;
$discussion->setRelation('startUser', $user);
@ -174,9 +174,9 @@ class Discussion extends AbstractModel
*/
public function hide(User $actor = null)
{
if (! $this->hide_time) {
$this->hide_time = time();
$this->hide_user_id = $actor ? $actor->id : null;
if (! $this->hidden_at) {
$this->hidden_at = time();
$this->hidden_user_id = $actor ? $actor->id : null;
$this->raise(new Hidden($this));
}
@ -191,9 +191,9 @@ class Discussion extends AbstractModel
*/
public function restore()
{
if ($this->hide_time !== null) {
$this->hide_time = null;
$this->hide_user_id = null;
if ($this->hidden_at !== null) {
$this->hidden_at = null;
$this->hidden_user_id = null;
$this->raise(new Restored($this));
}
@ -209,9 +209,9 @@ class Discussion extends AbstractModel
*/
public function setStartPost(Post $post)
{
$this->start_time = $post->time;
$this->start_user_id = $post->user_id;
$this->start_post_id = $post->id;
$this->created_at = $post->time;
$this->user_id = $post->user_id;
$this->first_post_id = $post->id;
return $this;
}
@ -224,8 +224,8 @@ class Discussion extends AbstractModel
*/
public function setLastPost(Post $post)
{
$this->last_time = $post->time;
$this->last_user_id = $post->user_id;
$this->last_posted_at = $post->time;
$this->last_posted_user_id = $post->user_id;
$this->last_post_id = $post->id;
$this->last_post_number = $post->number;
@ -240,7 +240,7 @@ class Discussion extends AbstractModel
public function refreshLastPost()
{
/** @var Post $lastPost */
if ($lastPost = $this->comments()->latest('time')->first()) {
if ($lastPost = $this->comments()->latest('created_at')->first()) {
$this->setLastPost($lastPost);
}
@ -254,7 +254,7 @@ class Discussion extends AbstractModel
*/
public function refreshCommentsCount()
{
$this->comments_count = $this->comments()->count();
$this->comment_count = $this->comments()->count();
return $this;
}
@ -266,7 +266,7 @@ class Discussion extends AbstractModel
*/
public function refreshParticipantsCount()
{
$this->participants_count = $this->participants()->count('users.id');
$this->participant_count = $this->participants()->count('users.id');
return $this;
}
@ -286,7 +286,7 @@ class Discussion extends AbstractModel
*/
public function mergePost(MergeableInterface $post)
{
$lastPost = $this->posts()->latest('time')->first();
$lastPost = $this->posts()->latest('created_at')->first();
$post = $post->saveAfter($lastPost);
@ -322,7 +322,7 @@ class Discussion extends AbstractModel
{
return $this->posts()
->where('is_private', false)
->whereNull('hide_time')
->whereNull('hidden_at')
->where('type', 'comment');
}
@ -349,7 +349,7 @@ class Discussion extends AbstractModel
*/
public function startPost()
{
return $this->belongsTo(Post::class, 'start_post_id');
return $this->belongsTo(Post::class, 'first_post_id');
}
/**
@ -359,7 +359,7 @@ class Discussion extends AbstractModel
*/
public function startUser()
{
return $this->belongsTo(User::class, 'start_user_id');
return $this->belongsTo(User::class, 'user_id');
}
/**

View File

@ -93,7 +93,7 @@ class DiscussionPolicy extends AbstractPolicy
if (! $actor->hasPermission('discussion.hide')) {
$query->where(function ($query) use ($actor) {
$query->whereNull('discussions.hide_time')
->orWhere('start_user_id', $actor->id)
->orWhere('user_id', $actor->id)
->orWhere(function ($query) use ($actor) {
$this->events->fire(
new ScopeModelVisibility($query, $actor, 'hide')
@ -106,8 +106,8 @@ class DiscussionPolicy extends AbstractPolicy
// current user, or the user is allowed to edit the discussion's posts.
if (! $actor->hasPermission('discussion.editPosts')) {
$query->where(function ($query) use ($actor) {
$query->where('comments_count', '>', 0)
->orWhere('start_user_id', $actor->id)
$query->where('comment_count', '>', 0)
->orWhere('user_id', $actor->id)
->orWhere(function ($query) use ($actor) {
$this->events->dispatch(
new ScopeModelVisibility($query, $actor, 'editPosts')
@ -124,12 +124,12 @@ class DiscussionPolicy extends AbstractPolicy
*/
public function rename(User $actor, Discussion $discussion)
{
if ($discussion->start_user_id == $actor->id) {
if ($discussion->user_id == $actor->id) {
$allowRenaming = $this->settings->get('allow_renaming');
if ($allowRenaming === '-1'
|| ($allowRenaming === 'reply' && $discussion->participants_count <= 1)
|| ($discussion->start_time->diffInMinutes(new Carbon) < $allowRenaming)) {
|| ($allowRenaming === 'reply' && $discussion->participant_count <= 1)
|| ($discussion->created_at->diffInMinutes(new Carbon) < $allowRenaming)) {
return true;
}
}
@ -142,7 +142,7 @@ class DiscussionPolicy extends AbstractPolicy
*/
public function hide(User $actor, Discussion $discussion)
{
if ($discussion->start_user_id == $actor->id && $discussion->participants_count <= 1) {
if ($discussion->user_id == $actor->id && $discussion->participant_count <= 1) {
return true;
}
}

View File

@ -54,7 +54,7 @@ class DiscussionRenamedLogger
$post = $event->discussion->mergePost($post);
if ($event->discussion->start_user_id !== $event->actor->id) {
if ($event->discussion->user_id !== $event->actor->id) {
$blueprint = new DiscussionRenamedBlueprint($post);
if ($post->exists) {

View File

@ -49,7 +49,7 @@ class DiscussionRepository
*/
public function getReadIds(User $user)
{
return Discussion::leftJoin('users_discussions', 'users_discussions.discussion_id', '=', 'discussions.id')
return Discussion::leftJoin('discussions_users', 'discussions_users.discussion_id', '=', 'discussions.id')
->where('user_id', $user->id)
->whereRaw('read_number >= last_post_number')
->pluck('id')

View File

@ -23,7 +23,7 @@ class DiscussionSearch extends AbstractSearch
/**
* {@inheritdoc}
*/
protected $defaultSort = ['lastTime' => 'desc'];
protected $defaultSort = ['lastPostedAt' => 'desc'];
/**
* @var array

View File

@ -54,6 +54,6 @@ class AuthorGambit extends AbstractRegexGambit
$ids[] = $this->users->getIdForUsername($username);
}
$search->getQuery()->whereIn('start_user_id', $ids, 'and', $negate);
$search->getQuery()->whereIn('user_id', $ids, 'and', $negate);
}
}

View File

@ -37,9 +37,9 @@ class CreatedGambit extends AbstractRegexGambit
// provided with a YYYY-MM-DD..YYYY-MM-DD range, then find discussions
// that were started during that period.
if (empty($matches[3])) {
$search->getQuery()->whereDate('start_time', $negate ? '!=' : '=', $matches[1]);
$search->getQuery()->whereDate('created_at', $negate ? '!=' : '=', $matches[1]);
} else {
$search->getQuery()->whereBetween('start_time', [$matches[1], $matches[3]], 'and', $negate);
$search->getQuery()->whereBetween('created_at', [$matches[1], $matches[3]], 'and', $negate);
}
}
}

View File

@ -34,9 +34,9 @@ class HiddenGambit extends AbstractRegexGambit
$search->getQuery()->where(function ($query) use ($negate) {
if ($negate) {
$query->whereNull('hide_time')->where('comments_count', '>', 0);
$query->whereNull('hidden_at')->where('comment_count', '>', 0);
} else {
$query->whereNotNull('hide_time')->orWhere('comments_count', 0);
$query->whereNotNull('hidden_at')->orWhere('comment_count', 0);
}
});
}

View File

@ -53,9 +53,9 @@ class UnreadGambit extends AbstractRegexGambit
$search->getQuery()->where(function ($query) use ($readIds, $negate, $actor) {
if (! $negate) {
$query->whereNotIn('id', $readIds)->where('last_time', '>', $actor->read_time ?: 0);
$query->whereNotIn('id', $readIds)->where('last_posted_at', '>', $actor->marked_all_as_read_at ?: 0);
} else {
$query->whereIn('id', $readIds)->orWhere('last_time', '<=', $actor->read_time ?: 0);
$query->whereIn('id', $readIds)->orWhere('last_posted_at', '<=', $actor->marked_all_as_read_at ?: 0);
}
});
}

View File

@ -26,8 +26,8 @@ use Illuminate\Database\Eloquent\Builder;
*
* @property int $user_id
* @property int $discussion_id
* @property \Carbon\Carbon|null $read_time
* @property int|null $read_number
* @property \Carbon\Carbon|null $last_read_at
* @property int|null $last_read_post_number
* @property Discussion $discussion
* @property \Flarum\User\User $user
*/
@ -38,12 +38,12 @@ class UserState extends AbstractModel
/**
* {@inheritdoc}
*/
protected $table = 'users_discussions';
protected $table = 'discussions_users';
/**
* {@inheritdoc}
*/
protected $dates = ['read_time'];
protected $dates = ['last_read_at'];
/**
* Mark the discussion as being read up to a certain point. Raises the
@ -54,9 +54,9 @@ class UserState extends AbstractModel
*/
public function read($number)
{
if ($number > $this->read_number) {
$this->read_number = $number;
$this->read_time = time();
if ($number > $this->last_read_at) {
$this->last_read_at = $number;
$this->last_read_at = time();
$this->raise(new UserRead($this));
}

View File

@ -34,9 +34,9 @@ use Flarum\User\User;
* @property string $type
* @property int|null $subject_id
* @property mixed|null $data
* @property \Carbon\Carbon $time
* @property bool $is_read
* @property bool $is_deleted
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $read_at
* @property \Carbon\Carbon $deleted_at
* @property \Flarum\User\User|null $user
* @property \Flarum\User\User|null $sender
* @property \Flarum\Database\AbstractModel|null $subject
@ -51,7 +51,7 @@ class Notification extends AbstractModel
/**
* {@inheritdoc}
*/
protected $dates = ['time'];
protected $dates = ['created_at', 'read_at', 'deleted_at'];
/**
* A map of notification types and the model classes to use for their
@ -70,7 +70,7 @@ class Notification extends AbstractModel
*/
public function read()
{
$this->is_read = true;
$this->read_at = time();
}
/**

View File

@ -27,11 +27,11 @@ class NotificationRepository
{
$primaries = Notification::select(
app('flarum.db')->raw('MAX(id) AS id'),
app('flarum.db')->raw('SUM(is_read = 0) AS unread_count')
app('flarum.db')->raw('SUM(read_at IS NULL) AS unread_count')
)
->where('user_id', $user->id)
->whereIn('type', $user->getAlertableNotificationTypes())
->where('is_deleted', false)
->whereNull('deleted_at')
->groupBy('type', 'subject_id')
->orderByRaw('MAX(time) DESC')
->skip($offset)
@ -40,7 +40,7 @@ class NotificationRepository
return Notification::select('notifications.*', app('flarum.db')->raw('p.unread_count'))
->mergeBindings($primaries->getQuery())
->join(app('flarum.db')->raw('('.$primaries->toSql().') p'), 'notifications.id', '=', app('flarum.db')->raw('p.id'))
->latest('time')
->latest('created_at')
->get();
}
@ -53,6 +53,6 @@ class NotificationRepository
*/
public function markAllAsRead(User $user)
{
Notification::where('user_id', $user->id)->update(['is_read' => true]);
Notification::where('user_id', $user->id)->update(['read_at' => time()]);
}
}

View File

@ -51,7 +51,7 @@ class CommentPost extends Post
{
$post = new static;
$post->time = time();
$post->created_at = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = static::$type;
@ -77,8 +77,8 @@ class CommentPost extends Post
if ($this->content !== $content) {
$this->content = $content;
$this->edit_time = time();
$this->edit_user_id = $actor->id;
$this->edited_at = time();
$this->edited_user_id = $actor->id;
$this->raise(new Revised($this));
}
@ -94,9 +94,9 @@ class CommentPost extends Post
*/
public function hide(User $actor = null)
{
if (! $this->hide_time) {
$this->hide_time = time();
$this->hide_user_id = $actor ? $actor->id : null;
if (! $this->hidden_at) {
$this->hidden_at = time();
$this->hidden_user_id = $actor ? $actor->id : null;
$this->raise(new Hidden($this));
}
@ -111,9 +111,9 @@ class CommentPost extends Post
*/
public function restore()
{
if ($this->hide_time !== null) {
$this->hide_time = null;
$this->hide_user_id = null;
if ($this->hidden_at !== null) {
$this->hidden_at = null;
$this->hidden_user_id = null;
$this->raise(new Restored($this));
}

View File

@ -24,14 +24,14 @@ use Illuminate\Database\Eloquent\Builder;
* @property int $id
* @property int $discussion_id
* @property int $number
* @property \Carbon\Carbon $time
* @property \Carbon\Carbon $created_at
* @property int|null $user_id
* @property string|null $type
* @property string|null $content
* @property \Carbon\Carbon|null $edit_time
* @property int|null $edit_user_id
* @property \Carbon\Carbon|null $hide_time
* @property int|null $hide_user_id
* @property \Carbon\Carbon|null $edited_at
* @property int|null $edited_user_id
* @property \Carbon\Carbon|null $hidden_at
* @property int|null $hidden_user_id
* @property \Flarum\Discussion\Discussion|null $discussion
* @property User|null $user
* @property User|null $editUser
@ -51,7 +51,7 @@ class Post extends AbstractModel
/**
* {@inheritdoc}
*/
protected $dates = ['time', 'edit_time', 'hide_time'];
protected $dates = ['created_at', 'edited_at', 'hidden_at'];
/**
* Casts properties to a specific type.
@ -93,7 +93,7 @@ class Post extends AbstractModel
// discussion.
static::creating(function (Post $post) {
$post->type = $post::$type;
$post->number = ++$post->discussion->number_index;
$post->number = ++$post->discussion->post_number_index;
$post->discussion->save();
});
@ -170,7 +170,7 @@ class Post extends AbstractModel
*/
public function editUser()
{
return $this->belongsTo('Flarum\User\User', 'edit_user_id');
return $this->belongsTo('Flarum\User\User', 'edited_user_id');
}
/**
@ -180,7 +180,7 @@ class Post extends AbstractModel
*/
public function hideUser()
{
return $this->belongsTo('Flarum\User\User', 'hide_user_id');
return $this->belongsTo('Flarum\User\User', 'hidden_user_id');
}
/**

View File

@ -81,7 +81,7 @@ class PostPolicy extends AbstractPolicy
// discussion.
if (! $actor->hasPermission('discussion.editPosts')) {
$query->where(function ($query) use ($actor) {
$query->whereNull('posts.hide_time')
$query->whereNull('posts.hidden_at')
->orWhere('user_id', $actor->id)
->orWhereExists(function ($query) use ($actor) {
$query->selectRaw('1')
@ -107,12 +107,12 @@ class PostPolicy extends AbstractPolicy
// A post is allowed to be edited if the user has permission to moderate
// the discussion which it's in, or if they are the author and the post
// hasn't been deleted by someone else.
if ($post->user_id == $actor->id && (! $post->hide_time || $post->hide_user_id == $actor->id)) {
if ($post->user_id == $actor->id && (! $post->hidden_at || $post->hidden_user_id == $actor->id)) {
$allowEditing = $this->settings->get('allow_post_editing');
if ($allowEditing === '-1'
|| ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number)
|| ($post->time->diffInMinutes(new Carbon) < $allowEditing)) {
|| ($post->created_at->diffInMinutes(new Carbon) < $allowEditing)) {
return true;
}
}

View File

@ -11,12 +11,14 @@
namespace Flarum\User;
use DateTime;
use Carbon\Carbon;
use Flarum\Database\AbstractModel;
use Flarum\User\Exception\InvalidConfirmationTokenException;
/**
* @todo document database columns with @property
* @property string $token
* @property \Carbon\Carbon $created_at
* @property string $payload
*/
class AuthToken extends AbstractModel
{
@ -37,10 +39,15 @@ class AuthToken extends AbstractModel
*/
public $incrementing = false;
/**
* {@inheritdoc}
*/
protected $primaryKey = 'token';
/**
* Generate an email token for the specified user.
*
* @param string $email
* @param string $payload
*
* @return static
*/
@ -48,7 +55,7 @@ class AuthToken extends AbstractModel
{
$token = new static;
$token->id = str_random(40);
$token->token = str_random(40);
$token->payload = $payload;
$token->created_at = time();
@ -80,17 +87,18 @@ class AuthToken extends AbstractModel
* Find the token with the given ID, and assert that it has not expired.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $id
* @param string $token
*
* @throws \Flarum\User\Exception\InvalidConfirmationTokenException
* @throws InvalidConfirmationTokenException
*
* @return static
* @return AuthToken
*/
public function scopeValidOrFail($query, $id)
public function scopeValidOrFail($query, string $token)
{
$token = $query->find($id);
/** @var AuthToken $token */
$token = $query->find($token);
if (! $token || $token->created_at < new DateTime('-1 day')) {
if (! $token || $token->created_at->lessThan(Carbon::now()->subDay())) {
throw new InvalidConfirmationTokenException;
}

View File

@ -107,7 +107,7 @@ class RequestPasswordResetHandler
$data = [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('resetPassword', ['token' => $token->id]),
'{url}' => $this->url->to('forum')->route('resetPassword', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title'),
];

View File

@ -14,7 +14,9 @@ namespace Flarum\User;
use Flarum\Database\AbstractModel;
/**
* @todo document database columns with @property
* @property string $token
* @property \Carbon\Carbon $created_at
* @property int $user_id
*/
class PasswordToken extends AbstractModel
{
@ -35,17 +37,22 @@ class PasswordToken extends AbstractModel
*/
public $incrementing = false;
/**
* {@inheritdoc}
*/
protected $primaryKey = 'token';
/**
* Generate a password token for the specified user.
*
* @param int $userId
* @return static
*/
public static function generate($userId)
public static function generate(int $userId)
{
$token = new static;
$token->id = str_random(40);
$token->token = str_random(40);
$token->user_id = $userId;
$token->created_at = time();

View File

@ -39,6 +39,7 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* @property int $id
* @property string $username
* @property string $display_name
* @property string $email
* @property bool $is_email_confirmed
* @property string $password