<?php

namespace app\FeedsController; 


use lithium\security\Auth;
use app\models\Post;

class FeedsController extends \lithium\action\Controller {
	//Class Variables
	//public $validates =


	public function index() 
	{
		//If the user isn't authenticated
		if(!Auth::check('default')) 
		{ 
			//Redirect them to the login/welcome page
			$this->redirect('Users::login'); 
		}	
		
		//else
		//Figure out what user is logged in (from their session cookie presumably)
		/* Display the users last 20 posts in decending order. */
		$user = Session::read('username'); 

		//Since there can only be one of each username, getting the first occurence of $user should be fine
		$query = User::find('first', array('conditions' => array('username' => $user)));
		$feed = $query->posts; 


		return compact($feed); //Return the feed array to the 
	}

	/**
	* New needs to do a few things
	* 1) Validation, 
	*	 Ensure that the post is unique,
	*	 Flood protection
	*	 Spam Protection at some point
	* 2) Storage 
	*	The post needs to be stored in the users feed as well as users who are friends with them
	*	 
	*/
	public function new()
	{
		
	}

	/**
	* Hide needs to put a "HIDES" edge on the graph
	* By getting the users document from the database, querying its id
	*then doing something like $thisUser->hides($username)
	*/
	public function hide($username)
	{
		
	}

	
	/**
	*Does the same sort of validation as new, but deletes a post obviously :P
	*/
	public function delete($id)
	{
		$user = Auth::check('default');
		$post = Post::find($id);

		if ($post->username == $user['username']) {
			$post->delete();
		}

		return $this->redirect('Feed::Index');

	}
}

?>