Skip to content

Commit

Permalink
消息通知有新回复
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent aaec05b commit 7ec12fc
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;

use Auth;

class User extends Authenticatable implements MustVerifyEmailContract
{
use Notifiable, MustVerifyEmailTrait;
use MustVerifyEmailTrait;

use Notifiable {
notify as protected laravelNotify;
}
public function notify($instance)
{
// 如果要通知的人是当前用户,就不必通知了!
if ($this->id == Auth::id()) {
return;
}

// 只有数据库类型通知才需提醒,直接发送 Email 或者其他的都 Pass
if (method_exists($instance, 'toDatabase')) {
$this->increment('notification_count');
}

$this->laravelNotify($instance);
}

protected $fillable = [
'name', 'email', 'password', 'introduction', 'avatar',
Expand Down
46 changes: 46 additions & 0 deletions app/Notifications/TopicReplied.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Models\Reply;

class TopicReplied extends Notification
{
use Queueable;

public $reply;

public function __construct(Reply $reply)
{
// 注入回复实体,方便 toDatabase 方法中的使用
$this->reply = $reply;
}

public function via($notifiable)
{
// 开启通知的频道
return ['database'];
}

public function toDatabase($notifiable)
{
$topic = $this->reply->topic;
$link = $topic->link(['#reply' . $this->reply->id]);

// 存入数据库里的数据
return [
'reply_id' => $this->reply->id,
'reply_content' => $this->reply->content,
'user_id' => $this->reply->user->id,
'user_name' => $this->reply->user->name,
'user_avatar' => $this->reply->user->avatar,
'topic_link' => $link,
'topic_id' => $topic->id,
'topic_title' => $topic->title,
];
}
}
4 changes: 4 additions & 0 deletions app/Observers/ReplyObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Observers;

use App\Models\Reply;
use App\Notifications\TopicReplied;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
Expand All @@ -13,6 +14,9 @@ public function created(Reply $reply)
{
$reply->topic->reply_count = $reply->topic->replies->count();
$reply->topic->save();

// 通知话题作者有新的评论
$reply->topic->user->notify(new TopicReplied($reply));
}

public function creating(Reply $reply)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddNotificationCountToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('notification_count')->unsigned()->default(0);
});
}

public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('notification_count');
});
}
}

0 comments on commit 7ec12fc

Please sign in to comment.