diff --git a/app/Http/Controllers/LikeController.php b/app/Http/Controllers/LikeController.php index 1691f32cb..932958d5e 100644 --- a/app/Http/Controllers/LikeController.php +++ b/app/Http/Controllers/LikeController.php @@ -5,21 +5,23 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Auth, Hashids; use App\{Like, Profile, Status, User}; +use App\Jobs\LikePipeline\LikePipeline; class LikeController extends Controller { + public function __construct() + { + $this->middleware('auth'); + } + public function store(Request $request) { - if(Auth::check() === false) { abort(403); } $this->validate($request, [ 'item' => 'required|integer', ]); - $statusId = $request->item; - - $user = Auth::user(); - $profile = $user->profile; - $status = Status::findOrFail($statusId); + $profile = Auth::user()->profile; + $status = Status::findOrFail($request->input('item')); if($status->likes()->whereProfileId($profile->id)->count() !== 0) { $like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail(); @@ -32,6 +34,8 @@ class LikeController extends Controller $like->status_id = $status->id; $like->save(); + LikePipeline::dispatch($like); + return redirect($status->url()); } } diff --git a/app/Jobs/LikePipeline/LikePipeline.php b/app/Jobs/LikePipeline/LikePipeline.php new file mode 100644 index 000000000..a7fb71d57 --- /dev/null +++ b/app/Jobs/LikePipeline/LikePipeline.php @@ -0,0 +1,60 @@ +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); + } + } +} diff --git a/app/Like.php b/app/Like.php index 32c99554e..a4ce9f0a8 100644 --- a/app/Like.php +++ b/app/Like.php @@ -8,11 +8,25 @@ class Like extends Model { public function actor() { - return $this->belongsTo(Profile::class); + return $this->belongsTo(Profile::class, 'profile_id', 'id'); } public function status() { 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 "{$actorName} " . + __('notification.likedPhoto'); + } } diff --git a/resources/lang/en/notification.php b/resources/lang/en/notification.php new file mode 100644 index 000000000..7e5bb85e3 --- /dev/null +++ b/resources/lang/en/notification.php @@ -0,0 +1,7 @@ + 'liked your photo.', + +]; \ No newline at end of file