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(); }
private function getUserStates($userId) { $states = array(); State::whereRaw('owner_id = ?', array($userId))->orderBy('timestamp', 'asc')->get()->each(function ($state) use(&$states) { $states[] = $state; }); return $states; }
/** * 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(); }
/** * 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(); }