/** * Store a newly message. * * @return Response */ public function send() { $chatId = Input::get('chat_id'); $user = Auth::user(); if (($content = Input::get('content')) && (int) $chatId > 0 && !is_null($chat = Chat::find((int) $chatId)) && $chat->hasMember($user->id)) { foreach ($chat->getUsersIds() as $id) { if ($user->isBlocked($id)) { return Response::json(['error' => ['message' => 'Cant send to group with users which blocked', 'status_code' => 1002]], 403); } } if (isset($content['text']) || isset($content['image_id']) && MessageAttachment::whereRaw('id = ? and chat_id = ?', array((int) $content['image_id'], $chat->id))->get()->count() > 0 || isset($content['car']) && ($car = Car::find((int) $content['car']['id'])) || isset($content['geo']) && isset($content['geo']['lat']) && isset($content['geo']['long']) && isset($content['geo']['location'])) { if (isset($content['text']) && strlen($content['text']) > 2500) { return $this->respondInsufficientPrivileges('Слишком длинный текст'); } $message = new Message(); $message->chat_id = $chat->id; $message->user_id = $user->id; if (isset($content['text'])) { $message->text = $content['text']; // } else if(isset($content['image_id'])) { // $message->image_id = (int)$content['image_id']; } else { if (isset($content['car'])) { $message->car_id = $car->id; if ((bool) $content['car']['car_with_number']) { $message->car_number = $car->number; } } else { if (isset($content['geo'])) { $message->lat = $content['geo']['lat']; $message->lng = $content['geo']['long']; $message->location = $content['geo']['location']; } } } if (isset($content['image_id'])) { $message->image_id = (int) $content['image_id']; } $message->save(); $chat->timestamp = DB::raw('NOW()'); $chat->save(); foreach ($chat->getUsers() as $user) { $unread = new MessageUnread(); $unread->message_id = $message->id; $unread->user_id = $user->id; $unread->chat_id = $chat->id; $unread->save(); } $message = Message::find($message->id); foreach ($chat->getMembersTokens() as $token) { $state = new StateSender($token); $state->setMessageAsAdded($message->getAsArray(true)); $state->send(); } return $this->respond($message->getAsArray()); } else { return $this->setStatusCode(403)->respondWithError('Unset necessary parameter in correct format'); } } else { return $this->setStatusCode(500)->respondWithError('Chat doesn\'t exist'); } }