progress commit on comment replies

This commit is contained in:
Peter Deltchev 2017-11-15 11:12:08 -08:00
parent 61ee103299
commit fe5a26bd62
7 changed files with 154 additions and 18 deletions

View file

@ -20,7 +20,10 @@
namespace Poniverse\Ponyfm\Commands;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Notification;
use Poniverse\Ponyfm\Contracts\Commentable;
use Poniverse\Ponyfm\Jobs\ProcessComment;
use Poniverse\Ponyfm\Models\Album;
use Poniverse\Ponyfm\Models\Comment;
use Poniverse\Ponyfm\Models\Playlist;
@ -31,6 +34,8 @@ use Validator;
class CreateCommentCommand extends CommandBase
{
use DispatchesJobs;
private $_input;
private $_id;
private $_type;
@ -110,10 +115,13 @@ class CreateCommentCommand extends CommandBase
App::abort(400, 'This comment is being added to an invalid entity!');
}
/** @var Commentable comment_count */
$entity->comment_count = Comment::where($column, $this->_id)->count();
$entity->save();
// Sends notifications for the comment and its mentions.
Notification::newComment($comment);
$this->dispatch(new ProcessComment($comment))->onQueue('notifications');
return CommandResponse::succeed(Comment::mapPublic($comment));
}

View file

@ -66,4 +66,11 @@ interface NotificationHandler
* @return void
*/
public function newFavourite(Favouritable $entityBeingFavourited, User $favouriter);
/**
* @param Comment $commentBeingRepliedTo
* @param Comment $theReply
* @return void
*/
public function newCommentReply(Comment $commentBeingRepliedTo, Comment $theReply);
}

View file

@ -0,0 +1,60 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2017 Peter Deltchev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Poniverse\Ponyfm\Jobs;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Notification;
use Poniverse\Ponyfm\Models\Comment;
use SerializesModels;
class ProcessComment extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $comment;
/**
* Create a new job instance.
*
* @param Comment $comment
*/
public function __construct(Comment $comment)
{
$this->comment = $comment;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->beforeHandle();
$replies = Comment::findMany($this->comment->getMentionedCommentIds());
foreach ($replies as $reply) {
Notification::newCommentReply($this->comment, $reply);
}
}
}

View file

@ -86,4 +86,11 @@ class NotificationManager implements NotificationHandler
{
$this->dispatchNotification(__FUNCTION__, func_get_args());
}
/**
* @inheritdoc
*/
public function newCommentReply(Comment $commentBeingRepliedTo, Comment $theReply) {
$this->dispatchNotification(__FUNCTION__, func_get_args());
}
}

View file

@ -163,4 +163,19 @@ class Comment extends Model
parent::delete();
});
}
/**
* Parses the comment for any mentioned comments (replies).
* For a reply to be valid, it must either be followed by whitespace or be at
* the very end of the comment body. It also must be at the beginning of the
* string or preceded by whitespace.
*
* @return int[]
*/
public function getMentionedCommentIds():array {
$matches = [];
preg_match_all('/(\s|^)>c(?P<commentId>\d+)(\s|$)/', $this->content, $matches);
return array_map('intval', $matches['commentId']);
}
}

View file

@ -73,23 +73,6 @@ $factory->define(\Poniverse\Ponyfm\Models\Genre::class, function(\Faker\Generato
];
});
/**
*
* @property integer $id
* @property integer $user_id
* @property string $title
* @property string $slug
* @property string $description
* @property integer $cover_id
* @property integer $track_count
* @property integer $view_count
* @property integer $download_count
* @property integer $favourite_count
* @property integer $comment_count
* @property \Carbon\Carbon $created_at
* @property string $updated_at
* @property \Carbon\Carbon $deleted_at
*/
$factory->define(\Poniverse\Ponyfm\Models\Album::class, function(\Faker\Generator $faker) {
return [
'title' => $faker->sentence(5),
@ -97,3 +80,11 @@ $factory->define(\Poniverse\Ponyfm\Models\Album::class, function(\Faker\Generato
'description' => $faker->paragraph(5),
];
});
$factory->define(\Poniverse\Ponyfm\Models\Comment::class, function(\Faker\Generator $faker) {
return [
'ip_address' => $faker->ipv6,
'content' => $faker->slug,
];
});

48
tests/CommentsTest.php Normal file
View file

@ -0,0 +1,48 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2017 Peter Deltchev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Carbon\Carbon;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Poniverse\Ponyfm\Models\Album;
use Poniverse\Ponyfm\Models\Comment;
use Poniverse\Ponyfm\Models\Genre;
use Poniverse\Ponyfm\Models\Track;
use Poniverse\Ponyfm\Models\User;
class CommentsTest extends TestCase {
use DatabaseMigrations;
use WithoutMiddleware;
public function testCommentMentionsParsing() {
/** @var Comment $comment */
$comment = factory(Comment::class)->make();
$comment->content = <<<EOF
>c1234 This>c24 is an awesome track!!! >c65437
>u4678
>4bsfsd
gfdsgfds>c16437boomboom
>47
>c44
EOF;
$this->assertEquals([1234, 65437, 44], $comment->getMentionedCommentIds());
}
}