commenting_user_id = $user->id; $comment->comment = $request->comment; $post->postComments()->save($comment); if ($comment->post->user->id != Auth::id()) { $deviceKeys = []; if ($comment->post->user->device_key && $comment->post->user->setting->notificationEnabled == 1) { $title = 'New Comment'; $body = $user->first_name . ' has commented on your post'; $deviceId = $comment->post->user->device_key; Arr::prepend($deviceKeys, $deviceId); $this->sendAppNotification($deviceKeys, $title, $body); } $comment->notification()->create([ 'notifiable_user_id' => $comment->post->user->id, 'title' => $title, 'content' => $body ]); } return $this->apiResponse(true, 'Comment added successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function updatePostComment(Request $request, PostComment $postComment) { try { $post = Post::findOrFail($postComment->post_id); $post->postComments()->where('id', $postComment->id)->update([ 'comment' => $request->comment ]); return $this->apiResponse(true, 'Comment updated successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function deletePostComment(PostComment $postComment) { try { if ($postComment->notification) { $postComment->notification->delete(); } if($postComment->postCommentReplies()){ $postComment->postCommentReplies->delete(); } $postComment->delete(); return $this->apiResponse(true, 'Comment deleted successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function addReplyToPostComment(Request $request, PostComment $postComment) { try { $user = Auth::user(); $postCommentReply = new PostCommentReply(); $postCommentReply->replying_user_id = $user->id; $postCommentReply->reply = $request->reply; $postComment->postCommentReplies()->save($postCommentReply); //Notification to comment owner if ($postCommentReply->postComment->user->id != Auth::id()) { if ($postCommentReply->postComment->user->device_key && $postCommentReply->postComment->user->setting->notificationEnabled == 1) { $deviceKeys = []; $title = 'New Comment Reply'; $body = $user->first_name . ' has replied on your comment'; $deviceId = $postCommentReply->postComment->user->device_key; Arr::prepend($deviceKeys, $deviceId); $this->sendAppNotification($deviceKeys, $title, $body); } $postCommentReply->notification()->create([ 'notifiable_user_id' => $postCommentReply->postComment->user->id, 'title' => $title, 'content' => $body ]); } //Notification to the post owner if ($postCommentReply->postComment->post->user->id != Auth::id()) { if ($postCommentReply->postComment->post->user->device_key && $postCommentReply->postComment->post->user->setting->notificationEnabled == 1) { $deviceKeys = []; $title = 'New Comment Reply'; $body = $user->first_name . ' has replied on a comment on your post'; $deviceId = $postCommentReply->postComment->post->user->device_key; Arr::prepend($deviceKeys, $deviceId); $this->sendAppNotification($deviceKeys, $title, $body); } $postCommentReply->notification()->create([ 'notifiable_user_id' => $postCommentReply->postComment->post->user->id, 'title' => $title, 'content' => $body ]); } return $this->apiResponse(true, 'Reply added successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function updateReplyToPostComment(Request $request, PostCommentReply $postCommentReply) { try { $postComment = PostComment::findOrFail($postCommentReply->post_comment_id); $postComment->postCommentReplies()->where('id', $postCommentReply->id)->update([ 'reply' => $request->reply ]); return $this->apiResponse(true, 'Reply updated successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function deleteReplyToPostComment(PostCommentReply $postCommentReply) { try { if ($postCommentReply->notification) { $postCommentReply->notification->delete(); } $postCommentReply->delete(); return $this->apiResponse(true, 'Reply removed successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function addPostHug(Request $request, Post $post) { try { $user = Auth::user(); if ($request->status == 'true') { $checkHug = PostHug::where(['hugging_user_id' => $user->id, 'post_id' => $post->id])->first(); if (!$checkHug) { $hug = new PostHug(); $hug->hugging_user_id = $user->id; $post->postHugs()->save($hug); if ($hug->post->user->id != Auth::id()) { if ($hug->post->user->device_key && $hug->post->user->setting->notificationEnabled == 1) { $deviceKeys = []; $title = 'New Post Hug'; $body = $user->first_name . ' has hugged on your post'; $deviceId = $hug->post->user->device_key; Arr::prepend($deviceKeys, $deviceId); $this->sendAppNotification($deviceKeys, $title, $body); } $hug->notification()->create([ 'notifiable_user_id' => $hug->post->user->id, 'title' => $title, 'content' => $body ]); return $this->apiResponse(true, 'Post hugged successfully'); } } return $this->apiResponse(true, 'Post already hugged'); } elseif ($request->status == 'false') { $checkHug = PostHug::where(['hugging_user_id' => $user->id, 'post_id' => $post->id])->first(); if ($checkHug) { if ($checkHug->notification) { $checkHug->notification->delete(); } $checkHug->delete(); return $this->apiResponse(true, 'Hug removed successfully'); } return $this->apiResponse(true, 'Hug does not exist'); } return $this->apiResponse(true, 'post hugged successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function reportPost(Request $request, Post $post) { try { if ($this->checkPostReportRequest($post)) { return $this->apiResponse(false, 'Your Request was already completed'); } $user = Auth::user(); ReportedPost::create([ 'post_id' => $post->id, 'reporting_user_id' => $user->id, 'reason' => $request->reason ]); return $this->apiResponse(true, 'Request submitted successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } public function hidePost(Post $post) { try { if ($this->checkPostHideRequest($post)) { return $this->apiResponse(false, 'Your Request was already completed'); } $user = Auth::user(); HiddenPost::create([ 'post_id' => $post->id, 'hide_on' => $user->id, ]); return $this->apiResponse(true, 'Request submitted successfully'); } catch (\Throwable $exception) { return $this->apiResponse(false, $exception->getMessage()); } } }