Example #1
0
 private function sendStatesToDevises($tokens, $states)
 {
     foreach ($tokens as $token) {
         $stateSender = new StateSender($token);
         foreach ($states as $state) {
             if ($state->object == 'post') {
                 $post = Post::find($state->object_id);
                 if (!$post) {
                     $this->error('Post not found');
                 }
                 $comment = Comment::find($state->subject_id);
                 if (!$comment) {
                     $this->error('Comment not found');
                 }
                 $user = User::find($state->user_id);
                 if (!$user) {
                     $this->error('User not found');
                 }
                 if ($post && $user) {
                     if ($state->event == 'commented' && $comment) {
                         $stateSender->setPostAsCommented($post, $comment, $user, true);
                     } else {
                         if ($state->event == 'liked') {
                             $stateSender->setPostAsLiked($post, $user, true);
                         }
                     }
                 }
             } else {
                 if ($state->object == 'comment' && $state->event == 'liked') {
                     $comment = Comment::find($state->object_id);
                     if (!$comment) {
                         $this->error('Comment not found');
                     }
                     $user = User::find($state->user_id);
                     if (!$user) {
                         $this->error('User not found');
                     }
                     if ($user && $comment) {
                         $stateSender->setCommentAsLiked($comment, $user, true);
                     }
                 }
             }
         }
         $stateSender->send();
     }
 }
Example #2
0
 /**
  * Store a newly created resource in storage.
  * POST posts/{id}/comments
  *
  * @param int $id
  * @return Response
  */
 public function store($id)
 {
     $user = Auth::user();
     //		$validator = Comment::validate(Input::all());
     //
     //		if ($validator->fails())
     //			return $this->respondInsufficientPrivileges($validator->messages()->all());
     if (Input::get('text') == '' & !Input::has('attachments')) {
         return $this->respondInsufficientPrivileges('Send some text');
     }
     if (strlen(Input::get('text')) > 2500) {
         return $this->respondInsufficientPrivileges('Слишком длинный текст');
     }
     $post = Post::find($id);
     if (!$post) {
         return $this->respondNotFound('Post not found');
     }
     $comment = new Comment(Input::all());
     $comment->user()->associate(Auth::user());
     $category = $post->category;
     if ($category && $post->comments()->save($comment)) {
         if ($post->user_id != $comment->user_id && ($device = Device::where('user_id', $comment->user_id)->first())) {
             $token = $device->auth_token;
             $state = new StateSender($token);
             $state->setPostAsCommented($post, $comment, Auth::user());
         }
         //			$category->updateCount('comments');
         if (Input::has('attachments')) {
             $attachments = Input::get('attachments');
             foreach ($attachments as $attachment) {
                 $carHelper = new Helpers\carHelper();
                 if ($attachment['type'] == 'Geo') {
                     $geo = Geo::create(['long' => $attachment['long'], 'lat' => $attachment['lat'], 'location' => $attachment['location']]);
                     $comment->geos()->save($geo);
                 }
                 if ($attachment['type'] == 'Car') {
                     $car = $carHelper::fetchCar($user, $attachment['id']);
                     if ($car) {
                         $comment->cars()->attach($car->id);
                     }
                 }
                 if ($attachment['type'] == 'CarNumber') {
                     $car = $carHelper::fetchCar($user, $attachment['id']);
                     if ($car) {
                         $comment->carsWithNumbers()->attach($car->id);
                     }
                 }
                 if ($attachment['type'] == 'Image') {
                     $image = Image::find($attachment['id']);
                     if ($image) {
                         $comment->images()->save($image);
                     }
                 }
             }
         }
         $comment->load('cars', 'geos', 'images');
         return $this->respond($this->collectionTransformer->transformComment($comment));
     }
     return $this->respondServerError();
 }