示例#1
0
 public function unlike($type, $id)
 {
     $type = $this->str($type);
     $user = Auth::user();
     $like = $this->get($type, $id, $user->id);
     if ($like) {
         if ($like->delete()) {
             $object = strtolower($type);
             State::whereRaw('object=? and object_id=? and event=\'liked\'', array($object, $id))->delete();
             Notification::whereRaw('object=? and object_id=? and event=\'liked\'', array($object, $id))->get()->each(function ($notification) use($user) {
                 NotificationUser::whereRaw('notification_id=? and user_id=?', array($notification->id, $user->id))->get()->each(function ($notificationsUser) {
                     $notificationsUser->is_removed = 1;
                     $notificationsUser->save();
                 });
                 $notification->is_removed = 1;
                 $notification->save();
             });
             Notification::whereRaw('object=? and event=\'liked\' and is_removed=0 and ' . '(select count(nu.id) from notifications_users nu where nu.notification_id=notifications.id and nu.is_removed=0)=0', array($object))->get()->each(function ($notification) {
                 $notification->is_removed = 1;
                 $notification->save();
             });
             return $this->respondNoContent();
         }
     } else {
         return $this->respondNotFound('Like not found');
     }
     return $this->respondServerError();
 }
示例#2
0
 /**
  * Api function for crawl send notification to devices.
  *
  * @return Response
  */
 public function getSendNotificationDevices()
 {
     $data = get();
     // Valid get params.
     $valids = Validator::make($data, ['token' => 'required|in:' + EXR_TOKEN], EXRHelper::getMessage());
     // Check valid.
     if ($valids->fails()) {
         return Response::json(array('status' => 'Error', 'data' => $valids->messages()), 403);
     }
     // Get Currency
     $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get();
     if (!count($currencies)) {
         Currency::where('is_send', true)->update(array('is_send' => false));
         $currencies = Currency::whereRaw('is_send = ?', array(false))->take(5)->get();
     }
     foreach ($currencies as $currency) {
         $message = $tye = '';
         if (Cache::has('exr_notification_msg_' . $currency->id)) {
             $data = Cache::get('exr_notification_msg_' . $currency->id);
             $message = isset($data['message']) ? $data['message'] : '';
             $type = isset($data['type']) ? $data['type'] : '';
         } else {
             // Get today currency
             $today = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) = CURDATE()', array($currency->id))->first();
             if (!$today) {
                 return Response::json(array('status' => 'error', 'data' => 'Error'), 500);
             }
             $mimax = CurrencyDaily::whereRaw('currency_id = ? AND DATE(created_at) >= NOW() - INTERVAL ? WEEK', array($currency->id, EXR_LIMIT_WEEK))->select(DB::raw('MIN(buy) as min, MAX(buy) as max'))->first();
             if ($mimax->min > $today->buy) {
                 $message = sprintf(EXR_MIN_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy);
                 $tye = 'currency_min';
             } else {
                 if ($mimax->max < $today->buy) {
                     $message = sprintf(EXR_MAX_MSG, $today->name, EXR_LIMIT_WEEK, $today->buy);
                     $type = 'currency_max';
                 } else {
                     $message = '';
                     $type = '';
                 }
             }
             if ($message) {
                 Cache::put('exr_notification_msg_' . $currency->id, array('message' => $message, 'type' => $type), EXR_CACHE_MINUTE);
             }
         }
         if (!$message) {
             return Response::json(array('status' => 'error', 'data' => 'Error'), 500);
         }
         // Get 1000 devices
         $records = Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ?', array($currency->id, $type, false))->take(EXR_LIMIT_DEVICES)->get();
         if (!count($records)) {
             // Update is_send for currency when send all devices.
             $currency->is_send = true;
             $currency->save();
             // Clear if crawl end devices.
             Notification::whereRaw('currency_id = ? AND notify_type = ? AND is_send = ? AND DATE(updated_at) != CURDATE()', array($currency->id, $type, true))->update(array('is_send', false));
         }
         // Convert devices registations ids to array.
         $deviceIds = $records->lists('device_id');
         $response = '';
         if (count($deviceIds) <= EXR_LIMIT_DEVICES && !empty($message)) {
             // Send to notification to it
             $response = Notification::sendNtfsToDevices($message, $deviceIds);
         }
     }
     return Response::json(array('status' => 'success', 'data' => $response));
 }
示例#3
0
 /**
  * Remove the specified resource from storage.
  * DELETE /comments/{id}
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $validator = $this->validateId($id);
     if ($validator->fails()) {
         return $this->respondInsufficientPrivileges($validator->messages()->all());
     }
     $comment = Comment::find($id);
     if ($comment) {
         $post = $comment->post;
         $category = $post->category;
         if ($comment->delete()) {
             State::whereRaw('object=\'comment\' and object_id=?', array($id))->delete();
             NotificationUser::whereRaw('subject=\'comment\' and subject_id=? and is_removed=0', array($id))->get()->each(function ($notificationsUser) {
                 $notificationsUser->is_removed = 1;
                 $notificationsUser->save();
             });
             Notification::whereRaw('object=\'post\' and event=\'commented\' and is_removed=0 and ' . '(select count(nu.id) from notifications_users nu where nu.notification_id=notifications.id and nu.is_removed=0)=0')->get()->each(function ($notification) {
                 $notification->is_removed = 1;
                 $notification->save();
             });
             return $this->respondNoContent();
         }
     } else {
         return $this->respondNotFound('Comment not found');
     }
     return $this->respondServerError();
 }
示例#4
0
 /**
  * Remove the specified resource from storage.
  * DELETE /posts/{id}
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $post = Post::find($id);
     if (!$post) {
         return $this->respondNotFound();
     }
     $user = Auth::user();
     if (!$user->can('Post.delete', $post)) {
         return $this->respondInsufficientPrivileges();
     }
     if ($post->delete()) {
         $category = $post->category;
         $category->updateCount('posts');
         $commentsIds = array();
         Comment::where('post_id', $id)->get()->each(function ($comment) use($commentsIds) {
             $commentsIds[] = $comment->id;
         });
         if (count($commentsIds) > 0) {
             State::whereRaw('object=\'comment\'')->whereIn('object_id', $commentsIds)->delete();
         }
         $post->comments()->delete();
         $category->updateCount('comments');
         State::whereRaw('object=\'post\' and object_id=?', array($id))->delete();
         Notification::whereRaw('object=\'post\' and object_id=? and is_removed=0', array($id))->get()->each(function ($notification) {
             NotificationUser::where('notification_id', $notification->id)->get()->each(function ($notificationsUser) {
                 $notificationsUser->is_removed = 1;
                 $notificationsUser->save();
             });
             $notification->is_removed = 1;
             $notification->save();
         });
         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);
 }