Laravel 5.3 - 用户集合(关注者)的单一通知

24

当我只有一个可通知的user时,将插入一条记录到notifications表中,并通过渠道发送mail/sms,这个过程是完美工作的。

问题出现在我有一个user集合,即1000个用户关注我的情况下,当我发布更新时。使用建议的Notifiable trait进行多用户场景通知的处理如下:

  1. 发送1k封mails/sms(没问题)
  2. 添加1k条到数据库中的notifications表中

在将1k个通知添加到数据库中的notifications表中并不是最优解。因为所有行的toArray数据都相同,notifications表中的其他所有内容也相同,唯一的区别就是notifiable_typeusernotifiable_id不同而已。

一个开箱即用的最优解如下:

  1. Laravel会注意到它是一个array notifiable_type
  2. 将单个通知保存为notifiable_typeuser_arrayusernotifiable_id为0(零只用于表示这是一个多可通知用户)
  3. 创建/使用另一个表notifications_read,将它刚刚创建的notification_id作为foreign_key,并插入1k行,只包括以下字段:

    notification_id notifiable_id notifiable_type read_at

我希望现在已经有一种方法可以实现这个,因为我的应用程序已经到了这个地步,我很想使用内置的通知和通道来处理这种情况。 我正在发送电子邮件 / 短信通知,重复1k次没有问题,但输入相同数据到数据库中需要优化解决。 您有什么想法/建议如何在这种情况下继续进行?
2个回答

14

更新于2017年1月14日:实现了更正确的方法

快速示例:

use Illuminate\Support\Facades\Notification;
use App\Notifications\SomethingCoolHappen;

Route::get('/step1', function () {
    // example - my followers
    $followers = App\User::all();

    // notify them
    Notification::send($followers, new SomethingCoolHappen(['arg1' => 1, 'arg2' => 2]));
});

Route::get('/step2', function () {
    // my follower
    $user = App\User::find(10);

    // check unread subnotifications
    foreach ($user->unreadSubnotifications as $subnotification) {
        var_dump($subnotification->notification->data);
        $subnotification->markAsRead();
    }
});

如何让它工作?

步骤1 - 迁移 - 创建表(subnotifications)

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

class CreateSubnotificationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('subnotifications', function (Blueprint $table) {
            // primary key
            $table->increments('id')->primary();

            // notifications.id
            $table->uuid('notification_id');

            // notifiable_id and notifiable_type
            $table->morphs('notifiable');

            // follower - read_at
            $table->timestamp('read_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('subnotifications');
    }
}

第二步 - 让我们为新的子通知表创建一个模型

<?php
// App\Notifications\Subnotification.php
namespace App\Notifications;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;

class Subnotification extends Model
{
    // we don't use created_at/updated_at
    public $timestamps = false;

    // nothing guarded - mass assigment allowed
    protected $guarded = [];

    // cast read_at as datetime
    protected $casts = [
        'read_at' => 'datetime',
    ];

    // set up relation to the parent notification
    public function notification()
    {
        return $this->belongsTo(DatabaseNotification::class);
    }

    /**
     * Get the notifiable entity that the notification belongs to.
     */
    public function notifiable()
    {
        return $this->morphTo();
    }

    /**
     * Mark the subnotification as read.
     *
     * @return void
     */
    public function markAsRead()
    {
        if (is_null($this->read_at)) {
            $this->forceFill(['read_at' => $this->freshTimestamp()])->save();
        }
    }
}

第三步 - 创建自定义数据库通知渠道
更新:使用静态变量$map来保留第一个通知的id,并插入下一个通知(具有相同的数据),而无需在notifications表中创建记录。

<?php
// App\Channels\SubnotificationsChannel.php
namespace App\Channels;

use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\Notification;

class SubnotificationsChannel
{
    /**
     * Send the given notification.
     *
     * @param  mixed                                  $notifiable
     * @param  \Illuminate\Notifications\Notification $notification
     *
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        static $map = [];

        $notificationId = $notification->id;

        // get notification data
        $data = $this->getData($notifiable, $notification);

        // calculate hash
        $hash = md5(json_encode($data));

        // if hash is not in map - create parent notification record
        if (!isset($map[$hash])) {
            // create original notification record with empty notifiable_id
            DatabaseNotification::create([
                'id'              => $notificationId,
                'type'            => get_class($notification),
                'notifiable_id'   => 0,
                'notifiable_type' => get_class($notifiable),
                'data'            => $data,
                'read_at'         => null,
            ]);

            $map[$hash] = $notificationId;
        } else {
            // otherwise use another/first notification id
            $notificationId = $map[$hash];
        }

        // create subnotification
        $notifiable->subnotifications()->create([
            'notification_id' => $notificationId,
            'read_at'         => null
        ]);
    }

    /**
     * Prepares data
     *
     * @param mixed                                  $notifiable
     * @param \Illuminate\Notifications\Notification $notification
     *
     * @return mixed
     */
    public function getData($notifiable, Notification $notification)
    {
        return $notification->toArray($notifiable);
    }
}

第四步 - 创建通知
更新: 现在通知支持所有渠道,不仅仅是子通知。

<?php
// App\Notifications\SomethingCoolHappen.php
namespace App\Notifications;

use App\Channels\SubnotificationsChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class SomethingCoolHappen extends Notification
{
    use Queueable;

    protected $data;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        /**
         * THIS IS A GOOD PLACE FOR DETERMINING NECESSARY CHANNELS
         */
        $via = [];
        $via[] = SubnotificationsChannel::class;
        //$via[] = 'mail';
        return $via;
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'https://laravel.com')
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return $this->data;
    }
}

