Add LikePipeline and like notifications

This commit is contained in:
Daniel Supernault 2018-05-26 20:26:44 -06:00
parent de9237b5e1
commit 3ec93d096c
4 changed files with 92 additions and 7 deletions

View file

@ -5,21 +5,23 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Auth, Hashids; use Auth, Hashids;
use App\{Like, Profile, Status, User}; use App\{Like, Profile, Status, User};
use App\Jobs\LikePipeline\LikePipeline;
class LikeController extends Controller class LikeController extends Controller
{ {
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request) public function store(Request $request)
{ {
if(Auth::check() === false) { abort(403); }
$this->validate($request, [ $this->validate($request, [
'item' => 'required|integer', 'item' => 'required|integer',
]); ]);
$statusId = $request->item; $profile = Auth::user()->profile;
$status = Status::findOrFail($request->input('item'));
$user = Auth::user();
$profile = $user->profile;
$status = Status::findOrFail($statusId);
if($status->likes()->whereProfileId($profile->id)->count() !== 0) { if($status->likes()->whereProfileId($profile->id)->count() !== 0) {
$like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail(); $like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail();
@ -32,6 +34,8 @@ class LikeController extends Controller
$like->status_id = $status->id; $like->status_id = $status->id;
$like->save(); $like->save();
LikePipeline::dispatch($like);
return redirect($status->url()); return redirect($status->url());
} }
} }

View file

@ -0,0 +1,60 @@
<?php
namespace App\Jobs\LikePipeline;
use Cache, Log, Redis;
use App\{Like, Notification};
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class LikePipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $like;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Like $like)
{
$this->like = $like;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$like = $this->like;
$status = $this->like->status;
$actor = $this->like->actor;
try {
$notification = new Notification;
$notification->profile_id = $status->profile_id;
$notification->actor_id = $actor->id;
$notification->action = 'like';
$notification->message = $like->toText();
$notification->rendered = $like->toHtml();
$notification->save();
Cache::forever('notification.' . $notification->id, $notification);
$redis = Redis::connection();
$key = config('cache.prefix').':user.' . $status->profile_id . '.notifications';
$redis->lpush($key, $notification->id);
} catch (Exception $e) {
Log::error($e);
}
}
}

View file

@ -8,11 +8,25 @@ class Like extends Model
{ {
public function actor() public function actor()
{ {
return $this->belongsTo(Profile::class); return $this->belongsTo(Profile::class, 'profile_id', 'id');
} }
public function status() public function status()
{ {
return $this->belongsTo(Status::class); return $this->belongsTo(Status::class);
} }
public function toText()
{
$actorName = $this->actor->username;
return "{$actorName} " . __('notification.likedPhoto');
}
public function toHtml()
{
$actorName = $this->actor->username;
$actorUrl = $this->actor->url();
return "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> " .
__('notification.likedPhoto');
}
} }

View file

@ -0,0 +1,7 @@
<?php
return [
'likedPhoto' => 'liked your photo.',
];