public function fire($job, $emergencyId) { Log::debug($emergencyId); $this->login = getenv('SMS_LOGIN'); $this->pass = getenv('SMS_PASS'); $emergency = Emergency::find($emergencyId); if (!$emergency) { return $job->delete(); } if ($emergency->delivered_at) { return $job->delete(); } $createdAt = Carbon::createFromTimestamp($emergency->created_at); // Check feedback only if 30 seconds passed if ($createdAt->diffInSeconds(Carbon::now()) > 30) { $this->checkResults($emergency, function (Response $response) use($emergency, $job) { // var_dump($response->xml()); Log::debug('SMS Checker', ['sms']); Log::debug($response->getBody(true), ['sms']); $payload = $response->xml()->httpOut->sms; if ($payload['delivered_date'] && (int) $payload['result_id'] != 7) { $emergency->delivered_at = Carbon::parse((string) $payload['delivered_date']); $emergency->via_sms = true; $emergency->getMembersTokens()->each(function ($token) use($emergency) { $state = new StateSender($token->auth_token); $state->updateCounts('emergencies', $emergency->id); $state->setEmergencyAsDelivered($emergency->id, $emergency->delivered_at, true); $state->send(); }); } if (!in_array((int) $payload['result_id'], [2, 3, 4, 6, 8])) { $emergency->failed = true; $emergency->delivered_at = null; } $job->delete(); return $emergency->save(); }); } else { $job->release(10); } }
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(); } }
public function deliver($id, $message_id) { $chat = CarChat::find($id); if (!$chat) { return $this->respondNotFound('chat.not-found'); } $message = $chat->messages()->find($message_id); if (!$message) { return $this->respondNotFound('Message not found'); } $message->delivered_at = Carbon::now(); $message->save(); $chat->getMembersTokens()->each(function ($token) use($chat, $message) { $state = new StateSender($token->auth_token, true); $state->setMessageAsDelivered($chat->id, $message->id, $message->delivered_at); $state->send(); }); return $this->respondNoContent(); }
public function setAsRead($commentId) { if ((int) $commentId > 0 && !is_null($comment = Comment::find((int) $commentId))) { if ($token = $comment->getOwnerToken()) { $state = new StateSender($token); $state->setCommentAsRead($commentId); $state->send(); } return $this->respondNoContent(); } else { return $this->respondWithError('Comment doesn\'t exist'); } }
public function setAsRead($postId) { if ((int) $postId > 0 && !is_null($post = Post::find((int) $postId))) { if ($token = $post->getOwnerToken()) { $state = new StateSender($token); $state->setPostAsRead($postId); Comment::where('post_id', $postId)->get()->each(function ($comment) use($state) { $state->setCommentAsRead($comment->id); }); $state->send(); } return $this->respondNoContent(); } else { return $this->respondWithError('Post doesn\'t exist'); } }
public function deliver($id) { $emergency = Emergency::find($id); if (!$emergency) { return $this->respondNotFound('Срочный вызов не найден'); } $emergency->delivered_at = Carbon::now(); if ($emergency->save()) { $emergency->getMembersTokens()->each(function ($token) use($emergency) { $stateSender = new StateSender($token->auth_token); $stateSender->setEmergencyAsDelivered($emergency->id, $emergency->delivered_at); $stateSender->send(); }); return $this->respondNoContent(); } return $this->respondServerError(); }
/** * Display a listing of notifications. * * @param int|null $lastNotificationId Last chat ID * @param int $size Response size * @return Response */ public function getList($lastNotificationId = null, $size = 20) { $result = array(); $size = (int) $size > 0 ? (int) $size : 20; $query = sprintf('select max(x.id) as "before" from (select id from notifications n where n.owner_id = %d order by n.timestamp desc offset %d) x;', Auth::user()->id, self::AMOUNT_LIMIT); if (($data = DB::select($query)) && !is_null($idBeforeRemove = $data[0]->before)) { Notification::whereRaw('owner_id = ? and id < ?', array(Auth::user()->id, $idBeforeRemove))->update(array('is_removed' => 1)); } Notification::whereRaw('owner_id = ? and is_removed = 0 and timestamp > (CURRENT_TIMESTAMP - INTERVAL \'' . self::TIME_LIMIT . ' second\')' . (is_null($lastNotificationId) ? '' : ' and id < ' . (int) $lastNotificationId), array(Auth::user()->id))->orderBy('timestamp', 'desc')->orderBy('id', 'desc')->get()->take($size)->each(function ($notification) use(&$result) { $notificationUsers = NotificationUser::whereRaw('notification_id = ? and is_removed = 0', array($notification->id))->orderBy('timestamp', 'desc')->get(); $usersCount = $notificationUsers->count(); $type = $notification->object; $event = $notification->event; $data = array('id' => $notification->id, 'type' => $type, 'amount' => $usersCount, 'timestamp' => $notification->getTimeStamp()); // var_dump($type.' '.$event); $self = $this; if ($type == 'post') { $post = Post::find($notification->object_id); $data['post_id'] = $post->id; $data['post_title'] = $post->text; if ($event == 'commented') { $notificationUsers->take(2)->each(function ($user) use(&$data, $self) { $comment = Comment::find($user->subject_id); if ($comment) { $data['comments'][] = $self->collectionTransformer->transformComment($comment); } else { unset($data['id']); unset($data['type']); unset($data['amount']); unset($data['timestamp']); unset($data['post_id']); unset($data['post_title']); } }); } else { if ($event == 'liked') { $notificationUsers->take(2)->each(function ($user) use(&$data, $self) { $user = User::withTrashed()->find($user->user_id); $data['likes'][] = $self->collectionTransformer->transformUserToSmall($user); }); } else { unset($data['id']); unset($data['type']); unset($data['amount']); unset($data['timestamp']); unset($data['post_id']); unset($data['post_title']); } } } else { if ($type == 'comment') { $comment = Comment::find($notification->object_id); if ($comment) { $post = Post::find($comment->post_id); if ($post) { $data['post_id'] = $post->id; $data['post_title'] = $post->text; $data['comment_id'] = $comment->id; } if ($event == 'liked') { $notificationUsers->take(2)->each(function ($user) use(&$data, $self) { $user = User::withTrashed()->find($user->user_id); $data['likes'][] = $self->collectionTransformer->transformUserToSmall($user); }); } } else { unset($data['id']); unset($data['type']); unset($data['amount']); unset($data['timestamp']); unset($data['post_id']); unset($data['post_title']); } } else { unset($data['id']); unset($data['type']); unset($data['amount']); unset($data['timestamp']); unset($data['post_id']); unset($data['post_title']); } } // $filter = function ($data) { // return array_filter($data) != []; // }; // $result['notifications'][] = array_filter($data, $filter); if (!empty($data)) { $result['notifications'][] = $data; } }); if (is_null($lastNotificationId)) { foreach (Auth::user()->getTokens() as $token) { $state = new StateSender($token); $state->setAllPostsAsRead(); $state->setAllCommentsAsRead(); $state->send(); } } return $this->respond($result); }
private function updateCounts($chat) { Auth::user()->devices->map(function ($device) use($chat) { $state = new StateSender($device->auth_token); $state->updateCounts('chats', $chat->id); $state->send(); }); }
/** * Update delivered timestamp * * @return Response */ public function deliver() { $message = Message::find(Input::get('id')); $user = Auth::user(); $member = $message->chat->members->filter(function ($member) use($user) { return $member->id == $user->id; }); if (!$member) { return $this->respondNotFound('Chat member not found'); } if (!$message) { return $this->respondNotFound('Message not found'); } $message->delivered_at = Carbon::now()->timestamp; if ($message->save()) { $message->chat->members->each(function ($member) use($message) { $member->devices->each(function ($device) use($message) { $state = new StateSender($device->auth_token); $state->setMessageAsDelivered($message->chat->id, $message->id, $message->delivered_at); $state->send(); }); }); return $this->respondNoContent(); } return $this->respondServerError(); }