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 $request)
    {
        $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($reply));
            }
        }

        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

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 2
发起讨论 查看所有版本


zh117
关于 56 节的遗漏?
1 个点赞 | 0 个回复 | 问答
sargerase
都没有这个静态函数为何还能用
0 个点赞 | 3 个回复 | 分享