Example #1
0
 /**
  * Create a new user
  *
  * @param Request $request
  * @return Response
  * @throws FacebookRequestException
  */
 public function addAction(Request $request)
 {
     $data = $request->json()->get('User');
     if (!$data) {
         return showErrorResponse('Incorrect request parameters', HTTP_UNPROCESSABLE_ENTITY);
     }
     try {
         $new_user = new Users();
         $new_user->addUser($data);
     } catch (\Exception $e) {
         return showErrorResponse($e->getMessage());
     }
     //Send push notifications to all Facebook friends who are using Masarap
     $fb_access_token = $data[CONSTANTS::KEY_FB_ACCESS_TOKEN];
     if (!$fb_access_token) {
         return showErrorResponse('Failed to access Facebook account');
     }
     FacebookSession::setDefaultApplication(Config::get('services.facebook.client_id'), Config::get('services.facebook.client_secret'));
     FacebookSession::enableAppSecretProof(false);
     $facebook_session = new FacebookSession($fb_access_token);
     $facebook_response = (new FacebookRequest($facebook_session, 'GET', '/me/friends/'))->execute();
     $friend_list = $facebook_response->getResponse();
     $failed_notifications = array();
     foreach ($friend_list->data as $friend) {
         $friend_user = Users::getByFbId($friend->id);
         if (!$friend_user) {
             continue;
         }
         $params = array(CONSTANTS::KEY_USER_ID_FROM => $new_user->id, CONSTANTS::KEY_USER_ID_TO => $friend_user->id, CONSTANTS::KEY_TYPE => CONSTANTS::NOTIFICATION_TYPE_FRIEND_JOIN, CONSTANTS::KEY_TYPE_ID => $new_user->id);
         try {
             $notification = new Notification();
             $notification->addGeneralNotification($params);
         } catch (\Exception $e) {
             $failed_notifications[] = $friend_user->id;
         }
     }
     $json_return[KeyParser::data] = array(KeyParser::user => ModelFormatter::userLongFormat($new_user), KeyParser::message => 'User successfully registered and push notifications are sent to Facebook friends', KeyParser::unsent_notifications => $failed_notifications);
     return response()->json($json_return);
 }