post = $post; $this->userDeviceToken = $userDeviceToken; } /** * @return array */ public function getAllPosts($request) { $response = []; $data = $request->all(); try { $user = loggedInUser(); $postData = $this->post ->isPublish() ->withCount(['likes', 'comments']) ->with([ 'tagUsers:id,name,avatar', 'postImages', 'category', // 'postMention', 'createdBy:id,name,avatar,created_at,updated_at', 'updatedBy:id,name,avatar,created_at,updated_at', 'blockedUser', 'hiddenPost' ]); if (isset($data['type']) && !empty($data['type'])) { $postData = $postData->where('type', ($data['type'] == Constant::STATUS_ONE) ? Constant::NULL : $data['type']); } $postData = $postData->whereDoesntHave('blockedUser', function($query) use ($user) { $query->where('user_blocked.blocked_by', '=', $user->id) ->where('user_blocked.user_id', '!=', 'posts.user_id'); }); $postData = $postData->whereDoesntHave('hiddenPost', function($query) use ($user) { $query->where('post_hidden.user_id', '=', $user->id) ->where('post_hidden.post_id', '!=', 'posts.id'); }); // new // $postData = $postData->where(function ($query) { // $query->where('type', '=', Constant::NULL) // ->orWhere('type', '=', Constant::STATUS_ONE); // })->latest()->get()->toArray(); // if (!empty($postData)) { // foreach ($postData as $post) { // $isFriends = $post['created_by']['is_friends']; // if ($isFriends == Constant::STATUS_ONE || $post['user_id'] == loggedInUser()->id) { // $responseData[] = $post; // } else { // $response['data'] = []; // } // } // // $response['data'] = new Paginator($responseData, Constant::PAGINATE_LIMIT); // $response['data'] = $this->paginate($responseData, $data['limit'] ?? 1, $data['page'] ?? Constant::STATUS_ONE, $request); // // ->paginate($data['limit'] ?? Constant::PAGINATE_LIMIT, ['*'], 'page', $data['page'] ?? Constant::STATUS_ONE); // $response['status'] = Constant::CODE_200; // } // Optimized $postData = $postData->where(function ($query) { $query->where('type', '=', Constant::NULL) ->orWhere('type', '=', Constant::STATUS_ONE); })->whereExists(function ($query) { $query->select("requests.*") ->from('requests') ->where('status', Constant::STATUS_ONE) // ->where(function ($query) { // $query->whereRaw('requests.sender_id = posts.user_id') // ->orWhereRaw('requests.receiver_id = posts.user_id'); // }) ->where(function ($query) { $query->where('requests.sender_id', loggedInUser()->id) ->orWhere('requests.receiver_id', loggedInUser()->id); }) ->orWhere('posts.user_id', loggedInUser()->id); })->latest()->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1); $response['data'] = $postData; $response['status'] = Constant::CODE_200; } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('auth.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function getUserPost($data) { $response = []; $responseData = []; try { $postData = $this->post ->isPublish() ->withCount(['likes', 'comments']) ->with([ 'tagUsers:id,name,avatar', 'postImages', 'category', 'postMention', 'createdBy:id,name,avatar,created_at,updated_at', 'updatedBy:id,name,avatar,created_at,updated_at' ])->where(function ($query) { $query->where('type', '=', Constant::NULL) ->orWhere('type', '=', Constant::STATUS_ONE); }); if (!empty($postData)) { if (!empty($data['user_id'])) { $postData = $postData->where('created_by', $data['user_id'])->latest()->get()->toArray(); foreach ($postData as $post) { $isFriends = $post['created_by']['is_friends']; if ($isFriends == Constant::STATUS_ONE && $post['privacy'] == Constant::STATUS_TWO || $post['privacy'] == Constant::STATUS_ONE) { $responseData[] = $post; } else { $response['data'] = []; } } $response['data'] = new Paginator($responseData, Constant::PAGINATE_LIMIT); // $this->paginate($responseData,$data['limit'] ?? Constant::PAGINATE_LIMIT, $data['page'] ?? Constant::STATUS_ONE); $response['status'] = Constant::CODE_200; } else { $postData = $postData->where('created_by', loggedInUser()->id)->latest()->paginate(10)->toArray(); $response['data'] = $postData; $response['status'] = Constant::CODE_200; } } // $name = Route::currentRouteName(); // if ($name == 'api.post.sant.user') { // $postData = $postData->where('type_id', $data['type_id'] ?? Constant::NULL)->where('type', Constant::POST_TYPE_SANT); // } else { // $postData = $postData->where('type', Constant::NULL)->where('created_by', loggedInUser()->id); // } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('auth.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @param array $data * @return array */ public function storePost($data) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { if ((!empty($data['type']) && $data['type'] == 3)) { if (!empty($data['type_id'])) { $typeIds = explode(',', $data['type_id']); } if (!empty($typeIds)) { $post = []; $sangh = []; foreach ($typeIds as $typeId) { $post = $this->post->query()->create( [ 'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL, 'created_by' => $typeId ?? Constant::NULL, 'privacy' => $data['privacy'], 'location' => $data['location'], 'latitude' => $data['latitude'], 'longitude' => $data['longitude'], 'status' => $data['status'], 'category_id' => $data['category_id'], 'description' => $data['description'] ?? Constant::NULL, 'type_id' => $typeId ?? Constant::NULL, 'type' => $data['type'] ?? Constant::NULL, ] ); $sangh = Sangh::where('id', $typeId)->first(); $response['data']['created_by'] = [ 'id' => $sangh->id, 'name' => $sangh->name, 'avatar' => $sangh->avatar, 'created_at' => $sangh->created_at, 'updated_at' => $sangh->updated_at ]; $response['data']['updated_by'] = [ 'id' => $sangh->id, 'name' => $sangh->name, 'avatar' => $sangh->avatar, 'created_at' => $sangh->created_at, 'updated_at' => $sangh->updated_at ]; $followers = []; if (!empty($data['privacy'] && $data['privacy'] == 2)) { $followers = SanghMember::where('sangh_id', $sangh->id) ->where('status', Constant::STATUS_ONE) ->where('user_id', '!=', loggedInUser()->id) ->pluck('user_id'); } else { $followers = $sangh->followers()->pluck('user_id'); } if (!empty($followers)) { foreach ($followers as $follower) { dispatch(new SendNewPostForSanghPost($sangh, $post, $follower)); } } if (isset($data['images']) && !empty($data['images']) && isset($data['user_id']) && !empty($data['user_id'])) { $post->tagUsers()->sync($data['user_id']); $fileName = uploadMultipleImage($data, 'images', Constant::POST_IMAGE_UPLOAD_PATH . Constant::SLASH); if (!empty($fileName)) { foreach ($fileName as $file) { $post->postImages()->create($file); } } } else if(isset($data['images']) && !empty($data['images'])) { $fileName = uploadMultipleImage($data, 'images', Constant::POST_IMAGE_UPLOAD_PATH . Constant::SLASH); if (!empty($fileName)) { foreach ($fileName as $file) { $post->postImages()->create($file); } } } // $response['data'] = $post->loadCount(['likes', 'comments'])->load(['category', 'tagUsers:id,name,avatar', 'postImages'])->toArray(); } } } else { $post = $this->post->query()->create( [ 'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL, 'created_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL, 'privacy' => $data['privacy'], 'location' => $data['location'], 'latitude' => $data['latitude'], 'longitude' => $data['longitude'], 'status' => $data['status'], 'category_id' => $data['category_id'], 'description' => $data['description'] ?? Constant::NULL, 'type_id' => $data['type_id'] ?? Constant::NULL, 'type' => $data['type'] ?? Constant::NULL, ] ); if (isset($data['images']) && !empty($data['images']) && isset($data['user_id']) && !empty($data['user_id'])) { $post->tagUsers()->sync($data['user_id']); $fileName = uploadMultipleImage($data, 'images', Constant::POST_IMAGE_UPLOAD_PATH . Constant::SLASH); if (!empty($fileName)) { foreach ($fileName as $file) { $post->postImages()->create($file); } } } else if(isset($data['images']) && !empty($data['images'])) { $fileName = uploadMultipleImage($data, 'images', Constant::POST_IMAGE_UPLOAD_PATH . Constant::SLASH); if (!empty($fileName)) { foreach ($fileName as $file) { $post->postImages()->create($file); } } } } if (!empty($post)) { if ((!empty($data['type']) && $data['type'] == 1) && (!empty($data['category_id']) && $data['category_id'] == 1)) { loggedInUser()->addKarmaPoints($post, loggedInUser()->id, config('config-variables.karma_points_message.post_dharmik'), config('config-variables.karma_points.post_dharmik'), config('config-variables.karma_points_key.post_dharmik'), []); } //Add karma points for sangh dharmik post if ((!empty($data['type']) && $data['type'] == 3) && (!empty($data['category_id']) && $data['category_id'] == 1)) { $sangh = Sangh::where('id', $data['type_id'])->first(); $sangh->addKarmaPoints($sangh, $sangh->id, config('config-variables.karma_points_message.sangh_add_post_dharmik'), config('config-variables.karma_points.sangh_add_post_dharmik'), config('config-variables.karma_points_key.sangh_add_post_dharmik'), []); } // Tag user in post if (isset($data['user_id']) && !empty($data['user_id'])) { //Push Notification if ($data['user_id'] > 0) { $post->tagUsers()->sync($data['user_id']); foreach ($data['user_id'] as $taggedUser) { $user = loggedInUser(); dispatch(new SendTagPost($user, $post, $taggedUser)); } } } if (isset($data['mentions']) && !empty($data['mentions'])) { foreach ($data['mentions'] as $mention) { if ($mention['type'] == 'user') { $mentionType = Constant::STATUS_ONE; } else if ($mention['type'] == 'sant') { $mentionType = Constant::STATUS_TWO; } else if ($mention['type'] == 'sangh') { $mentionType = Constant::STATUS_THREE; } PostMention::create([ 'post_id' => $post->id, 'mention_id' => $mention['id'], 'type' => $mentionType ]); } } if ($post->type == Constant::STATUS_ONE) { $sentRequests = loggedInUser()->friendRequests(); $receivedRequests = loggedInUser()->myFriendRequests(); $receivedRequests = $receivedRequests->where('requests.receiver_id', loggedInUser()->id) ->where('requests.status', Constant::STATUS_ONE) ->pluck('requests.sender_id'); $sentRequests = $sentRequests->where('requests.sender_id', loggedInUser()->id) ->where('requests.status', Constant::STATUS_ONE) ->pluck('requests.receiver_id'); $friends = $sentRequests->merge($receivedRequests); //Push Notification to all friends if (!empty($friends)) { foreach ($friends as $friend) { $user = loggedInUser(); dispatch(new SendNewPostToFriends($user, $post, $friend)); } } } if ($post->type == 3) { // $sangh = Sangh::where('id', $post->type_id)->first(); $response['data'] = $post->loadCount(['likes', 'comments'])->load(['category', 'tagUsers:id,name,avatar', 'postImages'])->toArray(); // $response['data']['created_by'] = [ // 'id' => $sangh->id, // 'name' => $sangh->name, // 'avatar' => $sangh->avatar, // 'created_at' => $sangh->created_at, // 'updated_at' => $sangh->updated_at // ]; // $response['data']['updated_by'] = [ // 'id' => $sangh->id, // 'name' => $sangh->name, // 'avatar' => $sangh->avatar, // 'created_at' => $sangh->created_at, // 'updated_at' => $sangh->updated_at // ]; } else { $response['data'] = $post->loadCount(['likes', 'comments'])->load(['category', 'tagUsers:id,name,avatar', 'postImages', 'createdBy:id,name,avatar,created_at,updated_at'])->toArray(); } $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.store'); unset($response['error']); } } catch (Exception $ex) { Log::error($ex); } return $response; } /** * @param $data * @return array */ public function updatePost($data , object $post) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); $id = $post->id; $post = $post->update( [ 'user_id' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL, 'updated_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL, 'privacy' => $data['privacy'], 'category_id' => $data['category_id'], 'status' => $data['status'], 'location' => $data['location'], 'latitude' => $data['latitude'], 'longitude' => $data['longitude'], 'description' => $data['description'] ?? Constant::NULL, ] ); $post = $this->post->find($id); // Tag uset in post // Tag uset in post if (isset($data['user_id']) && !empty($data['user_id'])) { $post->tagUsers()->sync($data['user_id']); } if (isset($data['removed_image_id']) && !empty($data['removed_image_id'])) { if (count($data['removed_image_id']) > 0) { // Remove image from storage $post->postImages()->whereIn('id', $data['removed_image_id'])->each( function ($image) { if (isset($image->image_name) && !empty($image->getRawOriginal('image_name'))) { deleteFile(Constant::POST_IMAGE_UPLOAD_PATH, $image->getRawOriginal('image_name')); } }); $post->postImages()->whereIn('id', $data['removed_image_id'])->delete(); } } if (isset($data['images']) && !empty($data['images'])) { $fileName = uploadMultipleImage($data, 'images', Constant::POST_IMAGE_UPLOAD_PATH . Constant::SLASH); if (!empty($fileName)) { foreach ($fileName as $file) { $post->postImages()->create($file); } } } if ($data['user_id'] > 0) { $post->tagUsers()->sync($data['user_id']); foreach ($data['user_id'] as $taggedUser) { $user = loggedInUser(); dispatch(new SendTagPost($user, $post, $taggedUser)); } } if (isset($data['mentions']) && !empty($data['mentions'])) { $post->postMention()->delete(); $mentionData = collect($data['mentions'])->map(function ($mention) { if ($mention['type'] == 'user') { $mentionType = Constant::STATUS_ONE; } else if ($mention['type'] == 'sant') { $mentionType = Constant::STATUS_TWO; } else if ($mention['type'] == 'sangh') { $mentionType = Constant::STATUS_THREE; } return ['mention_id' => $mention['id'], 'type' => $mentionType]; })->toArray(); $post->postMention()->createMany($mentionData); } // Remove karma points of post $points = KarmaPointsTransaction::where(['user_id' => $user->id, 'pointable_id' => $post->id])->first(); if ($points && $post->category_id != Constant::STATUS_ONE) { $points->delete(); $user->decrement('karma_dhan',10); } if (!empty($post)) { $response['data'] = $post->loadCount(['likes', 'comments'])->load(['category', 'tagUsers:id,name,avatar', 'postImages','createdBy:id,name,avatar,created_at,updated_at','updatedBy:id,name,avatar,created_at,updated_at'])->toArray(); $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.update'); unset($response['error']); } } catch (Exception $ex) { Log::error($ex); } return $response; } /** * @param $data * @return array */ public function destroyPost(object $post) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); // Remove image from storage $post->postImages()->each( function ($image) { if (isset($image->image_name) && !empty($image->getRawOriginal('image_name'))) { deleteFile(Constant::POST_IMAGE_UPLOAD_PATH, $image->getRawOriginal('image_name')); } }); $post->postImages()->delete(); // Remove karma points of post $points = KarmaPointsTransaction::where(['user_id' => $user->id, 'pointable_id' => $post->id, 'pointable_type' => 'App\Models\Post'])->first(); $notifications = Notifications::whereIn('type', ['TagPost', 'LikePost', 'CommentPost', 'NewSanghPost', 'NewFriendPost']) ->whereJsonContains('extra_fields->from_id', $post->id); if ($notifications) { $notifications->delete(); } $post = $post->delete(); if ($points) { $points->delete(); $user->decrement('karma_dhan',10); } if (!empty($post)) { $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.destroy'); unset($response['error']); } } catch (Exception $ex) { Log::error($ex); } return $response; } /** * Get info of post */ public function showPost(object $post) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { if ($post->type === Constant::STATUS_THREE) { $response['data'] = $post->loadCount(['likes', 'comments']) ->load(['category', 'tagUsers:id,name,avatar', 'postImages', 'createdBy:id,name,avatar,created_at,updated_at', 'updatedBy:id,name,avatar,created_at,updated_at', 'created_by:id,name,avatar,created_at,updated_at', 'updated_by:id,name,avatar,created_at,updated_at', 'sanghDetails:id,name,avatar' ])->toArray(); } else { $response['data'] = $post->loadCount(['likes', 'comments']) ->load(['category', 'tagUsers:id,name,avatar', 'postImages', 'createdBy:id,name,avatar,created_at,updated_at', 'updatedBy:id,name,avatar,created_at,updated_at', 'sanghDetails:id,name,avatar' ])->toArray(); } $response['data']['comments'] = collect($post->comments()->orderBy('id', 'DESC')->with('replies')->paginate(5)); $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.show'); unset($response['error']); } catch (Exception $ex) { Log::error($ex); } return $response; } /** * @return array */ public function getCategory() { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $categories = Category::get()->toArray(); if ($categories) { $response['data'] = $categories; $response['message'] = trans('api.posts.category_list'); $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; unset($response['error']); } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('api.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function likePost($data) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); $post = Post::findorFail($data['post_id']); if (!$post->likes->contains($user->id)) { $post->likes()->attach(['user_id' => $user->id]); $postLikeCount = $post->where('id', $data['post_id'])->withCount('likes')->value('likes_count'); $response['data']['likes_count'] = $postLikeCount; $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.like'); unset($response['error']); //Push Notification to user on like if ($user->id != $post->created_by) { dispatch(new SendLikePost($user, $post)); } } else { $post->likes()->detach(['user_id' => $user->id]); $postLikeCount = $post->where('id', $data['post_id'])->withCount('likes')->value('likes_count'); $response['data']['likes_count'] = $postLikeCount; $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.dislike'); unset($response['error']); } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('api.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function addComment($data) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); $post = Post::findorFail($data['post_id']); $userData = User::where('id', $user->id)->select('id', 'name', 'avatar')->first()->toArray(); if ($post) { $commentData = [ 'user_id' => $user->id, 'reply_id' => $data['reply_id'] ?? null, 'comment' => $data['comment'], ]; $comments = $post->comments()->create($commentData); $postCount = $post->where('id', $data['post_id'])->withCount('comments')->value('comments_count'); $response['data']['comment'] = $comments; $response['data']['comments_count'] = $postCount; $response['data']['comment']['user'] = $userData; $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.comments.store'); unset($response['error']); //Push Notification if ($user->id != $post->created_by) { dispatch(new SendCommentPost($user, $post)); } } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('api.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function updateComment($data) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); $post = Post::findorFail($data['post_id']); if ($post) { $commentData = [ 'user_id' => $user->id, 'comment' => $data['comment'], ]; $post->comments()->where('id', $data['comment_id'])->update($commentData); $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.comments.update'); unset($response['error']); } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('api.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function removeComment($data) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); $postComment = PostComment::findorFail($data['comment_id']); if ($postComment) { $postComment->delete(); $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.comments.destroy'); unset($response['error']); } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('api.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * The attributes that are mass assignable. * * @var array */ public function paginate($items, $perPage = Constant::PAGINATE_LIMIT, $page = null, $request) { $page = $page ?: (Paginator::resolveCurrentPage() ?: Constant::STATUS_ONE); $items = $items instanceof Collection ? $items : Collection::make($items); // return new LengthAwarePaginator(array_slice(array_values($items), $offset, $perPage, true), count($items), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]); return new LengthAwarePaginator($items->forPage($page, $perPage)->values(), $items->count(), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]); } /** * Get info of post */ public function getDraftPost() { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $user = loggedInUser(); $getDraftPost = $this->post->isDraft() ->where('user_id', loggedInUser()->id) ->with([ 'tagUsers:id,name,avatar', 'postImages', 'category', 'createdBy:id,name,avatar,created_at,updated_at', 'updatedBy:id,name,avatar,created_at,updated_at' ])->where(function ($query) { $query->where('type', '=', Constant::NULL) ->orWhere('type', '=', Constant::STATUS_ONE); })->first(); $isMember = loggedInUser()->whereHas('sanghMembers', function($query) use ($user){ $query->where('sangh_members.status', Constant::STATUS_ONE) ->where('sangh_members.user_id', $user->id) ->where('sangh_members.core_committee', Constant::STATUS_ONE); // ->orWhere('sangh_members.role', Constant::ADMIN) // ->orWhere('sangh_members.is_owner', Constant::STATUS_ONE); })->first(); if ($isMember) { $isSanghMember = Constant::STATUS_ONE; } else { $isSanghMember = Constant::STATUS_ZERO; } $response['data'] = ($getDraftPost) ? $getDraftPost->toArray() : Constant::NULL; $response['is_sangh_member'] = $isSanghMember; $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.posts.show'); unset($response['error']); } catch (Exception $ex) { Log::error($ex); } return $response; } /** * @return array */ public function getSantPost($data) { $response = []; $responseData = []; try { $postData = $this->post ->withCount(['likes', 'comments']) ->with([ 'tagUsers:id,name,avatar', 'postImages', 'category', 'createdBy:id,name,avatar,created_at,updated_at', 'updatedBy:id,name,avatar,created_at,updated_at' ])->where(function ($query) { $query->where('type', '=', Constant::POST_TYPE_SANT); }); if (!empty($postData)) { if (empty($data['sant_id'])) { $postData = $postData->latest()->get()->toArray(); foreach ($postData as $post) { // && $post['privacy'] == Constant::STATUS_TWO || $post['privacy'] == Constant::STATUS_ONE if (loggedInUser()->followings->contains($post['type_id']) || $post['created_by']['id'] === loggedInUser()->id) { $responseData[] = $post; } else { $response['data'] = []; } } // $response['data'] = new Paginator($responseData, Constant::PAGINATE_LIMIT); // $this->paginate($responseData,$data['limit'] ?? Constant::PAGINATE_LIMIT, $data['page'] ?? Constant::STATUS_ONE); $response['data'] = $this->paginate($responseData, $data['limit'] ?? 10, $data['page'] ?? Constant::STATUS_ONE, $data); $response['status'] = Constant::CODE_200; } else { $postData = $postData->where('type_id', $data['sant_id'])->latest()->paginate(10); $response['data'] = $postData; $response['status'] = Constant::CODE_200; } } // $name = Route::currentRouteName(); // if ($name == 'api.post.sant.user') { // $postData = $postData->where('type_id', $data['type_id'] ?? Constant::NULL)->where('type', Constant::POST_TYPE_SANT); // } else { // $postData = $postData->where('type', Constant::NULL)->where('created_by', loggedInUser()->id); // } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('auth.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function getPostComments(array $data, object $post) { $response = [ 'status' => Constant::CODE_403, 'error' => trans('api.something_went_wrong'), 'success' => Constant::STATUS_FALSE ]; try { $post = Post::findorFail($post->id); if ($post) { $comments = $post->comments()->with('user:id,avatar,name')->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1); $response['data'] = $comments; $response['status'] = Constant::CODE_200; $response['success'] = Constant::STATUS_TRUE; $response['message'] = trans('api.comments.list'); unset($response['error']); } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('api.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function getSanghPost($data) { $response = []; $responseData = []; try { $postData = $this->post ->withCount(['likes', 'comments']) ->with([ 'tagUsers:id,name,avatar', 'postImages', 'category', 'created_by:id,name,avatar,created_at,updated_at', 'updated_by:id,name,avatar,created_at,updated_at', 'sanghDetails:id,name,avatar', ])->where(function ($query) { $query->where('type', '=', Constant::POST_TYPE_SANGH); }); if (!empty($postData)) { if (empty($data['sangh_id'])) { // $sangh = Sangh::where('id', $data['sangh_id'])->first(); $postData = $postData->latest()->get(); $loggedInUser = loggedInUser(); $sanghMembers = $loggedInUser->sanghMembers->pluck('id')->toArray(); $sanghFollowings = $loggedInUser->sanghFollowings->pluck('id')->toArray(); foreach ($postData as $post) { $typeId = $post->type_id; $userId = $post->user_id; // && $post['privacy'] == Constant::STATUS_TWO || $post['privacy'] == Constant::STATUS_ONE if (in_array($typeId, $sanghMembers) || in_array($typeId, $sanghFollowings) || $userId === $loggedInUser->id) { $responseData[] = $post; } else { $response['data'] = []; } } $response['data'] = $this->paginate($responseData, $data['limit'] ?? 10, $data['page'] ?? Constant::STATUS_ONE, $data); $response['status'] = Constant::CODE_200; } else { // $isFollowing = loggedInUser()->sanghFollowings->contains($data['sangh_id']); $isMember = loggedInUser()->sanghMembers->contains($data['sangh_id']); if ($isMember) { $response['data'] = $postData->where('type_id', $data['sangh_id'])->latest()->paginate(10); } else { $response['data'] = $postData->where('type_id', $data['sangh_id'])->latest()->paginate(10); } $response['status'] = Constant::CODE_200; } } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('auth.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } /** * @return array */ public function hidePost($data) { $response = []; try { $user = loggedInUser(); $postHidden = PostHidden::where(['post_id' => $data['id'], 'user_id' => $user->id])->first(); if ($user) { if (!$postHidden && isset($data['status']) && $data['status'] === Constant::STATUS_ONE) { PostHidden::create([ 'post_id' => $data['id'], 'user_id' => $user->id ]); $response['message'] = trans('api.posts.hide'); $response['status'] = Constant::CODE_200; } else if ($postHidden && (isset($data['status']) && $data['status'] === Constant::STATUS_ZERO)) { $postHidden->delete(); $response['message'] = trans('api.posts.unhide'); $response['status'] = Constant::CODE_200; } else if ($postHidden && isset($data['status']) && $data['status'] === Constant::STATUS_ONE) { $response['message'] = trans('api.posts.already_hidden'); $response['status'] = Constant::CODE_403; } else if (!$postHidden && isset($data['status']) && $data['status'] === Constant::STATUS_ZERO) { $response['message'] = trans('api.user.already_visible'); $response['status'] = Constant::CODE_403; } } else { $response['message'] = trans('auth.something_went_wrong'); $response['status'] = Constant::CODE_401; } } catch (\Exception $ex) { Log::error($ex); $response['message'] = trans('auth.something_went_wrong'); $response['status'] = Constant::CODE_403; } return $response; } }