Database changes (#15)

* Implement database changes

* Split foreign keys into their own migration

* Use whereColumn

* Rename flag.time

* Rename forum.flagCount

* Rename forum.newFlagCount
This commit is contained in:
Toby Zerner 2018-09-17 04:19:41 +09:30 committed by Franz Liedke
parent f218f14160
commit ec60fed381
11 changed files with 92 additions and 26 deletions

View File

@ -41,7 +41,7 @@ export default class FlagList extends Component {
<span className="Notification-content">
{app.translator.trans('flarum-flags.forum.flagged_posts.item_text', {username: username(post.user()), em: <em/>, discussion: post.discussion().title()})}
</span>
{humanTime(flag.time())}
{humanTime(flag.createdAt())}
<div className="Notification-excerpt">
{post.contentPlain()}
</div>
@ -63,7 +63,7 @@ export default class FlagList extends Component {
* been loaded.
*/
load() {
if (app.cache.flags && !app.session.user.attribute('newFlagsCount')) {
if (app.cache.flags && !app.session.user.attribute('newFlagCount')) {
return;
}
@ -72,8 +72,8 @@ export default class FlagList extends Component {
app.store.find('flags')
.then(flags => {
app.session.user.pushAttributes({newFlagsCount: 0});
app.cache.flags = flags.sort((a, b) => b.time() - a.time());
app.session.user.pushAttributes({newFlagCount: 0});
app.cache.flags = flags.sort((a, b) => b.createdAt() - a.createdAt());
})
.catch(() => {})
.then(() => {

View File

@ -21,10 +21,10 @@ export default class FlagsDropdown extends NotificationsDropdown {
}
getUnreadCount() {
return app.cache.flags ? app.cache.flags.length : app.forum.attribute('flagsCount');
return app.cache.flags ? app.cache.flags.length : app.forum.attribute('flagCount');
}
getNewCount() {
return app.session.user.attribute('newFlagsCount');
return app.session.user.attribute('newFlagCount');
}
}

View File

@ -1,5 +1,4 @@
import Model from 'flarum/Model';
import mixin from 'flarum/utils/mixin';
class Flag extends Model {}
@ -7,10 +6,10 @@ Object.assign(Flag.prototype, {
type: Model.attribute('type'),
reason: Model.attribute('reason'),
reasonDetail: Model.attribute('reasonDetail'),
time: Model.attribute('time', Model.transformDate),
createdAt: Model.attribute('createdAt', Model.transformDate),
post: Model.hasOne('post'),
user: Model.hasOne('user')
});
export default Flag;
export default Flag;

View File

@ -0,0 +1,14 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
return Migration::renameColumn('flags', 'time', 'created_at');

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
// Delete rows with non-existent entities so that we will be able to create
// foreign keys without any issues.
$schema->getConnection()
->table('flags')
->whereNotExists(function ($query) {
$query->selectRaw(1)->from('posts')->whereColumn('id', 'post_id');
})
->delete();
$schema->getConnection()
->table('flags')
->whereNotExists(function ($query) {
$query->selectRaw(1)->from('users')->whereColumn('id', 'user_id');
})
->update(['user_id' => null]);
$schema->table('flags', function (Blueprint $table) {
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
},
'down' => function (Builder $schema) {
$schema->table('flags', function (Blueprint $table) {
$table->dropForeign(['post_id', 'user_id']);
});
}
];

View File

@ -0,0 +1,14 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Flarum\Database\Migration;
return Migration::renameColumn('users', 'flags_read_time', 'read_flags_at');

View File

@ -41,12 +41,12 @@ class ListFlagsController extends AbstractListController
{
$actor = $request->getAttribute('actor');
$actor->flags_read_time = time();
$actor->read_flags_at = time();
$actor->save();
return Flag::whereVisibleTo($actor)
->with($this->extractInclude($request))
->latest('flags.time')
->latest('flags.created_at')
->groupBy('post_id')
->get();
}

View File

@ -31,7 +31,7 @@ class FlagSerializer extends AbstractSerializer
'type' => $flag->type,
'reason' => $flag->reason,
'reasonDetail' => $flag->reason_detail,
'time' => $this->formatDate($flag->time),
'createdAt' => $this->formatDate($flag->created_at),
];
}

View File

@ -65,7 +65,7 @@ class CreateFlagHandler
$flag->type = 'user';
$flag->reason = array_get($data, 'attributes.reason');
$flag->reason_detail = array_get($data, 'attributes.reasonDetail');
$flag->time = time();
$flag->created_at = time();
$flag->save();

View File

@ -23,12 +23,7 @@ class Flag extends AbstractModel
/**
* {@inheritdoc}
*/
protected $table = 'flags';
/**
* {@inheritdoc}
*/
protected $dates = ['time'];
protected $dates = ['created_at'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo

View File

@ -51,7 +51,7 @@ class AddFlagsApi
public function configureModelDates(ConfigureModelDates $event)
{
if ($event->isModel(User::class)) {
$event->dates[] = 'flags_read_time';
$event->dates[] = 'read_flags_at';
}
}
@ -64,14 +64,14 @@ class AddFlagsApi
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');
if ($event->attributes['canViewFlags']) {
$event->attributes['flagsCount'] = (int) $this->getFlagsCount($event->actor);
$event->attributes['flagCount'] = (int) $this->getFlagCount($event->actor);
}
$event->attributes['guidelinesUrl'] = $this->settings->get('flarum-flags.guidelines_url');
}
if ($event->isSerializer(CurrentUserSerializer::class)) {
$event->attributes['newFlagsCount'] = (int) $this->getNewFlagsCount($event->model);
$event->attributes['newFlagCount'] = (int) $this->getNewFlagCount($event->model);
}
if ($event->isSerializer(PostSerializer::class)) {
@ -83,7 +83,7 @@ class AddFlagsApi
* @param User $actor
* @return int
*/
protected function getFlagsCount(User $actor)
protected function getFlagCount(User $actor)
{
return Flag::whereVisibleTo($actor)->distinct()->count('flags.post_id');
}
@ -92,12 +92,12 @@ class AddFlagsApi
* @param User $actor
* @return int
*/
protected function getNewFlagsCount(User $actor)
protected function getNewFlagCount(User $actor)
{
$query = Flag::whereVisibleTo($actor);
if ($time = $actor->flags_read_time) {
$query->where('flags.time', '>', $time);
if ($time = $actor->read_flags_at) {
$query->where('flags.created_at', '>', $time);
}
return $query->distinct()->count('flags.post_id');