步骤五 - “followers” 助手特性

<?php
// App\Notifications\HasSubnotifications.php
namespace App\Notifications;

trait HasSubnotifications
{
    /**
     * Get the entity's notifications.
     */
    public function Subnotifications()
    {
        return $this->morphMany(Subnotification::class, 'notifiable')
            ->orderBy('id', 'desc');
    }

    /**
     * Get the entity's read notifications.
     */
    public function readSubnotifications()
    {
        return $this->Subnotifications()
            ->whereNotNull('read_at');
    }

    /**
     * Get the entity's unread notifications.
     */
    public function unreadSubnotifications()
    {
        return $this->Subnotifications()
            ->whereNull('read_at');
    }
}

Step 6 - update your Users model
Updated: no required followers method

namespace App;

use App\Notifications\HasSubnotifications;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Adding helpers to followers:
     *
     * $user->subnotifications - all subnotifications
     * $user->unreadSubnotifications - all unread subnotifications
     * $user->readSubnotifications - all read subnotifications
     */
    use HasSubnotifications;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}


谢谢!这个很好用 :) 在第四步中,我注意到toMail丢失了,我有一些逻辑来查找想要电子邮件的关注者,但是我应该把那个逻辑放在哪里呢?由于示例使用$user->notify(new FollowersUpdate(...,当我在第4步中放置toMail时,它会向$user发送电子邮件,这不是我们想要的,因为我们想要使用一些用户详细信息向关注者发送电子邮件。我认为将其放入FollowerChannel中并不正确,因为它处理数据库表条目?您有什么想法可以实现正确的位置吗? - Wonka
@Wonka,正如之前所做的那样。该方法仅实现了自定义数据库通道。您可以尝试将逻辑放在FollowerChannel中,但是您是正确的 - 那不是个好主意。换句话说,您必须同时使用Notifications:原始通知与短信/电子邮件以及FollowersUpdate(以进行子通知)。我相信这对于您的优化来说是微不足道的代价。 - Leonid Shumakov
@Wonka,我再想了一下,提出了更正确的解决方案。请查看。希望你会发现它更有用。 - Leonid Shumakov
谢谢 :) 我使用了您更新的示例,它允许在单个通知中添加多个通道,因此对于100个关注者,它会添加100个子通知,但即使只有80个关注者想要电子邮件,它也会发送100封电子邮件,另外20个人也会收到电子邮件。我打开了这个问题:http://stackoverflow.com/questions/41643939/laravel-5-3-single-notification-file-for-different-user-collections 但是提供的解决方案是针对每个$follower进行foreach以确定他们的偏好并循环通知,而不是发送$followers并在通道中处理偏好。 - Wonka
@Wonka,是的,请在第4步(通知)中通过方法放置所需的逻辑。$notifiable 是您关注者的 Eloquent 模型。仅向 Gmail 用户发送电子邮件:if (stripos($notifiable->email, '@gmail.com') !== false) $via[] = 'mail'; - Leonid Shumakov
显示剩余3条评论

7

是的,你说得对。我想使用默认的 Notifiable 特性,可以创建一个 自定义通道

您可以查看 Illuminate\Notifications\Channels\DatabaseChannel 类来进行默认创建,并将其调整为适合于关联表的方式。

希望这能帮助您创建一个新的通道,并将 HasDatabasePivotNotifications 特性(或类似名称)实现到您自己的 Notifiable 特性中。


3
你介意分享一些具体的代码来展示如何实现吗? - Wonka
我添加了100分的赏金,因为我不确定如何完全实现你的解决方案。你能否用示例代码/伪代码详细说明你的解决方案,以便我可以实现并报告结果?基于点赞数量,很多人似乎对此感兴趣 :) - Wonka
1
我会在周日之前(从现在起3天)添加。 - Isfettcom
@Isfettcom,您能否展示一下实现方式? - bzeaman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接