1
0
mirror of https://github.com/flarum/core.git synced 2025-10-19 18:56:44 +02:00

Merge pull request #1344 from flarum/1236-database-changes

Database changes
This commit is contained in:
Franz Liedke
2018-09-16 20:44:29 +02:00
committed by GitHub
126 changed files with 1427 additions and 585 deletions

View File

@@ -11,22 +11,26 @@
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
{
/**
* {@inheritdoc}
*/
protected $table = 'auth_tokens';
protected $table = 'registration_tokens';
/**
* {@inheritdoc}
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at'];
@@ -37,10 +41,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,9 +57,9 @@ class AuthToken extends AbstractModel
{
$token = new static;
$token->id = str_random(40);
$token->token = str_random(40);
$token->payload = $payload;
$token->created_at = time();
$token->created_at = Carbon::now();
return $token;
}
@@ -80,17 +89,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

@@ -103,7 +103,7 @@ class EditUserHandler
}
}
if ($actor->isAdmin() && ! empty($attributes['isActivated'])) {
if ($actor->isAdmin() && ! empty($attributes['isEmailConfirmed'])) {
$user->activate();
}
@@ -114,7 +114,7 @@ class EditUserHandler
$validate['password'] = $attributes['password'];
}
if (! empty($attributes['readTime'])) {
if (! empty($attributes['markedAllAsReadAt'])) {
$this->assertPermission($isSelf);
$user->markAllAsRead();
}

View File

@@ -112,7 +112,7 @@ class RegisterUserHandler
}
}
if ($actor->isAdmin() && array_get($data, 'attributes.isActivated')) {
if ($actor->isAdmin() && array_get($data, 'attributes.isEmailConfirmed')) {
$user->activate();
}

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

@@ -128,7 +128,7 @@ class EmailConfirmationMailer
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->id]),
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
}

View File

@@ -11,22 +11,22 @@
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 int $user_id
* @property \Carbon\Carbon $created_at
* @property string $email
*/
class EmailToken extends AbstractModel
{
/**
* {@inheritdoc}
*/
protected $table = 'email_tokens';
/**
* {@inheritdoc}
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at'];
@@ -37,6 +37,11 @@ class EmailToken extends AbstractModel
*/
public $incrementing = false;
/**
* {@inheritdoc}
*/
protected $primaryKey = 'token';
/**
* Generate an email token for the specified user.
*
@@ -49,10 +54,10 @@ class EmailToken extends AbstractModel
{
$token = new static;
$token->id = str_random(40);
$token->token = str_random(40);
$token->user_id = $userId;
$token->email = $email;
$token->created_at = time();
$token->created_at = Carbon::now();
return $token;
}
@@ -77,9 +82,10 @@ class EmailToken extends AbstractModel
*/
public function scopeValidOrFail($query, $id)
{
/** @var EmailToken $token */
$token = $query->find($id);
if (! $token || $token->created_at < new DateTime('-1 day')) {
if (! $token || $token->created_at->diffInDays() >= 1) {
throw new InvalidConfirmationTokenException;
}

View File

@@ -11,20 +11,20 @@
namespace Flarum\User;
use Carbon\Carbon;
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
{
/**
* {@inheritdoc}
*/
protected $table = 'password_tokens';
/**
* {@inheritdoc}
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at'];
@@ -35,19 +35,24 @@ 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();
$token->created_at = Carbon::now();
return $token;
}

View File

@@ -11,6 +11,7 @@
namespace Flarum\User;
use Carbon\Carbon;
use DomainException;
use Flarum\Database\AbstractModel;
use Flarum\Database\ScopeVisibilityTrait;
@@ -23,7 +24,6 @@ use Flarum\Group\Group;
use Flarum\Group\Permission;
use Flarum\Http\UrlGenerator;
use Flarum\Notification\Notification;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\User\Event\Activated;
use Flarum\User\Event\AvatarChanged;
use Flarum\User\Event\CheckingPassword;
@@ -39,19 +39,19 @@ use Illuminate\Contracts\Session\Session;
/**
* @property int $id
* @property string $username
* @property string $display_name
* @property string $email
* @property bool $is_activated
* @property bool $is_email_confirmed
* @property string $password
* @property string $locale
* @property string|null $avatar_path
* @property string $avatar_url
* @property string|null $avatar_url
* @property array $preferences
* @property \Carbon\Carbon|null $join_time
* @property \Carbon\Carbon|null $last_seen_time
* @property \Carbon\Carbon|null $read_time
* @property \Carbon\Carbon|null $notifications_read_time
* @property int $discussions_count
* @property int $comments_count
* @property \Carbon\Carbon|null $joined_at
* @property \Carbon\Carbon|null $last_seen_at
* @property \Carbon\Carbon|null $marked_all_as_read_at
* @property \Carbon\Carbon|null $read_notifications_at
* @property int $discussion_count
* @property int $comment_count
*/
class User extends AbstractModel
{
@@ -59,18 +59,15 @@ class User extends AbstractModel
use ScopeVisibilityTrait;
/**
* {@inheritdoc}
*/
protected $table = 'users';
/**
* {@inheritdoc}
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'join_time',
'last_seen_time',
'read_time',
'notifications_read_time'
'joined_at',
'last_seen_at',
'marked_all_as_read_at',
'read_notifications_at'
];
/**
@@ -81,7 +78,7 @@ class User extends AbstractModel
protected $permissions = null;
/**
* @var SessionInterface
* @var Session
*/
protected $session;
@@ -128,22 +125,6 @@ class User extends AbstractModel
static::deleted(function (User $user) {
$user->raise(new Deleted($user));
// Delete all of the posts by the user. Before we delete them
// in a big batch query, we will loop through them and raise a
// PostWasDeleted event for each post.
$posts = $user->posts()->allTypes();
foreach ($posts->cursor() as $post) {
$user->raise(new PostDeleted($post));
}
$posts->delete();
$user->read()->detach();
$user->groups()->detach();
$user->accessTokens()->delete();
$user->notifications()->delete();
});
static::$dispatcher->dispatch(
@@ -166,7 +147,7 @@ class User extends AbstractModel
$user->username = $username;
$user->email = $email;
$user->password = $password;
$user->join_time = time();
$user->joined_at = Carbon::now();
$user->raise(new Registered($user));
@@ -271,7 +252,7 @@ class User extends AbstractModel
*/
public function markAllAsRead()
{
$this->read_time = time();
$this->marked_all_as_read_at = Carbon::now();
return $this;
}
@@ -283,7 +264,7 @@ class User extends AbstractModel
*/
public function markNotificationsAsRead()
{
$this->notifications_read_time = time();
$this->read_notifications_at = Carbon::now();
return $this;
}
@@ -296,7 +277,7 @@ class User extends AbstractModel
*/
public function changeAvatarPath($path)
{
$this->avatar_path = $path;
$this->avatar_url = $path;
$this->raise(new AvatarChanged($this));
@@ -307,17 +288,16 @@ class User extends AbstractModel
* Get the URL of the user's avatar.
*
* @todo Allow different storage locations to be used
* @param string|null $value
* @return string
*/
public function getAvatarUrlAttribute()
public function getAvatarUrlAttribute(string $value = null)
{
if ($this->avatar_path) {
if (strpos($this->avatar_path, '://') !== false) {
return $this->avatar_path;
}
return app(UrlGenerator::class)->to('forum')->path('assets/avatars/'.$this->avatar_path);
if ($value && strpos($value, '://') === false) {
return app(UrlGenerator::class)->to('forum')->path('assets/avatars/'.$value);
}
return $value;
}
/**
@@ -366,8 +346,8 @@ class User extends AbstractModel
*/
public function activate()
{
if ($this->is_activated !== true) {
$this->is_activated = true;
if ($this->is_email_confirmed !== true) {
$this->is_email_confirmed = true;
$this->raise(new Activated($this));
}
@@ -438,7 +418,7 @@ class User extends AbstractModel
*
* @return int
*/
public function getUnreadNotificationsCount()
public function getUnreadNotificationCount()
{
return $this->getUnreadNotifications()->count();
}
@@ -455,8 +435,8 @@ class User extends AbstractModel
if (is_null($cached)) {
$cached = $this->notifications()
->whereIn('type', $this->getAlertableNotificationTypes())
->where('is_read', 0)
->where('is_deleted', 0)
->whereNull('read_at')
->where('is_deleted', false)
->get();
}
@@ -468,10 +448,10 @@ class User extends AbstractModel
*
* @return int
*/
public function getNewNotificationsCount()
public function getNewNotificationCount()
{
return $this->getUnreadNotifications()->filter(function ($notification) {
return $notification->time > $this->notifications_read_time ?: 0;
return $notification->created_at > $this->read_notifications_at ?: 0;
})->count();
}
@@ -570,7 +550,7 @@ class User extends AbstractModel
*/
public function updateLastSeen()
{
$this->last_seen_time = time();
$this->last_seen_at = Carbon::now();
return $this;
}
@@ -612,7 +592,7 @@ class User extends AbstractModel
*/
public function discussions()
{
return $this->hasMany('Flarum\Discussion\Discussion', 'start_user_id');
return $this->hasMany('Flarum\Discussion\Discussion');
}
/**
@@ -622,7 +602,7 @@ class User extends AbstractModel
*/
public function read()
{
return $this->belongsToMany('Flarum\Discussion\Discussion', 'users_discussions');
return $this->belongsToMany('Flarum\Discussion\Discussion');
}
/**
@@ -632,7 +612,7 @@ class User extends AbstractModel
*/
public function groups()
{
return $this->belongsToMany('Flarum\Group\Group', 'users_groups');
return $this->belongsToMany('Flarum\Group\Group');
}
/**
@@ -659,7 +639,7 @@ class User extends AbstractModel
// more than a guest. If they are activated, we can give them the
// standard 'member' group, as well as any other groups they've been
// assigned to.
if ($this->is_activated) {
if ($this->is_email_confirmed) {
$groupIds = array_merge($groupIds, [Group::MEMBER_ID], $this->groups->pluck('id')->all());
}
@@ -764,9 +744,9 @@ class User extends AbstractModel
*
* @return $this
*/
public function refreshCommentsCount()
public function refreshCommentCount()
{
$this->comments_count = $this->posts()->count();
$this->comment_count = $this->posts()->count();
return $this;
}
@@ -776,9 +756,9 @@ class User extends AbstractModel
*
* @return $this
*/
public function refreshDiscussionsCount()
public function refreshDiscussionCount()
{
$this->discussions_count = $this->discussions()->count();
$this->discussion_count = $this->discussions()->count();
return $this;
}

View File

@@ -69,16 +69,16 @@ class UserMetadataUpdater
$user = $post->user;
if ($user && $user->exists) {
$user->refreshCommentsCount()->save();
$user->refreshCommentCount()->save();
}
}
private function updateDiscussionsCount(Discussion $discussion)
{
$user = $discussion->startUser;
$user = $discussion->user;
if ($user && $user->exists) {
$user->refreshDiscussionsCount()->save();
$user->refreshDiscussionCount()->save();
}
}
}