From eaf465914ae526a9aac0522f71d8ccbcc2f0e7ef Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 4 Jun 2018 02:16:33 -0600 Subject: [PATCH] Add comment notifications --- app/Http/Controllers/AccountController.php | 4 ++- app/Http/Controllers/CommentController.php | 2 ++ app/Jobs/FollowPipeline/FollowPipeline.php | 2 ++ app/Jobs/LikePipeline/LikePipeline.php | 2 ++ app/Notification.php | 10 ++++++ app/Status.php | 13 ++++++++ ...ons_table_add_polymorphic_relationship.php | 31 +++++++++++++++++++ resources/lang/en/notification.php | 1 + resources/views/account/activity.blade.php | 20 ++++++++++++ 9 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2018_06_04_061435_update_notifications_table_add_polymorphic_relationship.php diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index f2d1f4092..4a0b5bd5f 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -16,7 +16,9 @@ class AccountController extends Controller public function notifications(Request $request) { $profile = Auth::user()->profile; - $notifications = $this->fetchNotifications($profile->id); + //$notifications = $this->fetchNotifications($profile->id); + $notifications = Notification::whereProfileId($profile->id) + ->orderBy('id','desc')->take(30)->simplePaginate(); return view('account.activity', compact('profile', 'notifications')); } diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 1815914c3..1ec7bbf4d 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use App\Jobs\CommentPipeline\CommentPipeline; use App\Jobs\StatusPipeline\NewStatusPipeline; use Auth, Hashids; use App\{Comment, Profile, Status}; @@ -40,6 +41,7 @@ class CommentController extends Controller $reply->save(); NewStatusPipeline::dispatch($reply, false); + CommentPipeline::dispatch($status, $reply); if($request->ajax()) { $response = ['code' => 200, 'msg' => 'Comment saved', 'username' => $profile->username, 'url' => $reply->url(), 'profile' => $profile->url()]; diff --git a/app/Jobs/FollowPipeline/FollowPipeline.php b/app/Jobs/FollowPipeline/FollowPipeline.php index 6688575f3..f7cfa6506 100644 --- a/app/Jobs/FollowPipeline/FollowPipeline.php +++ b/app/Jobs/FollowPipeline/FollowPipeline.php @@ -46,6 +46,8 @@ class FollowPipeline implements ShouldQueue $notification->action = 'follow'; $notification->message = $follower->toText(); $notification->rendered = $follower->toHtml(); + $notification->item_id = $target->id; + $notification->item_type = "App\Profile"; $notification->save(); Cache::forever('notification.' . $notification->id, $notification); diff --git a/app/Jobs/LikePipeline/LikePipeline.php b/app/Jobs/LikePipeline/LikePipeline.php index 8e57072dd..28410bfc3 100644 --- a/app/Jobs/LikePipeline/LikePipeline.php +++ b/app/Jobs/LikePipeline/LikePipeline.php @@ -49,6 +49,8 @@ class LikePipeline implements ShouldQueue $notification->action = 'like'; $notification->message = $like->toText(); $notification->rendered = $like->toHtml(); + $notification->item_id = $status->id; + $notification->item_type = "App\Status"; $notification->save(); Cache::forever('notification.' . $notification->id, $notification); diff --git a/app/Notification.php b/app/Notification.php index 4102ba56e..c40e0a553 100644 --- a/app/Notification.php +++ b/app/Notification.php @@ -17,4 +17,14 @@ class Notification extends Model return $this->belongsTo(Profile::class, 'profile_id', 'id'); } + public function item() + { + return $this->morphTo(); + } + + public function status() + { + return $this->belongsTo(Status::class, 'item_id', 'id'); + } + } diff --git a/app/Status.php b/app/Status.php index 3bf4fc9ca..e5e1a59b9 100644 --- a/app/Status.php +++ b/app/Status.php @@ -101,4 +101,17 @@ class Status extends Model return $obj; } + public function replyToText() + { + $actorName = $this->profile->username; + return "{$actorName} " . __('notification.commented'); + } + + public function replyToHtml() + { + $actorName = $this->profile->username; + $actorUrl = $this->profile->url(); + return "{$actorName} " . + __('notification.commented'); + } } diff --git a/database/migrations/2018_06_04_061435_update_notifications_table_add_polymorphic_relationship.php b/database/migrations/2018_06_04_061435_update_notifications_table_add_polymorphic_relationship.php new file mode 100644 index 000000000..c55fa1573 --- /dev/null +++ b/database/migrations/2018_06_04_061435_update_notifications_table_add_polymorphic_relationship.php @@ -0,0 +1,31 @@ +bigInteger('item_id')->unsigned()->nullable()->after('actor_id'); + $table->string('item_type')->nullable()->after('item_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/lang/en/notification.php b/resources/lang/en/notification.php index 99b6ed099..2f85c4386 100644 --- a/resources/lang/en/notification.php +++ b/resources/lang/en/notification.php @@ -4,5 +4,6 @@ return [ 'likedPhoto' => 'liked your photo.', 'startedFollowingYou' => 'started following you.', + 'commented' => 'commented on your post.', ]; \ No newline at end of file diff --git a/resources/views/account/activity.blade.php b/resources/views/account/activity.blade.php index 241d46f8a..a6bd0c388 100644 --- a/resources/views/account/activity.blade.php +++ b/resources/views/account/activity.blade.php @@ -18,8 +18,12 @@ {{$notification->created_at->diffForHumans(null, true, true, true)}} + @if($notification->item_id) + + @endif @break + @case('follow') @@ -38,6 +42,22 @@ @endif @break + + @case('comment') + + + + + {!! $notification->rendered !!} + {{$notification->created_at->diffForHumans(null, true, true, true)}} + + + @if($notification->item_id) + + @endif + + @break + @endswitch @endforeach