From ec60fed3812d71091f1bbf14e4f766640016c3ed Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 17 Sep 2018 04:19:41 +0930 Subject: [PATCH] Database changes (#15) * Implement database changes * Split foreign keys into their own migration * Use whereColumn * Rename flag.time * Rename forum.flagCount * Rename forum.newFlagCount --- .../flags/js/src/forum/components/FlagList.js | 8 ++-- .../js/src/forum/components/FlagsDropdown.js | 4 +- extensions/flags/js/src/forum/models/Flag.js | 5 +-- ...change_flags_rename_time_to_created_at.php | 14 ++++++ ...7_101600_change_flags_add_foreign_keys.php | 44 +++++++++++++++++++ ...ename_flags_read_time_to_read_flags_at.php | 14 ++++++ .../Api/Controller/ListFlagsController.php | 4 +- .../src/Api/Serializer/FlagSerializer.php | 2 +- .../flags/src/Command/CreateFlagHandler.php | 2 +- extensions/flags/src/Flag.php | 7 +-- extensions/flags/src/Listener/AddFlagsApi.php | 14 +++--- 11 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 extensions/flags/migrations/2018_06_27_101500_change_flags_rename_time_to_created_at.php create mode 100644 extensions/flags/migrations/2018_06_27_101600_change_flags_add_foreign_keys.php create mode 100644 extensions/flags/migrations/2018_06_27_105100_change_users_rename_flags_read_time_to_read_flags_at.php diff --git a/extensions/flags/js/src/forum/components/FlagList.js b/extensions/flags/js/src/forum/components/FlagList.js index 6781cf36f..57e7446dc 100644 --- a/extensions/flags/js/src/forum/components/FlagList.js +++ b/extensions/flags/js/src/forum/components/FlagList.js @@ -41,7 +41,7 @@ export default class FlagList extends Component { {app.translator.trans('flarum-flags.forum.flagged_posts.item_text', {username: username(post.user()), em: , discussion: post.discussion().title()})} - {humanTime(flag.time())} + {humanTime(flag.createdAt())}
{post.contentPlain()}
@@ -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(() => { diff --git a/extensions/flags/js/src/forum/components/FlagsDropdown.js b/extensions/flags/js/src/forum/components/FlagsDropdown.js index 88e5b7760..0b8896d8f 100644 --- a/extensions/flags/js/src/forum/components/FlagsDropdown.js +++ b/extensions/flags/js/src/forum/components/FlagsDropdown.js @@ -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'); } } diff --git a/extensions/flags/js/src/forum/models/Flag.js b/extensions/flags/js/src/forum/models/Flag.js index 32e9d4724..e89cac078 100644 --- a/extensions/flags/js/src/forum/models/Flag.js +++ b/extensions/flags/js/src/forum/models/Flag.js @@ -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; \ No newline at end of file +export default Flag; diff --git a/extensions/flags/migrations/2018_06_27_101500_change_flags_rename_time_to_created_at.php b/extensions/flags/migrations/2018_06_27_101500_change_flags_rename_time_to_created_at.php new file mode 100644 index 000000000..430461756 --- /dev/null +++ b/extensions/flags/migrations/2018_06_27_101500_change_flags_rename_time_to_created_at.php @@ -0,0 +1,14 @@ + + * + * 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'); diff --git a/extensions/flags/migrations/2018_06_27_101600_change_flags_add_foreign_keys.php b/extensions/flags/migrations/2018_06_27_101600_change_flags_add_foreign_keys.php new file mode 100644 index 000000000..92c14ab01 --- /dev/null +++ b/extensions/flags/migrations/2018_06_27_101600_change_flags_add_foreign_keys.php @@ -0,0 +1,44 @@ + + * + * 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']); + }); + } +]; diff --git a/extensions/flags/migrations/2018_06_27_105100_change_users_rename_flags_read_time_to_read_flags_at.php b/extensions/flags/migrations/2018_06_27_105100_change_users_rename_flags_read_time_to_read_flags_at.php new file mode 100644 index 000000000..4169a9881 --- /dev/null +++ b/extensions/flags/migrations/2018_06_27_105100_change_users_rename_flags_read_time_to_read_flags_at.php @@ -0,0 +1,14 @@ + + * + * 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'); diff --git a/extensions/flags/src/Api/Controller/ListFlagsController.php b/extensions/flags/src/Api/Controller/ListFlagsController.php index 5c7dced46..77296808e 100644 --- a/extensions/flags/src/Api/Controller/ListFlagsController.php +++ b/extensions/flags/src/Api/Controller/ListFlagsController.php @@ -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(); } diff --git a/extensions/flags/src/Api/Serializer/FlagSerializer.php b/extensions/flags/src/Api/Serializer/FlagSerializer.php index 91eafb8b4..233bf5ef9 100644 --- a/extensions/flags/src/Api/Serializer/FlagSerializer.php +++ b/extensions/flags/src/Api/Serializer/FlagSerializer.php @@ -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), ]; } diff --git a/extensions/flags/src/Command/CreateFlagHandler.php b/extensions/flags/src/Command/CreateFlagHandler.php index b4d1ef39e..f52942274 100644 --- a/extensions/flags/src/Command/CreateFlagHandler.php +++ b/extensions/flags/src/Command/CreateFlagHandler.php @@ -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(); diff --git a/extensions/flags/src/Flag.php b/extensions/flags/src/Flag.php index 5795418e3..e5da8cb16 100644 --- a/extensions/flags/src/Flag.php +++ b/extensions/flags/src/Flag.php @@ -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 diff --git a/extensions/flags/src/Listener/AddFlagsApi.php b/extensions/flags/src/Listener/AddFlagsApi.php index 665845906..7d4e896ac 100755 --- a/extensions/flags/src/Listener/AddFlagsApi.php +++ b/extensions/flags/src/Listener/AddFlagsApi.php @@ -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');