コード例 #1
0
ファイル: ChatsController.php プロジェクト: SenhorBardell/yol
 private function markAsRead($chat, $user)
 {
     $deliverState = false;
     foreach ($chat->getMembersTokens(true) as $token) {
         MessageUnread::whereRaw('chat_id = ' . $chat->id)->get()->each(function ($unread) use($chat, $user, $deliverState) {
             if ($unread->user_id == $user->id) {
                 $unread->delete();
                 Message::where('chat_id', $chat->id)->whereNull('delivered_at')->get()->map(function ($message) use($chat, $user, $deliverState) {
                     if (!$message->delivered_at) {
                         $message->delivered_at = Carbon::now();
                     }
                     if (!$message->viewed_at) {
                         $message->viewed_at = Carbon::now();
                     }
                     $message->save();
                 });
             }
         });
         $state = new StateSender($token);
         if ($deliverState) {
             $state->setChatAsDelivered($chat->id, $user->id);
         }
         $state->setChatAsRead($chat->id, $user->id);
         $state->send();
     }
     $this->updateCounts($chat);
     return $this;
 }
コード例 #2
0
 /**
  * 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');
     }
 }