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 efa3b62fb8
commit a2927b725f
21 changed files with 122 additions and 106 deletions

View File

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

View File

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

View File

@ -30,18 +30,18 @@ use Flarum\Util\Str;
* @property int $id * @property int $id
* @property string $title * @property string $title
* @property string $slug * @property string $slug
* @property int $comments_count * @property int $comment_count
* @property int $participants_count * @property int $participant_count
* @property int $number_index * @property int $post_number_index
* @property \Carbon\Carbon $start_time * @property \Carbon\Carbon $created_at
* @property int|null $start_user_id * @property int|null $user_id
* @property int|null $start_post_id * @property int|null $first_post_id
* @property \Carbon\Carbon|null $last_time * @property \Carbon\Carbon|null $last_posted_at
* @property int|null $last_user_id * @property int|null $last_posted_user_id
* @property int|null $last_post_id * @property int|null $last_post_id
* @property int|null $last_post_number * @property int|null $last_post_number
* @property \Carbon\Carbon|null $hide_time * @property \Carbon\Carbon|null $hidden_at
* @property int|null $hide_user_id * @property int|null $hidden_user_id
* @property UserState|null $state * @property UserState|null $state
* @property \Illuminate\Database\Eloquent\Collection $posts * @property \Illuminate\Database\Eloquent\Collection $posts
* @property \Illuminate\Database\Eloquent\Collection $comments * @property \Illuminate\Database\Eloquent\Collection $comments
@ -73,7 +73,7 @@ class Discussion extends AbstractModel
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $dates = ['start_time', 'last_time', 'hide_time']; protected $dates = ['created_at', 'last_posted_at', 'hidden_at'];
/** /**
* Casts properties to a specific type. * Casts properties to a specific type.
@ -138,8 +138,8 @@ class Discussion extends AbstractModel
$discussion = new static; $discussion = new static;
$discussion->title = $title; $discussion->title = $title;
$discussion->start_time = time(); $discussion->created_at = time();
$discussion->start_user_id = $user->id; $discussion->user_id = $user->id;
$discussion->setRelation('startUser', $user); $discussion->setRelation('startUser', $user);
@ -174,9 +174,9 @@ class Discussion extends AbstractModel
*/ */
public function hide(User $actor = null) public function hide(User $actor = null)
{ {
if (! $this->hide_time) { if (! $this->hidden_at) {
$this->hide_time = time(); $this->hidden_at = time();
$this->hide_user_id = $actor ? $actor->id : null; $this->hidden_user_id = $actor ? $actor->id : null;
$this->raise(new Hidden($this)); $this->raise(new Hidden($this));
} }
@ -191,9 +191,9 @@ class Discussion extends AbstractModel
*/ */
public function restore() public function restore()
{ {
if ($this->hide_time !== null) { if ($this->hidden_at !== null) {
$this->hide_time = null; $this->hidden_at = null;
$this->hide_user_id = null; $this->hidden_user_id = null;
$this->raise(new Restored($this)); $this->raise(new Restored($this));
} }
@ -209,9 +209,9 @@ class Discussion extends AbstractModel
*/ */
public function setStartPost(Post $post) public function setStartPost(Post $post)
{ {
$this->start_time = $post->time; $this->created_at = $post->time;
$this->start_user_id = $post->user_id; $this->user_id = $post->user_id;
$this->start_post_id = $post->id; $this->first_post_id = $post->id;
return $this; return $this;
} }
@ -224,8 +224,8 @@ class Discussion extends AbstractModel
*/ */
public function setLastPost(Post $post) public function setLastPost(Post $post)
{ {
$this->last_time = $post->time; $this->last_posted_at = $post->time;
$this->last_user_id = $post->user_id; $this->last_posted_user_id = $post->user_id;
$this->last_post_id = $post->id; $this->last_post_id = $post->id;
$this->last_post_number = $post->number; $this->last_post_number = $post->number;
@ -240,7 +240,7 @@ class Discussion extends AbstractModel
public function refreshLastPost() public function refreshLastPost()
{ {
/** @var Post $lastPost */ /** @var Post $lastPost */
if ($lastPost = $this->comments()->latest('time')->first()) { if ($lastPost = $this->comments()->latest('created_at')->first()) {
$this->setLastPost($lastPost); $this->setLastPost($lastPost);
} }
@ -254,7 +254,7 @@ class Discussion extends AbstractModel
*/ */
public function refreshCommentsCount() public function refreshCommentsCount()
{ {
$this->comments_count = $this->comments()->count(); $this->comment_count = $this->comments()->count();
return $this; return $this;
} }
@ -266,7 +266,7 @@ class Discussion extends AbstractModel
*/ */
public function refreshParticipantsCount() public function refreshParticipantsCount()
{ {
$this->participants_count = $this->participants()->count('users.id'); $this->participant_count = $this->participants()->count('users.id');
return $this; return $this;
} }
@ -286,7 +286,7 @@ class Discussion extends AbstractModel
*/ */
public function mergePost(MergeableInterface $post) public function mergePost(MergeableInterface $post)
{ {
$lastPost = $this->posts()->latest('time')->first(); $lastPost = $this->posts()->latest('created_at')->first();
$post = $post->saveAfter($lastPost); $post = $post->saveAfter($lastPost);
@ -322,7 +322,7 @@ class Discussion extends AbstractModel
{ {
return $this->posts() return $this->posts()
->where('is_private', false) ->where('is_private', false)
->whereNull('hide_time') ->whereNull('hidden_at')
->where('type', 'comment'); ->where('type', 'comment');
} }
@ -349,7 +349,7 @@ class Discussion extends AbstractModel
*/ */
public function startPost() 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() 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')) { if (! $actor->hasPermission('discussion.hide')) {
$query->where(function ($query) use ($actor) { $query->where(function ($query) use ($actor) {
$query->whereNull('discussions.hide_time') $query->whereNull('discussions.hide_time')
->orWhere('start_user_id', $actor->id) ->orWhere('user_id', $actor->id)
->orWhere(function ($query) use ($actor) { ->orWhere(function ($query) use ($actor) {
$this->events->fire( $this->events->fire(
new ScopeModelVisibility($query, $actor, 'hide') 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. // current user, or the user is allowed to edit the discussion's posts.
if (! $actor->hasPermission('discussion.editPosts')) { if (! $actor->hasPermission('discussion.editPosts')) {
$query->where(function ($query) use ($actor) { $query->where(function ($query) use ($actor) {
$query->where('comments_count', '>', 0) $query->where('comment_count', '>', 0)
->orWhere('start_user_id', $actor->id) ->orWhere('user_id', $actor->id)
->orWhere(function ($query) use ($actor) { ->orWhere(function ($query) use ($actor) {
$this->events->dispatch( $this->events->dispatch(
new ScopeModelVisibility($query, $actor, 'editPosts') new ScopeModelVisibility($query, $actor, 'editPosts')
@ -124,12 +124,12 @@ class DiscussionPolicy extends AbstractPolicy
*/ */
public function rename(User $actor, Discussion $discussion) 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'); $allowRenaming = $this->settings->get('allow_renaming');
if ($allowRenaming === '-1' if ($allowRenaming === '-1'
|| ($allowRenaming === 'reply' && $discussion->participants_count <= 1) || ($allowRenaming === 'reply' && $discussion->participant_count <= 1)
|| ($discussion->start_time->diffInMinutes(new Carbon) < $allowRenaming)) { || ($discussion->created_at->diffInMinutes(new Carbon) < $allowRenaming)) {
return true; return true;
} }
} }
@ -142,7 +142,7 @@ class DiscussionPolicy extends AbstractPolicy
*/ */
public function hide(User $actor, Discussion $discussion) 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; return true;
} }
} }

View File

@ -54,7 +54,7 @@ class DiscussionRenamedLogger
$post = $event->discussion->mergePost($post); $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); $blueprint = new DiscussionRenamedBlueprint($post);
if ($post->exists) { if ($post->exists) {

View File

@ -49,7 +49,7 @@ class DiscussionRepository
*/ */
public function getReadIds(User $user) 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) ->where('user_id', $user->id)
->whereRaw('read_number >= last_post_number') ->whereRaw('read_number >= last_post_number')
->pluck('id') ->pluck('id')

View File

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

View File

@ -54,6 +54,6 @@ class AuthorGambit extends AbstractRegexGambit
$ids[] = $this->users->getIdForUsername($username); $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 // provided with a YYYY-MM-DD..YYYY-MM-DD range, then find discussions
// that were started during that period. // that were started during that period.
if (empty($matches[3])) { if (empty($matches[3])) {
$search->getQuery()->whereDate('start_time', $negate ? '!=' : '=', $matches[1]); $search->getQuery()->whereDate('created_at', $negate ? '!=' : '=', $matches[1]);
} else { } 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) { $search->getQuery()->where(function ($query) use ($negate) {
if ($negate) { if ($negate) {
$query->whereNull('hide_time')->where('comments_count', '>', 0); $query->whereNull('hidden_at')->where('comment_count', '>', 0);
} else { } 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) { $search->getQuery()->where(function ($query) use ($readIds, $negate, $actor) {
if (! $negate) { 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 { } 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 $user_id
* @property int $discussion_id * @property int $discussion_id
* @property \Carbon\Carbon|null $read_time * @property \Carbon\Carbon|null $last_read_at
* @property int|null $read_number * @property int|null $last_read_post_number
* @property Discussion $discussion * @property Discussion $discussion
* @property \Flarum\User\User $user * @property \Flarum\User\User $user
*/ */
@ -38,12 +38,12 @@ class UserState extends AbstractModel
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $table = 'users_discussions'; protected $table = 'discussions_users';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $dates = ['read_time']; protected $dates = ['last_read_at'];
/** /**
* Mark the discussion as being read up to a certain point. Raises the * 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) public function read($number)
{ {
if ($number > $this->read_number) { if ($number > $this->last_read_at) {
$this->read_number = $number; $this->last_read_at = $number;
$this->read_time = time(); $this->last_read_at = time();
$this->raise(new UserRead($this)); $this->raise(new UserRead($this));
} }

View File

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

View File

@ -27,11 +27,11 @@ class NotificationRepository
{ {
$primaries = Notification::select( $primaries = Notification::select(
app('flarum.db')->raw('MAX(id) AS id'), 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) ->where('user_id', $user->id)
->whereIn('type', $user->getAlertableNotificationTypes()) ->whereIn('type', $user->getAlertableNotificationTypes())
->where('is_deleted', false) ->whereNull('deleted_at')
->groupBy('type', 'subject_id') ->groupBy('type', 'subject_id')
->orderByRaw('MAX(time) DESC') ->orderByRaw('MAX(time) DESC')
->skip($offset) ->skip($offset)
@ -40,7 +40,7 @@ class NotificationRepository
return Notification::select('notifications.*', app('flarum.db')->raw('p.unread_count')) return Notification::select('notifications.*', app('flarum.db')->raw('p.unread_count'))
->mergeBindings($primaries->getQuery()) ->mergeBindings($primaries->getQuery())
->join(app('flarum.db')->raw('('.$primaries->toSql().') p'), 'notifications.id', '=', app('flarum.db')->raw('p.id')) ->join(app('flarum.db')->raw('('.$primaries->toSql().') p'), 'notifications.id', '=', app('flarum.db')->raw('p.id'))
->latest('time') ->latest('created_at')
->get(); ->get();
} }
@ -53,6 +53,6 @@ class NotificationRepository
*/ */
public function markAllAsRead(User $user) 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 = new static;
$post->time = time(); $post->created_at = time();
$post->discussion_id = $discussionId; $post->discussion_id = $discussionId;
$post->user_id = $userId; $post->user_id = $userId;
$post->type = static::$type; $post->type = static::$type;
@ -77,8 +77,8 @@ class CommentPost extends Post
if ($this->content !== $content) { if ($this->content !== $content) {
$this->content = $content; $this->content = $content;
$this->edit_time = time(); $this->edited_at = time();
$this->edit_user_id = $actor->id; $this->edited_user_id = $actor->id;
$this->raise(new Revised($this)); $this->raise(new Revised($this));
} }
@ -94,9 +94,9 @@ class CommentPost extends Post
*/ */
public function hide(User $actor = null) public function hide(User $actor = null)
{ {
if (! $this->hide_time) { if (! $this->hidden_at) {
$this->hide_time = time(); $this->hidden_at = time();
$this->hide_user_id = $actor ? $actor->id : null; $this->hidden_user_id = $actor ? $actor->id : null;
$this->raise(new Hidden($this)); $this->raise(new Hidden($this));
} }
@ -111,9 +111,9 @@ class CommentPost extends Post
*/ */
public function restore() public function restore()
{ {
if ($this->hide_time !== null) { if ($this->hidden_at !== null) {
$this->hide_time = null; $this->hidden_at = null;
$this->hide_user_id = null; $this->hidden_user_id = null;
$this->raise(new Restored($this)); $this->raise(new Restored($this));
} }

View File

@ -24,14 +24,14 @@ use Illuminate\Database\Eloquent\Builder;
* @property int $id * @property int $id
* @property int $discussion_id * @property int $discussion_id
* @property int $number * @property int $number
* @property \Carbon\Carbon $time * @property \Carbon\Carbon $created_at
* @property int|null $user_id * @property int|null $user_id
* @property string|null $type * @property string|null $type
* @property string|null $content * @property string|null $content
* @property \Carbon\Carbon|null $edit_time * @property \Carbon\Carbon|null $edited_at
* @property int|null $edit_user_id * @property int|null $edited_user_id
* @property \Carbon\Carbon|null $hide_time * @property \Carbon\Carbon|null $hidden_at
* @property int|null $hide_user_id * @property int|null $hidden_user_id
* @property \Flarum\Discussion\Discussion|null $discussion * @property \Flarum\Discussion\Discussion|null $discussion
* @property User|null $user * @property User|null $user
* @property User|null $editUser * @property User|null $editUser
@ -51,7 +51,7 @@ class Post extends AbstractModel
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $dates = ['time', 'edit_time', 'hide_time']; protected $dates = ['created_at', 'edited_at', 'hidden_at'];
/** /**
* Casts properties to a specific type. * Casts properties to a specific type.
@ -93,7 +93,7 @@ class Post extends AbstractModel
// discussion. // discussion.
static::creating(function (Post $post) { static::creating(function (Post $post) {
$post->type = $post::$type; $post->type = $post::$type;
$post->number = ++$post->discussion->number_index; $post->number = ++$post->discussion->post_number_index;
$post->discussion->save(); $post->discussion->save();
}); });
@ -170,7 +170,7 @@ class Post extends AbstractModel
*/ */
public function editUser() 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() 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. // discussion.
if (! $actor->hasPermission('discussion.editPosts')) { if (! $actor->hasPermission('discussion.editPosts')) {
$query->where(function ($query) use ($actor) { $query->where(function ($query) use ($actor) {
$query->whereNull('posts.hide_time') $query->whereNull('posts.hidden_at')
->orWhere('user_id', $actor->id) ->orWhere('user_id', $actor->id)
->orWhereExists(function ($query) use ($actor) { ->orWhereExists(function ($query) use ($actor) {
$query->selectRaw('1') $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 // 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 // the discussion which it's in, or if they are the author and the post
// hasn't been deleted by someone else. // 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'); $allowEditing = $this->settings->get('allow_post_editing');
if ($allowEditing === '-1' if ($allowEditing === '-1'
|| ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number) || ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number)
|| ($post->time->diffInMinutes(new Carbon) < $allowEditing)) { || ($post->created_at->diffInMinutes(new Carbon) < $allowEditing)) {
return true; return true;
} }
} }

View File

@ -11,12 +11,14 @@
namespace Flarum\User; namespace Flarum\User;
use DateTime; use Carbon\Carbon;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\User\Exception\InvalidConfirmationTokenException; 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 class AuthToken extends AbstractModel
{ {
@ -37,10 +39,15 @@ class AuthToken extends AbstractModel
*/ */
public $incrementing = false; public $incrementing = false;
/**
* {@inheritdoc}
*/
protected $primaryKey = 'token';
/** /**
* Generate an email token for the specified user. * Generate an email token for the specified user.
* *
* @param string $email * @param string $payload
* *
* @return static * @return static
*/ */
@ -48,7 +55,7 @@ class AuthToken extends AbstractModel
{ {
$token = new static; $token = new static;
$token->id = str_random(40); $token->token = str_random(40);
$token->payload = $payload; $token->payload = $payload;
$token->created_at = time(); $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. * Find the token with the given ID, and assert that it has not expired.
* *
* @param \Illuminate\Database\Eloquent\Builder $query * @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; throw new InvalidConfirmationTokenException;
} }

View File

@ -107,7 +107,7 @@ class RequestPasswordResetHandler
$data = [ $data = [
'{username}' => $user->display_name, '{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'), '{forum}' => $this->settings->get('forum_title'),
]; ];

View File

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

View File

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