Add comment notifications

This commit is contained in:
Daniel Supernault 2018-06-04 02:16:33 -06:00
parent 059791c6af
commit eaf465914a
9 changed files with 84 additions and 1 deletions

View file

@ -16,7 +16,9 @@ class AccountController extends Controller
public function notifications(Request $request) public function notifications(Request $request)
{ {
$profile = Auth::user()->profile; $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')); return view('account.activity', compact('profile', 'notifications'));
} }

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Jobs\CommentPipeline\CommentPipeline;
use App\Jobs\StatusPipeline\NewStatusPipeline; use App\Jobs\StatusPipeline\NewStatusPipeline;
use Auth, Hashids; use Auth, Hashids;
use App\{Comment, Profile, Status}; use App\{Comment, Profile, Status};
@ -40,6 +41,7 @@ class CommentController extends Controller
$reply->save(); $reply->save();
NewStatusPipeline::dispatch($reply, false); NewStatusPipeline::dispatch($reply, false);
CommentPipeline::dispatch($status, $reply);
if($request->ajax()) { if($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Comment saved', 'username' => $profile->username, 'url' => $reply->url(), 'profile' => $profile->url()]; $response = ['code' => 200, 'msg' => 'Comment saved', 'username' => $profile->username, 'url' => $reply->url(), 'profile' => $profile->url()];

View file

@ -46,6 +46,8 @@ class FollowPipeline implements ShouldQueue
$notification->action = 'follow'; $notification->action = 'follow';
$notification->message = $follower->toText(); $notification->message = $follower->toText();
$notification->rendered = $follower->toHtml(); $notification->rendered = $follower->toHtml();
$notification->item_id = $target->id;
$notification->item_type = "App\Profile";
$notification->save(); $notification->save();
Cache::forever('notification.' . $notification->id, $notification); Cache::forever('notification.' . $notification->id, $notification);

View file

@ -49,6 +49,8 @@ class LikePipeline implements ShouldQueue
$notification->action = 'like'; $notification->action = 'like';
$notification->message = $like->toText(); $notification->message = $like->toText();
$notification->rendered = $like->toHtml(); $notification->rendered = $like->toHtml();
$notification->item_id = $status->id;
$notification->item_type = "App\Status";
$notification->save(); $notification->save();
Cache::forever('notification.' . $notification->id, $notification); Cache::forever('notification.' . $notification->id, $notification);

View file

@ -17,4 +17,14 @@ class Notification extends Model
return $this->belongsTo(Profile::class, 'profile_id', 'id'); 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');
}
} }

View file

@ -101,4 +101,17 @@ class Status extends Model
return $obj; 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 "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> " .
__('notification.commented');
}
} }

View file

@ -0,0 +1,31 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateNotificationsTableAddPolymorphicRelationship extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('notifications', function (Blueprint $table) {
$table->bigInteger('item_id')->unsigned()->nullable()->after('actor_id');
$table->string('item_type')->nullable()->after('item_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View file

@ -4,5 +4,6 @@ return [
'likedPhoto' => 'liked your photo.', 'likedPhoto' => 'liked your photo.',
'startedFollowingYou' => 'started following you.', 'startedFollowingYou' => 'started following you.',
'commented' => 'commented on your post.',
]; ];

View file

@ -18,8 +18,12 @@
<span class="text-muted notification-timestamp pl-1">{{$notification->created_at->diffForHumans(null, true, true, true)}}</span> <span class="text-muted notification-timestamp pl-1">{{$notification->created_at->diffForHumans(null, true, true, true)}}</span>
</span> </span>
<span class="float-right notification-action"> <span class="float-right notification-action">
@if($notification->item_id)
<a href="{{$notification->status->url()}}"><img src="{{$notification->status->thumb()}}" width="32px" height="32px"></a>
@endif
</span> </span>
@break @break
@case('follow') @case('follow')
<span class="notification-icon pr-3"> <span class="notification-icon pr-3">
<img src="{{$notification->actor->avatarUrl()}}" width="32px" class="rounded-circle"> <img src="{{$notification->actor->avatarUrl()}}" width="32px" class="rounded-circle">
@ -38,6 +42,22 @@
</span> </span>
@endif @endif
@break @break
@case('comment')
<span class="notification-icon pr-3">
<img src="{{$notification->actor->avatarUrl()}}" width="32px" class="rounded-circle">
</span>
<span class="notification-text">
{!! $notification->rendered !!}
<span class="text-muted notification-timestamp pl-1">{{$notification->created_at->diffForHumans(null, true, true, true)}}</span>
</span>
<span class="float-right notification-action">
@if($notification->item_id)
<a href="{{$notification->status->parent()->url()}}"><img src="{{$notification->status->parent()->thumb()}}" width="32px" height="32px"></a>
@endif
</span>
@break
@endswitch @endswitch
</li> </li>
@endforeach @endforeach