mirror of
https://github.com/flarum/framework.git
synced 2025-05-05 11:04:04 +08:00
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:
parent
f218f14160
commit
ec60fed381
@ -41,7 +41,7 @@ export default class FlagList extends Component {
|
|||||||
<span className="Notification-content">
|
<span className="Notification-content">
|
||||||
{app.translator.trans('flarum-flags.forum.flagged_posts.item_text', {username: username(post.user()), em: <em/>, discussion: post.discussion().title()})}
|
{app.translator.trans('flarum-flags.forum.flagged_posts.item_text', {username: username(post.user()), em: <em/>, discussion: post.discussion().title()})}
|
||||||
</span>
|
</span>
|
||||||
{humanTime(flag.time())}
|
{humanTime(flag.createdAt())}
|
||||||
<div className="Notification-excerpt">
|
<div className="Notification-excerpt">
|
||||||
{post.contentPlain()}
|
{post.contentPlain()}
|
||||||
</div>
|
</div>
|
||||||
@ -63,7 +63,7 @@ export default class FlagList extends Component {
|
|||||||
* been loaded.
|
* been loaded.
|
||||||
*/
|
*/
|
||||||
load() {
|
load() {
|
||||||
if (app.cache.flags && !app.session.user.attribute('newFlagsCount')) {
|
if (app.cache.flags && !app.session.user.attribute('newFlagCount')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,8 +72,8 @@ export default class FlagList extends Component {
|
|||||||
|
|
||||||
app.store.find('flags')
|
app.store.find('flags')
|
||||||
.then(flags => {
|
.then(flags => {
|
||||||
app.session.user.pushAttributes({newFlagsCount: 0});
|
app.session.user.pushAttributes({newFlagCount: 0});
|
||||||
app.cache.flags = flags.sort((a, b) => b.time() - a.time());
|
app.cache.flags = flags.sort((a, b) => b.createdAt() - a.createdAt());
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
@ -21,10 +21,10 @@ export default class FlagsDropdown extends NotificationsDropdown {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getUnreadCount() {
|
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() {
|
getNewCount() {
|
||||||
return app.session.user.attribute('newFlagsCount');
|
return app.session.user.attribute('newFlagCount');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import Model from 'flarum/Model';
|
import Model from 'flarum/Model';
|
||||||
import mixin from 'flarum/utils/mixin';
|
|
||||||
|
|
||||||
class Flag extends Model {}
|
class Flag extends Model {}
|
||||||
|
|
||||||
@ -7,7 +6,7 @@ Object.assign(Flag.prototype, {
|
|||||||
type: Model.attribute('type'),
|
type: Model.attribute('type'),
|
||||||
reason: Model.attribute('reason'),
|
reason: Model.attribute('reason'),
|
||||||
reasonDetail: Model.attribute('reasonDetail'),
|
reasonDetail: Model.attribute('reasonDetail'),
|
||||||
time: Model.attribute('time', Model.transformDate),
|
createdAt: Model.attribute('createdAt', Model.transformDate),
|
||||||
|
|
||||||
post: Model.hasOne('post'),
|
post: Model.hasOne('post'),
|
||||||
user: Model.hasOne('user')
|
user: Model.hasOne('user')
|
||||||
|
@ -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');
|
@ -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']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
];
|
@ -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');
|
@ -41,12 +41,12 @@ class ListFlagsController extends AbstractListController
|
|||||||
{
|
{
|
||||||
$actor = $request->getAttribute('actor');
|
$actor = $request->getAttribute('actor');
|
||||||
|
|
||||||
$actor->flags_read_time = time();
|
$actor->read_flags_at = time();
|
||||||
$actor->save();
|
$actor->save();
|
||||||
|
|
||||||
return Flag::whereVisibleTo($actor)
|
return Flag::whereVisibleTo($actor)
|
||||||
->with($this->extractInclude($request))
|
->with($this->extractInclude($request))
|
||||||
->latest('flags.time')
|
->latest('flags.created_at')
|
||||||
->groupBy('post_id')
|
->groupBy('post_id')
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ class FlagSerializer extends AbstractSerializer
|
|||||||
'type' => $flag->type,
|
'type' => $flag->type,
|
||||||
'reason' => $flag->reason,
|
'reason' => $flag->reason,
|
||||||
'reasonDetail' => $flag->reason_detail,
|
'reasonDetail' => $flag->reason_detail,
|
||||||
'time' => $this->formatDate($flag->time),
|
'createdAt' => $this->formatDate($flag->created_at),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class CreateFlagHandler
|
|||||||
$flag->type = 'user';
|
$flag->type = 'user';
|
||||||
$flag->reason = array_get($data, 'attributes.reason');
|
$flag->reason = array_get($data, 'attributes.reason');
|
||||||
$flag->reason_detail = array_get($data, 'attributes.reasonDetail');
|
$flag->reason_detail = array_get($data, 'attributes.reasonDetail');
|
||||||
$flag->time = time();
|
$flag->created_at = time();
|
||||||
|
|
||||||
$flag->save();
|
$flag->save();
|
||||||
|
|
||||||
|
@ -23,12 +23,7 @@ class Flag extends AbstractModel
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected $table = 'flags';
|
protected $dates = ['created_at'];
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
protected $dates = ['time'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
@ -51,7 +51,7 @@ class AddFlagsApi
|
|||||||
public function configureModelDates(ConfigureModelDates $event)
|
public function configureModelDates(ConfigureModelDates $event)
|
||||||
{
|
{
|
||||||
if ($event->isModel(User::class)) {
|
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');
|
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');
|
||||||
|
|
||||||
if ($event->attributes['canViewFlags']) {
|
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');
|
$event->attributes['guidelinesUrl'] = $this->settings->get('flarum-flags.guidelines_url');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($event->isSerializer(CurrentUserSerializer::class)) {
|
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)) {
|
if ($event->isSerializer(PostSerializer::class)) {
|
||||||
@ -83,7 +83,7 @@ class AddFlagsApi
|
|||||||
* @param User $actor
|
* @param User $actor
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function getFlagsCount(User $actor)
|
protected function getFlagCount(User $actor)
|
||||||
{
|
{
|
||||||
return Flag::whereVisibleTo($actor)->distinct()->count('flags.post_id');
|
return Flag::whereVisibleTo($actor)->distinct()->count('flags.post_id');
|
||||||
}
|
}
|
||||||
@ -92,12 +92,12 @@ class AddFlagsApi
|
|||||||
* @param User $actor
|
* @param User $actor
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function getNewFlagsCount(User $actor)
|
protected function getNewFlagCount(User $actor)
|
||||||
{
|
{
|
||||||
$query = Flag::whereVisibleTo($actor);
|
$query = Flag::whereVisibleTo($actor);
|
||||||
|
|
||||||
if ($time = $actor->flags_read_time) {
|
if ($time = $actor->read_flags_at) {
|
||||||
$query->where('flags.time', '>', $time);
|
$query->where('flags.created_at', '>', $time);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $query->distinct()->count('flags.post_id');
|
return $query->distinct()->count('flags.post_id');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user