Menu

56.@某人(一)

本节说明

  • 对应视频教程第 56 小节:Mentioned Users Notifications: Part 1

本节内容

我们下一个准备实现的功能是 @某人。本节我们先编写必要的测试,为了更好地通过测试,我们的写法可能不是很简洁,但是我们之后会进行重构。首先我们要新建新的测试文件:

$ php artisan make:test MentionUsersTest

建立第一个测试:
forum\tests\Feature\MentionUsersTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class MentionUsersTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    public function mentioned_users_in_a_reply_are_notified()
    {
        $john = create('App\User',['name' => 'John']);

        $this->signIn($john);

        $jane = create('App\User',['name' => 'Jane']);

        $thread = create('App\Thread');

        $reply = make('App\Reply',[
           'body' => '@Jane look at this. And also @Luke'
        ]);

        $this->json('post',$thread->path() . '/replies',$reply->toArray());

        $this->assertCount(1,$jane->notifications);
    }
}

在测试中,John 发表了回复,并且在回复里 @ 了 Jane,所以 Jane 收到了一条通知。接下来我们修改控制器:
forum\app\Http\Controllers\RepliesController.php

    .
    .
    public function store($channelId, Thread $thread, CreatePostRequest $form)
    {
        $reply = $thread->addReply([
            'body' => request('body'),
            'user_id' => auth()->id(),
        ]);

        // Inspect the body of the reply for the username mentions
        preg_match_all('/\@([^\s\.]+)/',$reply->body,$matches);

        $names = $matches[1];

        // And then notify user
        foreach ($names as $name){
            $user = User::whereName($name)->first();

            if($user){
                $user->notify(new YouWereMentioned);
            }
        }

        return $reply->load('owner');
    }
    .
    .

我们使用了正则表达式,匹配出所有 @ 符号之后的名字,当然,这里指的是英文名。然后,对于匹配到的每一个用户名,我们进行通知。我们使用了一个新的通知类:YouWereMentioned,但是我们还没有建立它。我们来新建通知文件:

$ php artisan make:notification YouWereMentioned

修改内容为以下:
forum\app\Notifications\YouWereMentioned.php

<?php

namespace App\Notifications;

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

class YouWereMentioned extends Notification
{
    use Queueable;

    protected $reply;

    /**
     * YouWereMentioned constructor.
     * @param $reply
     */
    public function __construct($reply)
    {
        $this->reply = $reply;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'message' => $this->reply->owner->name . 'mentioned you in ' . $this->reply->thread->title,
            'link' => $this->reply->path()
        ];
    }
}

接下来,我们运行测试:
file
现在测试已经通过。为了方便测试,我们新注册一个名为 NoNo2 的用户,然后我们在系统进行测试:
file

本文章首发在 Laravel China 社区
上一篇 下一篇
讨论数量: 1
发起讨论


zh117
关于 56 节的遗漏?
0 个点赞 | 0 个回复 | 问答