function watch(Request $request, Application $app)
 {
     if ($request->request->get('action') && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $repo = new UserWatchesSiteRepository();
         if ($request->request->get('action') == 'watch') {
             $repo->startUserWatchingSite($app['currentUser'], $app['currentSite']);
         } else {
             if ($request->request->get('action') == 'unwatch') {
                 $repo->stopUserWatchingSite($app['currentUser'], $app['currentSite']);
             }
         }
         // redirect here because if we didn't the twig global and $app vars would be wrong (the old state)
         // this is an easy way to get round that.
         return $app->redirect('/watch');
     }
     $repo = new \repositories\UserNotificationPreferenceRepository();
     $preferences = array();
     foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
         if (!isset($preferences[$extension->getId()])) {
             $preferences[$extension->getId()] = array();
         }
         foreach ($extension->getUserNotificationPreferenceTypes() as $type) {
             $userPref = $repo->load($app['currentUser'], $extension->getId(), $type);
             $preferences[$extension->getId()][$type] = array('email' => $userPref->getIsEmail());
         }
     }
     return $app['twig']->render('site/index/watch.html.twig', array('preferences' => $preferences));
 }
 function watch($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Area does not exist.");
     }
     if ($request->request->get('action') && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $repo = new UserWatchesAreaRepository();
         if ($request->request->get('action') == 'watch') {
             $repo->startUserWatchingArea($app['currentUser'], $this->parameters['area']);
             $app['flashmessages']->addMessage("Watching!");
         } else {
             if ($request->request->get('action') == 'unwatch') {
                 $repo->stopUserWatchingArea($app['currentUser'], $this->parameters['area']);
                 $app['flashmessages']->addMessage("No longer watching");
             }
         }
         // redirect here because if we didn't the  $this->parameters vars would be wrong (the old state)
         // this is an easy way to get round that. Also it's nice UI to go back to the area page.
         return $app->redirect('/area/' . $this->parameters['area']->getSlugForURL());
     }
     $repo = new \repositories\UserNotificationPreferenceRepository();
     $this->parameters['preferences'] = array();
     foreach ($app['extensions']->getExtensionsIncludingCore() as $extension) {
         if (!isset($this->parameters['preferences'][$extension->getId()])) {
             $this->parameters['preferences'][$extension->getId()] = array();
         }
         foreach ($extension->getUserNotificationPreferenceTypes() as $type) {
             $userPref = $repo->load($app['currentUser'], $extension->getId(), $type);
             $this->parameters['preferences'][$extension->getId()][$type] = array('email' => $userPref->getIsEmail());
         }
     }
     return $app['twig']->render('site/area/watch.html.twig', $this->parameters);
 }