Exemple #1
0
 public static function likePost($input)
 {
     $validation = Validator::make($input, Posts::$likePostRules);
     if ($validation->fails()) {
         return Response::json(array('status' => '0', 'msg' => $validation->getMessageBag()->first()), 200);
     } else {
         $access_token = $input['token'];
         $post_id = $input['post_id'];
         $current_time = new DateTime();
         $user_id = Users::getUserIdByToken($access_token);
         if ($user_id) {
             $uname = Users::getUsername($user_id);
             $other_id = Posts::getPostOwner($post_id);
             $push_status = Users::getPushSettings($other_id);
             $user_det = Users::getUserType($user_id);
             $user_type = $user_det->user_type;
             $push_ids = Users::getUserPushIds($other_id);
             $reg_id = $push_ids->reg_id;
             $device_token = $push_ids->apn_id;
             $post_data = DB::table('likes')->select('*')->where('post_id', $post_id)->where('user_id', $user_id)->first();
             if (!$post_data) {
                 $like_id = DB::table('likes')->insertGetId(array('user_id' => $user_id, 'post_id' => $post_id, 'created_at' => $current_time));
                 if ($user_id != $other_id) {
                     $post_desc = Posts::PostDesc($other_id, $post_id);
                     $notify_message = $uname . " liked your post";
                     $Ninput['sender_id'] = $user_id;
                     $Ninput['receiver_id'] = Posts::getPostOwner($post_id);
                     $Ninput['type_id'] = $like_id;
                     $Ninput['type'] = 'like';
                     $Ninput['title'] = $notify_message;
                     Notifications::addNotification($Ninput);
                     if ($push_status) {
                         Notifications::SendPushNotification($notify_message, '', $device_token, 2, $post_id, $user_id);
                         Notifications::SendPushNotification($notify_message, $reg_id, '', 2, $post_desc, $user_type);
                     }
                 }
                 if ($like_id) {
                     return Response::json(array('status' => '1', 'msg' => 'Post Liked'), 200);
                 } else {
                     return Response::json(array('status' => '0', 'msg' => "Error"), 200);
                 }
             } else {
                 return Response::json(array('status' => '2', 'msg' => 'Post Already liked by user'), 200);
             }
         } else {
             return Response::json(array('status' => '0', 'msg' => "Token Expired"), 200);
         }
     }
 }