Revert notifications_from table

I didn't think this change through and it's going to be too difficult
to implement right now. It can wait until we do the notifications
revamp. For now reverting back to the old structure, with the
`sender_id` column renamed to `from_user_id`.
This commit is contained in:
Toby Zerner 2018-07-21 18:35:50 +09:30
parent 420bb2efc8
commit aa4c4b07bd
8 changed files with 21 additions and 70 deletions

View File

@ -1,24 +0,0 @@
<?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;
use Illuminate\Database\Schema\Blueprint;
return Migration::createTable(
'notifications_from',
function (Blueprint $table) {
$table->integer('id')->unsigned();
$table->integer('from_user_id')->unsigned();
$table->foreign('id')->references('id')->on('notifications')->onDelete('cascade');
$table->foreign('from_user_id')->references('id')->on('users')->onDelete('cascade');
}
);

View File

@ -1,34 +0,0 @@
<?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\Builder;
return [
'up' => function (Builder $schema) {
$query = $schema->getConnection()->table('notifications')
->whereExists(function ($query) {
$query->selectRaw(1)->from('users')->whereRaw('id = sender_id');
});
foreach ($query->cursor() as $notification) {
$insert = [
'id' => $notification->id,
'from_user_id' => $notification->sender_id
];
$schema->getConnection()->table('notifications_from')->updateOrInsert($insert, $insert);
}
},
'down' => function (Builder $schema) {
$schema->getConnection()->table('notifications_from')->truncate();
}
];

View File

@ -16,9 +16,10 @@ use Illuminate\Database\Schema\Builder;
return [
'up' => function (Builder $schema) {
$schema->table('notifications', function (Blueprint $table) {
$table->dropColumn('sender_id', 'subject_type');
$table->dropColumn('subject_type');
$table->renameColumn('time', 'created_at');
$table->renameColumn('sender_id', 'from_user_id');
$table->dateTime('read_at')->nullable();
});
@ -34,10 +35,10 @@ return [
'down' => function (Builder $schema) {
$schema->table('notifications', function (Blueprint $table) {
$table->integer('sender_id')->unsigned()->nullable();
$table->string('subject_type', 200)->nullable();
$table->renameColumn('created_at', 'time');
$table->renameColumn('from_user_id', 'sender_id');
$table->boolean('is_read');
});

View File

@ -23,14 +23,22 @@ return [
})
->delete();
$schema->getConnection()
->table('notifications')
->whereNotExists(function ($query) {
$query->selectRaw(1)->from('users')->whereRaw('id = from_user_id');
})
->update(['from_user_id' => null]);
$schema->table('notifications', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('from_user_id')->references('id')->on('users')->onDelete('set null');
});
},
'down' => function (Builder $schema) {
$schema->table('notifications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['user_id', 'from_user_id']);
});
}
];

View File

@ -49,7 +49,7 @@ class Notification extends AbstractModel
*
* @var array
*/
protected $dates = ['created_at', 'read_at', 'deleted_at'];
protected $dates = ['created_at', 'read_at'];
/**
* A map of notification types and the model classes to use for their
@ -112,7 +112,7 @@ class Notification extends AbstractModel
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id');
return $this->belongsTo(User::class);
}
/**
@ -122,7 +122,7 @@ class Notification extends AbstractModel
*/
public function sender()
{
return $this->belongsTo(User::class, 'sender_id');
return $this->belongsTo(User::class, 'from_user_id');
}
/**
@ -132,7 +132,7 @@ class Notification extends AbstractModel
*/
public function subject()
{
return $this->morphTo('subject', 'subjectModel', 'subject_id');
return $this->morphTo('subject', 'subjectModel');
}
/**

View File

@ -32,7 +32,7 @@ class NotificationRepository
)
->where('user_id', $user->id)
->whereIn('type', $user->getAlertableNotificationTypes())
->whereNull('deleted_at')
->where('is_deleted', false)
->groupBy('type', 'subject_id')
->orderByRaw('MAX(created_at) DESC')
->skip($offset)

View File

@ -225,10 +225,10 @@ class NotificationSyncer
protected function getAttributes(Blueprint\BlueprintInterface $blueprint)
{
return [
'type' => $blueprint::getType(),
'sender_id' => ($sender = $blueprint->getSender()) ? $sender->id : null,
'type' => $blueprint::getType(),
'from_user_id' => ($sender = $blueprint->getSender()) ? $sender->id : null,
'subject_id' => ($subject = $blueprint->getSubject()) ? $subject->id : null,
'data' => ($data = $blueprint->getData()) ? json_encode($data) : null
'data' => ($data = $blueprint->getData()) ? json_encode($data) : null
];
}
}

View File

@ -435,7 +435,7 @@ class User extends AbstractModel
$cached = $this->notifications()
->whereIn('type', $this->getAlertableNotificationTypes())
->whereNull('read_at')
->whereNull('deleted_at')
->where('is_deleted', false)
->get();
}