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(); }
/** * 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(); }
/** * 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); }